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