home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / ccua_dbc.zip / DB_FUNCS.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  16KB  |  538 lines

  1. /****************************************************************************/
  2. /*                        DATABOSS MODULE: DB_FUNCS.C                       */
  3. /****************************************************************************/
  4.  
  5. #include "db_lsc.h"
  6.  
  7. #ifdef __TURBOC__
  8.     #include <conio.h>
  9.     #include "db_types.h"
  10.     #include <dir.h>
  11. #else
  12.     #include <graph.h>
  13.     #include "db_types.h"
  14.     #include "db_conio.h"
  15.     #include <direct.h>
  16.     #include <errno.h>
  17. #endif
  18. #include <dos.h>
  19. #include <io.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "db_dos.h"
  25. #include "db_file.h"
  26. #include "db_key.h"
  27. #include "db_str.h"
  28. #include "db_funcs.h"
  29. #include "db_sets.h"
  30.  
  31.  
  32. /****************************  GLOBAL CONSTANTS  ****************************/
  33.  
  34. const uchar ToAlt[] = {
  35.     130,46,47,129,248,249,250,251,252,253,254,255,128,
  36.     58,59,60,131,62,63,64,
  37.     158,176,174,160,146,161,162,163,151,164,165,166,178,
  38.     177,152,153,144,147,159,148,150,175,145,173,149,172};
  39. set _ALPHA = {
  40.     0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0xFC,
  41.     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  42.     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  43.     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F};
  44. set _WORDSET = {
  45.     0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x03,
  46.     0xFE,0xFF,0xFF,0x07,0xFE,0xFF,0xFF,0x07,
  47.     0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,
  48.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  49. set _DIGITS = {
  50.     0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x03,
  51.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  54. set _SPLITSET = {
  55.     0x00,0x00,0x00,0x00,0x11,0xF7,0x00,0x04,
  56.     0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,
  57.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  59. set ExitSet = {
  60.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62.     0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x78,
  63.     0x1F,0x03,0xF3,0xFF,0xFF,0xFF,0xE3,0x00};
  64.  
  65. const uchar HexDigit[] = "0123456789ABCDEF";
  66.  
  67. set _NumFld = {
  68.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  69.     0x02,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
  70.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  71.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  72.  
  73. uchar  LineStr[41] = "╪╧╤╞╡╘╛╒╕╫╨╥╟╢╙╜╓╖╬╦╩╠╣╚╝╔╗═║┼┬┴├┤└┘┌┐─│";
  74. set    SLS         = {'─','└','┴','┬','├','┼','╟','╨','╥','╙','╓','╫','┌'};
  75. set    SRS         = {'─','┤','╢','╖','╜','┐','┴','┬','┼','╨','╥','╫','┘'};
  76. set    SUS         = {'│','┤','╡','╕','┐','┬','├','┼','╞','╤','╒','╪','┌'};
  77. set    SDS         = {'│','┤','╡','╛','└','┴','├','┼','╞','╧','╘','╪','┘'};
  78. set    DLS         = {'═','╞','╚','╔','╩','╦','╠','╬','╧','╤','╘','╒','╪'};
  79. set    DRS         = {'═','╡','╕','╣','╗','╝','╛','╩','╦','╬','╧','╤','╪'};
  80. set    DUS         = {'║','╢','╖','╣','╗','╟','╔','╦','╠','╬','╥','╓','╫'};
  81. set    DDS         = {'║','╢','╣','╝','╜','╟','╚','╩','╠','╬','╨','╙','╫'};
  82.  
  83. /**********************  GLOBAL INITIALIZED VARIABLES  **********************/
  84.  
  85. byte _av       = 112;
  86. byte _pv       = 7;
  87. byte _iv       = 7;
  88. byte _ev       = 15;
  89. byte errcolr   = 79;
  90. byte diratr    = 0x70;
  91. byte edt_spos  = 0;
  92. int _Tab_Space = 6;
  93.  
  94. /***************************  INTERNAL VARIABLES  ***************************/
  95.  
  96. static bool initialized = False;
  97.  
  98. /*****************************  IMPLEMENTATION  *****************************/
  99.  
  100. bool nocheck(string instr)
  101. {
  102.     return (True);
  103. }
  104.  
  105. void nohelp(void)
  106. {
  107. }
  108.  
  109. bool chkyesno(string instr)
  110. {
  111.     return ((bool) ((*instr == _YES) || (*instr == _NO)));
  112. }
  113.  
  114. void clr_line(byte y)
  115. {
  116.     gotoxy(1,y);
  117.     clreol();
  118. }
  119.  
  120.  
  121. void clearInputBuffer(void)                                          /* SN 3.5 */
  122. {                                                                    /* SN 3.5 */
  123.    while(kpressed()) getkey();                                       /* SN 3.5 */
  124.    return;                                                           /* SN 3.5 */
  125. }                                                                    /* SN 3.5 */
  126.  
  127. uchar smartCh(string startSet, uchar charA, uchar charB, uchar charC, uchar charD)   /* SN 3.5 */
  128. {
  129.    uchar   tempChar;                                                                 /* SN 3.5 */
  130.    string  tempSet;                                                                  /* SN 3.5 */
  131.    int     counter;                                                                  /* SN 3.5 */
  132.  
  133.    strcpy(tempSet, startSet);                                                        /* SN 3.5 */
  134.    if (charA != ' ') {                                                               /* SN 3.5 */
  135.       if (set_member(SUS, (byte)charA)) set_intersection(tempSet, tempSet, SDS);     /* SN 3.5 */
  136.       if (set_member(DUS, (byte)charA)) set_intersection(tempSet, tempSet, DDS);     /* SN 3.5 */
  137.    }
  138.    if (charB != ' ') {                                                               /* SN 3.5 */
  139.       if (set_member(SRS, (byte)charB)) set_intersection(tempSet, tempSet, SLS);     /* SN 3.5 */
  140.       if (set_member(DRS, (byte)charB)) set_intersection(tempSet, tempSet, DLS);     /* SN 3.5 */
  141.    }
  142.    if (charC != ' ') {                                                               /* SN 3.5 */
  143.       if (set_member(SDS, (byte)charC)) set_intersection(tempSet, tempSet, SUS);     /* SN 3.5 */
  144.       if (set_member(DDS, (byte)charC)) set_intersection(tempSet, tempSet, DUS);     /* SN 3.5 */
  145.    }
  146.    if (charD != ' ') {                                                               /* SN 3.5 */
  147.       if (set_member(SLS, (byte)charD)) set_intersection(tempSet, tempSet, SRS);     /* SN 3.5 */
  148.       if (set_member(DLS, (byte)charD)) set_intersection(tempSet, tempSet, DRS);     /* SN 3.5 */
  149.    }
  150.  
  151.    tempChar = ' ';                                                                   /* SN 3.5 */
  152.    if (!(*tempSet)) strcpy(tempSet, startSet);                                       /* SN 3.5 */
  153.    for(counter = 40; counter >= 0; counter--) {                                      /* SN 3.5 */
  154.       if (tempChar == ' ') {                                                         /* SN 3.5 */
  155.      if (strchr(tempSet, *(LineStr + counter))) tempChar = *(LineStr + counter); /* SN 3.5 */
  156.       }
  157.    }
  158.  
  159.    return(tempChar);                                                                 /* SN 3.5 */
  160. }
  161.  
  162.  
  163. void noise(int freq, double secs)
  164. {
  165.     int duration;
  166.  
  167.     delay(0);
  168.     duration = (int) floor(secs*1000);
  169.     sound(freq);
  170.     delay(duration);
  171.     nosound();
  172. }
  173.  
  174. void audible(audibletyp signal)
  175. {
  176.     switch (signal) {
  177.         case Beep    :
  178.         case Error   : noise(897,0.5); break;
  179.         case Warning : noise(200,0.05); break;
  180.         case Pip     : noise(3500,0.025); break;
  181.         case Fluff   : noise(30,0.025); break;
  182.     }
  183. }
  184.  
  185. void delayorkey(double secs)
  186. {
  187.     word count;
  188.  
  189.     count = 0;
  190.     do {
  191.         delay(100);
  192.     } while ((!kpressed()) && (++count < ((word) (secs*10))));
  193.     if (kpressed()) getkey();
  194. }
  195.  
  196.  
  197. long maximum(long number1, long number2)                             /* SN 3.5 */
  198. {                                                                    /* SN 3.5 */
  199.    return((number1 > number2) ? number1 : number2);                  /* SN 3.5 */
  200. }                                                                    /* SN 3.5 */
  201.  
  202. long minimum(long number1, long number2)                             /* SN 3.5 */
  203. {                                                                    /* SN 3.5 */
  204.    return((number1 < number2) ? number1 : number2);                  /* SN 3.5 */
  205. }                                                                    /* SN 3.5 */
  206.  
  207.  
  208. void fillstr(string s, int len, uchar ch)
  209. {
  210.     memset(s,ch,len);
  211.     s[len] = 0;
  212. }
  213.  
  214. strptr decchar(string sout, string s)
  215. {
  216.     byteptr sptr;
  217.  
  218.     sptr = (byteptr) sout;
  219.     while (*s)
  220.         *(sptr++) = (byte) (255-((byte) *(s++)));
  221.     *sptr = 0;
  222.     return (sout);
  223. }
  224.  
  225. strptr strip(string sout, string field)
  226. {
  227.     strptr sptr;
  228.  
  229.     sptr = sout;
  230.     do
  231.         if (*field != ' ') *(sptr++) = *field;
  232.     while (*(field++));
  233.     return (sout);
  234. }
  235.  
  236. strptr trim(string sout, string inpstr)
  237. {
  238.     int cp;
  239.  
  240.     cp = strlen(inpstr);
  241.     while (cp && (inpstr[cp - 1] == ' ')) cp--;
  242.     strncpy(sout,inpstr,cp);
  243.     sout[cp] = 0;
  244.     return (sout);
  245. }
  246.  
  247. strptr mirt(string sout, string inpstr)
  248. {
  249.     while (*inpstr == ' ') inpstr++;
  250.     strcpy(sout,inpstr);
  251.     return (sout);
  252. }
  253.  
  254. strptr pad(string sout, string str, int size, int side)
  255. {
  256.     int len,padding;
  257.  
  258.     len = strlen(str);
  259.     padding = size - len;
  260.     if (padding > 0)
  261.         if (side == 0) {
  262.             memmove(sout + padding,str,len+1);
  263.             memset(sout,' ',padding);
  264.         }
  265.         else {
  266.             memset(stpcpy(sout,str),' ',padding);
  267.             sout[size] = 0;
  268.         }
  269.     else
  270.         strcpy(sout,str);
  271.     return (sout);
  272. }
  273.  
  274. strptr centre(string sout, string str, int size)
  275. {
  276.     int len,lpad,rpad;
  277.  
  278.     len = strlen(str);
  279.     rpad = (size - len) / 2;
  280.     lpad = size - len - rpad;
  281.     if (size > len) {
  282.         memmove(sout + lpad,str,len);
  283.         memset(sout,' ',lpad);
  284.         memset(sout + lpad + len,' ',rpad);
  285.         sout[size] = 0;
  286.     }
  287.     else
  288.         strcpy(sout,str);
  289.     return (sout);
  290. }
  291.  
  292. bool empty(string field)
  293. {
  294.     while (*field == ' ') field++;
  295.     return ((bool) (*field == 0));
  296. }
  297.  
  298. bool no_spaces(string field)
  299. {
  300.     return ((bool) (strchr(field,' ') == NULL));
  301. }
  302.  
  303. strptr istr(string sout, long num, int s)
  304. {
  305.    string tempString;
  306.  
  307.     sprintf(tempString,"%*ld",s,num);
  308.    *(tempString + s) = '\0';
  309.    strcpy(sout, tempString);
  310.     return (sout);
  311. }
  312.  
  313. strptr fstr(string sout, double num, int s, int d)
  314. {
  315.    string tempString;
  316.    char *commaPos;
  317.  
  318.     sprintf(tempString,"%*.*lf", s,d,num);
  319.    *(tempString + s) = '\0';
  320.    if (*_dcpt != '.') {
  321.       commaPos = strchr(tempString, '.');
  322.       if (commaPos) *commaPos = *_dcpt;
  323.    }
  324.    strcpy(sout, tempString);
  325.     return (sout);
  326. }
  327.  
  328. double valu(string numstr)
  329. {
  330.    char *commaPos;
  331.    string numberString;
  332.  
  333.    strip(numberString, numstr);
  334.    if (*_dcpt != '.') {
  335.       commaPos = strchr(numberString, *_dcpt);
  336.       if (commaPos) *commaPos = '.';
  337.    }
  338.     return (atof(numberString));
  339. }
  340.  
  341. long ival(string numstr)
  342. {
  343.     return(atol(numstr));
  344. }
  345.  
  346. void float_ins(uchar c, string strg)
  347. {
  348.     strptr destptr;
  349.  
  350.     while (*strg == ' ') strg++;
  351.     destptr = strg;
  352.     memmove(++destptr,strg,strlen(strg) + 1);
  353.     *strg = c;
  354. }
  355.  
  356. bool path_ok(pathstr path)
  357. {
  358.     pathstr srchpath;
  359.     findrec frec;
  360.  
  361.     strcpy(srchpath,path);
  362.     if (*srchpath && (srchpath[strlen(srchpath) - 1] != '\\')) strcat(srchpath,"\\*.*");
  363.     else                                                       strcat(srchpath,"*.*");
  364.     return (db_findfirst(srchpath,&frec,A_DIREC));
  365. }
  366.  
  367. strptr parse_new_path(strptr sout, string path)
  368. {
  369.     string ts;
  370.     uchar  drv[2];
  371.     int p;
  372.  
  373.     strcpy(drv, " ");
  374.     strcpy(sout,path);
  375.     getcurrentdir(ts,0); drv[0] = ts[0];
  376.     if (strposstr(":\\",path) != 1) {
  377.         if (path[0] == '\\') strconcat(sout,drv,":",path,NULL);
  378.         else if (path[1] == ':') {
  379.             strinsert("\\",sout,2);
  380.             if (sout[3] == '\0')    getcurrentdir(sout,(byte)(sout[0]-64));
  381.             else if (!path_ok(sout)) {
  382.                 getcurrentdir(ts,(byte)(sout[0]-64));
  383.                 strconcat(sout,ts,strcopy(sout,sout,2,strlen(sout)-2),NULL);
  384.             }
  385.         }
  386.         else {
  387.             strconcat(sout,ts,"\\",path,NULL);
  388.             p = strposch('\\',sout);
  389.             if (p >= 0) strdelete(sout,p,1);
  390.         }
  391.     }
  392.     return(sout);
  393. }
  394.  
  395. void makedir(string dirstr)
  396. /* Expects full drive and path specification */
  397. {
  398.     uchar *s, *wks;
  399.     string ts, tdirstr;
  400.     int i, err;
  401.  
  402.     ts[0] = '\0';
  403.     parse_new_path(tdirstr,strip(tdirstr,dirstr));
  404.     s = tdirstr;
  405.     while (s[0] && s[1]) s++;
  406.     if (*s == '\\') *s = 0;
  407.     err = mkdir(tdirstr);
  408.     if (err != 0) err = errno;
  409.     if ((err == 2) || (err == 3)) {
  410.         while ((err == 2) || (err == 3) && (*tdirstr)){
  411.             while ((*s != '\\') && (s != tdirstr)) s--;
  412.             strconcat(ts,s,ts,NULL);
  413.             *s = 0;
  414.             err = mkdir(tdirstr);
  415.             if (err != 0) err = errno;
  416.         }
  417.         wks = ts;
  418.         while ((*wks) && (err == 0)) {
  419.             do {
  420.                 *s++ = *wks;
  421.                 wks++;
  422.             }       while ((*wks) && (*wks != '\\'));
  423.             *s = 0;
  424.             err = mkdir(tdirstr);
  425.         }
  426.     }
  427. }
  428.  
  429. bool pathcouldexist(string p)
  430. {
  431.     uchar baddirchrs[20];
  432.     string ts, tp;
  433.     bool valid;
  434.     int i;
  435.  
  436.     strcpy(baddirchrs, "=\"/+*.:;<>|?");
  437.     strip(tp,p);  valid = (tp[0] != '\0');
  438.     if (valid) {
  439.         ts[0] = '\0';
  440.         i = strposch('\\',tp);    /* Assume starts with 'X:\' */
  441.         if (i >= 0) strdelete(tp,0,i+1); else tp[0] = '\0';
  442.         while ((tp[0] != '\0') && (valid)) {
  443.             i = strposch('\\',tp);
  444.      if (i < 0) i = 0;
  445.             strcopy(ts,tp,0,i);
  446.             strdelete(tp,0,i+1);
  447.             if (strlen(ts) < 9)
  448.                 valid = (strpbrk(ts,baddirchrs) == NULL);
  449.             else
  450.                 valid = False;
  451.         }
  452.     }
  453.     return(valid);
  454. }
  455.  
  456. bool name_ok(string fname)
  457. {
  458.     string tfname;
  459.     uchar badnamechrs[20];
  460.     bool valid;
  461.     int i;
  462.  
  463.     strcpy(badnamechrs, "\"*+,./:;<=>?[\\]^|");
  464.     valid = True;
  465.     strcpy(tfname,fname);
  466.     i = strposch('.',tfname);
  467.     if (i >= 0) tfname[i] = '\0';
  468.     i = 0;
  469.     while ((valid) && (i < strlen(tfname))) {
  470.         valid = (strpbrk(tfname,badnamechrs) == NULL) && (tfname[i] >= '!') && (tfname[i] <= '~');
  471.         i++;
  472.     }
  473.     return(valid);
  474. }
  475.  
  476. bool exists(pathstr fname)
  477. {
  478.     return ((bool) (access(fname,0x42) == 0));
  479. }
  480.  
  481. strptr hexs(string sout, int num, int sp)
  482. {
  483.     if (num < 0) sprintf(sout,"$-%X",abs(num));
  484.     else         sprintf(sout,"$%X",num);
  485.     pad(sout,sout,sp,Left);
  486.     return (sout);
  487. }
  488.  
  489. strptr hexstr(string sout, int num)
  490. {
  491.     if (num < 0) sprintf(sout,"$-%X",abs(num));
  492.     else         sprintf(sout,"$%X",num);
  493.     return (sout);
  494. }
  495.  
  496. void allocstr(int fileHandle, strptr stringPointer)                                          /* SN 3.5 */
  497. {                                                                                            /* SN 3.5 */
  498.     byte   stringSize;                                                                        /* SN 3.5 */
  499.                                                   /* SN 3.5 */
  500.     stringPointer = 0;                                                                        /* SN 3.5 */
  501.     if (read(fileHandle, &stringSize, 1) != -1) {                                             /* SN 3.5 */
  502.         if (stringSize) {                                                                      /* SN 3.5 */
  503.      if ((stringPointer = malloc(stringSize + 1)) != NULL) {                                      /* SN 3.5 */
  504.          *stringPointer = stringSize;                                                     /* SN 3.5 */
  505.          if (read(fileHandle, (stringPointer + 1), stringSize) == -1) stringPointer = 0;  /* SN 3.5 */
  506.      }                                                                                   /* SN 3.5 */
  507.         }                                                                                      /* SN 3.5 */
  508.     }                                                                                         /* SN 3.5 */
  509.     return;                                                                                   /* SN 3.5 */
  510. }                                                                                            /* SN 3.5 */
  511.  
  512. void xchgstrp(strptr stringPointer, string aString)                                          /* SN 3.5 */
  513. {                                                                                            /* SN 3.5 */
  514.     if (stringPointer) free(stringPointer);                                                   /* SN 3.5 */
  515.     stringPointer = malloc(strlen(aString) + 1);                                              /* SN 3.5 */
  516.     memcpy(stringPointer, aString, strlen(aString));                                          /* SN 3.5 */
  517.     return;                                                                                   /* SN 3.5 */
  518. }                                                                                            /* SN 3.5 */
  519.  
  520. /**********************  UNIT INITIALIZATION/EXIT CODE  *********************/
  521.  
  522. void db_funcs_init(void)
  523. {
  524.     if (!initialized) {
  525.         initialized = True;
  526.         db_key_init();
  527. /***************************************************************************
  528. * [DG] 09/10/92 -- Added CtrlEnter, Tab, and STab to ExitSet, allowing     *
  529. *                  these keys to end field editing.                        *
  530. ***************************************************************************/
  531.         set_addmember(ExitSet, CtrlEnter);
  532.         set_addmember(ExitSet, Tab);
  533.         set_addmember(ExitSet, STab);
  534.     }
  535. }
  536.  
  537. /****************************  END OF DB_FUNCS.C  ***************************/
  538.