Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

File.h

Go to the documentation of this file.
00001 #ifndef Impala_Basis_File_h
00002 #define Impala_Basis_File_h
00003 
00004 #include <cstdio>
00005 #include <cstring>
00006 #include <iostream>
00007 #include "Basis/String.h"
00008 
00009 namespace Impala
00010 {
00011 
00012 
00016 class File
00017 {
00018 public:
00019 
00020     File()
00021     {
00022         mFp = 0;
00023         mBuf = 0;
00024     }
00025 
00026     File(CString fileName, String mode, bool printError = true,
00027          int lineBufSize = 100000)
00028     {
00029         mBuf = 0;
00030         if (!(mFp=fopen(fileName.c_str(), mode.c_str())))
00031         {
00032             if (printError)
00033                 std::cout << "Error opening (mode = " << mode << ") file "
00034                           << fileName << std::endl;
00035             return;
00036         }
00037         mBufSize = lineBufSize;
00038         mBuf = new char[mBufSize];
00039     }
00040 
00041     ~File()
00042     {
00043         Close();
00044         if (mBuf)
00045             delete mBuf;
00046     }
00047 
00048     bool
00049     Valid() const
00050     {
00051         return mFp != 0;
00052     }
00053 
00054     void
00055     Close()
00056     {
00057         if (mFp)
00058         {
00059             fclose(mFp);
00060             mFp = 0;
00061         }
00062     }
00063 
00064     bool
00065     Eof()
00066     {
00067         return feof(mFp) ? true : false;
00068     }
00069 
00070     // skipEC skips empty lines and comments starting with #
00071     // NOTE that the last line may be empty before eof is reached!
00072     String
00073     ReadLine(bool skipEC = false)
00074     {
00075         mBuf[0] = '\0';
00076         bool found = (skipEC) ? false : true;
00077         do
00078         {
00079             fgets(mBuf, mBufSize, mFp);
00080             if (strlen(mBuf) > 0 && mBuf[0] != '#' && mBuf[0] != '\r')
00081                 found = true;
00082             if (!found && feof(mFp))
00083                 found = true;
00084         }
00085         while (! found);
00086 
00087         int len = strlen(mBuf);
00088         if (len == 0)
00089             return String("");
00090         while ((mBuf[len-1] == '\n') || (mBuf[len-1] == '\r'))
00091         {   // strip trailing LF and CR chars
00092             mBuf[len-1] = '\0';
00093             len = len - 1;
00094         }
00095         return String(mBuf, len);
00096     }
00097 
00098     FILE*
00099     Fp()
00100     {
00101         return mFp;
00102     }
00103 
00104     void
00105     Rewind()
00106     {
00107         if (mFp)
00108         {
00109             if (fseek(mFp, 0, SEEK_SET) != 0)
00110                 std::cout << "[File::Rewind] error" << std::endl;
00111         }
00112     }
00113 
00114     int
00115     Size()
00116     {
00117         int pos = ftell(mFp);
00118         fseek(mFp, 0, SEEK_END);
00119         int size = ftell(mFp);
00120         fseek(mFp, 0, pos);
00121         return size;
00122     }
00123 
00124     int
00125     GetPosition()
00126     {
00127         return ftell(mFp);
00128     }
00129 
00130 private:
00131 
00132     // not supported : need to handle "copying" of FILE
00133     File(const File& f)
00134     {
00135     }
00136 
00137     File&
00138     operator=(const File& f)
00139     {
00140         return *this;
00141     }
00142             
00143     FILE* mFp;
00144     char* mBuf;
00145     int   mBufSize;
00146 };
00147 
00148 } // namespace Impala
00149 
00150 #endif

Generated on Fri Mar 19 09:30:42 2010 for ImpalaSrc by  doxygen 1.5.1