Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbfcreate: allow creation of Date and Logical fields #167 #168

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 29 additions & 1 deletion dbfcreate.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ int main(int argc, char **argv)
if (argc < 2)
{
printf("dbfcreate xbase_file [[-s field_name width], "
"[-n field_name width decimals]]...\n");
"[-n field_name width decimals], "
"[-d field_name], "
"[-l field_name]]...\n");
return 1;
}

Expand Down Expand Up @@ -65,6 +67,32 @@ int main(int argc, char **argv)
}
i += 3;
}
else if (i < argc - 1 && strcmp(argv[i], "-d") == 0)
rouault marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading