ucommon/file.h

Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 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, cout, cerr;
00078 
00083     file(FILE *file);
00084 
00091     file(const char *path, const char *mode, size_t size = 2);
00092 
00100     file(const char *path, char **argv, const char *mode, char **envp = NULL);
00101 
00105     file();
00106 
00110     ~file();
00111 
00116     inline operator bool()
00117         {return fp != NULL;}
00118 
00123     inline bool operator !()
00124         {return fp == NULL;}
00125 
00126     inline operator FILE *()
00127         {return fp;}
00128 
00135     void open(const char *path, const char *mode, size_t size = 2);
00136 
00143     void open(const char *path, char **argv, const char *mode, char **envp = NULL);
00144 
00149     int close(void);
00150 
00154     inline void clear(void)
00155         {if(fp) clearerr(fp);}
00156 
00161     bool good(void);
00162 
00167     int cancel(void);
00168 
00169     inline size_t put(const void *data, size_t size)
00170         { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
00171 
00172     inline size_t get(void *data, size_t size)
00173         { return fp == NULL ? 0 : fread(data, 1, size, fp);}
00174 
00175     inline int put(char value)
00176         { return fp == NULL ? EOF : fputc(value, fp);}
00177 
00178     inline int get(void)
00179         { return fp == NULL ? EOF : fgetc(fp);}
00180 
00181     inline int push(char value)
00182         { return fp == NULL ? EOF : ungetc(value, fp);}
00183 
00184     inline int puts(const char *data)
00185         { return fp == NULL ? 0 : fputs(data, fp);}
00186 
00187     inline char *gets(char *data, size_t size)
00188         { return fp == NULL ? NULL : fgets(data, size, fp);}
00189 
00190     template<typename T> inline size_t read(T* data, size_t count)
00191         { return fp == NULL ? 0 : fread(data, sizeof(T), count, fp);}
00192 
00193     template<typename T> inline size_t write(const T* data, size_t count)
00194         { return fp == NULL ? 0 : fwrite(data, sizeof(T), count, fp);}
00195 
00196     template<typename T> inline size_t read(T& data)
00197         { return fp == NULL ? 0 : fread(data, sizeof(T), 1, fp);}
00198 
00199     template<typename T> inline size_t write(const T& data)
00200         { return fp == NULL ? 0 : fwrite(data, sizeof(T), 1, fp);}
00201 
00202     inline void get(bookmark_t& pos)
00203         { if(fp) fsetpos(fp, &pos);}
00204 
00205     inline void set(bookmark_t& pos)
00206         { if(fp) fgetpos(fp, &pos);}
00207 
00208     int err(void) const;
00209 
00210     bool eof(void) const;
00211 
00212     template<typename T> inline void offset(long pos)
00213         {if(fp) fseek(fp, sizeof(const T) * pos, SEEK_CUR);}
00214 
00215     inline void seek(long offset)
00216         {if(fp) fseek(fp, offset, SEEK_SET);}
00217 
00218     inline void move(long offset)
00219         {if(fp) fseek(fp, offset, SEEK_CUR);}
00220 
00221     inline void append(void)
00222         {if (fp) fseek(fp, 0l, SEEK_END);}
00223 
00224     inline void rewind(void)
00225         {if(fp) ::rewind(fp);}
00226 
00227     inline void flush(void)
00228         {if(fp) ::fflush(fp);}
00229 
00230     size_t printf(const char *format, ...) __PRINTF(2, 3);
00231 
00232     size_t scanf(const char *format, ...) __SCANF(2, 3);
00233 
00234     bool is_tty(void) const;
00235 };
00236 
00240 typedef file file_t;
00241 
00242 END_NAMESPACE
00243 
00244 #endif
00245 

Generated on 14 Aug 2013 for UCommon by  doxygen 1.4.7