2 * Copyright (C) 2012, 2013
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
26 fs_file_t *fs_file_open(const char *filename, const char *mode) {
27 return (fs_file_t*)platform_fopen(filename, mode);
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);
34 int fs_file_printf(fs_file_t *fp, const char *format, ...) {
38 rt = platform_vfprintf((FILE*)fp, format, va);
44 void fs_file_close(fs_file_t *fp) {
45 platform_fclose((FILE*)fp);
48 size_t fs_file_write (
54 return platform_fwrite(buffer, size, count, (FILE*)fp);
57 int fs_file_error(fs_file_t *fp) {
58 return platform_ferror((FILE*)fp);
61 int fs_file_getc(fs_file_t *fp) {
62 return platform_fgetc((FILE*)fp);
65 int fs_file_puts(fs_file_t *fp, const char *str) {
66 return platform_fputs(str, (FILE*)fp);
69 int fs_file_seek(fs_file_t *fp, long int off, int whence) {
70 return platform_fseek((FILE*)fp, off, whence);
73 long int fs_file_tell(fs_file_t *fp) {
74 return platform_ftell((FILE*)fp);
77 int fs_file_flush(fs_file_t *fp) {
78 return platform_fflush((FILE*)fp);
82 * Implements libc getline for systems that don't have it, which is
83 * assmed all. This works the same as getline().
85 int fs_file_getline(char **lineptr, size_t *n, fs_file_t *stream) {
90 if (!lineptr || !n || !stream)
93 if (!(*lineptr = (char*)mem_a((*n=64))))
101 int c = fs_file_getc(stream);
104 *n += (*n > 16) ? *n : 64;
105 chr = *n + *lineptr - pos;
106 if (!(*lineptr = (char*)mem_r(*lineptr,*n)))
108 pos = *n - chr + *lineptr;
111 if (fs_file_error(stream))
126 return (ret = pos - *lineptr);
129 int fs_dir_make(const char *path) {
130 return platform_mkdir(path, 0700);
133 fs_dir_t *fs_dir_open(const char *name) {
134 return (fs_dir_t*)platform_opendir(name);
137 int fs_dir_close(fs_dir_t *dir) {
138 return platform_closedir((DIR*)dir);
141 fs_dirent_t *fs_dir_read(fs_dir_t *dir) {
142 return (fs_dirent_t*)platform_readdir((DIR*)dir);