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 | #if defined(AIX) || defined(LINUX) || defined(MAC)
|
---|
14 | #include <unistd.h>
|
---|
15 | #else
|
---|
16 | #include <io.h>
|
---|
17 | #endif
|
---|
18 | #include <fcntl.h>
|
---|
19 | #ifdef MAC
|
---|
20 | #include <types.h>
|
---|
21 | #include <stat.h>
|
---|
22 | #else
|
---|
23 | #include <sys/types.h>
|
---|
24 | #include <sys/stat.h>
|
---|
25 | #endif
|
---|
26 | #include "gbm.h"
|
---|
27 |
|
---|
28 | /*...vgbm\46\h:0:*/
|
---|
29 | /*...e*/
|
---|
30 |
|
---|
31 | /*...sgbm_same:0:*/
|
---|
32 | BOOLEAN gbm_same(const char *s1, const char *s2, int n)
|
---|
33 | {
|
---|
34 | for ( ; n--; s1++, s2++ )
|
---|
35 | if ( tolower(*s1) != tolower(*s2) )
|
---|
36 | return FALSE;
|
---|
37 | return TRUE;
|
---|
38 | }
|
---|
39 | /*...e*/
|
---|
40 | /*...sgbm_find_word:0:*/
|
---|
41 | const char *gbm_find_word(const char *str, const char *substr)
|
---|
42 | {
|
---|
43 | char buf[100+1], *s;
|
---|
44 | int len = strlen(substr);
|
---|
45 |
|
---|
46 | for ( s = strtok(strcpy(buf, str), " \t,");
|
---|
47 | s != NULL;
|
---|
48 | s = strtok(NULL, " \t,") )
|
---|
49 | if ( gbm_same(s, substr, len) && s[len] == '\0' )
|
---|
50 | {
|
---|
51 | int inx = s - buf;
|
---|
52 | return str + inx;
|
---|
53 | /* Avoid referencing buf in the final return.
|
---|
54 | lcc and a Mac compiler see the buf, and then
|
---|
55 | warn about possibly returning the address
|
---|
56 | of an automatic variable! */
|
---|
57 | }
|
---|
58 | return NULL;
|
---|
59 | }
|
---|
60 | /*...e*/
|
---|
61 | /*...sgbm_find_word_prefix:0:*/
|
---|
62 | const char *gbm_find_word_prefix(const char *str, const char *substr)
|
---|
63 | {
|
---|
64 | char buf[100+1], *s;
|
---|
65 | int len = strlen(substr);
|
---|
66 |
|
---|
67 | for ( s = strtok(strcpy(buf, str), " \t,");
|
---|
68 | s != NULL;
|
---|
69 | s = strtok(NULL, " \t,") )
|
---|
70 | if ( gbm_same(s, substr, len) )
|
---|
71 | {
|
---|
72 | int inx = s - buf;
|
---|
73 | return str + inx;
|
---|
74 | /* Avoid referencing buf in the final return.
|
---|
75 | lcc and a Mac compiler see the buf, and then
|
---|
76 | warn about possibly returning the address
|
---|
77 | of an automatic variable! */
|
---|
78 | }
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 | /*...e*/
|
---|
82 | /*...sgbm_file_\42\:0:*/
|
---|
83 | /* Looking at this, you might think that the gbm_file_* function pointers
|
---|
84 | could be made to point straight at the regular read,write etc..
|
---|
85 | If we do this then we get into problems with different calling conventions
|
---|
86 | (for example read is _Optlink under C-Set++ on OS/2), and also where
|
---|
87 | function arguments differ (the length field to read is unsigned on OS/2).
|
---|
88 | This simplest thing to do is simply to use the following veneers. */
|
---|
89 |
|
---|
90 | static int def_open(const char *fn, int mode)
|
---|
91 | { return open(fn, mode); }
|
---|
92 | static int def_create(const char *fn, int mode)
|
---|
93 | #ifdef MAC
|
---|
94 | { return open(fn, O_CREAT|O_TRUNC|mode); }
|
---|
95 | /* S_IREAD and S_IWRITE won't exist on the Mac until MacOS/X */
|
---|
96 | #else
|
---|
97 | { return open(fn, O_CREAT|O_TRUNC|mode, S_IREAD|S_IWRITE); }
|
---|
98 | #endif
|
---|
99 | static void def_close(int fd)
|
---|
100 | { close(fd); }
|
---|
101 | static long def_lseek(int fd, long pos, int whence)
|
---|
102 | { return lseek(fd, pos, whence); }
|
---|
103 | static int def_read(int fd, void *buf, int len)
|
---|
104 | { return read(fd, buf, len); }
|
---|
105 | static int def_write(int fd, const void *buf, int len)
|
---|
106 | #ifdef MAC
|
---|
107 | /* Prototype for write is missing a 'const' */
|
---|
108 | { return write(fd, (void *) buf, len); }
|
---|
109 | #else
|
---|
110 | { return write(fd, buf, len); }
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | int (*gbm_file_open )(const char *fn, int mode) = def_open ;
|
---|
114 | int (*gbm_file_create)(const char *fn, int mode) = def_create;
|
---|
115 | void (*gbm_file_close )(int fd) = def_close ;
|
---|
116 | long (*gbm_file_lseek )(int fd, long pos, int whence) = def_lseek ;
|
---|
117 | int (*gbm_file_read )(int fd, void *buf, int len) = def_read ;
|
---|
118 | int (*gbm_file_write )(int fd, const void *buf, int len) = def_write ;
|
---|
119 | /*...e*/
|
---|
120 | /*...sreading ahead:0:*/
|
---|
121 | #define AHEAD_BUF 0x4000
|
---|
122 |
|
---|
123 | typedef struct
|
---|
124 | {
|
---|
125 | byte buf[AHEAD_BUF];
|
---|
126 | int inx, cnt;
|
---|
127 | int fd;
|
---|
128 | } AHEAD;
|
---|
129 |
|
---|
130 | AHEAD *gbm_create_ahead(int fd)
|
---|
131 | {
|
---|
132 | AHEAD *ahead;
|
---|
133 |
|
---|
134 | if ( (ahead = malloc((size_t) sizeof(AHEAD))) == NULL )
|
---|
135 | return NULL;
|
---|
136 |
|
---|
137 | ahead->inx = 0;
|
---|
138 | ahead->cnt = 0;
|
---|
139 | ahead->fd = fd;
|
---|
140 |
|
---|
141 | return ahead;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void gbm_destroy_ahead(AHEAD *ahead)
|
---|
145 | {
|
---|
146 | free(ahead);
|
---|
147 | }
|
---|
148 |
|
---|
149 | int gbm_read_ahead(AHEAD *ahead)
|
---|
150 | {
|
---|
151 | if ( ahead->inx >= ahead->cnt )
|
---|
152 | {
|
---|
153 | ahead->cnt = gbm_file_read(ahead->fd, (char *) ahead->buf, AHEAD_BUF);
|
---|
154 | if ( ahead->cnt <= 0 )
|
---|
155 | return -1;
|
---|
156 | ahead->inx = 0;
|
---|
157 | }
|
---|
158 | return (int) (unsigned int) ahead->buf[ahead->inx++];
|
---|
159 | }
|
---|
160 | /*...e*/
|
---|