UCommon
file.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
24 #ifndef _UCOMMON_FILE_H_
25 #define _UCOMMON_FILE_H_
26 
27 #ifndef _UCOMMON_CONFIG_H_
28 #include <ucommon/platform.h>
29 #endif
30 
31 #ifndef _UCOMMON_PROTOCOLS_H_
32 #include <ucommon/protocols.h>
33 #endif
34 
35 #ifndef _UCOMMON_THREAD_H_
36 #include <ucommon/thread.h>
37 #endif
38 
39 #ifndef _UCOMMON_STRING_H_
40 #include <ucommon/string.h>
41 #endif
42 
43 #ifndef _UCOMMON_MEMORY_H_
44 #include <ucommon/memory.h>
45 #endif
46 
47 #ifndef _UCOMMON_FSYS_H_
48 #include <ucommon/fsys.h>
49 #endif
50 
51 #include <stdio.h>
52 
53 namespace ucommon {
54 
60 class __EXPORT file : public CharacterProtocol
61 {
62 private:
63  FILE *fp;
64 #ifdef _MSWINDOWS_
65  HANDLE pid;
66 #else
67  pid_t pid;
68 #endif
69  char *tmp;
70 
71  __DELETE_COPY(file);
72 
73  int _putch(int code);
74 
75  int _getch(void);
76 
77 public:
78  typedef ::fpos_t bookmark_t;
79 
80  static file cin;
81  static file cout;
82  static file cerr;
83 
88  file(FILE *file);
89 
96  file(const char *path, const char *mode, size_t size = 2);
97 
105  file(const char *path, char **argv, const char *mode, char **envp = NULL);
106 
110  file();
111 
115  ~file();
116 
121  inline operator bool() const {
122  return fp != NULL;
123  }
124 
129  inline bool operator !() const {
130  return fp == NULL;
131  }
132 
133  inline operator FILE *() const {
134  return fp;
135  }
136 
143  void open(const char *path, const char *mode, size_t size = 2);
144 
151  void open(const char *path, char **argv, const char *mode, char **envp = NULL);
152 
157  int close(void);
158 
162  inline void clear(void) {
163  if(fp)
164  clearerr(fp);
165  }
166 
171  bool good(void) const;
172 
177  int cancel(void);
178 
179  inline size_t put(const void *data, size_t size) {
180  return fp == NULL ? 0 : fwrite(data, 1, size, fp);
181  }
182 
183  inline size_t get(void *data, size_t size) {
184  return fp == NULL ? 0 : fread(data, 1, size, fp);
185  }
186 
187  inline int put(char value) {
188  return fp == NULL ? EOF : fputc(value, fp);
189  }
190 
191  inline int get(void) {
192  return fp == NULL ? EOF : fgetc(fp);
193  }
194 
195  inline int push(char value) {
196  return fp == NULL ? EOF : ungetc(value, fp);
197  }
198 
199  inline int puts(const char *data) {
200  return fp == NULL ? 0 : fputs(data, fp);
201  }
202 
203  inline char *gets(char *data, size_t size) {
204  return fp == NULL ? NULL : fgets(data, (socksize_t)size, fp);
205  }
206 
207  template<typename T> inline size_t read(T* data, size_t count) {
208  return fp == NULL ? 0 : fread(data, sizeof(T), count, fp);
209  }
210 
211  template<typename T> inline size_t write(const T* data, size_t count) {
212  return fp == NULL ? 0 : fwrite(data, sizeof(T), count, fp);
213  }
214 
215  template<typename T> inline size_t read(T& data) {
216  return fp == NULL ? 0 : fread(data, sizeof(T), 1, fp);
217  }
218 
219  template<typename T> inline size_t write(const T& data) {
220  return fp == NULL ? 0 : fwrite(data, sizeof(T), 1, fp);
221  }
222 
223  inline void get(bookmark_t& pos) {
224  if(fp)
225  fsetpos(fp, &pos);
226  }
227 
228  inline void set(bookmark_t& pos) {
229  if(fp)
230  fgetpos(fp, &pos);
231  }
232 
233  int err(void) const;
234 
235  bool eof(void) const;
236 
237  template<typename T> inline void offset(long pos) {
238  if(fp)
239  fseek(fp, sizeof(const T) * pos, SEEK_CUR);
240  }
241 
242  inline void seek(long offset) {
243  if(fp)
244  fseek(fp, offset, SEEK_SET);
245  }
246 
247  inline void move(long offset) {
248  if(fp)
249  fseek(fp, offset, SEEK_CUR);
250  }
251 
252  inline void append(void) {
253  if (fp)
254  fseek(fp, 0l, SEEK_END);
255  }
256 
257  inline void rewind(void) {
258  if(fp)
259  ::rewind(fp);
260  }
261 
262  inline void flush(void) {
263  if(fp)
264  ::fflush(fp);
265  }
266 
267  size_t printf(const char *format, ...) __PRINTF(2, 3);
268 
269  size_t scanf(const char *format, ...) __SCANF(2, 3);
270 
271  bool is_tty(void) const;
272 
273  inline FILE *operator*() const {
274  return fp;
275  }
276 };
277 
281 typedef file file_t;
282 
283 } // namespace ucommon
284 
285 #endif
286 
A common string class and character string support functions.
Thread classes and sychronization objects.
Various miscellaneous platform specific headers and defines.
Thread-aware file system manipulation class.
Access standard files through character protocol.
Definition: file.h:60
file file_t
Convience type for file.
Definition: file.h:281
Private heaps, pools, and associations.
Common namespace for all ucommon objects.
Definition: access.h:47
void clear(void)
Clear error state.
Definition: file.h:162
Abstract interfaces and support.
Common character processing protocol.
Definition: protocols.h:175