home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / WINWAIS / IR / CUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-07  |  10.5 KB  |  446 lines

  1. /* Wide AREA INFORMATION SERVER SOFTWARE
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    3.26.90      Harry Morris, morris@think.com
  6.    4.11.90  HWM - generalized conditional includes (see c-dialect.h)
  7. */
  8. #define _C_C_util_
  9.  
  10. #include <dos.h>
  11. #include <windows.h>
  12.  
  13. #undef TRUE
  14. #undef FALSE
  15. #undef IGNORE
  16.  
  17. #include "cdialect.h"
  18. #include "cutil.h"
  19. #include "panic.h"
  20.  
  21. #include <string.h>
  22. #include <varargs.h>
  23.  
  24. extern unsigned int vb_data;
  25. extern long allocated_memory;
  26. int total_segs;
  27.  
  28. typedef WORD HANDLE32;
  29. #define MAXMEMSEGS 5
  30. HANDLE hSegment[MAXMEMSEGS];
  31.  
  32. int fs_meminit(numsegs)
  33. int numsegs;
  34. {
  35.   BOOL bRetVal;
  36.   LPSTR lp;
  37.   WORD wSeg, wSize;
  38.   int x;
  39.  
  40.   bRetVal = 0;
  41.   total_segs = MIN(numsegs, MAXMEMSEGS); 
  42.  
  43.   for(x = 0; x < total_segs; x++)
  44.   {
  45.     if(!(hSegment[x] = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (long)(63L*1024L)))) 
  46.       return FALSE;
  47.     lp = GlobalLock(hSegment[x]);
  48.     wSeg = HIWORD(lp);
  49.     wSize = (WORD)GlobalSize(hSegment[x]) - 16;
  50.     bRetVal |= LocalInit(wSeg, 0, wSize);
  51.     GlobalUnlock(hSegment[x]); // undo localinit's global lock, but since we're
  52.   }                            // always in prot mode, leave the segment locked
  53.   return bRetVal;
  54. }
  55.  
  56. void* fs_malloc(size)
  57. long size;
  58. {
  59.     HANDLE hMem, hSeg;
  60.     LPSTR lp;
  61.     PSTR p;
  62.     WORD wSeg;
  63.     static int which = -1;
  64.     register void *ptr;
  65.     
  66.     if (!size)        // retrofit from b5
  67.       return((void*)NULL);
  68.     
  69.     if(size > 256L)
  70.     {
  71.       hSeg = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size); 
  72.       lp = GlobalLock(hSeg);
  73.       memset((void*)lp, 0, size);
  74.       return((void*)lp);
  75.     }
  76.     
  77.     p = 0;
  78.     
  79.     if(++which == total_segs) 
  80.       which = 0;
  81.  
  82.     lp = GlobalLock(hSegment[which]); // get the segment address
  83.     wSeg = HIWORD(lp);
  84.     asm push ds;
  85.     asm mov  ax, wSeg;
  86.     asm mov  ds, ax;
  87.     hMem = LocalAlloc(LMEM_DISCARDABLE | LMEM_MOVEABLE, size);
  88.     if(hMem)
  89.       p = LocalLock(hMem);
  90.     asm pop ds;
  91.     GlobalUnlock(hSegment[which]);
  92.     if(!hMem || !p)
  93.       MessageBox(GetFocus(), "Memory allocation failed", "WAIS Error", MB_OK);
  94.     ptr = (void*)MAKELONG(p, wSeg);
  95.     memset(ptr, 0, size);
  96.     return(ptr);
  97. }
  98.  
  99. void fs_free(ptr)
  100. void* ptr;
  101. {
  102.     HANDLE hMem, hSeg;
  103.     LPSTR lp;
  104.     WORD w, wSeg;
  105.     int x;
  106.  
  107.     if (ptr == NULL) 
  108.       return;
  109.     
  110.     w = LOWORD(ptr); 
  111.     hSeg = LOWORD(GlobalHandle(HIWORD(ptr)));
  112.     if(w == vb_data || hSeg == vb_data)
  113.       MessageBox(GetFocus(), "ATTEMPT TO FREE VB DATA", "WAIS Error", MB_OK);
  114.     for(x = 0; x < total_segs; x++)
  115.     {
  116.       if(hSeg == hSegment[x])
  117.       {
  118.         lp = GlobalLock(hSeg);
  119.         wSeg = HIWORD(lp);
  120.         asm push ds;
  121.         asm mov  ax, wSeg;
  122.         asm mov  ds, ax;
  123.         hMem = LocalHandle(w);
  124.         if(hMem) 
  125.         {
  126.           LocalUnlock(hMem);
  127.           LocalFree(hMem);
  128.         }
  129.         asm pop ds;
  130.         GlobalUnlock(hSeg);
  131.         if(!hMem)
  132.           MessageBox(GetFocus(), "Memory release failed", "WAIS Error", MB_OK);
  133.         return;
  134.       }
  135.     }
  136.     
  137.     GlobalUnlock(hSeg);
  138.     GlobalFree(hSeg);
  139.     return;
  140. }
  141.  
  142. void* fs_realloc(ptr,size)
  143. void* ptr;
  144. long size;
  145. {
  146.     HANDLE hMem, hNewMem, hSeg;
  147.     LPSTR lp;
  148.     PSTR p;
  149.     WORD w, wSeg;
  150.     int x;
  151.  
  152.     if (ptr == NULL)
  153.       return((void*)s_malloc((long)size));
  154.  
  155.     hMem = hNewMem = NULL;
  156.     p = 0;
  157.     
  158.     w = LOWORD(ptr);
  159.     hSeg = LOWORD(GlobalHandle(HIWORD(ptr)));
  160.     for(x = 0; x < total_segs; x++)
  161.     {
  162.       if(hSeg == hSegment[x])
  163.       {  
  164.         lp = GlobalLock(hSeg);
  165.         wSeg = HIWORD(lp);
  166.         asm push ds;
  167.         asm mov  ax, wSeg;
  168.         asm mov  ds, ax;
  169.         hMem = LocalHandle(w);
  170.         if(hMem)
  171.         {  
  172.           LocalUnlock(hMem);
  173.           hNewMem = LocalReAlloc(hMem, size, LMEM_DISCARDABLE | LMEM_MOVEABLE);
  174.           if(hNewMem)
  175.             p = LocalLock(hNewMem);
  176.         }
  177.         asm pop ds;
  178.         GlobalUnlock(hSeg);
  179.         if(!hMem || !hNewMem || !p) 
  180.           MessageBox(GetFocus(), "Memory reallocation failed", "WAIS Error", MB_OK);
  181.         return((void*)MAKELONG(p, wSeg));
  182.       }
  183.     }
  184.     
  185.     GlobalUnlock(hSeg);
  186.     hSeg = GlobalReAlloc(hSeg, size, GMEM_MOVEABLE | GMEM_ZEROINIT);
  187.     return((void*)GlobalLock(hSeg));
  188. }
  189.  
  190. void fs_memterm()
  191. {
  192.   int x;
  193.   int memsegs = total_segs;
  194.   
  195.   for(x = 0; x < memsegs; x++)
  196.   {  
  197.     GlobalUnlock(hSegment[x]);
  198.     GlobalFree(hSegment[x]);
  199.   }
  200. }
  201.  
  202.  
  203. void fs_checkPtr(ptr)
  204. void* ptr;
  205. {
  206.   if (ptr == NULL)
  207.     MessageBox(GetFocus(), "Memory reallocation (>1K) failed", "WAIS Error", MB_OK);
  208. }
  209.  
  210. char* s_strdup(s)
  211. char* s;
  212.  
  213. /* return a copy of s.  This is identical to the standard library routine
  214.    strdup(), except that it is safe.  If s == NULL or malloc fails,
  215.    appropriate action is taken.
  216.  */
  217. {
  218.   unsigned long len;
  219.   char* copy = NULL;
  220.  
  221.   if (s == NULL)                /* saftey check to postpone stupid errors */
  222.     return(NULL);
  223.  
  224.   len = strlen(s);              /* length of string - terminator */
  225.  
  226.   copy = (char*)s_malloc((long)((long)sizeof(char)*(len + 1L)));
  227.   strncpy(copy,s,len + 1);
  228.   return(copy);
  229. }
  230.  
  231. char* fs_strncat(dst,src,maxToAdd,maxTotal)
  232. char* dst;
  233.    char* src;
  234.    long maxToAdd;
  235.    long maxTotal;
  236.  
  237. /* like strncat, except the fourth argument limits the maximum total
  238.    length of the resulting string
  239.  */
  240. {
  241.   long dstSize = strlen(dst);
  242.   long srcSize = strlen(src);
  243.  
  244.   if (dstSize + srcSize < maxTotal) /* use regular old strncat */
  245.     return(strncat(dst,src,maxToAdd));
  246.   else
  247.     { long truncateTo = maxTotal - dstSize - 1;
  248.       char   saveChar = src[truncateTo];
  249.       char*  result = NULL;
  250.       src[truncateTo] = '\0';
  251.       result = strncat(dst,src,maxToAdd);
  252.       src[truncateTo] = saveChar;
  253.       return(result);
  254.     }
  255. }
  256.  
  257. /*----------------------------------------------------------------------*/
  258.  
  259. typedef long (longfunc) _AP((long c));
  260.  
  261. char*
  262. strtokf(s1,isDelimiter)
  263. char* s1;
  264. longfunc *isDelimiter; /* really *isDelimiter() */
  265.  
  266. /* This function is exactly like strtok, except that instead of passing a
  267.    delimiter string, you pass a function that decides if a character is
  268.    a delimiter or not, returning IS_DELIMITER or NOT_DELIMITER respecively.
  269.    Note that passing a NULL delimiter function will cause the last delimiter
  270.    to be used.
  271.  */
  272. {
  273.   static char* searchStr = NULL;
  274.   static longfunc *delimiterFunc;
  275.   long i;
  276.   char* startTok = NULL;
  277.  
  278.   if (s1 != NULL)               /* passing s1 = NULL says use the last pos */
  279.     searchStr = s1;
  280.  
  281.   if (isDelimiter != NULL)
  282.     delimiterFunc = isDelimiter;
  283.  
  284.   if (searchStr == NULL || searchStr[0] == '\0')
  285.     return(NULL);               /* nothing left to search */
  286.  
  287.   if (delimiterFunc == NULL)
  288.     return(NULL);               /* no delimiter to search with */
  289.  
  290.   /* find the start of the next token */
  291.   for (i = 0; searchStr[i] != '\0'; i++)
  292.     { if ((*delimiterFunc)((long)searchStr[i]) == NOT_DELIMITER)
  293.         break;
  294.     }
  295.  
  296.   if (searchStr[i] == '\0')
  297.     return(NULL);               /* read to end of search string */
  298.   else
  299.     startTok = searchStr + i;   /* remember the starting point for this token*/
  300.  
  301.   /* find the end of the next token */
  302.   for (; searchStr[i] != '\0'; i++)
  303.     { if ((*delimiterFunc)((long)searchStr[i]) == IS_DELIMITER)
  304.         break;
  305.     }
  306.  
  307.   /* if the end is a delimiter (and not just the end of the search string)
  308.      replace it with '\0', and put searchStr just beyond it, otherwise
  309.      put searchStr at the terminator. */
  310.   if (searchStr[i] != '\0')
  311.     { searchStr[i] = '\0';
  312.       searchStr = searchStr + i + 1;
  313.     }
  314.   else
  315.     searchStr = searchStr + i;
  316.  
  317.   return(startTok);
  318. }
  319.  
  320. /*----------------------------------------------------------------------*/
  321.  
  322. extern char* log_file_name;
  323. extern FILE* logfile;
  324.  
  325. /*----------------------------------------------------------------------*/
  326.  
  327. void
  328. warn(message)
  329. char* message;
  330.  
  331. {
  332.   printf("%s\n<press return to continue>\n",message);
  333.   getchar();
  334. }
  335.  
  336. /*----------------------------------------------------------------------*/
  337. boolean substrcmp(string1,string2)
  338. char *string1, *string2;
  339. {
  340.   /* compares the strings up until one of then ends.
  341.    * returns true if they are the same, false if not.
  342.    */
  343.   long count = 0;
  344.   while(true){
  345.     if(count >= strlen(string1) ||
  346.        count >= strlen(string2)){
  347.       return(true);
  348.     }
  349.     else if(string1[count] != string2[count]){
  350.       return(false);
  351.     }
  352.     count++;
  353.   }
  354. }
  355.  
  356. /*----------------------------------------------------------------------*/
  357. extern char *ctime();
  358. char *printable_time()
  359. {
  360.   static char *string;
  361.  
  362.   time_t tptr;
  363.   time(&tptr);
  364.   string = ctime(&tptr);
  365.   if(string){
  366.     if(string[strlen(string)-1] == '\n')
  367.       string[strlen(string)-1] = '\0';
  368.     return(string);
  369.   }
  370.   else
  371.     return("Time Unknown");
  372. }
  373.  
  374. /*----------------------------------------------------------------------*/
  375.  
  376. char char_downcase(long_ch)
  377. unsigned long long_ch;
  378. {
  379.   unsigned char ch = long_ch & 0xFF; /* just want one byte */
  380.   /* when ansi is the way of the world, this can be tolower */
  381.   return (((ch >= 'A') && (ch <= 'Z')) ? (ch + 'a' -'A') : ch);
  382. }
  383.  
  384. char *string_downcase(w)
  385. char *w;
  386. {
  387.   long i = 0;
  388.   while(w[i] != '\0'){
  389.     w[i] = char_downcase((unsigned long)w[i]);
  390.     i++;
  391.   }
  392.   return(w);
  393. }
  394.  
  395. /*----------------------------------------------------------------------*/
  396.  
  397.  
  398. /* parsing arguments functions */
  399.  
  400. char *next_arg(argc,argv)
  401. int *argc;
  402. char ***argv;
  403.  
  404.      /* Returns NULL when it is out of arguments,
  405.         This side effects both argc and argv.  argc always contains the number
  406.         of arguments left.
  407.         The first returned is the command name.
  408.         */
  409. {
  410.   if((*argc)-- > 0)
  411.     return(*((*argv)++));
  412.   else
  413.     return(NULL);
  414. }
  415.  
  416. /*----------------------------------------------------------------------*/
  417.  
  418. char *peek_arg(argc,argv)
  419. int *argc;
  420.     char ***argv;
  421.  
  422.      /* Returns the next argument without popping it.
  423.        Returns NULL when it is out of arguments.
  424.        */
  425. {
  426.   if((*argc) > 0)
  427.     return(**argv);
  428.   else
  429.     return(NULL);
  430. }
  431.  
  432. /*----------------------------------------------------------------------*/
  433.  
  434. void beFriendly()
  435. {
  436.   MSG msg;
  437.   
  438.   while(PeekMessage((LPMSG)&msg, 0, 0, 0, PM_REMOVE)) 
  439.   {
  440.     if(msg.message == WM_PAINT) /* leave WM_PAINT's in the queue */
  441.       GetMessage((LPMSG)&msg, 0, 0, 0);
  442.     TranslateMessage((LPMSG)&msg);
  443.     DispatchMessage((LPMSG)&msg);
  444.   }
  445. }
  446.