Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/util/xstdio.c
blob: fc7eb42a8fdf8feece5c0671516beee6401f7aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#ifndef _plan9_
#include <string.h>
#if defined(__EMX__) || defined(__APPLE__)
#include <sys/types.h>
#endif
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#else
#include <u.h>
#include <libc.h>
#endif
#include <config.h>
#include <filter.h>
#include <fractal.h>
#include <ui_helper.h>
#include <misc-f.h>
#ifdef _WIN32
#define strcmp stricmp
#endif
#ifdef DJGPP
#define strcmp stricmp
#endif

/* We reserve character 01 to application directory so we can easily refer to data files */
char *xio_appdir;
char *xio_homedir;

char *xio_fixpath(CONST char *c)
{
    if (c[0] == '~') {
	char *c1 = (char *) malloc(strlen(c) + strlen(xio_homedir) + 5);
	sprintf(c1, "%s%s", xio_homedir, c + 1);
	return c1;
    }
    if (c[0] == '\01') {
	char *c1 = (char *) malloc(strlen(c) + strlen(xio_appdir) + 5);
	sprintf(c1, "%s%s", xio_appdir, c + 1);
	return c1;
    }
    return mystrdup(c);
}

int
xio_getfiles(xio_constpath path1, char ***names, char ***dirs,
	     int *nnames2, int *ndirs2)
{
#ifdef _plan9_
    *nnames2 = *ndirs2 = 0;
#else
    char *path = xio_fixpath(path1);
    int maxnames = 200, maxdirs = 200;
    int nnames = 0, ndirs = 0;
    DIR *dir = opendir(path);
    struct stat buf;
    char buff[4096];
    int pathlen;
    struct dirent *e;
    if (dir == NULL)
	return 0;
    *nnames2 = 0;
    *ndirs2 = 0;
    e = readdir(dir);
    strcpy(buff, path);
    pathlen = (int) strlen(path);
    if (buff[pathlen - 1] != XIO_PATHSEP)
	buff[pathlen] = XIO_PATHSEP;
    else
	pathlen--;
    *names = (char **) malloc(maxnames * sizeof(**names));
    *dirs = (char **) malloc(maxdirs * sizeof(**dirs));
    free(path);
    while (e != NULL) {
	char *n = mystrdup(e->d_name);
	strcpy(buff + pathlen + 1, e->d_name);
	stat(buff, &buf);
	if (S_ISDIR(buf.st_mode)) {
	    if (ndirs == maxdirs)
		maxdirs *= 2, *dirs =
		    (char **) realloc(*dirs, maxdirs * sizeof(**dirs));
	    (*dirs)[ndirs] = n;
	    ndirs++;
	} else {
	    if (nnames == maxnames)
		maxnames *= 2, *names =
		    (char **) realloc(*names, maxnames * sizeof(**names));
	    (*names)[nnames] = n;
	    nnames++;
	}
	e = readdir(dir);
    }
    if (nnames)
	*names = (char **) realloc(*names, nnames * sizeof(**names));
    else
	free(*names), *names = NULL;
    if (ndirs)
	*dirs = (char **) realloc(*dirs, ndirs * sizeof(**dirs));
    else
	free(*dirs), *dirs = NULL;
    *nnames2 = nnames;
    *ndirs2 = ndirs;
    closedir(dir);
    return 1;
#endif
}

xio_path xio_getdirectory(xio_constpath filename)
{
    int i;
    xio_pathdata directory;
    for (i = (int) strlen(filename); i && filename[i] != '/' &&
	 filename[i] != '\\' && filename[i] != XIO_PATHSEP; i--);
    if (filename[i] == '/' || filename[i] == '\\'
	|| filename[i] == XIO_PATHSEP)
	i++;
    directory[i] = 0;
    i--;
    for (; i >= 0; i--)
	directory[i] = filename[i];
    return (mystrdup(directory));
}

