📄 warasciimapper.cpp
字号:
#include "StdAfx.h"#include "WarAsciiMapper.h" // class implemented/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarAsciiMapper::WarAsciiMapper(WarFileDriverFile *pDriverFile) throw(WarException): WarFileDriverFile(&(pDriverFile->GetDriver())), mIsAscii(true),#ifdef WIN32 mDoCrLfOut(true),#else mDoCrLfOut(false),#endif mAsciiPos((war_flen_t) -1), mBinPos(0), mpDriverFile(pDriverFile){}WarAsciiMapper::~WarAsciiMapper(){ if (mpDriverFile) delete mpDriverFile;}//============================= OPERATORS ====================================//============================= OPERATIONS ===================================void WarAsciiMapper::Open(const WarUrl& openUrl, war_uint32_t openFlags) throw(WarException){ mpDriverFile->Open(openUrl, openFlags); if (openFlags & F_TEXT) mIsAscii = true; mBinPos = AsciiGetPositon(); if (mBinPos <= 0) mAsciiPos = mBinPos = 0; // Start of file}void WarAsciiMapper::Close() throw(WarException){ mpDriverFile->Close(); mAsciiPos = mBinPos = (war_flen_t) -1;}war_uint32_t WarAsciiMapper::Read(war_cptr_t Buf, war_uint32_t bytes) throw(WarException){ war_uint32_t return_val = 0; if (mIsAscii) return_val = AsciiRead(Buf, bytes); else { return_val = mpDriverFile->Read(Buf, bytes); mBinPos += return_val; mAsciiPos = (war_flen_t) -1; } return return_val;}void WarAsciiMapper::Write(war_ccptr_t Buf, war_uint32_t bytes) throw(WarException){ if (mIsAscii) AsciiWrite(Buf, bytes); else { mpDriverFile->Write(Buf, bytes); mAsciiPos = (war_flen_t) -1; mBinPos += bytes; }}void WarAsciiMapper::Flush() throw(WarException){ mpDriverFile->Flush();}war_flen_t WarAsciiMapper::Seek(war_flen_t fileOffset, SeekModes seekMode) throw(WarException){ if (mIsAscii) return AsciiSeek(fileOffset, seekMode); mAsciiPos = (war_flen_t) -1; return mBinPos = mpDriverFile->Seek(fileOffset, seekMode);}//============================= ACCESS ===================================//============================= INQUIRY ===================================war_flen_t WarAsciiMapper::GetLength() throw(WarException){ if (mIsAscii) return AsciiGetLength(); return mpDriverFile->GetLength();}war_flen_t WarAsciiMapper::GetPosition() throw(WarException){ if (mIsAscii) return AsciiGetPositon(); return mpDriverFile->GetPosition();}bool WarAsciiMapper::IsEof() throw(WarException){ return mpDriverFile->IsEof();}/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////// Ascii mapping functions// Buf can be NULL to support AsciiSeek()war_uint32_t WarAsciiMapper::AsciiRead(war_cptr_t Buf, war_uint32_t bytes) throw(WarException){ war_uint32_t i_want = 0, i_got = 0; war_cstr_t p_destination = (war_cstr_t)Buf; char my_buffer[1024]; war_uint32_t bin_ofs = 0, AsciiOfs = 0; if (bytes < 2) { // We need at leaat 2 bytes to map CRLF! // 1 byte could cause an infinate loop WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); } while(AsciiOfs < bytes) { i_want = WarMin((bytes - AsciiOfs), sizeof(my_buffer)); i_got = mpDriverFile->Read(my_buffer, i_want); war_cstr_t p = my_buffer, pp = &my_buffer[i_got]; while((p < pp) && (AsciiOfs < bytes)) { switch(*p) { case '\r': // Ignore / Strip out ++bin_ofs; ++p; break; case '\n': // Return CRLF // If we can assume that the last read operation ended // with \r, continue with \n now... if (bin_ofs || (!bin_ofs && !mBinPos)) { if (p_destination) *p_destination++ = '\r'; if (++AsciiOfs == bytes) break; // Return \r. Next bufer will start at \n } if (p_destination) *p_destination++ = '\n'; ++AsciiOfs; ++bin_ofs; ++p; break; default: if (p_destination) *p_destination++ = *p++; else ++p; ++AsciiOfs; ++bin_ofs; break; } } if ((i_got < i_want) && IsEof()) break; // EOF } mBinPos += bin_ofs; mAsciiPos += AsciiOfs; return AsciiOfs;}#define OUT_FLUSH()\{\ if (index)\ mpDriverFile->Write(my_buffer, index);\ index = 0;\}#define OUT_CH(ch)\{\ my_buffer[index++] = ch;\ if (index >= sizeof(my_buffer))\ OUT_FLUSH()\}void WarAsciiMapper::AsciiWrite (war_ccptr_t Buf, war_uint32_t bytes) throw(WarException){ unsigned int index = 0; char my_buffer[1024]; war_cstr_t p = (war_cstr_t)Buf, pEnd = (((war_cstr_t)Buf) + bytes); war_uint32_t Ofs = 0; while(p < pEnd) { switch(*p) { case '\n': if (mDoCrLfOut) { OUT_CH('\r'); Ofs++; } OUT_CH('\n'); Ofs++; ++p; break; case '\r': ++p; break; default: OUT_CH(*p++); ++Ofs; break; } } OUT_FLUSH() mAsciiPos += bytes; mBinPos += Ofs;}#undef OUT_CH#undef OUT_FLUSHwar_flen_t WarAsciiMapper::AsciiSeek(war_flen_t fileOffset, SeekModes seekMode) throw(WarException){ switch(seekMode) { case WAR_SEEK_BEGIN: mpDriverFile->Seek(0); mAsciiPos = mBinPos = 0; break; case WAR_SEEK_CURRENT: AsciiDoSeek(fileOffset); break; case WAR_SEEK_END: AsciiDoSeek(); break; } return mAsciiPos;}void WarAsciiMapper::AsciiDoSeek(const war_flen_t position) throw(WarException){ war_uint32_t bytes = (war_uint32_t)(position & 0xffffffff); war_flen_t bytes_left = position; while(bytes_left) { if (bytes == 0) bytes = 0xffffffff; war_uint32_t i_got = AsciiRead(NULL, bytes); if (i_got != bytes) break; bytes_left -= i_got; bytes = (war_uint32_t)(bytes_left & 0xffffffff); }}war_flen_t WarAsciiMapper::AsciiGetLength() throw(WarException){ war_flen_t AsciiPos = mAsciiPos, BinPos = mBinPos; AsciiDoSeek(); war_flen_t return_val = mAsciiPos; mpDriverFile->Seek(BinPos); mAsciiPos = AsciiPos; mBinPos = BinPos; return return_val;}war_flen_t WarAsciiMapper::AsciiGetPositon() throw(WarException){ return mAsciiPos;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -