home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / EDUCATIO / STAGES12.ZIP / MISCS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  6.3 KB  |  277 lines

  1. #include "header.h"
  2. #if PC
  3.   #include <bios.h>
  4. #endif
  5. #include "file.h"
  6.  
  7. /************************************************************************/
  8. /**    miscellaneous functions                           **/
  9. /************************************************************************/
  10. /*      this file includes the functions:
  11.         toupper1
  12.         strupper
  13.         alphanumq
  14.         whitespq
  15.         stripwhitesp
  16.         numq
  17.         numstrq
  18.         makespstr
  19.         hitreturn
  20.         togglepr
  21. */
  22.  
  23. /********************************/
  24. /*     function: toupper1    */
  25. /********************************/
  26. /* replaces brain-damaged toupper of ctype.h:
  27.    if (c >= 'a') and (c <= 'z') then converts to upper
  28.    else returns c as it was. */
  29. toupper1 (c)
  30.   char c;
  31. {
  32.   /* decimal 'a' = 97, decimal 'z' = 122, decimal 'A' = 65 */
  33.   if ((c >= 97) && (c <= 122))
  34.     return(c-32);
  35.   else
  36.     return(c);
  37. } /* toupper1 */
  38.  
  39. /********************************/
  40. /*     function: strupper    */
  41. /********************************/
  42. /* converts a string to all upper case, in place */
  43. strupper (str)
  44.   char *str;
  45. {
  46.   int i;
  47.  
  48.   for ( i=0; i<strlen(str); i++ )
  49.     str[i] = toupper1(str[i]);
  50.   return(0);
  51. } /* strupper */
  52.  
  53. /********************************/
  54. /*     function: alphanumq    */
  55. /********************************/
  56. /* returns 1 if c is alphabetic/numeric/dash
  57.    returns 0 otherwise
  58.    this is used to check user input for legal cell names */
  59. alphanumq (c)
  60.   char c;
  61. {
  62.   if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) ||
  63.       ((c >= '0') && (c <= '9')) || (c=='-'))
  64.     return(1);
  65.   else
  66.     return(0);
  67. } /* alphanumq */
  68.  
  69. /********************************/
  70. /*     function: whitespq    */
  71. /********************************/
  72. /* returns 1 if c is whitespace (space/tab)
  73.    returns 0 otherwise */
  74. whitespq (c)
  75.   char c;
  76. {
  77.   /* c is a char but its integer value works too */
  78.   /* space=32  tab=9  LF=10  CR=13 */
  79.   if ((c==32) || (c==9) || (c==13))
  80.     return(1);
  81.   else
  82.     return(0);
  83. } /* whitespq */
  84.  
  85. /********************************/
  86. /*     function: stripwhitesp    */
  87. /********************************/
  88. /* strips leading and trailing whitespace from a string, in place */
  89. stripwhitesp (str)
  90.   char *str;
  91. { int i, j;
  92.  
  93. #if (DEBUG > 4)
  94. #if PC
  95.   clrscr1(1);
  96.   gotoxy(8, 5);
  97.   for ( i=0; i<strlen(str); i++ )
  98.     cprintf("<%d>", str[i]);
  99.   hitreturn(1);
  100. #else
  101.   for ( i=0; i<strlen(str); i++ )
  102.     printf("<%d>", str[i]);
  103.   printf("\n);
  104. #endif
  105. #endif
  106.   i = j = 0;
  107.   /* skip leading white space */
  108.   while (whitespq(str[j]))
  109.     j++;
  110.   while (! whitespq(str[j]))
  111.     str[i++] = str[j++];
  112.   str[i] = '\0';
  113.   return(0);
  114. } /* stripwhitesp */
  115.  
  116. /********************************/
  117. /*     function: numq        */
  118. /********************************/
  119. /* returns 1 if c is numeric/point/neg sign
  120.    returns 0 otherwise
  121.    this is used to check user input for legal numeric input */
  122. numq (c)
  123.   char c;
  124. {
  125.   if (((c>='0') && (c<='9')) || (c=='-') || (c=='.'))
  126.     return(1);
  127.   else
  128.     return(0);
  129. } /* numq */
  130.  
  131. /********************************/
  132. /*     function: numstrq    */
  133. /********************************/
  134. /* returns 1 if str is a legal floating point number
  135.    (digits, decimal point, negative sign)
  136.    returns 0 otherwise
  137.    this is used to check user input for legal numeric input */
  138. numstrq (str)
  139.   char *str;
  140. {
  141.   int i, len, dotflag;
  142.   
  143.   if ((strcmp(str, "q")==0) || (strcmp(str, "Q")==0))
  144.     printexit(13);
  145.   len = strlen(str);
  146.   if (len == 0)
  147.     return(0);                /* null input invalid */
  148.   dotflag = 0;
  149.   for ( i=0; i<len; i++ ) {
  150.     if ((str[i]=='-') && (i==0))
  151.       /* do nothing */;
  152.     else
  153.       if ((str[i]=='-') && i)
  154.     return(0);    /* neg sign must come 1st, if there is one */
  155.       else {
  156.     if ((str[i]=='.') && dotflag)
  157.       return(0);            /* too many decimal points */
  158.     else {
  159.       if ((str[i]=='.') && ! dotflag)
  160.         dotflag = 1;
  161.       else {
  162.         if ((str[i] < '0') || (str[i] > '9'))
  163.           return(0);        /* not a decimal digit */
  164.       }
  165.     }
  166.       }
  167.   } /* for */
  168.   return(1);        /* get this far, must be an ok number */
  169. } /* numstrq */
  170.  
  171. /********************************/
  172. /*     function: makespstr    */
  173. /********************************/
  174. /* makes a string of len spaces, in str */
  175. makespstr (str, len)
  176.   char *str;
  177.   int len;
  178. {
  179.   int i;
  180.  
  181.   for ( i=0; i<len; i++ )
  182.     str[i] = ' ';
  183.   str[i] = '\0';
  184.   return(0);
  185. } /* makespstr */
  186.  
  187. /********************************/
  188. /*     function: hitreturn    */
  189. /********************************/
  190. /* "press any key to continue"... win will be the active window after */
  191. hitreturn (win)
  192.   int win;
  193. {
  194. #if (! PC)
  195.   char c;
  196. #else
  197.   int key, keylo, keyhi;
  198. #endif
  199.  
  200. #if PC
  201.   clrscr1(2);
  202.   cprintf("%sPress any key to continue (F8 still toggles printer):", tab);
  203.   gotoxy(1, 2);
  204.   highvideo();
  205.   cprintf("%s%c%c%c ", tab2, HBAR, HBAR, RTRI);
  206.   normvideo();
  207. #else
  208.   printf("\n\n\t    type <Enter> to continue...");
  209. #endif
  210.  
  211. #if PC
  212.   /* just get any one key */
  213.   while (bioskey(1)==0);    /* sit & spin */
  214.   key = bioskey(0);        /* read key */
  215.   keylo = key & 0x000000FF;
  216.   keyhi = (key & 0x0000FF00) >> 8;    /* right shift 8 bits */
  217.   switch (keylo) {
  218.   case 0:    /* F key? */
  219.     switch (keyhi) {
  220.     case 66:    /* F8 */
  221.       togglepr();
  222.       break;
  223.     default:
  224.       break;
  225.     } /* switch keyhi */
  226.   default:
  227.     break;
  228.   } /* switch */
  229.   if (win)
  230.     makeactivewin(win);
  231. #else
  232.   while ((c=getc(stdin)) != '\n')
  233.     if (c=='q' || c=='Q')
  234.       printexit(9);
  235. #endif
  236.   return(0);
  237. } /* hitreturn */
  238.  
  239. #if PC
  240.  
  241. /********************************/
  242. /*     function: togglepr    */
  243. /********************************/
  244. /* toggles the printer on/off and prints the annunciator */
  245. togglepr ()
  246. {
  247.   HARDCOPY = HARDCOPY ^ 1;
  248.   if (! introprint) {
  249.     introprint= 1;
  250.     printintro(0, 0);
  251.   }
  252. /* no need for this anymore, there isn't a print annunciator anymore
  253.   printHARDCOPY(); */
  254.   return(0);
  255. } /* togglepr */
  256.  
  257. #endif
  258.  
  259. /********************************/
  260. /*     function: free1        */
  261. /********************************/
  262. /* checks for NULL ptrs to blocks before freeing */
  263. void free1 (blk)
  264.   char *blk;
  265. {
  266.   if (blk == NULL) {
  267. #if PC
  268.     clrscr1(1);
  269.     gotoxy(1, 5);
  270.     cprintf("%sfree1:  Can't free a NULL ptr", tab);
  271. #else
  272.     printf("\tfree1:  Can't free a NULL ptr\n");
  273. #endif
  274.     printexit(666);
  275.   } else
  276.     free(blk);
  277. } /* free1 */