xio_path xio_getfilename(CONST char *basename, CONST char *extension)
{
    static char name[40];
    int nimage = 0;
#ifdef _plan9_
#ifdef _plan9v4_
#define DIRLEN 116
    uchar edir[DIRLEN];
#else
    char edir[DIRLEN];
#endif
#else
    struct stat sb;
#endif
    char *base = xio_fixpath(basename);
    do {
	sprintf(name, "%s%i%s", base, nimage++, extension);
    }
#ifndef _plan9_
    while (stat(name, &sb) != -1);
#else
#ifdef _plan9v4_
    while (stat(name, edir, DIRLEN) != -1);
#else
    while (stat(name, edir) != -1);
#endif
#endif
    free(base);
    return (name);
}

xio_file xio_getrandomexample(xio_path name)
{
#ifndef _plan9_
    static CONST char *CONST paths[] = {	/*Where examples should be located? */
	EXAMPLESPATH,		/*Data path when XaoS is propertly installed */
	"\01" XIO_PATHSEPSTR "examples",
	/*XaoS was started from root of source tree */
	"\01" XIO_PATHSEPSTR ".." XIO_PATHSEPSTR "examples",
	"." XIO_PATHSEPSTR "examples",
	/*XaoS was started from root of source tree */
	".." XIO_PATHSEPSTR "examples",
	/*XaoS was started from bin directory in source tree */
	XIO_EMPTYPATH,		/*Oops...it's not. Try curent directory */
    };
    int i = -1, p;
    DIR *d = NULL;
    xio_file f;
    struct dirent *dir;
    int n;
    int max = 0;
    char *realpath = NULL;

    for (p = 0; p < (int) (sizeof(paths) / sizeof(char *)) && d == NULL;
	 p++) {
	char *pp = xio_fixpath(paths[p]);
	d = opendir(pp);
	free(pp);
	if (d != NULL) {
	    realpath = xio_fixpath(paths[p]);
	    max = 800 - (int) strlen(realpath);
	    for (i = 0; (dir = readdir(d)) != NULL; i++) {
		int s = (int) strlen(dir->d_name);
		if (s > max || s < 4
		    || strcmp(".xpf", dir->d_name + s - 4))
		    i--;
		/*free(dir); */
	    }
	    if (!i) {
		/*uih->errstring = "No *.xpf files found"; */
		closedir(d);
		free(realpath);
		d = NULL;
		continue;
	    }
	    break;
	}
    }
    if (d == NULL) {
	/*uih->errstring = "Can not open examples directory"; */
	return NULL;
    }



    rewinddir(d);
    dir = NULL;
    n = (int) ((number_t) i * rand() / (RAND_MAX + 1.0));

    for (i = 0; i <= n; i++) {
	int s;
	do {
	    /*if(dir!=NULL) free(dir); */
	    dir = readdir(d);
	    if (dir == NULL) {
		/*uih->errstring = "Reading of examples directory failed"; */
		closedir(d);
		free(realpath);
		return NULL;
	    }
	    s = (int) strlen(dir->d_name);
	}
	while (s > max || s < 4 || strcmp(".xpf", dir->d_name + s - 4));
    }
    if (dir == NULL) {
	/*uih->errstring = "Reading of examples directory failed"; */
	closedir(d);
	free(realpath);
	return NULL;
    }
    strcpy(name, realpath);
    if (name[strlen(name) - 1] != XIO_PATHSEP)
	strcat(name, XIO_PATHSEPSTR);
    strcat(name, dir->d_name);
    closedir(d);
    /*free(dir); */

    f = xio_ropen(name);
    free(realpath);
    return (f);
#endif
}

