From a0c1d7e8b4b0031801b8c64181b77336d3acc63d Mon Sep 17 00:00:00 2001 From: Andrea Bulgarelli Date: Mon, 13 Jan 2014 09:30:23 +0100 Subject: [PATCH] added File::fsize() and a parameter to File::open --- include/File.h | 3 +++ include/InputFile.h | 6 ++++-- include/OutputFile.h | 2 ++ src/File.cpp | 15 +++++++++++++++ src/InputFile.cpp | 4 ++++ src/OutputFile.cpp | 7 ++++++- 6 files changed, 34 insertions(+), 3 deletions(-) diff --git a/include/File.h b/include/File.h index eafc1f1..39bb238 100644 --- a/include/File.h +++ b/include/File.h @@ -114,6 +114,9 @@ public: /// Count the number of string lines into a text file. long getNumberOfStringLines(); + + /// The dimension of the file + dword fsize(); static dword byte_read; diff --git a/include/InputFile.h b/include/InputFile.h index 0697cf0..fe9abb2 100644 --- a/include/InputFile.h +++ b/include/InputFile.h @@ -35,16 +35,18 @@ public: virtual bool open( char** parameters ) throw(PacketExceptionIO*); - virtual void close() throw(PacketExceptionIO*); + virtual void close() throw(PacketExceptionIO*); virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*); - virtual char* readString() throw(PacketExceptionIO*); + virtual char* readString() throw(PacketExceptionIO*); virtual int getType() { return 0; }; + + virtual dword setpos(dword offset) throw(PacketExceptionIO*); protected: diff --git a/include/OutputFile.h b/include/OutputFile.h index bf0e98b..d53d425 100644 --- a/include/OutputFile.h +++ b/include/OutputFile.h @@ -36,6 +36,8 @@ public: virtual void close() throw(PacketExceptionIO*); + ///first parameter: filename + ///second parameter: fopen modes: w, r, a (optional) virtual bool open(char** parameters) throw(PacketExceptionIO*); virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*); diff --git a/src/File.cpp b/src/File.cpp index 4c51abb..c861645 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -345,3 +345,18 @@ bool File::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*) return true; } + +dword File::fsize(){ + if(closed) + return 0; + + long prev=ftell(fp); + if(prev == -1L) + return 0; + fseek(fp, 0L, SEEK_END); + long sz=ftell(fp); + if(sz == -1L) + return 0; + fseek(fp,prev,SEEK_SET); //go back to where we were + return sz; +} diff --git a/src/InputFile.cpp b/src/InputFile.cpp index 8176966..2e15db0 100644 --- a/src/InputFile.cpp +++ b/src/InputFile.cpp @@ -74,3 +74,7 @@ char* InputFile::readString() throw(PacketExceptionIO*) eof = file->isEOF(); return c; } + +dword InputFile::setpos(dword offset) throw(PacketExceptionIO*) { + return file->setpos(offset); +} diff --git a/src/OutputFile.cpp b/src/OutputFile.cpp index f6a93db..8725e48 100644 --- a/src/OutputFile.cpp +++ b/src/OutputFile.cpp @@ -44,7 +44,12 @@ void OutputFile::close() throw(PacketExceptionIO*) bool OutputFile::open(char** parameters) throw(PacketExceptionIO*) { - file->open(parameters[0], "w"); + if(parameters[1] != 0) { + file->open(parameters[0], parameters[1]); + } + else { + file->open(parameters[0], "w"); + } filename = parameters[0]; isclosed = false; return true; -- GitLab