diff --git a/src/QIFIO.c b/src/QIFIO.c index 003efec8ce..a4e0ccd5e8 100644 --- a/src/QIFIO.c +++ b/src/QIFIO.c @@ -46,10 +46,6 @@ #define WFLAGS (O_WRONLY | O_CREAT | O_TRUNC) #define RFLAGS O_RDONLY -#ifdef DEBUG -#undef DEBUG -#endif -#define DEBUG printf /********************************************************************\ * xaccReadQIFLine * @@ -193,64 +189,6 @@ char * xaccReadQIFCategory (int fd, Account * acc) return qifline; } -/********************************************************************\ - * read a sequence of categories, inserting them into - * the indicated group -\********************************************************************/ - -char * xaccReadQIFCatList (int fd, AccountGroup *grp) -{ - char * qifline; - Account *acc; - char *str, *tok; - - if (!grp) return 0x0; - do { - acc = mallocAccount(); - qifline = xaccReadQIFCategory (fd, acc); - if ('!' == qifline [0]) break; - - if (-1 == acc->type) { /* free up malloced data if unknown account type */ - freeAccount(acc); - continue; - } - if (!qifline) { /* free up malloced data if the read bombed. */ - freeAccount(acc); - continue; - } - - /* check to see if this is a sub-account. - * Sub-accounts will have a colon in the name */ - str = acc->accountName; - tok = strchr (str, ':'); - if (tok) { - Account *parent; - - /* find the parent account, and parent to it */ - *tok = 0x0; - parent = xaccGetAccountFromName (grp, str); - *tok = ':'; - - if (parent) { - xaccInsertSubAccount( parent, acc ); - /* trim off the parent account name ... */ - /* tok += sizeof(char); leave behind the colon ... */ - str = XtNewString (tok); - XtFree (acc->accountName); - acc->accountName = str; - } else { - insertAccount( grp, acc ); - } - } else { - insertAccount( grp, acc ); - } - - } while (qifline); - - - return qifline; -} - /********************************************************************\ * xaccReadQIFAccount * * reads in account name, description, etc. * @@ -272,7 +210,6 @@ char * xaccReadQIFAccount (int fd, Account * acc) acc -> description = 0x0; /* string */ qifline = xaccReadQIFLine (fd); - if (!qifline) return NULL; if ('!' == qifline [0]) return qifline; @@ -289,8 +226,14 @@ char * xaccReadQIFAccount (int fd, Account * acc) if (!strcmp (&qifline[1], "Bank\r\n")) { acc -> type = BANK; } else + if (!strcmp (&qifline[1], "Cash\r\n")) { + acc -> type = CASH; + } else if (!strcmp (&qifline[1], "Invst\r\n")) { acc -> type = STOCK; + } else + if (!strcmp (&qifline[1], "Oth A\r\n")) { + acc -> type = ASSET; } else { printf ("QIF Parse: Unsupported account type %s \n", &qifline[1]); acc -> type = -1; /* hack alert -- */ @@ -299,7 +242,6 @@ char * xaccReadQIFAccount (int fd, Account * acc) /* check for end-of-transaction marker */ if (!strcmp (qifline, "^^\r\n")) { - qifline = xaccReadQIFLine (fd); break; } else if ('!' == qifline [0]) break; @@ -312,6 +254,72 @@ char * xaccReadQIFAccount (int fd, Account * acc) return qifline; } +/********************************************************************\ + * read a sequence of accounts or categories, inserting them into + * the indicated group +\********************************************************************/ + +char * xaccReadQIFAccList (int fd, AccountGroup *grp, int cat) +{ + char * qifline; + Account *acc; + char *str, *tok; + + if (!grp) return 0x0; + do { + acc = mallocAccount(); + if (cat) { + qifline = xaccReadQIFCategory (fd, acc); + } else { + qifline = xaccReadQIFAccount (fd, acc); + } + if ('!' == qifline [0]) break; + + if (-1 == acc->type) { /* free up malloced data if unknown account type */ + freeAccount(acc); + continue; + } + if (!qifline) { /* free up malloced data if the read bombed. */ + freeAccount(acc); + continue; + } + + /* check to see if this is a sub-account. + * Sub-accounts will have a colon in the name */ + str = acc->accountName; + tok = strchr (str, ':'); + if (tok) { + Account *parent; + + /* find the parent account, and parent to it */ + *tok = 0x0; + parent = xaccGetAccountFromName (grp, str); + *tok = ':'; + + if (parent) { + Account *preexisting; + + /* trim off the parent account name ... */ + /* tok += sizeof(char); leave behind the colon ... */ + str = XtNewString (tok); + XtFree (acc->accountName); + acc->accountName = str; + + xaccInsertSubAccount( parent, acc ); + } else { + /* we should never get here if the qif file is OK */ + insertAccount( grp, acc ); + } + } else { + insertAccount( grp, acc ); + } + + } while (qifline); + + + return qifline; +} + /********************************************************************\ * xaccParseQIFDate * * parses date of the form MM/DD/YY * @@ -828,7 +836,7 @@ xaccReadQIFData( char *datafile ) if (!strcmp (qifline, "!Type:Cat\r\n")) { DEBUG ("got category\n"); - qifline = xaccReadQIFCatList (fd, grp); + qifline = xaccReadQIFAccList (fd, grp, 1); continue; } else @@ -875,12 +883,13 @@ xaccReadQIFData( char *datafile ) /* loop and read all of the account names and descriptions */ /* no actual dollar data is expected to be read here ... */ while (qifline) { - qifline = xaccReadQIFDiscard (fd); + qifline = xaccReadQIFAccList (fd, grp, 0); if (!qifline) break; if ('!' == qifline[0]) break; } } else { /* read account name, followed by dollar data ... */ + Account *preexisting; Account *acc = mallocAccount(); DEBUG ("got account\n"); qifline = xaccReadQIFAccount (fd, acc); @@ -892,7 +901,16 @@ xaccReadQIFData( char *datafile ) freeAccount(acc); continue; } - insertAccount( grp, acc ); + + /* check to see if we already know this account; + * if we do, use it, otherwise create it */ + preexisting = xaccGetAccountFromName (grp, acc->accountName); + if (preexisting) { + freeAccount (acc); + acc = preexisting; + } else { + insertAccount( grp, acc ); + } /* spin until start of transaction records */ /* in theory, in a perfect file, the transaction data follows immediately */