xio_file xio_getcatalog(CONST char *name)
{
    static CONST xio_constpath paths[] = {	/*Where catalogs should be located? */
	CATALOGSPATH,		/*Data path when XaoS is propertly installed */
#ifndef _plan9_
	"\01" XIO_PATHSEPSTR "catalogs" XIO_PATHSEPSTR,
	"\01" XIO_PATHSEPSTR ".." XIO_PATHSEPSTR "catalogs" XIO_PATHSEPSTR,
	"." XIO_PATHSEPSTR "catalogs" XIO_PATHSEPSTR,
	/*XaoS was started from root of source tree */
	".." XIO_PATHSEPSTR "catalogs" XIO_PATHSEPSTR,
	/*XaoS was started from bin directory in source tree */
#else
	"./catalogs/",
	/*XaoS was started from root of source tree */
	"../catalogs/",
	/*XaoS was started from bin directory in source tree */
#endif
	XIO_EMPTYPATH,		/*Oops...it's not. Try curent directory */
    };
    int i;
    xio_file f = XIO_FAILED;
    xio_pathdata tmp;
    for (i = 0;
	 i < (int) (sizeof(paths) / sizeof(char *)) && f == XIO_FAILED;
	 i++) {
	char *p = xio_fixpath(paths[i]);
	xio_addfname(tmp, p, name);
	free(p);
	f = xio_ropen(tmp);
	if (f == XIO_FAILED) {
	    xio_addextension(tmp, ".cat");
	    f = xio_ropen(tmp);
	}
    }
    return (f);
}

xio_file xio_gethelp(void)
{
    static CONST xio_constpath paths[] = {	/*Where help should be located? */
	HELPPATH,		/*Data path when XaoS is propertly installed */
#ifndef _plan9_
	"\01" XIO_PATHSEPSTR "help" XIO_PATHSEPSTR "xaos.hlp",
	"\01" XIO_PATHSEPSTR ".." XIO_PATHSEPSTR "help" XIO_PATHSEPSTR
	    "xaos.hlp",
	"." XIO_PATHSEPSTR "help" XIO_PATHSEPSTR "xaos.hlp",
	/*XaoS was started from root of source tree */
	".." XIO_PATHSEPSTR "help" XIO_PATHSEPSTR "xaos.hlp",
	/*XaoS was started from bin directory in source tree */
	"." XIO_PATHSEPSTR "xaos.hlp",
#else
	"./help/xaos.hlp",
	/*XaoS was started from root of source tree */
	"../help/xaos.hlp",
	/*XaoS was started from bin directory in source tree */
	"./xaos.hlp",
#endif
	/*Oops...it's not. Try curent directory */
    };
    int i;
    xio_file f = XIO_FAILED;
    for (i = 0;
	 i < (int) (sizeof(paths) / sizeof(char *)) && f == XIO_FAILED;
	 i++) {
	char *p = xio_fixpath(paths[i]);
	f = xio_ropen(p);
	free(p);
    }
    return (f);
}

xio_file xio_gettutorial(CONST char *name, xio_path tmp)
{
    int i;
    xio_file f = XIO_FAILED;
    static CONST xio_constpath paths[] = {	/*Where tutorials should be located? */
	TUTORIALPATH,		/*Data path when XaoS is propertly installed */
#ifndef _plan9_
	"\01" XIO_PATHSEPSTR "tutorial" XIO_PATHSEPSTR,
	"\01" XIO_PATHSEPSTR ".." XIO_PATHSEPSTR "tutorial" XIO_PATHSEPSTR,
	"." XIO_PATHSEPSTR "tutorial" XIO_PATHSEPSTR,	/*XaoS was started from root of source tree */
	".." XIO_PATHSEPSTR "tutorial" XIO_PATHSEPSTR,	/*XaoS was started from bin directory in source tree */
#else
	"./tutorial/",		/*XaoS was started from root of source tree */
	"../tutorial/",		/*XaoS was started from bin directory in source tree */
#endif
	XIO_EMPTYPATH,		/*Oops...it's not. Try curent directory */
    };

    for (i = 0;
	 i < (int) (sizeof(paths) / sizeof(char *)) && f == XIO_FAILED;
	 i++) {
	char *p = xio_fixpath(paths[i]);
	xio_addfname(tmp, p, name);
	f = xio_ropen(tmp);
	free(p);
    }
    return (f);
}

