Skip to content

Commit 4a12b37

Browse files
authored
dbfcreate: allow creation of Date and Logical fields #167 (#168)
* dbfcreate: allow creation of Date and Logical fields #167 * Documented in usage -d and -l new options
1 parent 36106c2 commit 4a12b37

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

dbfadd.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,33 @@ int main(int argc, char **argv)
4343
}
4444

4545
const int iRecord = DBFGetRecordCount(hDBF);
46-
46+
47+
SHPDate date;
48+
char bool;
49+
4750
// Loop assigning the new field values.
4851
for (int i = 0; i < DBFGetFieldCount(hDBF); i++)
4952
{
5053
if (strcmp(argv[i + 2], "") == 0)
5154
DBFWriteNULLAttribute(hDBF, iRecord, i);
5255
else if (DBFGetFieldInfo(hDBF, i, NULL, NULL, NULL) == FTString)
5356
DBFWriteStringAttribute(hDBF, iRecord, i, argv[i + 2]);
57+
else if (DBFGetFieldInfo(hDBF, i, NULL, NULL, NULL) == FTDate)
58+
{
59+
if (3 == sscanf(argv[i + 2], "%4d%2d%2d", &date.year, &date.month,
60+
&date.day))
61+
{
62+
DBFWriteDateAttribute(hDBF, iRecord, i, &date);
63+
}
64+
}
65+
else if (DBFGetFieldInfo(hDBF, i, NULL, NULL, NULL) == FTLogical)
66+
{
67+
if (1 == sscanf(argv[i + 2], "%c", &bool)) {
68+
DBFWriteLogicalAttribute(hDBF, iRecord, i, bool);
69+
}
70+
}
5471
else
72+
5573
DBFWriteDoubleAttribute(hDBF, iRecord, i, atof(argv[i + 2]));
5674
}
5775

dbfcreate.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ int main(int argc, char **argv)
2323
if (argc < 2)
2424
{
2525
printf("dbfcreate xbase_file [[-s field_name width], "
26-
"[-n field_name width decimals]]...\n");
26+
"[-n field_name width decimals], "
27+
"[-d field_name], "
28+
"[-l field_name]]...\n");
2729
return 1;
2830
}
2931

@@ -65,6 +67,32 @@ int main(int argc, char **argv)
6567
}
6668
i += 3;
6769
}
70+
else if (i < argc - 1 && strcmp(argv[i], "-d") == 0)
71+
{
72+
const char *field = argv[i + 1];
73+
const int width = 8;
74+
const int decimals = 0;
75+
if (DBFAddField(hDBF, field, FTDate, width, decimals) == -1)
76+
{
77+
printf("DBFAddField(%s,FTDate,%d,0) failed.\n", field, width);
78+
DBFClose(hDBF);
79+
return 4;
80+
}
81+
i += 1;
82+
}
83+
else if (i < argc - 1 && strcmp(argv[i], "-l") == 0)
84+
{
85+
const char *field = argv[i + 1];
86+
const int width = 1;
87+
const int decimals = 0;
88+
if (DBFAddField(hDBF, field, FTLogical, width, decimals) == -1)
89+
{
90+
printf("DBFAddField(%s,FTLogical,%d,0) failed.\n", field, width);
91+
DBFClose(hDBF);
92+
return 4;
93+
}
94+
i += 1;
95+
}
6896
else
6997
{
7098
printf("Argument incomplete, or unrecognised: %s\n", argv[i]);

tests/expect3.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Shape:2 (Polygon) nVertices=4, nParts=1
3131
(160,150, 0)
3232
(180,170, 0)
3333
(150,150, 0)
34-
Descriptio TestInt TestDouble
35-
Square with triangle missing 1 2.50000
36-
Smaller triangle 100 1000.25000
37-
(NULL) (NULL) (NULL)
34+
Descriptio TestInt TestDouble TestDate TestBool
35+
Square with triangle missing 1 2.50000 20241102 T
36+
Smaller triangle 100 1000.25000 20241102 F
37+
(NULL) (NULL) (NULL) (NULL) (NULL)

tests/test3.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ readonly EXPECT="${1:-$SCRIPTDIR/expect3.out}"
1212

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

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

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

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

2626
"${SHPDUMP:-./shpdump}" test.shp
2727
"${DBFDUMP:-./dbfdump}" test.dbf

0 commit comments

Comments
 (0)