00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "nfile.h"
00023
00024 NFile::NFile(void)
00025 : NIODevice(),
00026 m_handle(NULL),
00027 m_path()
00028 {
00029
00030 }
00031
00032 NFile::NFile(const NString &path, NIODevice::Mode mode)
00033 : NIODevice(),
00034 m_handle(NULL),
00035 m_path()
00036 {
00037 setFileName(path);
00038 open(mode);
00039 }
00040
00041
00042 NFile::~NFile(void) {
00043 closeDevice();
00044 }
00045
00046
00047 bool NFile::atEnd(void) const {
00048 if (feof(m_handle)) {
00049 return true;
00050 }
00051
00052 return false;
00053 }
00054
00055
00056 bool NFile::exists(void) const {
00057 struct stat sb;
00058
00059 if (stat(m_path.toChar(), &sb) == -1) {
00060 if (errno == ENOENT) {
00061 return false;
00062 }
00063 throw NIOException(nerror(errno), m_path,
00064 NException::BASEIO,
00065 NIOException::FILE,
00066 errno);
00067 }
00068
00069 return true;
00070 }
00071
00072
00073 bool NFile::exists(const NString &filepath) {
00074 struct stat sb;
00075
00076 if (stat(filepath.toChar(), &sb) == -1) {
00077 if (errno == ENOENT) {
00078 return false;
00079 }
00080 throw NIOException(nerror(errno), filepath,
00081 NException::BASEIO,
00082 NIOException::FILE,
00083 errno);
00084 }
00085
00086 return true;
00087 }
00088
00089
00090 int NFile::handle(void) const {
00091 #ifndef _MSC_VER
00092 return fileno(m_handle);
00093 #else
00094 return _fileno(m_handle);
00095 #endif
00096 }
00097
00098 char *NFile::getMode(NIODevice::Mode mode) const {
00099 if ((mode & NIODevice::ReadWrite) && !(mode & NIODevice::Truncate)
00100 || (mode & NIODevice::Append))
00101 {
00102 return "a+";
00103 }
00104
00105 if ((mode & NIODevice::ReadWrite) && !(mode & NIODevice::Append)
00106 || (mode & NIODevice::Truncate)) {
00107 return "w+";
00108 }
00109
00110 return "r";
00111 }
00112
00113
00114 void NFile::open(NIODevice::Mode mode) {
00115 if (mode & NIODevice::ReadOnly) {
00116 if (!exists()) {
00117 throw NIOException("Not found", m_path, NException::BASEIO,
00118 NIOException::FILE,
00119 errno);
00120 }
00121 }
00122
00123 m_handle = fopen(m_path.toChar(), getMode(mode));
00124 if (!m_handle) {
00125 throw NIOException(nerror(errno), m_path,
00126 NException::BASEIO,
00127 NIOException::FILE,
00128 NIOException::getFlagByErr(errno));
00129 }
00130 }
00131
00132
00133 void NFile::closeDevice(void) {
00134 if (m_handle) {
00135 fclose(m_handle);
00136 m_handle = NULL;
00137 }
00138
00139 }
00140
00141
00142 nint32 NFile::read(NDataStream *buffer) {
00143 #ifndef _MSC_VER
00144 char tmp[m_readBufferSize];
00145 #else
00146 char tmp[2048];
00147 #endif
00148 int bread = 0;
00149
00150 memset(tmp, 0, sizeof(tmp));
00151
00152 bread = fread(tmp, sizeof(tmp), 1, m_handle);
00153 if (bread == 0) {
00154 if (ferror(m_handle)) {
00155 return -1;
00156 }
00157 }
00158
00159 buffer->set(tmp, bread);
00160 return bread;
00161 }
00162
00163
00164 nint32 NFile::read(NDataStream *buffer, nuint32 bytes) {
00165 char tmp[1];
00166 int bread = 0;
00167 nuint32 btotal = 0;
00168
00169 *buffer = "";
00170 do {
00171 memset(tmp, 0, sizeof(tmp));
00172
00173 bread = fread(tmp, sizeof(tmp), 1, m_handle);
00174 if (bread > 0) {
00175 btotal += bread;
00176 buffer->append(tmp, bread);
00177 }
00178 else {
00179 if (ferror(m_handle)) {
00180 return -1;
00181 }
00182 }
00183 } while (!feof(m_handle) && btotal != bytes);
00184
00185 return btotal;
00186 }
00187
00188
00189 nint32 NFile::readLine(NDataStream *buffer) {
00190 char tmp[1];
00191 int bread = 0;
00192 nuint32 btotal = 0;
00193
00194 *buffer = "";
00195 do {
00196 memset(tmp, 0, sizeof(tmp));
00197
00198 bread = fread(tmp, sizeof(tmp), 1, m_handle);
00199 if (bread > 0) {
00200 if (tmp[0] == '\n' || tmp[0] == '\0' || tmp[0] == '\r') {
00201 break;
00202 }
00203
00204 btotal += bread;
00205 buffer->append(tmp, bread);
00206 }
00207 } while (!feof(m_handle));
00208
00209 return btotal;
00210 }
00211
00212
00213 nint32 NFile::write(const NDataStream &data, nuint32 bytes) {
00214 int btotal = 0;
00215
00216 if (bytes != 0 && (data.size() >= bytes)) {
00217 btotal = fwrite(data.data(), bytes, 1, m_handle);
00218 if (btotal == 0) {
00219 return -1;
00220 }
00221 }
00222 else {
00223 btotal = fwrite(data.data(), data.size(), 1, m_handle);
00224 }
00225
00226 return btotal;
00227 }
00228
00229
00230 nint64 NFile::size(void) const {
00231 struct stat fdata;
00232
00233 if (fstat(handle(), &fdata) < 0) {
00234 throw NIOException(nerror(errno), m_path,
00235 NException::BASEIO,
00236 NIOException::FILE,
00237 errno);
00238 }
00239
00240 return fdata.st_size;
00241 }
00242
00243
00244 void NFile::setFileName(const NString &path) {
00245 if (!m_path.isNull()) {
00246 closeDevice();
00247 }
00248
00249 m_path = path;
00250 }
00251
00252
00253 NString NFile::getFileName(void) const {
00254 return m_path;
00255 }
00256