00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "FileStream.h"
00018 #include "Log.h"
00019
00020 namespace nrEngine {
00021
00022
00023 FileStream::FileStream() : IStream(0), IResource("File")
00024 {
00025 }
00026
00027
00028 FileStream::~FileStream()
00029 {
00030 unloadResource();
00031 }
00032
00033
00034 Result FileStream::unloadResource()
00035 {
00036 close();
00037 mStream.reset();
00038 return OK;
00039 }
00040
00041
00042 Result FileStream::reloadResource(PropertyList* params)
00043 {
00044 return open(getResourceFilenameList().front());
00045 }
00046
00047
00048 Result FileStream::open (const std::string& fileName)
00049 {
00050
00051 mStream.reset (new std::ifstream(fileName.c_str()));
00052
00053
00054 if (!mStream->good())
00055 {
00056 NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "The file \"%s\" was not found", fileName.c_str());
00057 return FILE_NOT_FOUND;
00058 }
00059
00060
00061 mStream->seekg(0, std::ios_base::end);
00062 mSize = mStream->tellg();
00063 mStream->seekg(0, std::ios_base::beg);
00064
00065 return OK;
00066 }
00067
00068
00069 void FileStream::close()
00070 {
00071 if (!mStream) return;
00072 mStream->close();
00073 mStream.reset();
00074 mSize = 0;
00075 }
00076
00077
00078 size_t FileStream::read(void *buf, size_t size, size_t nmemb)
00079 {
00080 if (!mStream) return 0;
00081 mStream->read(static_cast<char*>(buf), size * nmemb);
00082 return mStream->gcount();
00083 }
00084
00085
00086 size_t FileStream::readDelim(void* buf, size_t count, const std::string& delim)
00087 {
00088 if (!mStream) return 0;
00089 if (delim.empty()){
00090 NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "FileStream::readDelim(): No delimiter provided");
00091 return 0;
00092 }
00093
00094 if (delim.size() > 1){
00095 NR_Log(Log::LOG_ENGINE, Log::LL_WARNING,
00096 "FileStream::readDelim(): Only first character of delimiter \"%s\" is used", delim.c_str());
00097 }
00098
00099
00100 bool trimCR = false;
00101 if (delim.at(0) == '\n'){
00102 trimCR = true;
00103 }
00104
00105
00106 mStream->getline(static_cast<char*>(buf), count+1, delim.at(0));
00107 size_t ret = mStream->gcount();
00108
00109 if (mStream->fail()){
00110
00111 if (ret == count){
00112
00113 mStream->clear();
00114 }else{
00115 NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "FileStream::readDelim(): Error during reading the stream");
00116 }
00117 }
00118
00119 if (!ret){
00120 (static_cast<char*>(buf))[1] = '\0';
00121 }else{
00122
00123 if (trimCR && (static_cast<char*>(buf))[ret] == '\r'){
00124 --ret;
00125 (static_cast<char*>(buf))[ret+1] = '\0';
00126 }
00127 }
00128
00129 return ret;
00130 }
00131
00132
00133 size_t FileStream::tell() const
00134 {
00135 if (!mStream) return 0;
00136 mStream->clear();
00137 return mStream->tellg();
00138 }
00139
00140
00141 bool FileStream::eof() const
00142 {
00143 if (!mStream) return true;
00144 return mStream->eof();
00145 }
00146
00147
00148 byte* FileStream::getData(size_t& count) const
00149 {
00150 if (!mStream || mSize <= 0) return NULL;
00151
00152 byte* data = new byte[mSize];
00153
00154
00155 mStream->read((char*)data, mSize);
00156
00157
00158 count = mStream->gcount();
00159 if (count < mSize){
00160 byte* ndata = new byte[count];
00161 memcpy(ndata, data, sizeof(byte) * count);
00162 delete [] data;
00163 return ndata;
00164 }
00165 return data;
00166 }
00167
00168
00169 bool FileStream::seek(int32 offset, int32 whence)
00170 {
00171 if (!mStream) return false;
00172 mStream->clear();
00173
00174 std::_Ios_Seekdir sd;
00175 if (whence == IStream::START)
00176 sd = std::ios::beg;
00177 else if (whence == IStream::END)
00178 sd = std::ios::end;
00179 else
00180 sd = std::ios::cur;
00181
00182 mStream->seekg(static_cast<std::ifstream::pos_type>(offset), sd);
00183 return true;
00184 }
00185
00186
00187 EmptyFileStream::EmptyFileStream()
00188 {
00189
00190 }
00191
00192
00193 EmptyFileStream::~EmptyFileStream()
00194 {
00195
00196 }
00197
00198
00199 size_t EmptyFileStream::read(void *buf, size_t size, size_t nmemb)
00200 {
00201 return 0;
00202 }
00203
00204
00205 size_t EmptyFileStream::readDelim(void* buf, size_t count, const std::string& )
00206 {
00207 return 0;
00208 }
00209
00210
00211 size_t EmptyFileStream::tell() const{
00212 return 0;
00213 }
00214
00215
00216 bool EmptyFileStream::eof() const{
00217 return true;
00218 }
00219
00220
00221 byte* EmptyFileStream::getData(size_t& count) const{
00222 count = 0;
00223 return NULL;
00224 }
00225
00226
00227 bool EmptyFileStream::seek(int32 offset, int32 whence)
00228 {
00229 return true;
00230 }
00231
00232
00233 void EmptyFileStream::close ()
00234 {
00235 return;
00236 }
00237
00238
00239 };
00240