source: trunk/dll/literal.c@ 2

Last change on this file since 2 was 2, checked in by root, 23 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1#define INCL_OS2
2#define INCL_WIN
3
4#include <os2.h>
5#include <ctype.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include "fm3dll.h"
10
11#pragma alloc_text(LITERAL,literal,index,fixup,wildcard)
12
13
14INT index (const CHAR *s,const CHAR c) {
15
16 CHAR *p;
17
18 p = strchr(s,c);
19 if(p == NULL || !*p)
20 return -1;
21 return (INT)(p - s);
22}
23
24
25/*
26 * literal.c
27 * Translate a string with tokens in it according to several conventions:
28 * 1. \x1b translates to CHAR(0x1b)
29 * 2. \27 translates to CHAR(27)
30 * 3. \" translates to "
31 * 4. \' translates to '
32 * 5. \\ translates to \
33 * 6. \r translates to carriage return
34 * 7. \n translates to linefeed
35 * 8. \b translates to backspace
36 * 9. \t translates to tab
37 * 10. \a translates to bell
38 * 11. \f translates to formfeed
39 *
40 * Synopsis
41 * *s = "this\x20is\32a test of \\MSC\\CSM\7"
42 * literal(s);
43 *
44 * ( s now equals "this is a test of \MSC\CSM")
45 */
46
47#define HEX "0123456789ABCDEF"
48#define DEC "0123456789"
49
50INT literal (CHAR *fsource) {
51
52 register INT wpos,w;
53 INT len,oldw;
54 CHAR *fdestin,*freeme,wchar;
55
56 if(!fsource ||
57 !*fsource)
58 return 0;
59 len = strlen(fsource) + 1;
60 freeme = fdestin = malloc(len + 1);
61 memset(fdestin,0,len); /* start out with zero'd string */
62
63 w = 0; /* set index to first character */
64 while(fsource[w]) { /* until end of string */
65 switch(fsource[w]) {
66 case '\\':
67 switch(fsource[w + 1]) {
68 case 'x' : /* hexadecimal */
69 wchar = 0;
70 w += 2; /* get past "\x" */
71 if(index(HEX,(CHAR)toupper(fsource[w])) != -1) {
72 oldw = w;
73 while(((wpos = index(HEX,(CHAR)toupper(fsource[w]))) != -1) &&
74 w < oldw + 2) {
75 wchar = (CHAR)(wchar << 4) + (CHAR)wpos;
76 w++;
77 }
78 }
79 else
80 wchar = 'x'; /* just an x */
81 w--;
82 *fdestin++ = wchar;
83 break;
84
85 case '\\' : /* we want a "\" */
86 w++;
87 *fdestin++ = '\\';
88 break;
89
90 case 't' : /* tab CHAR */
91 w++;
92 *fdestin++ = '\t';
93 break;
94
95 case 'n' : /* new line */
96 w++;
97 *fdestin++ = '\n';
98 break;
99
100 case 'r' : /* carr return */
101 w++;
102 *fdestin++ = '\r';
103 break;
104
105 case 'b' : /* back space */
106 w++;
107 *fdestin++ = '\b';
108 break;
109
110 case 'f': /* formfeed */
111 w++;
112 *fdestin++ = '\x0c';
113 break;
114
115 case 'a': /* bell */
116 w++;
117 *fdestin++ = '\07';
118 break;
119
120 case '\'' : /* single quote */
121 w++;
122 *fdestin++ = '\'';
123 break;
124
125 case '\"' : /* double quote */
126 w++;
127 *fdestin++ = '\"';
128 break;
129
130 default : /* decimal */
131 w++; /* get past "\" */
132 wchar = 0;
133 if(index(DEC,fsource[w]) != -1) {
134 oldw = w;
135 do { /* cvt to binary */
136 wchar = (CHAR)(wchar * 10 + (fsource[w++] - 48));
137 } while (index(DEC,fsource[w]) != -1 && w < oldw + 3);
138 w--;
139 }
140 else
141 wchar = fsource[w];
142 *fdestin ++ = wchar;
143 break;
144 }
145 break;
146
147 default :
148 *fdestin++ = fsource[w];
149 break;
150 }
151 w++;
152 }
153 *fdestin = 0; /* terminate the string */
154
155 len = fdestin - freeme;
156 memcpy(fsource,freeme,len + 1); /* swap 'em */
157 free(freeme);
158
159 return len; /* return length of string */
160}
161
162
163
164INT wildcard (const CHAR *fstra,const CHAR *fcarda,const BOOL notfile) {
165
166 /* returns TRUE if match */
167
168 register const CHAR *fstr = fstra,*fcard = fcarda;
169 register INT wmatch = TRUE;
170
171 while(wmatch && *fcard && *fstr) {
172 switch(*fcard) {
173 case '?' : /* character substitution */
174 fcard++;
175 if(notfile || (*fstr != '.' && *fstr != '/' && *fstr != '\\'))
176 fstr++; /* skip (match) next character */
177 break;
178
179 case '*' :
180 /* find next non-wild character in wildcard */
181 while(*fcard && (*fcard == '?' || *fcard == '*'))
182 fcard++;
183 if(!*fcard) /* if last char of wildcard is *, it matches */
184 return TRUE;
185 /* skip until partition, match, or eos */
186 while(*fstr && toupper(*fstr) != toupper(*fcard) &&
187 (notfile || (*fstr != '\\' &&
188 *fstr != '/' && *fstr != '.')))
189 fstr++;
190 if(!notfile && !*fstr) /* implicit '.' */
191 if(*fcard == '.')
192 fcard++;
193 break;
194
195 default :
196 if(!notfile && ((*fstr == '/' || *fstr == '\\') &&
197 (*fcard == '/' || *fcard == '\\')))
198 wmatch = TRUE;
199 else
200 wmatch = (toupper(*fstr) == toupper(*fcard));
201 fstr++;
202 fcard++;
203 break;
204 }
205 }
206
207 if ((*fcard && *fcard != '*') || *fstr)
208 return 0;
209 else
210 return wmatch;
211}
212
213
214CHAR * fixup (const CHAR *orig,CHAR *dest,const INT maxlen,const INT datalen) {
215
216 register const CHAR *o = orig;
217 register CHAR *d = dest,*tp;
218 CHAR temp[33] = "\\x";
219
220 *d = 0;
221 if(orig) {
222 while((o - orig) <
223 datalen &&
224 (d - dest) < maxlen) {
225 if(!isprint(*o)) {
226 if(*o == '\r') {
227 *d = '\\';
228 d++;
229 *d = 'r';
230 d++;
231 }
232 else if(*o == '\n') {
233 *d = '\\';
234 d++;
235 *d = 'n';
236 d++;
237 }
238 else if(*o == '\b') {
239 *d = '\\';
240 d++;
241 *d = 'b';
242 d++;
243 }
244 else {
245 sprintf(&temp[2],"%02hx",*o);
246 tp = temp;
247 while(*tp)
248 *d++ = *tp++;
249 }
250 o++;
251 }
252 else {
253 if(*o == '\\') {
254 *d = '\\';
255 d++;
256 *d = '\\';
257 d++;
258 o++;
259 }
260 else
261 *d++ = *o++;
262 }
263 *d = 0;
264 }
265 }
266 return dest;
267}
268
Note: See TracBrowser for help on using the repository browser.