UCommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // Copyright (C) 2015 Cherokees of Idaho. 00003 // 00004 // This file is part of GNU uCommon C++. 00005 // 00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License as published 00008 // by the Free Software Foundation, either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // GNU uCommon C++ is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00018 00024 #ifndef _UCOMMON_FILE_H_ 00025 #define _UCOMMON_FILE_H_ 00026 00027 #ifndef _UCOMMON_CONFIG_H_ 00028 #include <ucommon/platform.h> 00029 #endif 00030 00031 #ifndef _UCOMMON_PROTOCOLS_H_ 00032 #include <ucommon/protocols.h> 00033 #endif 00034 00035 #ifndef _UCOMMON_THREAD_H_ 00036 #include <ucommon/thread.h> 00037 #endif 00038 00039 #ifndef _UCOMMON_STRING_H_ 00040 #include <ucommon/string.h> 00041 #endif 00042 00043 #ifndef _UCOMMON_MEMORY_H_ 00044 #include <ucommon/memory.h> 00045 #endif 00046 00047 #ifndef _UCOMMON_FSYS_H_ 00048 #include <ucommon/fsys.h> 00049 #endif 00050 00051 #include <stdio.h> 00052 00053 namespace ucommon { 00054 00060 class __EXPORT file : public CharacterProtocol 00061 { 00062 private: 00063 FILE *fp; 00064 #ifdef _MSWINDOWS_ 00065 HANDLE pid; 00066 #else 00067 pid_t pid; 00068 #endif 00069 char *tmp; 00070 00071 int _putch(int code); 00072 00073 int _getch(void); 00074 00075 public: 00076 typedef ::fpos_t bookmark_t; 00077 00078 static file cin; 00079 static file cout; 00080 static file cerr; 00081 00086 file(FILE *file); 00087 00094 file(const char *path, const char *mode, size_t size = 2); 00095 00103 file(const char *path, char **argv, const char *mode, char **envp = NULL); 00104 00108 file(); 00109 00113 ~file(); 00114 00119 inline operator bool() const 00120 {return fp != NULL;} 00121 00126 inline bool operator !() const 00127 {return fp == NULL;} 00128 00129 inline operator FILE *() const 00130 {return fp;} 00131 00138 void open(const char *path, const char *mode, size_t size = 2); 00139 00146 void open(const char *path, char **argv, const char *mode, char **envp = NULL); 00147 00152 int close(void); 00153 00157 inline void clear(void) 00158 {if(fp) clearerr(fp);} 00159 00164 bool good(void) const; 00165 00170 int cancel(void); 00171 00172 inline size_t put(const void *data, size_t size) 00173 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);} 00174 00175 inline size_t get(void *data, size_t size) 00176 { return fp == NULL ? 0 : fread(data, 1, size, fp);} 00177 00178 inline int put(char value) 00179 { return fp == NULL ? EOF : fputc(value, fp);} 00180 00181 inline int get(void) 00182 { return fp == NULL ? EOF : fgetc(fp);} 00183 00184 inline int push(char value) 00185 { return fp == NULL ? EOF : ungetc(value, fp);} 00186 00187 inline int puts(const char *data) 00188 { return fp == NULL ? 0 : fputs(data, fp);} 00189 00190 inline char *gets(char *data, size_t size) 00191 { return fp == NULL ? NULL : fgets(data, size, fp);} 00192 00193 template<typename T> inline size_t read(T* data, size_t count) 00194 { return fp == NULL ? 0 : fread(data, sizeof(T), count, fp);} 00195 00196 template<typename T> inline size_t write(const T* data, size_t count) 00197 { return fp == NULL ? 0 : fwrite(data, sizeof(T), count, fp);} 00198 00199 template<typename T> inline size_t read(T& data) 00200 { return fp == NULL ? 0 : fread(data, sizeof(T), 1, fp);} 00201 00202 template<typename T> inline size_t write(const T& data) 00203 { return fp == NULL ? 0 : fwrite(data, sizeof(T), 1, fp);} 00204 00205 inline void get(bookmark_t& pos) 00206 { if(fp) fsetpos(fp, &pos);} 00207 00208 inline void set(bookmark_t& pos) 00209 { if(fp) fgetpos(fp, &pos);} 00210 00211 int err(void) const; 00212 00213 bool eof(void) const; 00214 00215 template<typename T> inline void offset(long pos) 00216 {if(fp) fseek(fp, sizeof(const T) * pos, SEEK_CUR);} 00217 00218 inline void seek(long offset) 00219 {if(fp) fseek(fp, offset, SEEK_SET);} 00220 00221 inline void move(long offset) 00222 {if(fp) fseek(fp, offset, SEEK_CUR);} 00223 00224 inline void append(void) 00225 {if (fp) fseek(fp, 0l, SEEK_END);} 00226 00227 inline void rewind(void) 00228 {if(fp) ::rewind(fp);} 00229 00230 inline void flush(void) 00231 {if(fp) ::fflush(fp);} 00232 00233 size_t printf(const char *format, ...) __PRINTF(2, 3); 00234 00235 size_t scanf(const char *format, ...) __SCANF(2, 3); 00236 00237 bool is_tty(void) const; 00238 }; 00239 00243 typedef file file_t; 00244 00245 } // namespace ucommon 00246 00247 #endif 00248