home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / chint / useful18.hnt < prev    next >
Encoding:
Text File  |  1993-06-21  |  9.6 KB  |  380 lines

  1.  
  2. #define _ENTRY_
  3.  
  4. #ifdef __TURBOC__
  5.   #include <conio.h>
  6. #else
  7.   #include <graph.h>
  8. #endif
  9. #include <io.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "db_lsc.h"
  14. #include "db_types.h"
  15. #include "db_conio.h"
  16. #include "db_dos.h"
  17. #include "db_file.h"
  18. #include "extfhc.h"
  19. #include "db_heap.h"
  20. #include "db_gvar.h"
  21. #include "db_key.h"
  22. #include "db_pswd.h"
  23. #include "db_sets.h"
  24. #include "db_str.h"
  25. #include "db_funcs.h"
  26. #include "db_date.h"
  27. #include "db_tree.h"
  28. ⁿUSESⁿ
  29.  
  30. typedef uchar  str12[13];
  31. typedef long   recnotyp[16+1];     /* Zeroth element not used */
  32.  
  33. unsigned _stklen = 0x4000;  /* Default stack of 16 K */
  34.  
  35. string _tts, _tranString;   /* _tranString for internal generator use only */
  36.  
  37. /*********************************  TYPES  **********************************/
  38.  
  39. #define maxfilno ⁿmaxfilnoⁿ
  40. #define maxkeyno ⁿmaxkeynoⁿ
  41.  
  42. str12 dbnames[maxfilno+1][maxkeyno+1] =     /* [1.. ,0.. ] */
  43.           ⁿdbnamesⁿ;
  44. byte keymode[maxfilno+1][maxkeyno+1] =       /* [1.. ,1.. ] */
  45.           ⁿkeymodeⁿ;
  46. byte keylen[maxfilno+1][maxkeyno+1] =        /* [1.. ,1.. ] */
  47.           ⁿkeylenⁿ;
  48. bool filinuse[maxfilno+1] =
  49.           ⁿfilinuseⁿ;
  50.  
  51. bool menudriven = ⁿmenudrivenⁿ;
  52.  
  53. /*******************************  VARIABLES  ********************************/
  54.  
  55. bool            fok;
  56. int             filno, dbsize[maxfilno+1];
  57. datafile        *datf[maxfilno+1];
  58. indexfile       *idxkey[maxfilno+1][maxkeyno+1];
  59. recnotyp        recno;
  60.  
  61. uchar           autoMode;         /* what is the program being asked to do */
  62. string          textFileName;  /* read/write information from/to this file */
  63. int             searchByKey;        /* if "getting" which key to search by */
  64. keystr          searchFor;        /* locate the record by looking for this */
  65.  
  66. ⁿRECMODⁿ
  67.  
  68. ⁿFUNCMODⁿ
  69.  
  70. strptr makekey(strptr sout, int fno, int kno)
  71. {
  72.   string ks, ts;
  73.  
  74.   ks[0] = '\0';
  75.  
  76.   ⁿmakekeyⁿ
  77.  
  78.   strcpy(sout,ks);
  79.   return(sout);
  80. }
  81.  
  82. strptr keyexpr(strptr sout, int fno, int kno, string kvar)
  83. {
  84.   string ts, ks;
  85.   byte   tlen;
  86.  
  87.   strcpy(ks,kvar);
  88.   tlen = strlen(ks);
  89.  
  90.   ⁿkeyexprⁿ
  91.  
  92.   ks[tlen] = Nul;
  93.   strcpy(sout,ks);
  94.   return(sout);
  95. }
  96.  
  97. strptr getakey(strptr sout, int fno, int kno)
  98. {
  99.   string tsout;
  100.  
  101.   keyexpr(sout,fno,kno,makekey(tsout,fno,kno));
  102.   return(sout);
  103. }
  104.  
  105. void simpleGetRec(int fno)
  106. /*
  107.   Read a record from file "fno" into the record buffer for that file.  The
  108.   record to read is determined by the value stored in the "recno[fno]", which
  109.   is a global variable.
  110. */
  111. {
  112.   switch (fno) {
  113.     ⁿgetrecⁿ
  114.   }
  115. }
  116.  
  117. void simpleAddRec(int filno)
  118. /*
  119.   Add a record to file "filno".
  120.  
  121.   Note that is function does not do any error checking when adding to the
  122.   index file.  If a key that does not allow duplicates has an attempt to
  123.   add a duplicate key, (OK == False after "addkey()"), then not attempt
  124.   to correct the situation is made.
  125. */
  126. {
  127.   int keyno;
  128.   keystr ks;
  129.  
  130.   switch (filno) {
  131.     ⁿADDRECⁿ
  132.   }
  133.   for (keyno=1; keyno<=maxkeyno; keyno++) {
  134.     if (keylen[filno,keyno] > 0) {
  135.       getakey(ks,filno,keyno);
  136.       addkey(idxkey[filno][keyno],&recno[filno],ks);
  137.     }
  138.   }
  139. }
  140.  
  141. void openfiles(void)
  142. {
  143.   int    i, j, fo_i, fo_j;
  144.   string prepend;
  145.   int    tlen;
  146.   string ts;
  147.  
  148.   fo_i = fo_j = 0;
  149.  
  150.   ⁿSIZESⁿ;
  151.  
  152.   strip(prepend,"");   /*MOD*/ /* Can only open file in current directory. */
  153.   tlen = strlen(prepend);
  154.   if ((tlen > 0) && (prepend[tlen-1] != '\\')) {
  155.     prepend[tlen]   = '\\';
  156.       prepend[tlen+1] = '\0';
  157.   }
  158.  
  159.   fok = True;
  160.   for (i=1;i <= maxfilno; i++)
  161.     if (fok) {
  162.       datf[i] = db_malloc(sizeof(datafile));
  163.       strconcat(ts,prepend,dbnames[i][0],NULL);
  164.       openfile(datf[i],ts,dbsize[i]);
  165.       fok = (bool)(fok && ok);
  166.       if (fok) {
  167.         fo_i = i;
  168.         fo_j = 0;
  169.         for (j=1; j <= maxkeyno; j++)
  170.           if (fok && (keylen[i][j] > 0)) {
  171.             idxkey[i][j] = db_malloc(sizeof(indexfile));
  172.             strconcat(ts,prepend,dbnames[i][j],NULL);
  173.             openindex(idxkey[i][j],ts,keylen[i][j],keymode[i][j]);
  174.             fok = (bool)(fok && ok);
  175.             if (fok) fo_j = j;
  176.           }
  177.       }
  178.       else if (filinuse[i]) {
  179.         strconcat(ts,prepend,dbnames[i][0],NULL);
  180.         makefile(datf[i],ts,dbsize[i]);
  181.         fok = ok;
  182.         if (fok) {
  183.  
  184. ⁿIFDEF MUSERⁿ
  185.           closefile(datf[i]);
  186.           strconcat(ts,prepend,dbnames[i][0],NULL);
  187.           openfile(datf[i],ts,dbsize[i]);
  188. ⁿENDDEFⁿ
  189.  
  190.           fo_i = i;  fo_j = 0;
  191.           for (j=1; j <= maxkeyno; j++)
  192.             if (fok && (keylen[i][j] > 0)) {
  193.               idxkey[i][j] = db_malloc(sizeof(indexfile));
  194.               strconcat(ts,prepend,dbnames[i][j],NULL);
  195.               makeindex(idxkey[i][j],ts,keylen[i][j],keymode[i][j]);
  196.               fok = (bool)(fok && ok);
  197.               if (fok) {
  198.  
  199. ⁿIFDEF MUSERⁿ
  200.                 closeindex(idxkey[i][j]);
  201.                 strconcat(ts,prepend,dbnames[i][j],NULL);
  202.                 openindex(idxkey[i][j],ts,keylen[i][j],keymode[i][j]);
  203.                 fo_j = j;
  204. ⁿELSEDEFⁿ
  205.                 fo_j = j;
  206. ⁿENDDEFⁿ
  207.  
  208.               }
  209.             }
  210.         }
  211.       }
  212.     }
  213.   if (!fok)
  214.     for (i=1; i <= maxfilno; i++)
  215.       if (i <= fo_i) {
  216.         closefile(datf[i]);
  217.         db_free(datf[i]);
  218.         for (j=1; j <= maxkeyno; j++)
  219.           if ((i < fo_i) || (j <= fo_j))
  220.             if (keylen[i][j] > 0) {
  221.               closeindex(idxkey[i][j]);
  222.               db_free(idxkey[i][j]);
  223.             }
  224.       }
  225. }
  226.  
  227. void closefiles(void)
  228. {
  229.   int i,j;
  230.  
  231.   for (i=1; i <= maxfilno; i++) {
  232.     closefile(datf[i]); db_free(datf[i]);
  233.     for (j=1; j <= maxkeyno; j++) if (keylen[i][j] > 0) {
  234.       closeindex(idxkey[i][j]);
  235.       db_free(idxkey[i][j]);
  236.     }
  237.   }
  238. }
  239.  
  240. void initialize(void)
  241. {
  242.   for (filno = 1; filno <= maxfilno; filno++) {
  243.     recno[filno] = 0;
  244.   }
  245.   filno = 1;
  246.   searchByKey = 1;
  247.   autoMode = ' ';
  248.   strcpy(textFileName,"TEST.TXT");
  249. }
  250.  
  251. void on_error(void)
  252. {
  253.   closefiles();
  254. }
  255.  
  256. void lookforparameters(void)
  257. {
  258.   int i;
  259.   string ts;
  260.  
  261.   for (i = 1; i < paramcount(); i++) {
  262.     strcpy(ts,paramstr(i));
  263.     if ((*ts == '-') || (*ts == '/')) {
  264.       strdelete(ts,0,1);
  265.  
  266.       if (ts[0] == 'I') autoMode = 'I';             /* Import a new record */
  267.  
  268.       if (ts[0] == 'E') autoMode = 'E';                 /* Export a record */
  269.  
  270.       if (ts[0] == 'D') {              /* Change the name of the text file */
  271.         strdelete(ts,0,1);                               /* Remove the "D" */
  272.         strcpy(textFileName,ts);               /* store the text file name */
  273.       }
  274.  
  275.       if (ts[0] == 'F')         {                /* Change the file number */
  276.         strdelete(ts,0,1);                               /* Remove the "F" */
  277.         filno = ival(ts);       /* store the new file number to read/write */
  278.       }
  279.  
  280.       if (ts[0] == 'S') {                          /* Search by key number */
  281.         strdelete(ts,0,1);                               /* Remove the "S" */
  282.         searchByKey = ival(ts);/* store the number of the key to search by */
  283.       }
  284.  
  285.       if (ts[0] == 'L') {                           /* Locate using string */
  286.         strdelete(ts,0,1);                               /* Remove the "L" */
  287.         strcpy(searchFor,ts);            /* store the string to search for */
  288.       }
  289.  
  290.     }
  291.   }
  292. }
  293.  
  294. void inputRecord(void)
  295. {
  296.   FILE *textFile;
  297.   string ts;
  298.  
  299.   textFile = fopen(textFileName,"rt");
  300.   if (textFile != NULL) {
  301.  
  302.     fgets(ts,256,textFile);                            /* read in a string */
  303.     ts[strlen(ts)-1] = '\0';             /* remove the "newline" character */
  304.     pad(ts,ts,8,Right);               /* pad it to match the input picture */
  305.     strcopy(SIMPLE1.TRAN_DATE,ts,0,8);          /* move data to the field */
  306.  
  307.     fgets(ts,256,textFile);                            /* read in a string */
  308.     ts[strlen(ts)-1] = '\0';             /* remove the "newline" character */
  309.     pad(ts,ts,10,Right);              /* pad it to match the input picture */
  310.     strcopy(SIMPLE1.TRAN_ITEM,ts,0,10);          /* move data to the field */
  311.  
  312.     fgets(ts,256,textFile);                            /* read in a string */
  313.     ts[strlen(ts)-1] = '\0';             /* remove the "newline" character */
  314.     pad(ts,ts,40,Right);              /* pad it to match the input picture */
  315.     strcopy(SIMPLE1.TRAN_COMMENT,ts,0,40);       /* move data to the field */
  316.  
  317.     fclose(textFile);
  318.  
  319.     SIMPLE1.recstat = 0;             /* record status flag set to "in use" */
  320.     simpleAddRec(filno);
  321.   }
  322. }
  323.  
  324. void outputRecord(void)
  325. {
  326.   FILE *textFile;
  327.  
  328. /* find first record with a key >= "searchFor" and print it */
  329.   searchkey(idxkey[filno][searchByKey],&recno[filno],searchFor);
  330.   if (ok) {
  331.     simpleGetRec(filno);
  332.  
  333.     textFile = fopen(textFileName,"w+t");
  334.     if (textFile != NULL) {
  335.       fprintf(textFile,"%s\n",SIMPLE1.TRAN_DATE);
  336.       fprintf(textFile,"%s\n",SIMPLE1.TRAN_ITEM);
  337.       fprintf(textFile,"%s\n",SIMPLE1.TRAN_COMMENT);
  338.       fclose(textFile);
  339.     }
  340.   }
  341. }
  342.  
  343. void do_something(void)
  344. {
  345.   lookforparameters();
  346.   if ((filno >= 1) &&
  347.       (filno <= maxfilno) &&
  348.       (searchByKey >= 1) &&
  349.       (searchByKey <= maxkeyno) &&
  350.       (keylen[filno][searchByKey] != 0)) {
  351.     switch (autoMode) {
  352.       case 'I' : inputRecord(); break;
  353.       case 'E' : outputRecord(); break;
  354.     }  /* end switch */
  355.   }
  356. }
  357.  
  358. void main(void)
  359. {
  360.   extfhc_init();
  361.   db_heap_init();
  362.   db_gvar_init();
  363.   db_key_init();
  364.   db_date_init();
  365.   db_funcs_init();
  366.   db_tree_init();
  367. ⁿIFDEF MUSERⁿ
  368.   multiuser = True;
  369. ⁿELSEDEFⁿ
  370.   multiuser = False;
  371. ⁿENDDEFⁿ
  372.   initindex();
  373.   openfiles();
  374.   if (fok) {
  375.     shutdown = on_error;
  376.     initialize();
  377.     do_something();
  378.     closefiles();
  379.   }
  380. }