OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // <stdio.h>-lookalike-ish. Note that this is a C header, so that crappy (and | |
6 // non-crappy) C programs can use it. | |
7 // | |
8 // In general, functions/types/macros are given "mojio_"/"MOJIO_"/etc. prefixes. | |
9 // There are a handful of exceptions (noted below). | |
10 // | |
11 // One should be able to have a drop-in <stdio.h> replacement using just (C99) | |
12 // inline functions, typedefs, and macro definitions. (For "native" apps, doing | |
13 // so in C is somewhat problematic, due to conflicts with the "native" C | |
14 // library.) | |
15 // | |
16 // TODO(vtl): The lack of |restrict|s in certain functions is slightly | |
17 // mysterious to me, but it's consistent with glibc. I don't know what the | |
18 // standard specifies. | |
19 | |
20 #ifndef MOJO_SERVICES_FILES_PUBLIC_C_MOJIO_STDIO_H_ | |
21 #define MOJO_SERVICES_FILES_PUBLIC_C_MOJIO_STDIO_H_ | |
22 | |
23 // Includes -------------------------------------------------------------------- | |
24 | |
25 // We need |va_list|. | |
26 #include <stdarg.h> | |
27 | |
28 // <stdio.h> is required to define |NULL| (as a macro) and |size_t|. We don't | |
29 // define our own (with prefixes), and just use the standard ones from | |
30 // <stddef.h>. | |
31 #include <stddef.h> | |
32 | |
33 #include "files/public/c/mojio_config.h" | |
34 | |
35 // Macros ---------------------------------------------------------------------- | |
36 | |
37 // Default buffer size. | |
38 #define MOJIO_BUFSIZ MOJIO_CONFIG_BUFSIZ | |
39 | |
40 // |EOF|: -1 in the known universe. Probably nothing would work if different. | |
41 #define MOJIO_EOF (-1) | |
42 | |
43 // Recommended size for arrays meant to hold a filename. | |
44 #define MOJIO_FILENAME_MAX MOJIO_CONFIG_FILENAME_MAX | |
45 | |
46 // Supposed maximum number of files opened simultaneously. May be a total lie. | |
47 #define MOJIO_FOPEN_MAX MOJIO_CONFIG_FOPEN_MAX | |
48 | |
49 // Minimum size needed for |mojio_tmpnam()| (includes terminating null). | |
50 #define MOJIO_L_tmpnam 20 | |
51 | |
52 // Minimum number of unique names guaranteed possible from |mojio_tmpnam()|. | |
53 // (Note: This is (2*26+10)^3, in case you're wondering.) | |
54 #define MOJIO_TMP_MAX 238328 | |
55 | |
56 // "Whence". These are duplicated (verbatim) in mojio_unistd.h. | |
57 #define MOJIO_SEEK_SET 0 | |
58 #define MOJIO_SEEK_CUR 1 | |
59 #define MOJIO_SEEK_END 2 | |
60 | |
61 // For |mojio_setvbuf()| (excuse the extra underscores). | |
62 #define MOJIO__IOFBF | |
63 #define MOJIO__IOLBF | |
64 #define MOJIO__IONBF | |
65 | |
66 // |stdin|/|stdout|/|stderr| are actually required to be macros. Haha. (We | |
67 // actually define globals further below.) It's actually somewhat reasonable: | |
68 // it'd allow one to make a <stdio.h> from this by defining |stdin| to be | |
69 // |mojio_stdin|, etc. | |
70 #define mojio_stdin mojio_stdin | |
71 #define mojio_stdout mojio_stdout | |
72 #define mojio_stderr mojio_stderr | |
73 | |
74 // Types ----------------------------------------------------------------------- | |
75 | |
76 // <stdio.h> is required to define two types in addition to |size_t|: | |
77 | |
78 // |FILE| is fully opaque (but must be a typedef) -- only |FILE*|s are ever | |
79 // used. | |
80 typedef struct MojioFileImpl MOJIO_FILE; | |
81 | |
82 // |fpos_t| is supposedly opaque (e.g., could theoretically be a struct), but | |
83 // callers must be able to declare instances. | |
84 typedef mojio_config_int64 mojio_fpos_t; | |
85 | |
86 // Functions ------------------------------------------------------------------- | |
87 | |
88 #ifdef __cplusplus | |
89 extern "C" { | |
90 #endif | |
91 | |
92 // TODO(vtl): None of the things below are implemented yet. | |
93 | |
94 // Operations on files: | |
95 int mojio_remove(const char* filename); | |
96 int mojio_rename(const char* oldname, const char* newname); | |
97 MOJIO_FILE* mojio_tmpfile(void); | |
98 char* mojio_tmpnam(char* s); | |
99 | |
100 // File access: | |
101 int mojio_fclose(MOJIO_FILE* stream); | |
102 int mojio_fflush(MOJIO_FILE* stream); | |
103 MOJIO_FILE* mojio_fopen(const char* MOJIO_CONFIG_RESTRICT filename, | |
104 const char* MOJIO_CONFIG_RESTRICT mode); | |
105 MOJIO_FILE* mojio_freopen(const char* MOJIO_CONFIG_RESTRICT filename, | |
106 const char* MOJIO_CONFIG_RESTRICT mode, | |
107 MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream); | |
108 void mojio_setbuf(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
109 char* MOJIO_CONFIG_RESTRICT buffer); | |
110 int mojio_setvbuf(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
111 char* MOJIO_CONFIG_RESTRICT buffer, | |
112 int mode, | |
113 size_t size); | |
114 | |
115 // Formatted input/output: | |
116 int mojio_fprintf(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
117 const char* MOJIO_CONFIG_RESTRICT format, | |
118 ...); | |
119 int mojio_fscanf(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
120 const char* MOJIO_CONFIG_RESTRICT format, | |
121 ...); | |
122 int mojio_printf(const char* MOJIO_CONFIG_RESTRICT format, ...); | |
123 int mojio_scanf(const char* MOJIO_CONFIG_RESTRICT format, ...); | |
124 int mojio_snprintf(char* MOJIO_CONFIG_RESTRICT s, | |
125 size_t n, | |
126 const char* MOJIO_CONFIG_RESTRICT format, | |
127 ...); | |
128 int mojio_sprintf(char* MOJIO_CONFIG_RESTRICT s, | |
129 const char* MOJIO_CONFIG_RESTRICT format, | |
130 ...); | |
131 int mojio_sscanf(const char* MOJIO_CONFIG_RESTRICT s, | |
132 const char* MOJIO_CONFIG_RESTRICT format, | |
133 ...); | |
134 int mojio_vfprintf(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
135 const char* MOJIO_CONFIG_RESTRICT format, | |
136 va_list arg); | |
137 int mojio_vfscanf(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
138 const char* MOJIO_CONFIG_RESTRICT format, | |
139 va_list arg); | |
140 int mojio_vprintf(const char* MOJIO_CONFIG_RESTRICT format, va_list arg); | |
141 int mojio_vscanf(const char* MOJIO_CONFIG_RESTRICT format, va_list arg); | |
142 int mojio_vsnprintf(char* MOJIO_CONFIG_RESTRICT s, | |
143 size_t n, | |
144 const char* MOJIO_CONFIG_RESTRICT format, | |
145 va_list arg); | |
146 int mojio_vsprintf(char* MOJIO_CONFIG_RESTRICT s, | |
147 const char* MOJIO_CONFIG_RESTRICT format, | |
148 va_list arg); | |
149 int mojio_vsscanf(const char* MOJIO_CONFIG_RESTRICT s, | |
150 const char* MOJIO_CONFIG_RESTRICT format, | |
151 va_list arg); | |
152 | |
153 // Character input/output: | |
154 int mojio_fgetc(MOJIO_FILE* stream); | |
155 char* mojio_fgets(char* MOJIO_CONFIG_RESTRICT s, | |
156 int n, | |
157 MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream); | |
158 int mojio_fputc(int c, MOJIO_FILE* stream); | |
159 int mojio_fputs(const char* MOJIO_CONFIG_RESTRICT s, | |
160 MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream); | |
161 int mojio_getc(MOJIO_FILE* stream); | |
162 int mojio_getchar(void); | |
163 char* mojio_gets(char* s); | |
164 int mojio_putc(int c, MOJIO_FILE* stream); | |
165 int mojio_putchar(int c); | |
166 int mojio_puts(const char* s); | |
167 int mojio_ungetc(int c, MOJIO_FILE* stream); | |
168 | |
169 // Direct input/output: | |
170 size_t mojio_fread(void* MOJIO_CONFIG_RESTRICT ptr, | |
171 size_t size, | |
172 size_t nmemb, | |
173 MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream); | |
174 size_t mojio_fwrite(const void* MOJIO_CONFIG_RESTRICT ptr, | |
175 size_t size, | |
176 size_t nmemb, | |
177 MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream); | |
178 | |
179 // File positioning: | |
180 int mojio_fgetpos(MOJIO_FILE* MOJIO_CONFIG_RESTRICT stream, | |
181 fpos_t* MOJIO_CONFIG_RESTRICT pos); | |
182 int mojio_fseek(MOJIO_FILE* stream, long offset, int whence); | |
183 int mojio_fsetpos(MOJIO_FILE* stream, const fpos_t* pos); | |
184 long mojio_ftell(MOJIO_FILE* stream); | |
185 void mojio_rewind(MOJIO_FILE* stream); | |
186 | |
187 // Error-handling: | |
188 void mojio_clearerr(MOJIO_FILE* stream); | |
189 int mojio_feof(MOJIO_FILE* stream); | |
190 int mojio_ferror(MOJIO_FILE* stream); | |
191 void mojio_perror(const char* s); | |
192 | |
193 // Globals --------------------------------------------------------------------- | |
194 | |
195 extern MOJIO_FILE* mojio_stdin; | |
196 extern MOJIO_FILE* mojio_stdout; | |
197 extern MOJIO_FILE* mojio_stderr; | |
198 | |
199 #ifdef __cplusplus | |
200 } // extern "C" | |
201 #endif | |
202 | |
203 #endif // MOJO_SERVICES_FILES_PUBLIC_C_MOJIO_STDIO_H_ | |
OLD | NEW |