ucommon
file.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
23 #ifndef _UCOMMON_FILE_H_
24 #define _UCOMMON_FILE_H_
25 
26 #ifndef _UCOMMON_CONFIG_H_
27 #include <ucommon/platform.h>
28 #endif
29 
30 #ifndef _UCOMMON_PROTOCOLS_H_
31 #include <ucommon/protocols.h>
32 #endif
33 
34 #ifndef _UCOMMON_THREAD_H_
35 #include <ucommon/thread.h>
36 #endif
37 
38 #ifndef _UCOMMON_STRING_H_
39 #include <ucommon/string.h>
40 #endif
41 
42 #ifndef _UCOMMON_MEMORY_H_
43 #include <ucommon/memory.h>
44 #endif
45 
46 #ifndef _UCOMMON_FSYS_H_
47 #include <ucommon/fsys.h>
48 #endif
49 
50 #include <stdio.h>
51 
52 namespace ucommon {
53 
59 class __EXPORT file : public CharacterProtocol
60 {
61 private:
62  FILE *fp;
63 #ifdef _MSWINDOWS_
64  HANDLE pid;
65 #else
66  pid_t pid;
67 #endif
68  char *tmp;
69 
70  int _putch(int code);
71 
72  int _getch(void);
73 
74 public:
75  typedef ::fpos_t bookmark_t;
76 
77  static file cin;
78  static file cout;
79  static file cerr;
80 
85  file(FILE *file);
86 
93  file(const char *path, const char *mode, size_t size = 2);
94 
102  file(const char *path, char **argv, const char *mode, char **envp = NULL);
103 
107  file();
108 
112  ~file();
113 
118  inline operator bool() const
119  {return fp != NULL;}
120 
125  inline bool operator !() const
126  {return fp == NULL;}
127 
128  inline operator FILE *() const
129  {return fp;}
130 
137  void open(const char *path, const char *mode, size_t size = 2);
138 
145  void open(const char *path, char **argv, const char *mode, char **envp = NULL);
146 
151  int close(void);
152 
156  inline void clear(void)
157  {if(fp) clearerr(fp);}
158 
163  bool good(void) const;
164 
169  int cancel(void);
170 
171  inline size_t put(const void *data, size_t size)
172  { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
173 
174  inline size_t get(void *data, size_t size)
175  { return fp == NULL ? 0 : fread(data, 1, size, fp);}
176 
177  inline int put(char value)
178  { return fp == NULL ? EOF : fputc(value, fp);}
179 
180  inline int get(void)
181  { return fp == NULL ? EOF : fgetc(fp);}
182 
183  inline int push(char value)
184  { return fp == NULL ? EOF : ungetc(value, fp);}
185 
186  inline int puts(const char *data)
187  { return fp == NULL ? 0 : fputs(data, fp);}
188 
189  inline char *gets(char *data, size_t size)
190  { return fp == NULL ? NULL : fgets(data, size, fp);}
191 
192  template<typename T> inline size_t read(T* data, size_t count)
193  { return fp == NULL ? 0 : fread(data, sizeof(T), count, fp);}
194 
195  template<typename T> inline size_t write(const T* data, size_t count)
196  { return fp == NULL ? 0 : fwrite(data, sizeof(T), count, fp);}
197 
198  template<typename T> inline size_t read(T& data)
199  { return fp == NULL ? 0 : fread(data, sizeof(T), 1, fp);}
200 
201  template<typename T> inline size_t write(const T& data)
202  { return fp == NULL ? 0 : fwrite(data, sizeof(T), 1, fp);}
203 
204  inline void get(bookmark_t& pos)
205  { if(fp) fsetpos(fp, &pos);}
206 
207  inline void set(bookmark_t& pos)
208  { if(fp) fgetpos(fp, &pos);}
209 
210  int err(void) const;
211 
212  bool eof(void) const;
213 
214  template<typename T> inline void offset(long pos)
215  {if(fp) fseek(fp, sizeof(const T) * pos, SEEK_CUR);}
216 
217  inline void seek(long offset)
218  {if(fp) fseek(fp, offset, SEEK_SET);}
219 
220  inline void move(long offset)
221  {if(fp) fseek(fp, offset, SEEK_CUR);}
222 
223  inline void append(void)
224  {if (fp) fseek(fp, 0l, SEEK_END);}
225 
226  inline void rewind(void)
227  {if(fp) ::rewind(fp);}
228 
229  inline void flush(void)
230  {if(fp) ::fflush(fp);}
231 
232  size_t printf(const char *format, ...) __PRINTF(2, 3);
233 
234  size_t scanf(const char *format, ...) __SCANF(2, 3);
235 
236  bool is_tty(void) const;
237 };
238 
242 typedef file file_t;
243 
244 } // namespace ucommon
245 
246 #endif
247 
Thread-aware file system manipulation class.
Common namespace for all ucommon objects.
Definition: access.h:46
Access standard files through character protocol.
Definition: file.h:59
Thread classes and sychronization objects.
A common string class and character string support functions.
Abstract interfaces and support.
Private heaps, pools, and associations.
void clear(void)
Clear error state.
Definition: file.h:156
Various miscellaneous platform specific headers and defines.
Common character processing protocol.
Definition: protocols.h:174