Skip to content

Commit

Permalink
dbfcreate: allow creation of Date and Logical fields OSGeo#167
Browse files Browse the repository at this point in the history
  • Loading branch information
zezzagio committed Nov 3, 2024
1 parent 4ae3a7b commit 21abd05
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
20 changes: 19 additions & 1 deletion dbfadd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,33 @@ int main(int argc, char **argv)
}

const int iRecord = DBFGetRecordCount(hDBF);


SHPDate date;
char bool;

// Loop assigning the new field values.
for (int i = 0; i < DBFGetFieldCount(hDBF); i++)
{
if (strcmp(argv[i + 2], "") == 0)
DBFWriteNULLAttribute(hDBF, iRecord, i);
else if (DBFGetFieldInfo(hDBF, i, NULL, NULL, NULL) == FTString)
DBFWriteStringAttribute(hDBF, iRecord, i, argv[i + 2]);
else if (DBFGetFieldInfo(hDBF, i, NULL, NULL, NULL) == FTDate)
{
if (3 == sscanf(argv[i + 2], "%4d%2d%2d", &date.year, &date.month,
&date.day))
{
DBFWriteDateAttribute(hDBF, iRecord, i, &date);
}
}
else if (DBFGetFieldInfo(hDBF, i, NULL, NULL, NULL) == FTLogical)
{
if (1 == sscanf(argv[i + 2], "%c", &bool)) {
DBFWriteLogicalAttribute(hDBF, iRecord, i, bool);
}
}
else

DBFWriteDoubleAttribute(hDBF, iRecord, i, atof(argv[i + 2]));
}

Expand Down
26 changes: 26 additions & 0 deletions dbfcreate.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ int main(int argc, char **argv)
}
i += 3;
}
else if (i < argc - 1 && strcmp(argv[i], "-d") == 0)
{
const char *field = argv[i + 1];
const int width = 8;
const int decimals = 0;
if (DBFAddField(hDBF, field, FTDate, width, decimals) == -1)
{
printf("DBFAddField(%s,FTDate,%d,0) failed.\n", field, width);
DBFClose(hDBF);
return 4;
}
i += 1;
}
else if (i < argc - 1 && strcmp(argv[i], "-l") == 0)
{
const char *field = argv[i + 1];
const int width = 1;
const int decimals = 0;
if (DBFAddField(hDBF, field, FTLogical, width, decimals) == -1)
{
printf("DBFAddField(%s,FTLogical,%d,0) failed.\n", field, width);
DBFClose(hDBF);
return 4;
}
i += 1;
}
else
{
printf("Argument incomplete, or unrecognised: %s\n", argv[i]);
Expand Down
8 changes: 4 additions & 4 deletions tests/expect3.out
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Shape:2 (Polygon) nVertices=4, nParts=1
(160,150, 0)
(180,170, 0)
(150,150, 0)
Descriptio TestInt TestDouble
Square with triangle missing 1 2.50000
Smaller triangle 100 1000.25000
(NULL) (NULL) (NULL)
Descriptio TestInt TestDouble TestDate TestBool
Square with triangle missing 1 2.50000 20241102 T
Smaller triangle 100 1000.25000 20241102 F
(NULL) (NULL) (NULL) (NULL) (NULL)
8 changes: 4 additions & 4 deletions tests/test3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ readonly EXPECT="${1:-$SCRIPTDIR/expect3.out}"

{
"${SHPCREATE:-./shpcreate}" test polygon
"${DBFCREATE:-./dbfcreate}" test.dbf -s Description 30 -n TestInt 6 0 -n TestDouble 16 5
"${DBFCREATE:-./dbfcreate}" test.dbf -s Description 30 -n TestInt 6 0 -n TestDouble 16 5 -d TestDate -l TestBool

"${SHPADD:-./shpadd}" test 0 0 100 0 100 100 0 100 0 0 + 20 20 20 30 30 30 20 20
"${DBFADD:-./dbfadd}" test.dbf "Square with triangle missing" 1.4 2.5
"${DBFADD:-./dbfadd}" test.dbf "Square with triangle missing" 1.4 2.5 20241102 T

"${SHPADD:-./shpadd}" test 150 150 160 150 180 170 150 150
"${DBFADD:-./dbfadd}" test.dbf "Smaller triangle" 100 1000.25
"${DBFADD:-./dbfadd}" test.dbf "Smaller triangle" 100 1000.25 20241102 F

"${SHPADD:-./shpadd}" test 150 150 160 150 180 170 150 150
"${DBFADD:-./dbfadd}" test.dbf "" "" ""
"${DBFADD:-./dbfadd}" test.dbf "" "" "" "" ""

"${SHPDUMP:-./shpdump}" test.shp
"${DBFDUMP:-./dbfdump}" test.dbf
Expand Down

0 comments on commit 21abd05

Please sign in to comment.