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