Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4176e9

Browse files
committed
Clean up bogus checking of date and numeric fields in DBF files,
per report from Boris van Schooten.
1 parent fbdb203 commit b4176e9

File tree

1 file changed

+15
-38
lines changed

1 file changed

+15
-38
lines changed

contrib/dbase/dbf2pg.c

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,8 @@ char *Escape_db(char *);
6363
char *convert_charset(char *string);
6464
#endif
6565
void usage(void);
66-
unsigned int isinteger(char *);
6766

6867

69-
70-
unsigned int
71-
isinteger(char *buff)
72-
{
73-
char *i = buff;
74-
75-
while (*i != '\0')
76-
{
77-
if (i == buff)
78-
if ((*i == '-') ||
79-
(*i == '+'))
80-
{
81-
i++;
82-
continue;
83-
}
84-
if (!isdigit((unsigned char) *i))
85-
return 0;
86-
i++;
87-
}
88-
return 1;
89-
}
90-
9168
static inline void
9269
strtoupper(char *string)
9370
{
@@ -471,35 +448,35 @@ do_inserts(PGconn *conn, char *table, dbhead * dbh)
471448
/* handle the date first - liuk */
472449
if (fields[h].db_type == 'D')
473450
{
474-
if ((strlen(foo) == 8) && isinteger(foo))
451+
if (strlen(foo) == 0)
475452
{
453+
/* assume empty string means a NULL */
454+
strcat(query, "\\N");
455+
}
456+
else if (strlen(foo) == 8 &&
457+
strspn(foo, "0123456789") == 8)
458+
{
459+
/* transform YYYYMMDD to Postgres style */
476460
snprintf(pgdate, 11, "%c%c%c%c-%c%c-%c%c",
477461
foo[0], foo[1], foo[2], foo[3],
478462
foo[4], foo[5], foo[6], foo[7]);
479463
strcat(query, pgdate);
480464
}
481465
else
482466
{
483-
/*
484-
* empty field must be inserted as NULL value in
485-
* this way
486-
*/
487-
strcat(query, "\\N");
467+
/* try to insert it as-is */
468+
strcat(query, foo);
488469
}
489470
}
490-
else if ((fields[h].db_type == 'N') &&
491-
(fields[h].db_dec == 0))
471+
else if (fields[h].db_type == 'N')
492472
{
493-
if (isinteger(foo))
494-
strcat(query, foo);
495-
else
473+
if (strlen(foo) == 0)
496474
{
475+
/* assume empty string means a NULL */
497476
strcat(query, "\\N");
498-
if (verbose)
499-
fprintf(stderr, "Illegal numeric value found "
500-
"in record %d, field \"%s\"\n",
501-
i, fields[h].db_name);
502477
}
478+
else
479+
strcat(query, foo);
503480
}
504481
else
505482
{

0 commit comments

Comments
 (0)