]> git.xonotic.org Git - xonotic/gmqcc.git/blob - fs.c
beb48192f85d3f36cb3fc64677452a6608e3b21b
[xonotic/gmqcc.git] / fs.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24 #include "platform.h"
25
26 FILE *fs_file_open(const char *filename, const char *mode) {
27     return platform_fopen(filename, mode);
28 }
29
30 size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) {
31     return platform_fread(buffer, size, count, fp);
32 }
33
34 int fs_file_printf(FILE *fp, const char *format, ...) {
35     int      rt;
36     va_list  va;
37     va_start(va, format);
38     rt = platform_vfprintf(fp, format, va);
39     va_end  (va);
40
41     return rt;
42 }
43
44 void fs_file_close(FILE *fp) {
45     platform_fclose (fp);
46 }
47
48 size_t  fs_file_write (
49     const void    *buffer,
50     size_t         size,
51     size_t         count,
52     FILE          *fp
53 ) {
54     return platform_fwrite(buffer, size, count, fp);
55 }
56
57 int fs_file_error(FILE *fp) {
58     return platform_ferror(fp);
59 }
60
61 int fs_file_getc(FILE *fp) {
62     return platform_fgetc(fp);
63 }
64
65 int fs_file_puts(FILE *fp, const char *str) {
66     return platform_fputs(str, fp);
67 }
68
69 int fs_file_seek(FILE *fp, long int off, int whence) {
70     return platform_fseek(fp, off, whence);
71 }
72
73 long int fs_file_tell(FILE *fp) {
74     return platform_ftell(fp);
75 }
76
77 /*
78  * Implements libc getline for systems that don't have it, which is
79  * assmed all.  This works the same as getline().
80  */
81 int fs_file_getline(char **lineptr, size_t *n, FILE *stream) {
82     int   chr;
83     int   ret;
84     char *pos;
85
86     if (!lineptr || !n || !stream)
87         return -1;
88     if (!*lineptr) {
89         if (!(*lineptr = (char*)mem_a((*n=64))))
90             return -1;
91     }
92
93     chr = *n;
94     pos = *lineptr;
95
96     for (;;) {
97         int c = fs_file_getc(stream);
98
99         if (chr < 2) {
100             *n += (*n > 16) ? *n : 64;
101             chr = *n + *lineptr - pos;
102             if (!(*lineptr = (char*)mem_r(*lineptr,*n)))
103                 return -1;
104             pos = *n - chr + *lineptr;
105         }
106
107         if (ferror(stream))
108             return -1;
109         if (c == EOF) {
110             if (pos == *lineptr)
111                 return -1;
112             else
113                 break;
114         }
115
116         *pos++ = c;
117         chr--;
118         if (c == '\n')
119             break;
120     }
121     *pos = '\0';
122     return (ret = pos - *lineptr);
123 }
124
125 int fs_dir_make(const char *path) {
126     return platform_mkdir(path, 0700);
127 }
128
129 DIR *fs_dir_open(const char *name) {
130     return platform_opendir(name);
131 }
132
133 int fs_dir_close(DIR *dir) {
134     return platform_closedir(dir);
135 }
136
137 struct dirent *fs_dir_read(DIR *dir) {
138     return platform_readdir(dir);
139 }