00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdarg.h>
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035
00036 #include "File.h"
00037
00038 #ifdef SOCKETS_NAMESPACE
00039 namespace SOCKETS_NAMESPACE {
00040 #endif
00041
00042
00043 File::File()
00044 :m_fil(NULL)
00045 ,m_b_close(true)
00046 ,m_rptr(0)
00047 ,m_wptr(0)
00048 {
00049 }
00050
00051
00052 File::File(FILE *fil)
00053 :m_fil(fil)
00054 ,m_b_close(false)
00055 ,m_rptr(0)
00056 ,m_wptr(0)
00057 {
00058 }
00059
00060
00061 File::File(const std::string& path, const std::string& mode)
00062 :m_fil(NULL)
00063 ,m_b_close(true)
00064 ,m_rptr(0)
00065 ,m_wptr(0)
00066 {
00067 fopen(path, mode);
00068 }
00069
00070
00071 File::~File()
00072 {
00073 if (m_b_close)
00074 {
00075 fclose();
00076 }
00077 }
00078
00079
00080 bool File::fopen(const std::string& path, const std::string& mode)
00081 {
00082 m_path = path;
00083 m_mode = mode;
00084 #if defined( _WIN32) && !defined(__CYGWIN__)
00085 if (fopen_s(&m_fil, path.c_str(), mode.c_str()))
00086 m_fil = NULL;
00087 #else
00088 m_fil = ::fopen(path.c_str(), mode.c_str());
00089 #endif
00090
00091 return m_fil ? true : false;
00092 }
00093
00094
00095 void File::fclose() const
00096 {
00097 if (m_fil)
00098 {
00099 ::fclose(m_fil);
00100 m_fil = NULL;
00101 }
00102 }
00103
00104
00105
00106 size_t File::fread(char *ptr, size_t size, size_t nmemb) const
00107 {
00108 size_t r = 0;
00109 if (m_fil)
00110 {
00111 fseek(m_fil, m_rptr, SEEK_SET);
00112 r = ::fread(ptr, size, nmemb, m_fil);
00113 m_rptr = ftell(m_fil);
00114 }
00115 return r;
00116 }
00117
00118
00119 size_t File::fwrite(const char *ptr, size_t size, size_t nmemb)
00120 {
00121 size_t r = 0;
00122 if (m_fil)
00123 {
00124 fseek(m_fil, m_wptr, SEEK_SET);
00125 r = ::fwrite(ptr, size, nmemb, m_fil);
00126 m_wptr = ftell(m_fil);
00127 }
00128 return r;
00129 }
00130
00131
00132
00133 char *File::fgets(char *s, int size) const
00134 {
00135 char *r = NULL;
00136 if (m_fil)
00137 {
00138 fseek(m_fil, m_rptr, SEEK_SET);
00139 r = ::fgets(s, size, m_fil);
00140 m_rptr = ftell(m_fil);
00141 }
00142 return r;
00143 }
00144
00145
00146 void File::fprintf(const char *format, ...)
00147 {
00148 if (!m_fil)
00149 return;
00150 va_list ap;
00151 va_start(ap, format);
00152 fseek(m_fil, m_wptr, SEEK_SET);
00153 vfprintf(m_fil, format, ap);
00154 m_wptr = ftell(m_fil);
00155 va_end(ap);
00156 }
00157
00158
00159 off_t File::size() const
00160 {
00161 struct stat st;
00162 if (stat(m_path.c_str(), &st) == -1)
00163 {
00164 return 0;
00165 }
00166 return st.st_size;
00167 }
00168
00169
00170 bool File::eof() const
00171 {
00172 if (m_fil)
00173 {
00174 if (feof(m_fil))
00175 return true;
00176 }
00177 return false;
00178 }
00179
00180
00181 void File::reset_read() const
00182 {
00183 m_rptr = 0;
00184 }
00185
00186
00187 void File::reset_write()
00188 {
00189 m_wptr = 0;
00190 }
00191
00192
00193 const std::string& File::Path() const
00194 {
00195 return m_path;
00196 }
00197
00198
00199 #ifdef SOCKETS_NAMESPACE
00200 }
00201 #endif
00202
00203