]> git.xonotic.org Git - xonotic/gmqcc.git/blob - fs.c
c91805e52af160eeff680a7cad82302ab8816c41
[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 fs_file_t *fs_file_open(const char *filename, const char *mode) {
27     return (fs_file_t*)platform_fopen(filename, mode);
28 }
29
30 size_t fs_file_read(void *buffer, size_t size, size_t count, fs_file_t *fp) {
31     return platform_fread(buffer, size, count, (FILE*)fp);
32 }
33
34 int fs_file_printf(fs_file_t *fp, const char *format, ...) {
35     int      rt;
36     va_list  va;
37     va_start(va, format);
38     rt = platform_vfprintf((FILE*)fp, format, va);
39     va_end  (va);
40
41     return rt;
42 }
43
44 void fs_file_close(fs_file_t *fp) {
45     platform_fclose((FILE*)fp);
46 }
47
48 size_t  fs_file_write (
49     const void    *buffer,
50     size_t         size,
51     size_t         count,
52     fs_file_t     *fp
53 ) {
54     return platform_fwrite(buffer, size, count, (FILE*)fp);
55 }
56
57 int fs_file_error(fs_file_t *fp) {
58     return platform_ferror((FILE*)fp);
59 }
60
61 int fs_file_getc(fs_file_t *fp) {
62     return platform_fgetc((FILE*)fp);
63 }
64
65 int fs_file_puts(fs_file_t *fp, const char *str) {
66     return platform_fputs(str, (FILE*)fp);
67 }
68
69 int fs_file_seek(fs_file_t *fp, long int off, int whence) {
70     return platform_fseek((FILE*)fp, off, whence);
71 }
72
73 long int fs_file_tell(fs_file_t *fp) {
74     return platform_ftell((FILE*)fp);
75 }
76
77 int fs_file_flush(fs_file_t *fp) {
78     return platform_fflush((FILE*)fp);
79 }
80
81 /*
82  * Implements libc getline for systems that don't have it, which is
83  * assmed all.  This works the same as getline().
84  */
85 int fs_file_getline(char **lineptr, size_t *n, fs_file_t *stream) {
86     int   chr;
87     int   ret;
88     char *pos;
89
90     if (!lineptr || !n || !stream)
91         return -1;
92     if (!*lineptr) {
93         if (!(*lineptr = (char*)mem_a((*n=64))))
94             return -1;
95     }
96
97     chr = *n;
98     pos = *lineptr;
99
100     for (;;) {
101         int c = fs_file_getc(stream);
102
103         if (chr < 2) {
104             *n += (*n > 16) ? *n : 64;
105             chr = *n + *lineptr - pos;
106             if (!(*lineptr = (char*)mem_r(*lineptr,*n)))
107                 return -1;
108             pos = *n - chr + *lineptr;
109         }
110
111         if (fs_file_error(stream))
112             return -1;
113         if (c == EOF) {
114             if (pos == *lineptr)
115                 return -1;
116             else
117                 break;
118         }
119
120         *pos++ = c;
121         chr--;
122         if (c == '\n')
123             break;
124     }
125     *pos = '\0';
126     return (ret = pos - *lineptr);
127 }
128
129 int fs_dir_make(const char *path) {
130     return platform_mkdir(path, 0700);
131 }
132
133 fs_dir_t *fs_dir_open(const char *name) {
134     return (fs_dir_t*)platform_opendir(name);
135 }
136
137 int fs_dir_close(fs_dir_t *dir) {
138     return platform_closedir((DIR*)dir);
139 }
140
141 fs_dirent_t *fs_dir_read(fs_dir_t *dir) {
142     return (fs_dirent_t*)platform_readdir((DIR*)dir);
143 }