home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gifbla20.zip / SOURCE / UBASIC.C < prev    next >
C/C++ Source or Header  |  1992-12-15  |  4KB  |  151 lines

  1.  
  2. /* ubasic.c - Some basic routines, UNIX version. */
  3.  
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #include "ubasic.h"
  9.  
  10. extern void *malloc P((size_t size),());
  11. extern void free P((void *block),());
  12.  
  13. void *(*basic_alloc) P((size_t size),()) = malloc;
  14. void (*basic_free) P((void *block),()) = free;
  15.  
  16. free_fnames(fnames,count,freearr)
  17. char **fnames; int count; int freearr;
  18. {
  19.     int i;
  20.  
  21.     if (fnames == NULL)
  22.         return;
  23.     for (i=0; i<count; i++)
  24.         (*basic_free)(fnames[i]);
  25.     if (freearr)
  26.         (*basic_free)(fnames);
  27. }
  28.  
  29. static int
  30. extract_long(str,l)
  31. unsigned char *str; long *l;
  32. {
  33.     int negative;
  34.     long oldval;
  35.  
  36.     (*l) = 0;
  37.     negative = (*str=='-');
  38.     if (*str=='+' || *str=='-')
  39.         str++;
  40.     do {
  41.         if (!isdigit(*str))
  42.             return FALSE;
  43.         oldval = (*l);
  44.         (*l) = (*l)*10 + (*str) - '0';
  45.         if ((*l) < oldval)
  46.             return FALSE;
  47.         str++;
  48.     } while (*str != '\0');
  49.     if (negative)
  50.         (*l) = -(*l);
  51.     return TRUE;
  52. }
  53.  
  54. process_command_line(pargc,pargv,opts)
  55. int *pargc; char **(*pargv); OPTION *opts;
  56. {
  57.     int argno,newargno;
  58.     OPTION *optr;
  59.     char **newargv;
  60.     long larg;
  61.  
  62.     if ((*pargc) <= 0) {
  63.         (*pargc) = 0;
  64.         (*pargv) = NULL;
  65.         return;
  66.     }
  67.     newargno = 1;
  68.     for (argno=1; argno<(*pargc); argno++) {
  69.         if (opts != NULL) {
  70.             for (optr=opts; optr->op_name!=NULL; optr++)
  71.                 if (strcmp((*pargv)[argno],optr->op_name) == 0)
  72.                     break;
  73.         }
  74.         if (opts==NULL || optr->op_name==NULL)
  75.             newargno++;
  76.         else if (optr->op_type==STRARGOPT || optr->op_type==INTARGOPT
  77.             || optr->op_type==LONGARGOPT) {
  78.             if (argno == (*pargc)-1)
  79.                 uhalt(("argument required for %s option\n",optr->op_name));
  80.             else
  81.                 argno++;
  82.         }
  83.     }
  84.     if ((newargv=(*basic_alloc)((size_t)newargno*sizeof(newargv[0])))==NULL
  85.         || (newargv[0]=(*basic_alloc)(strlen((*pargv)[0])+1))==NULL)
  86.         uhalt(("out of memory while processing command line\n"));
  87.     strcpy(newargv[0],(*pargv)[0]);
  88.     argno = newargno = 1;
  89.     for (argno=1; argno<(*pargc); argno++) {
  90.         if (opts != NULL) {
  91.             for (optr=opts; optr->op_name!=NULL; optr++)
  92.                 if (strcmp((*pargv)[argno],optr->op_name) == 0)
  93.                     break;
  94.         }
  95.         if (opts==NULL || optr->op_name==NULL) {
  96.             newargv[newargno] = (*basic_alloc)(strlen((*pargv)[argno])+1);
  97.             if (newargv[newargno] == NULL)
  98.                 uhalt(("out of memory while copying file names\n"));
  99.             strcpy(newargv[newargno],(*pargv)[argno]);
  100.             newargno++;
  101.         } else {
  102.             switch (optr->op_type) {
  103.             case SWITCHONOPT:
  104.                 (*(int *)optr->op_pval) = TRUE;
  105.                 break;
  106.             case SWITCHOFFOPT:
  107.                 (*(int *)optr->op_pval) = FALSE;
  108.                 break;
  109.             case STRARGOPT: case INTARGOPT: case LONGARGOPT:
  110.                 argno++;
  111.                 if (optr->op_type == STRARGOPT) {
  112.                     (*((char **)optr->op_pval)) =
  113.                         (*basic_alloc)(strlen((*pargv)[argno])+1);
  114.                     if ((*((char **)optr->op_pval)) == NULL)
  115.                         uhalt(("out of memory while copying %s option",
  116.                             optr->op_name));
  117.                     strcpy((*((char **)optr->op_pval)),(*pargv)[argno]);
  118.                 } else if (!extract_long(
  119.                         (unsigned char *)((*pargv)[argno]),&larg))
  120.                     uhalt(("invalid integer for %s option\n",optr->op_name));
  121.                 else if (optr->op_type == LONGARGOPT)
  122.                     (*(long *)optr->op_pval) = larg;
  123.                 else if (larg != (long)(int)larg)
  124.                     uhalt(("integer overflow in %s option\n",optr->op_name));
  125.                 else
  126.                     (*(int *)optr->op_pval) = (int)larg;
  127.                 break;
  128.             default:
  129.                 uhalt(("error: unknown option type %d\n",(int)optr->op_type));
  130.                 break;
  131.             }
  132.         }
  133.     }
  134.     (*pargc) = newargno;
  135.     (*pargv) = newargv;
  136. }
  137.  
  138. long
  139. divup(n,m)
  140. long n; long m;
  141. {
  142.     return (n+m-1)/m;
  143. }
  144.  
  145. long
  146. multup(n,m)
  147. long n; long m;
  148. {
  149.     return m*divup(n,m);
  150. }
  151.