home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbmsrc.zip / gbmhelp.c < prev    next >
C/C++ Source or Header  |  1998-04-11  |  3KB  |  134 lines

  1. /*
  2.  
  3. gbmhelp.c - Helpers for GBM file I/O stuff
  4.  
  5. */
  6.  
  7. /*...sincludes:0:*/
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <stddef.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #if defined(AIX) || defined(LINUX)
  16. #include <unistd.h>
  17. #else
  18. #include <io.h>
  19. #endif
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include "gbm.h"
  24.  
  25. /*...vgbm\46\h:0:*/
  26. /*...e*/
  27.  
  28. /*...sgbm_same:0:*/
  29. BOOLEAN gbm_same(const char *s1, const char *s2, int n)
  30.     {
  31.     for ( ; n--; s1++, s2++ )
  32.         if ( tolower(*s1) != tolower(*s2) )
  33.             return FALSE;
  34.     return TRUE;
  35.     }
  36. /*...e*/
  37. /*...sgbm_find_word:0:*/
  38. const char *gbm_find_word(const char *str, const char *substr)
  39.     {
  40.     char buf[100+1], *s;
  41.     int  len = strlen(substr);
  42.  
  43.     for ( s  = strtok(strcpy(buf, str), " \t,");
  44.           s != NULL;
  45.           s  = strtok(NULL, " \t,") )
  46.         if ( gbm_same(s, substr, len) && s[len] == '\0' )
  47.             return str + (s - buf);
  48.     return NULL;
  49.     }
  50. /*...e*/
  51. /*...sgbm_find_word_prefix:0:*/
  52. const char *gbm_find_word_prefix(const char *str, const char *substr)
  53.     {
  54.     char buf[100+1], *s;
  55.     int  len = strlen(substr);
  56.  
  57.     for ( s  = strtok(strcpy(buf, str), " \t,");
  58.           s != NULL;
  59.           s  = strtok(NULL, " \t,") )
  60.         if ( gbm_same(s, substr, len) )
  61.             return str + (s - buf);
  62.     return NULL;
  63.     }
  64. /*...e*/
  65. /*...sgbm_file_\42\:0:*/
  66. /* Looking at this, you might think that the gbm_file_* function pointers
  67.    could be made to point straight at the regular read,write etc..
  68.    If we do this then we get into problems with different calling conventions
  69.    (for example read is _Optlink under C-Set++ on OS/2), and also where
  70.    function arguments differ (the length field to read is unsigned on OS/2).
  71.    This simplest thing to do is simply to use the following veneers. */
  72.  
  73. static int def_open(const char *fn, int mode)
  74.     { return open(fn, mode); }
  75. static int def_create(const char *fn, int mode)
  76.     { return open(fn, O_CREAT|O_TRUNC|mode, S_IREAD|S_IWRITE); }
  77. static void def_close(int fd)
  78.     { close(fd); }
  79. static long def_lseek(int fd, long pos, int whence)
  80.     { return lseek(fd, pos, whence); }
  81. static int def_read(int fd, void *buf, int len)
  82.     { return read(fd, buf, len); }
  83. static int def_write(int fd, const void *buf, int len)
  84.     { return write(fd, buf, len); }
  85.  
  86. int  (*gbm_file_open  )(const char *fn, int mode)         = def_open  ;
  87. int  (*gbm_file_create)(const char *fn, int mode)         = def_create;
  88. void (*gbm_file_close )(int fd)                           = def_close ;
  89. long (*gbm_file_lseek )(int fd, long pos, int whence)     = def_lseek ;
  90. int  (*gbm_file_read  )(int fd, void *buf, int len)       = def_read  ;
  91. int  (*gbm_file_write )(int fd, const void *buf, int len) = def_write ;
  92. /*...e*/
  93. /*...sreading ahead:0:*/
  94. #define    AHEAD_BUF 0x4000
  95.  
  96. typedef struct
  97.     {
  98.     byte buf[AHEAD_BUF];
  99.     int inx, cnt;
  100.     int fd;
  101.     } AHEAD;
  102.  
  103. AHEAD *gbm_create_ahead(int fd)
  104.     {
  105.     AHEAD *ahead;
  106.  
  107.     if ( (ahead = malloc((size_t) sizeof(AHEAD))) == NULL )
  108.         return NULL;
  109.  
  110.     ahead->inx = 0;
  111.     ahead->cnt = 0;
  112.     ahead->fd  = fd;
  113.  
  114.     return ahead;
  115.     }
  116.  
  117. void gbm_destroy_ahead(AHEAD *ahead)
  118.     {
  119.     free(ahead);
  120.     }    
  121.  
  122. int gbm_read_ahead(AHEAD *ahead)
  123.     {
  124.     if ( ahead->inx >= ahead->cnt )
  125.         {
  126.         ahead->cnt = gbm_file_read(ahead->fd, (char *) ahead->buf, AHEAD_BUF);
  127.         if ( ahead->cnt <= 0 )
  128.             return -1;
  129.         ahead->inx = 0;
  130.         }
  131.     return (int) (unsigned int) ahead->buf[ahead->inx++];
  132.     }
  133. /*...e*/
  134.