home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Utilities / amiCheck / Source / impexp.c < prev    next >
C/C++ Source or Header  |  1998-06-11  |  9KB  |  439 lines

  1. /* impexp.c 
  2.  *
  3.  *  defines the import/export filters
  4.  */
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <exec/types.h>
  8. #include <exec/lists.h>
  9. #include <exec/nodes.h>
  10. #include <exec/memory.h>
  11. #include <stdio.h>  
  12. #include <intuition/gadgetclass.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15. #include "amiCheck.h"
  16. #include "dataBase.h"
  17. #include "regGadget.h"
  18. #include "impexp.h"
  19.  
  20. extern char currFile[FILESIZE];
  21. extern ULONG regValidRows, numRows;
  22.  
  23. /* prototypes */
  24. void ImportAddEntry(entryNode *);
  25.  
  26. ULONG row;
  27. filterNode *selFilt;
  28. entryNode *selEntry;
  29. static ULONG filtentries;
  30.  
  31. /*
  32.  * ASCII filter
  33.  */
  34.  
  35. /*********** the delimited format is as follows  ******************
  36. *source  [comment (suggest account file name)]
  37. *type     ["withdrawal" | "deposit" | check number]
  38. *amount     [xxx.xx]
  39. *date     [mm/dd/yyyy]
  40. *name     [payee field or name]
  41. *addr1
  42. *addr2
  43. *addr3
  44. *memo     [memo field]
  45. *state    [Y|N][Y|N][Y|N] (ie: YNN, YYY) = cleared y/n, tax-deduct y/n, voided y/n
  46. *category 0.00 [category]
  47. *category 0.00 [category]
  48. *... <as  many categories as needed> 
  49. *
  50. *Note that category may appear once, not at all, or as many times as 
  51. *necessary. Non-unique categories are ignored for import. Currently,
  52. *amicheck only supports one category, and if no category is assigned,
  53. *the "none" category is assumed. 
  54. *
  55. *On import, the state flag "voided" is ignored for withdrawals and deposits
  56. *(checks only).
  57. *******************************************************************/
  58.     
  59. /**************************************************************
  60. * ImportAscii()
  61. *
  62. *    Import an ascii file of transactions
  63. ***************************************************************/
  64. BOOL ImportAscii(char *file)
  65. {
  66. BOOL ret = TRUE;
  67. char cat_amnt[50],word[50], str[200],*data;
  68. entryNode entry;
  69. int x;
  70. int state = 0;
  71. FILE *infile;
  72. struct Node *node;
  73.  
  74.  filtentries = 0;
  75.  
  76.  /* remember this bit later */
  77.  row = selRow;
  78.  if (row != -1) {
  79.     selFilt = (filterNode *)DataOrd2Node((struct List *)&filtered,row);
  80.     selEntry = selFilt->entry;
  81.  }
  82.  
  83.  memset(&entry,0,sizeof(entryNode));
  84.  entry.type = -1;
  85.  
  86.  strncpy(entry.category,"None",CATNAMESIZE);
  87.  
  88.  if ( (infile = fopen(file,"r")) == NULL)
  89.     return (FALSE);
  90.  
  91.  while (fgets(str,200,infile)) {
  92.  
  93.     /* remove \n */
  94.     if (strlen(str) > 0 && str[strlen(str)-1] == '\n')
  95.         str[strlen(str)-1]=0;
  96.  
  97.     /* get control word */
  98.     if ( (x=sscanf(str,"%s",word)) != 1) 
  99.         continue;
  100.  
  101.     /* now get data string */
  102.     data=strstr(str,word) + strlen(word);
  103.     while (*data == ' ' && *data != 0)
  104.         data++;
  105.     
  106.     /* remove starting line */
  107.     if (state == 0) {
  108.         if (stricmp(word,"Source") != 0) {
  109.             ret = FALSE;
  110.             break;
  111.         }
  112.         else {
  113.             state++;
  114.             continue;
  115.         }
  116.     }
  117.  
  118. newitem:
  119.     /** ADD CURRENT ENTRY? **/
  120.     if (state == 0x7fff ) {
  121.         ImportAddEntry(&entry);
  122.         memset(&entry,0,sizeof(entryNode));
  123.         strncpy(entry.category,"None",CATNAMESIZE);
  124.         entry.type = -1;
  125.         state = 1;
  126.     }
  127.  
  128.     /** TYPE **/
  129.     if ( state == 1 ) {
  130.         if (stricmp(word,"type") != 0) {
  131.             ret = FALSE;
  132.             break;
  133.         }
  134.         else {
  135.             if (stricmp(data,"withdrawal") == 0)
  136.                 entry.type = WITHDRAWALTYPE;
  137.             else if (stricmp(data,"deposit") == 0)
  138.                 entry.type = DEPOSITTYPE;
  139.             else    {
  140.                 entry.type = CHECKTYPE;
  141.                 entry.check = (unsigned short)atoi(data);
  142.             }
  143.             state++;
  144.             continue;
  145.         }
  146.     }
  147.  
  148.     /** AMOUNT **/
  149.     if (state == 2 ) {
  150.         if (stricmp(word,"amount") != 0) {
  151.             ret = FALSE;
  152.             break;
  153.         }
  154.         else  {
  155.             DataStoreAmnt(&entry.amount,data);
  156.             state++;
  157.             continue;
  158.         }
  159.     }
  160.  
  161.     /** DATE **/
  162.     if (state == 3) {
  163.         if (stricmp(word,"date") != 0) {
  164.             ret = FALSE;
  165.             break;
  166.         }
  167.         else {
  168.             DataStoreDate(&entry.date,data);
  169.             state++;
  170.             continue;
  171.         }
  172.     }
  173.  
  174.     /** NAME **/
  175.     if (state == 4) {
  176.         if (stricmp(word,"name") != 0) {
  177.             ret = FALSE;
  178.             break;
  179.         }
  180.         else {
  181.             strncpy(entry.name,data,STRINGSIZE);
  182.             state++;
  183.             continue;
  184.         }
  185.     }
  186.  
  187.     /** ADDRESS **/
  188.     if (state == 5) {
  189.         if (stricmp(word,"addr1") != 0) {
  190.             ret = FALSE;
  191.             break;
  192.         }
  193.         else {
  194.             state++;
  195.             continue;
  196.         }
  197.     }
  198.     if (state == 6) {
  199.         if (stricmp(word,"addr2") != 0) {
  200.             ret = FALSE;
  201.             break;
  202.         }
  203.         else {
  204.             state++;
  205.             continue;
  206.         }
  207.     }
  208.     if (state == 7) {
  209.         if (stricmp(word,"addr3") != 0) {
  210.             ret = FALSE;
  211.             break;
  212.         }
  213.         else {
  214.             state++;
  215.             continue;
  216.         }
  217.     }
  218.  
  219.     /** MEMO **/
  220.     if (state == 8 ) {
  221.         if (stricmp(word,"memo") != 0) {
  222.             ret = FALSE;
  223.             break;
  224.         }
  225.         else {
  226.             strncpy(entry.memo,data,STRINGSIZE);
  227.             state++;
  228.             continue;
  229.         }
  230.     }
  231.  
  232.     /** STATE **/
  233.     if (state == 9 ) {
  234.         if (stricmp(word,"state") != 0) {
  235.             ret = FALSE;
  236.             break;
  237.         }
  238.         else  {
  239.             if (strlen(data) != 3)
  240.                 break;
  241.             if (data[0] == 'Y')
  242.                 entry.flags |= CLEARED;
  243.             if (data[1] == 'Y')
  244.                 entry.flags |= TAXDEDUCT;
  245.             if (data[2] == 'Y' && entry.type == CHECKTYPE)
  246.                 entry.flags |= VOIDED;
  247.  
  248.             state++;
  249.             continue;
  250.         }
  251.     }
  252.  
  253.     /** CATEGORIES **/
  254.     if (state > 9) {
  255.         if (stricmp(word,"category") != 0) {
  256.             if (stricmp(word,"type") == 0) {
  257.                 state = 0x7fff;
  258.                 goto newitem;
  259.             }
  260.             else {
  261.                 ret = FALSE;
  262.                 break;
  263.             }
  264.         }
  265.         else {
  266.             /* pull out bogus amount */
  267.             if ( (x=sscanf(data,"%s",cat_amnt)) != 1) 
  268.                 continue;
  269.  
  270.             /* verify it */
  271.             if (stricmp(cat_amnt,"0.00") != 0) {
  272.                 ret = FALSE;
  273.                 break;
  274.             }
  275.  
  276.             /* now move data string */
  277.             data=strstr(data,cat_amnt) + strlen(cat_amnt);
  278.             while (*data == ' ' && *data != 0)
  279.                 data++;
  280.  
  281.             /* TEST FOR CATEGORY EXISTANCE */
  282.             if ( (node = DataFindName(&categories,data)) == NULL)
  283.                 strncpy(entry.category,"None",CATNAMESIZE);
  284.             else    strncpy(entry.category,node->ln_Name,CATNAMESIZE);
  285.             state++;
  286.             continue;
  287.         }
  288.     }
  289.  
  290.     
  291.     ret = FALSE;
  292.     break;
  293.  }
  294.  
  295.  /* last entry */
  296.  if (state > 9 && entry.type != -1) 
  297.     ImportAddEntry(&entry);
  298.  
  299.  /* conclude with updating register */
  300.  if (filtentries > 0) {
  301.     selRow = row;
  302.     RegRedrawFilter(filtentries);
  303.     AmiUpdateFilt(filtStr);    
  304.  }
  305.  AmiUpdateCurr(currStr);
  306.  
  307.  fclose(infile);
  308.  return (ret);
  309. }
  310.  
  311. /**************************************************************
  312. * ExportAscii()
  313. *
  314. *    Export the filter as an ascii file
  315. ***************************************************************/
  316. BOOL ExportAscii(char *file)
  317. {
  318. char tempAmnt[AMNTSIZE+1];
  319. char str[200];
  320. BOOL ret = TRUE;
  321. filterNode *filt = NULL;
  322. FILE *outfile;
  323.  
  324.  if ( (outfile = fopen(file,"w")) == NULL)
  325.     return (FALSE);
  326.  
  327.  /* export starting information */
  328.  fprintf(outfile,"source   %s\n\n",currFile);
  329.   
  330.  while ( (filt = (filterNode *)DataGetNext((struct List *)&filtered,(struct Node *)filt)) != NULL) {
  331.  
  332.     /* export entry type */
  333.     switch (filt->entry->type) {
  334.         case CHECKTYPE:
  335.             sprintf(str,"%05d",filt->entry->check);
  336.             break;
  337.         case WITHDRAWALTYPE:
  338.             sprintf(str,"Withdrawal");
  339.             break;
  340.         case DEPOSITTYPE:
  341.             sprintf(str,"Deposit");
  342.             break;
  343.     }
  344.     fprintf(outfile, "type     %s\n",str);
  345.     
  346.     /* export entry amount */
  347.     DataBuildAmnt(TRUE,&filt->entry->amount,tempAmnt);
  348.     fprintf(outfile, "amount   % 9s\n",tempAmnt);
  349.     
  350.     /* export date */
  351.     DataBuildDate(&filt->entry->date,str);
  352.     fprintf(outfile, "date     %s\n",str);
  353.  
  354.     /* export name */
  355.     fprintf(outfile, "name     %s\n",filt->entry->name);
  356.  
  357.     /* export address */
  358.     fprintf(outfile, "addr1\naddr2\naddr3\n");
  359.  
  360.     /* export memo */
  361.     fprintf(outfile, "memo     %s\n",filt->entry->memo);
  362.  
  363.     /* export state */
  364.     str[3]=0;
  365.     if (filt->entry->flags & CLEARED)
  366.         str[0]='Y';
  367.     else    str[0]='N';
  368.  
  369.     if (filt->entry->flags & TAXDEDUCT)
  370.         str[1]='Y';
  371.     else    str[1]='N';
  372.     
  373.     if (filt->entry->flags & VOIDED)
  374.         str[2]='Y';
  375.     else    str[2]='N';
  376.  
  377.     fprintf(outfile, "state    %s\n",str);
  378.  
  379.     /* export category */
  380.     fprintf(outfile, "category 0.00 %s\n",filt->entry->category);
  381.  
  382.     /* cute newline */
  383.     fprintf(outfile,"\n");
  384.  }
  385.  
  386.  
  387.  fclose(outfile);
  388.  return (ret);
  389. }
  390.  
  391. /***************************************************************
  392. * ImportAddEntry()
  393. *
  394. *    Import adding a complete entry into database 
  395. ****************************************************************/
  396. void ImportAddEntry(entryNode *entry)
  397. {
  398.  filterNode *filt;
  399.  entryNode *pos;
  400.  
  401.  /* insert into database */
  402.  if (row == -1)
  403.      DataAddEntry(entry,&entries,&filt);
  404.  else {
  405.     DataInsertEntry(selFilt,selEntry,entry,&entries,&filt,&pos);
  406.     if (pos != NULL) selEntry = pos;
  407.     if (filt != NULL) selFilt = filt;
  408.  }
  409.  
  410.  amiChangedItems = TRUE;
  411.  
  412.  /* update current balance */
  413.  if (!(entry->flags & VOIDED)) {
  414.     DataUpdateBal(entry->type,&entry->amount, &amntState.currAmnt, currStr);
  415.     /*AmiUpdateCurr(currStr);*/
  416.  }
  417.  
  418.  /* update the filter */
  419.  if (filt != NULL) {
  420.     filtentries++;
  421.  
  422. #if 0
  423.     if (row == -1) {
  424.         RegAddEntry(filt);
  425.         AmiDisableSel(TRUE);
  426.     }
  427.     else {
  428.         RegInsertEntry(row,filt);
  429.     }
  430. #endif
  431.     /* update filter balance */
  432.     if (!(entry->flags & VOIDED)) {
  433.         DataUpdateBal(entry->type, &entry->amount, &filtAmnt, filtStr);
  434.     /*    AmiUpdateFilt(filtStr);    */
  435.     }
  436.  }
  437.  
  438. }
  439.