PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
stdio.h
1 /*
2  * Implementation of stdio library functions
3  * Copyright (c) 2011-2013 Parallax, Inc.
4  * Copyright (c) 2015 Total Spectrum Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  * +--------------------------------------------------------------------
25  */
26 
27 #ifndef _STDIO_H
28 #define _STDIO_H
29 
30 #if defined(__cplusplus)
31 extern "C" {
32 #endif
33 
34  /* forward declaration */
35  typedef struct _FILE FILE;
36 
37 #include <sys/size_t.h>
38 #include <sys/va_list.h>
39 #include <sys/driver.h>
40 #include <sys/null.h>
41 #include <sys/thread.h>
42 
43 #if defined(__GNUC__)
44 #define _PRINTF_FUNC __attribute__((format (printf, 1, 2)))
45 #define _FPRINTF_FUNC __attribute__((format (printf, 2, 3)))
46 #else
47 #define _PRINTF_FUNC
48 #define _FPRINTF_FUNC
49 #endif
50 
51 #define EOF (-1)
52 #define BUFSIZ 512
53 #define FOPEN_MAX 8
54 #define FILENAME_MAX 256
55 
56 #define SEEK_SET 0
57 #define SEEK_CUR 1
58 #define SEEK_END 2
59 
60  typedef unsigned long fpos_t;
61 
62  struct _FILE {
63 
64 /* FILE structure flags */
65 #define _IOREAD 0x0001 /* file may be read from */
66 #define _IOWRT 0x0002 /* file may be written to */
67 #define _IOBIN 0x0004 /* file is in "binary" mode */
68 #define _IODEV 0x0008 /* file is a character device */
69 #define _IOCOOKED 0x0010 /* do terminal input processing */
70 #define _IORW 0x0080 /* file is open for update (r+w) */
71 #define _IOFBF 0x0100 /* i/o is fully buffered */
72 #define _IOLBF 0x0200 /* i/o is line buffered */
73 #define _IONBF 0x0400 /* i/o is not buffered */
74 #define _IOFREEBUF 0x0800 /* buffer needs freeing */
75 #define _IOEOF 0x1000 /* EOF has been reached */
76 #define _IOERR 0x2000 /* an error has occured */
77 #define _IOAPPEND 0x4000 /* data should be appended to file */
78 #define _IONONBLOCK 0x8000 /* i/o should be non-blocking */
79 
80  long _cnt; /* # of bytes in buffer */
81  unsigned char *_ptr; /* current buffer pointer */
82  unsigned char *_base; /* base of file buffer */
83  unsigned int _flag; /* file status flags */
84  long _bsiz; /* buffer size */
85 
86  /* lock for multi-threaded access to FILE struct */
87  _atomic_t _lock;
88 
89  /* driver for this file */
90  struct __driver *_drv;
91 
92  #define _SMALL_BUFSIZ 8
93  /* a default buffer for character I/O */
94  unsigned char _chbuf[_SMALL_BUFSIZ];
95 
96  /* auxiliary information that the driver may need */
97  unsigned long drvarg[4];
98 
99  };
100 
101  extern FILE __files[];
102 #define stdin (&__files[0])
103 #define stdout (&__files[1])
104 #define stderr (&__files[2])
105 #define fileno(x) (x - __files)
106 
107  FILE *fopen(const char *name, const char *mode);
108  FILE *freopen(const char *name, const char *mode, FILE *fp);
109  int fclose(FILE *fp);
110  int fflush(FILE *fp);
111  /* fdopen will always fail right now */
112  FILE *fdopen(int fd, const char *mode);
113 
114  void setbuf(FILE *fp, char *buf);
115  int setvbuf(FILE *fp, char *buf, int mode, size_t size);
116 #if defined(_BSD_SOURCE)
117  void setbuffer(FILE *fp, char *buf, size_t size);
118  void setlinebuf(FILE *fp);
119 #endif
120 #if defined(_GNU_SOURCE) || defined(_POSIX_SOURCE)
121  FILE *fmemopen(void *buf, size_t size, const char *mode);
122 #endif
123 
124  int fputc(int c, FILE *fp);
125  int fputs(const char *s, FILE *fp);
126  int puts(const char *s);
127  int putc(int c, FILE *fp);
128  int putchar(int c);
129 
130  int fgetc(FILE *fp);
131  char *fgets(char *s, int size, FILE *fp);
132  char *gets(char *buf);
133  int ungetc(int c, FILE *fp);
134  int getc(FILE *fp);
135  int getchar(void);
136 
137  size_t fread(void *ptr, size_t size, size_t nmeb, FILE *fp);
138  size_t fwrite(const void *ptr, size_t size, size_t nmeb, FILE *fp);
139 
140  int printf(const char *fmt, ...);
141  int fprintf(FILE *fp, const char *fmt, ...);
142  int sprintf(char *str, const char *format, ...);
143  int snprintf(char *str, size_t size, const char *format, ...);
144  int __simple_printf(const char *fmt, ...) _PRINTF_FUNC;
145  int __simple_float_printf(const char *fmt, ...) _PRINTF_FUNC;
146  void perror(const char *msg);
147 
148  int vprintf(const char *fmt, __va_list ap);
149  int vfprintf(FILE *fp, const char *fmt, __va_list ap);
150  int vsprintf(char *str, const char *format, __va_list ap);
151  int vsnprintf(char *str, size_t size, const char *format, __va_list ap);
152 
153  int scanf(const char *fmt, ...);
154  int fscanf(FILE *fp, const char *fmt, ...);
155  int sscanf(const char *str, const char *fmt, ...);
156 
157  int vscanf(const char *fmt, __va_list ap);
158  int vfscanf(FILE *fp, const char *fmt, __va_list ap);
159  int vsscanf(const char *str, const char *fmt, __va_list ap);
160 
161  int remove(const char *filename);
162  int rename(const char *oldname, const char *newname);
163 
164  void clearerr(FILE *fp);
165  int feof(FILE *fp);
166  int ferror(FILE *fp);
167 
168  int fseek(FILE *fp, long offset, int whence);
169  long ftell(FILE *fp);
170  void rewind(FILE *fp);
171 
172  int fgetpos(FILE *fp, fpos_t *pos);
173  int fsetpos(FILE *fp, fpos_t *pos);
174 
175  /* prefix for temporary name */
176 #define P_tmpdir ""
177  /* maximum length of temporary file returned by tmpnam */
178 #define L_tmpnam 16
179  /* number of unique temporary names available */
180 #define TMP_MAX 0xffffff
181 
182  FILE *tmpfile(void);
183  char *tmpnam(char *s);
184 
185 #ifndef __PROPELLER_COG__
186 #define putc(x, stream) fputc(x, stream)
187 //#define putchar(x) fputc(x, stdout) // actually a simpler putchar is default
188 #define getc(stream) fgetc(stream)
189 #define getchar() fgetc(stdin)
190 #endif
191 
192  /* internal functions */
193  /* set up the FILE pointer in fp to point to a particular driver */
194  FILE *__fopen_driver(FILE *fp, struct __driver *drv, const char *name, const char *mode);
195  /* find a free FILE slot in the table, and mark it as in use with driver drv */
196  FILE *__fopen_findslot(struct __driver *drv);
197 
198  /* set up a FILE pointer to do I/O from a string */
199  FILE *__string_file(FILE *fp, char *str, const char *mode, size_t len);
200 
201  /* lock used to let multiple threads work together nicer */
202  extern _atomic_t __stdio_lock;
203 #define __lock_stdio() __lock(&__stdio_lock)
204 #define __unlock_stdio() __unlock(&__stdio_lock)
205 
206  /* internal structures and defines for printf */
207  typedef struct _printf_info {
208  // per argument state
209  int width;
210  int prec;
211  int pad; // padding character '0' or ' '
212  int spec; // actual specification character
213  unsigned int alt :1; // the '#' flag
214  unsigned int space :1;
215  unsigned int left :1; // the '-' flag was specified
216  unsigned int showsign :1; // the '+' flag
217  unsigned int longflag :1; // 'l' modifier appeared (used for %ls, %lc)
218 
219  int size; // size of argument in bytes
220 
221  // global state
222  int byteswritten;
223  int (*putchar)(int c, void *arg);
224  void *putarg;
225  } _Printf_info;
226 
227  typedef int (*_FmtPutfunc)(int, void *);
228  int _dofmt( _FmtPutfunc put, void *outarg, const char *fmt, __va_list *args);
229 
230 #if defined(__cplusplus)
231 }
232 #endif
233 
234 #endif
_printf_info
Definition: stdio.h:207
driver.h
Contains driver API for stdio devices.
_FILE
Definition: stdio.h:62
__driver
Generic and customizable driver struct for stdio devices.
Definition: driver.h:88
__driver::fclose
int(* fclose)(FILE *fp)
Definition: driver.h:117
__driver::fopen
int(* fopen)(FILE *fp, const char *name, const char *mode)
Definition: driver.h:110
__driver::remove
int(* remove)(const char *name)
Definition: driver.h:156