/[debian-med]/trunk/packages/boxshade/trunk/bx_misc.c
ViewVC logotype

Contents of /trunk/packages/boxshade/trunk/bx_misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 145 - (show annotations) (download)
Sat Oct 7 01:02:32 2006 UTC (6 years, 7 months ago) by charles-guest
File MIME type: text/plain
File size: 9018 byte(s)
[svn-inject] Forking boxshade source to Trunk
1 #include "bx_types.h"
2 #include <stdarg.h>
3
4 #ifdef MSDOS
5 #include <io.h>
6 #else
7 #include <unistd.h>
8 #endif
9
10 MYFILE outfile = { NULL, FALSE};
11 FILE *parfile = NULL;
12
13 char depend_err[50] = "";
14
15 void *Malloc(size_t sze) {
16 void *res = malloc(sze);
17 assert(res!=NULL);
18 return res;
19 }
20
21 int min(int a, int b)
22 {
23 if (a < b)
24 return a;
25 return b;
26 }
27
28 static char *binpath = NULL;
29
30 void save_binpath(char *arg0) {
31 int sl = strlen(arg0);
32 char *ap = "";
33
34 /* Accept three path seperators: / \ : */
35 while (sl > 0 && strchr("\\/:", arg0[sl-1]) == NULL) --sl;
36 if (sl <= 0) return;
37
38 if (arg0[sl-1] == ':') ap = ":.";
39 binpath = Malloc(sl + strlen(ap) );
40 if (binpath == NULL) return;
41 strncpy(binpath, arg0, sl-1);
42 strcpy(binpath+sl-1, ap);
43 }
44
45 char *get_logical(char *logical)
46 {
47 char *r;
48 static char res[300];
49
50 r = getenv(logical);
51 if (r == NULL) r = "";
52 strcpy(res, r);
53 if (*res != '\0' || strcmp(logical, "BOXDIR")!=0 || binpath == NULL)
54 return res;
55
56 /* looking for BOXDIR -> default to exe directory ! */
57 strcpy(res, binpath); /* exec path */
58 return res;
59 }
60
61 BOOL fexist(char *fname)
62 {
63 BOOL Result;
64 FILE *f;
65
66 f = fopen(fname, TXT_RD);
67 Result = (f != NULL);
68 if (f != NULL)
69 fclose(f);
70 return Result;
71 }
72
73 int indx(char *mainstring, char *substring)
74 {
75 char *cp;
76
77 if (*mainstring == '\0')
78 return 0;
79 cp = strstr(mainstring, substring);
80 if (cp == NULL) return 0;
81 return (int)(cp-mainstring)+1;
82 }
83
84 MYFILE *outopen(MYFILE *mf, char *fn) {
85 mf->bin = EOLmode != EOL_default;
86 mf->f = fopen(fn, (mf->bin ? BIN_WR : TXT_WR));
87 return (mf->f != NULL ? mf : NULL);
88 }
89
90 static char *uwb = NULL;
91 void uwriteln(MYFILE *f, char *fmt, ...) {
92 va_list argptr;
93
94 if ( !f->bin) {
95 defeol:
96 va_start(argptr, fmt);
97 vfprintf(f->f, fmt, argptr);
98 va_end(argptr);
99 fputs("\n", f->f);
100 return;
101 }
102
103 if (uwb == NULL) {
104 uwb = malloc(4000);
105 if (uwb == NULL)
106 goto defeol;
107 }
108
109 {
110 char *cp1, *cp2;
111 int cnt;
112
113 va_start(argptr, fmt);
114 cnt = vsprintf(uwb, fmt, argptr);
115 va_end(argptr);
116 uwb[cnt] = '\n';
117 uwb[cnt+1] = '\0';
118
119 cp1 = uwb;
120 while (cp1 != NULL && *cp1 != '\0') {
121 cp2 = strchr(cp1, '\n');
122 if (cp2 != NULL) {
123 *cp2 = '\0';
124 ++cp2;
125 }
126 fputs(cp1, f->f);
127 if (cp2 != NULL) {
128 fflush(f->f);
129 switch (EOLmode) {
130 case EOL_dos : putc('\r', f->f);
131 putc('\n', f->f);
132 break;
133 case EOL_mac : putc('\r', f->f); break;
134 case EOL_unix :
135 default : putc('\n', f->f); break;
136 }
137 }
138 cp1 = cp2;
139 }
140 }
141 }
142
143
144 double str2real(void *s)
145 {
146 double r;
147 int err;
148
149 err = (sscanf((char *)s, "%lg", &r) == 0);
150 if (err != 0) {
151 strcpy(depend_err, "str2real");
152 return 0.0;
153 } else
154 return r;
155 }
156
157
158 int str2int(void *s)
159 {
160 int i, err;
161
162 err = (sscanf((char *)s, "%d", &i) == 0);
163 if (err != 0) {
164 strcpy(depend_err, "str2real");
165 return 0;
166 } else
167 return i;
168 }
169
170
171 #if 0
172
173 static long rand_m = 100000000L, rand_m1 = 10000, rand_b = 31415821L;
174
175 static long rand_a[55];
176 static int rand_j, filecount;
177
178 char lowcase(char c)
179 {
180 if (c <= 90 && c >= 65)
181 return (c + 32);
182 else
183 return c;
184 }
185
186 char *int2str(char *Result, int intg, int dig)
187 {
188 char s[100];
189
190 sprintf(s, "%*d", dig, intg);
191 return strcpy(Result, s);
192 }
193
194
195 char *real2str(char *Result, double rl, int dig1, int dig2)
196 {
197 char s[100];
198
199 sprintf(s, "%*.*f", dig1, dig2, rl);
200 return strcpy(Result, s);
201 }
202
203 char *get_command_line(char *Result)
204 {
205 string255 cl;
206 int i;
207
208 *cl = '\0';
209 if (P_argc > 1) {
210 for (i = 1; i < P_argc; i++)
211 strcat(cl, P_argv[i]);
212 }
213 return strcpy(Result, cl);
214 }
215
216 static char *getTEMPname(char *Result, char *s_)
217 {
218 string255 s;
219
220 strcpy(s, s_);
221 s[strlen(s) - 1] = '~';
222 return strcpy(Result, s);
223 }
224
225
226 void fopen_(FILE **f, char *fname_, char acc)
227 {
228 /*acc = r,w,a */
229 string255 fname;
230 FILE *tmpfile;
231 string255 tmpname, line;
232 char c;
233 char buf1[6144], buf2[6144];
234 BOOL uxmode;
235 char tmpfile_NAME[_FNSIZE];
236
237 strcpy(fname, fname_);
238 tmpfile = NULL;
239 if (acc == 'w' || acc == 'W') {
240 assign(*f, fname);
241 /* p2c: dep_tp.pas, line 128: Note: REWRITE does not specify a name [181] */
242 if (*f != NULL)
243 rewind(*f);
244 else
245 *f = tmpfile();
246 if (*f == NULL)
247 _EscIO(FileNotFound);
248 } else if (acc == 'a' || acc == 'A') {
249 assign(*f, fname);
250 /* p2c: dep_tp.pas, line 133: Note: APPEND does not specify a name [181] */
251 if (*f != NULL)
252 rewind(*f);
253 else
254 *f = tmpfile();
255 if (*f == NULL)
256 _EscIO(FileNotFound);
257 } else {
258 assign(*f, fname);
259 rewind(*f);
260 /* p2c: dep_tp.pas, line 139:
261 * Note: Turbo Pascal conditional compilation directive was ignored [218] */
262 /*$IFNDEF NO_UNIX*/
263 do {
264 c = getc(*f);
265 } while (!(P_eoln(*f) || c == '\n'));
266 rewind(*f);
267 if (c == '\n') {
268 uxmode = TRUE;
269 /* p2c: dep_tp.pas, line 142:
270 * Warning: Symbol 'UXMODE' is not defined [221] */
271 } else
272 uxmode = FALSE;
273 if (uxmode) {
274 setvbuf(*f, buf1, _IOFBF, 6144);
275 getTEMPname(tmpname, fname);
276 strcpy(tmpfile_NAME, tmpname);
277 if (tmpfile != NULL)
278 tmpfile = freopen(tmpfile_NAME, "w", tmpfile);
279 else
280 tmpfile = fopen(tmpfile_NAME, "w");
281 _SETIO(tmpfile != NULL, FileNotFound);
282 /* p2c: dep_tp.pas, line 149:
283 * Note: Make sure setvbuf() call occurs when file is open [200] */
284 setvbuf(*f, buf2, _IOFBF, 6144);
285 while (!P_eof(*f)) {
286 ureadln(f, (void *)line);
287 if (P_ioresult != 0)
288 goto _Ll_exit;
289 _SETIO(fprintf(tmpfile, "%s\n", line) >= 0, FileWriteError);
290 }
291 if (tmpfile != NULL)
292 fclose(tmpfile);
293 tmpfile = NULL;
294 if (*f != NULL)
295 fclose(*f);
296 *f = NULL;
297 /* p2c: dep_tp.pas, line 157: Note: Encountered an ASSIGN statement [179] */
298 assign(*f, tmpname);
299 rewind(*f);
300 _Ll_exit:
301 if (P_ioresult != 0)
302 strcpy(depend_err, "could not convert unix format");
303 }
304 /* p2c: dep_tp.pas, line 145:
305 * Note: Make sure setvbuf() call occurs when file is open [200] */
306 /*$endif*/
307 }
308 /* p2c: dep_tp.pas, line 127: Note: Encountered an ASSIGN statement [179] */
309 if (tmpfile != NULL)
310 fclose(tmpfile);
311
312 /* p2c: dep_tp.pas, line 132: Note: Encountered an ASSIGN statement [179] */
313 /* p2c: dep_tp.pas, line 137: Note: Encountered an ASSIGN statement [179] */
314 }
315
316 char *substr_(char *Result, void *original, int pos1, int count)
317 {
318 return strsub(Result, (char[256])original, pos1, count);
319 }
320
321
322 static long mult(long p, long q)
323 {
324 long p1, p0, q1, q0;
325
326 p1 = p / rand_m1;
327 p0 = p % rand_m1;
328 /* p2c: dep_tp.pas, line 182:
329 * Note: Using % for possibly-negative arguments [317] */
330 q1 = q / rand_m1;
331 q0 = q % rand_m1;
332 /* p2c: dep_tp.pas, line 183:
333 * Note: Using % for possibly-negative arguments [317] */
334 return (((p0 * q1 + p1 * q0) % rand_m1 * rand_m1 + p0 * q0) % rand_m);
335 /* p2c: dep_tp.pas, line 184:
336 * Note: Using % for possibly-negative arguments [317] */
337 /* p2c: dep_tp.pas, line 184:
338 * Note: Using % for possibly-negative arguments [317] */
339 }
340
341
342 void rand_init(int seed)
343 {
344 rand_a[0] = seed;
345 rand_j = 0;
346 do {
347 rand_j++;
348 rand_a[rand_j] = (mult(rand_b, rand_a[rand_j - 1]) + 1) % rand_m;
349 /* p2c: dep_tp.pas, line 193:
350 * Note: Using % for possibly-negative arguments [317] */
351 } while (rand_j != 54);
352 }
353
354
355 int rand_int(int limit)
356 {
357 /*from 0 to limit-1*/
358 rand_j = (rand_j + 1) % 55;
359 /* p2c: dep_tp.pas, line 198:
360 * Note: Using % for possibly-negative arguments [317] */
361 rand_a[rand_j] = (rand_a[(rand_j + 23) % 55] + rand_a[(rand_j + 54) % 55]) %
362 rand_m;
363 /* p2c: dep_tp.pas, line 199:
364 * Note: Using % for possibly-negative arguments [317] */
365 /* p2c: dep_tp.pas, line 200:
366 * Note: Using % for possibly-negative arguments [317] */
367 /* p2c: dep_tp.pas, line 200:
368 * Note: Using % for possibly-negative arguments [317] */
369 return (rand_a[rand_j] / rand_m1 * limit / rand_m1);
370 }
371
372
373 double rand_real(void)
374 {
375 /* from 0(incl) to 1(excl) */
376 return (rand_int(32000) / 32000.0);
377 }
378
379
380 typedef char string1[2];
381
382
383 void ureadln(FILE **f, void *line)
384 {
385 BOOL done;
386 char c;
387 char STR1[256];
388
389 done = FALSE;
390 *(string1)line = '\0';
391 do {
392 c = getc(*f);
393 if (c == '\n')
394 done = TRUE;
395 else if (c != '\015') {
396 sprintf(STR1, "%s%c", (char[256])line, c);
397 strcpy((char *)(&line), STR1);
398 }
399 if (P_eoln(*f)) {
400 fscanf(*f, "%*[^\n]");
401 getc(*f);
402 done = TRUE;
403 }
404 } while (!done);
405 }
406
407
408 BOOL getunixmode(char *fname)
409 {
410 BOOL Result;
411 char c;
412 FILE *f;
413 char f_NAME[_FNSIZE];
414
415 f = NULL;
416 strcpy(f_NAME, fname);
417 if (f != NULL)
418 f = freopen(f_NAME, "r", f);
419 else
420 f = fopen(f_NAME, "r");
421 if (f == NULL)
422 _EscIO(FileNotFound);
423 do {
424 c = getc(f);
425 } while (!(c == '\n' || P_eoln(f)));
426 if (c == '\n')
427 Result = TRUE;
428 else
429 Result = FALSE;
430 if (f != NULL)
431 fclose(f);
432 f = NULL;
433 if (f != NULL)
434 fclose(f);
435 return Result;
436 }
437
438 void _dep_tp_init(void)
439 {
440 static int _was_initialized = 0;
441 if (_was_initialized++)
442 return;
443 filecount = 100;
444 rand_init(1);
445 }
446 #endif

  ViewVC Help
Powered by ViewVC 1.1.5