int xio_exist(xio_constpath name)
{
#ifdef _plan9_
    return (0);
#else
    struct stat buf;
    return (!stat(name, &buf));
#endif
}

static int sputc(int c, xio_file f)
{
    return putc(c, (FILE *) f->data);
}

static int sputs(CONST char *c, xio_file f)
{
    return fputs(c, (FILE *) f->data);
}

static int sungetc(int c, xio_file f)
{
    return ungetc(c, (FILE *) f->data);
}

static int sgetc(xio_file f)
{
    return getc((FILE *) f->data);
}

static int sfeof(xio_file f)
{
    return feof((FILE *) f->data);
}

static int sflush(xio_file f)
{
    return fflush((FILE *) f->data);
}

static int ssclose(xio_file f)
{
    int r = fclose((FILE *) f->data);
    free(f);
    return r;
}

xio_file xio_ropen(CONST char *name)
{
    xio_file f = (xio_file) calloc(1, sizeof(*f));
    name = xio_fixpath(name);
    f->data = (void *) fopen(name, "rt");
    /*free (name); */
    if (!f->data) {
	free(f);
	return 0;
    }
    f->fclose = ssclose;
    f->xeof = sfeof;
    f->fgetc = sgetc;
    f->fungetc = sungetc;
    return f;
}

xio_file xio_wopen(CONST char *name)
{
    xio_file f = (xio_file) calloc(1, sizeof(*f));
    name = xio_fixpath(name);
    f->data = (void *) fopen(name, "wt");
    /*free (name); */
    if (!f->data) {
	free(f);
	return 0;
    }
    f->fputc = sputc;
    f->fputs = sputs;
    f->fclose = ssclose;
    f->flush = sflush;
    return f;
}

#ifdef DJGPP
#define DRIVES
#endif
#ifdef _WIN32
#define DRIVES
#endif
void xio_init(CONST char *name)
{
    if (getenv("HOME"))
	xio_homedir = mystrdup(getenv("HOME"));
    else
	xio_homedir = mystrdup("./");
    if (
#ifdef DRIVES
	   (((name[0] >= 'a' && name[0] <= 'z')
	     || (name[0] >= 'A' && name[0] <= 'Z')) && name[1] == ':'
	    && (name[2] == '\\' || name[2] == '/')) ||
#endif
	   name[0] == '/' || name[0] == '\\' || name[0] == XIO_PATHSEP
	   || name[0] == '~') {
	char *c = mystrdup(name);
	int i;
	int pos = 0;
	for (i = 0; i < (int) strlen(c); i++)
	    if (name[i] == '/' || name[i] == '\\'
		|| name[i] == XIO_PATHSEP)
		pos = i;
	c[pos] = 0;
	xio_appdir = xio_fixpath(c);
	free(c);
    } else {
	char buf[4096];
	buf[0] = '.';
	buf[1] = 0;
#ifndef _plan9_
	getcwd(buf, sizeof(buf));
#endif
	xio_appdir = mystrdup(buf);
	{
	    char *c = mystrdup(name), *c1;
	    int i;
	    int pos = 0;
	    for (i = 0; i < (int) strlen(c); i++)
		if (name[i] == '/' || name[i] == '\\'
		    || name[i] == XIO_PATHSEP)
		    pos = i;
	    c[pos] = 0;
	    c1 = (char *) malloc(strlen(c) + strlen(xio_appdir) + 2);
	    sprintf(c1, "%s%s%s", xio_appdir, XIO_PATHSEPSTR, c);
	    free(c);
	    free(xio_appdir);
	    xio_appdir = c1;
	}
    }
}

void xio_uninit()
{
    free(xio_appdir);
    free(xio_homedir);
}