/home/coin/SVN-release/OS-2.4.2/OS/src/OSUtils/OSFileUtil.cpp

Go to the documentation of this file.
00001 /* $Id: OSFileUtil.cpp 4292 2011-09-21 05:47:18Z kmartin $ */
00018 #include <cstring>
00019 #include "OSFileUtil.h"
00020 #include "OSErrorClass.h"
00021 using std::cout;
00022 using std::endl;
00023 
00024 
00025 FileUtil::FileUtil()
00026 {
00027 } // end constructor
00028 
00029 
00030 FileUtil::~FileUtil()
00031 {
00032 } // end destructor
00033 
00034 std::string FileUtil::getFileAsString( const char* fname)
00035 {
00036     try
00037     {
00038         std::ostringstream outStr;
00039         std::ostringstream fileName;
00040         fileName << fname << std::endl;
00041         std::string soutString;
00042         char ch;
00043         //std::cout << "Inside FileUtil:getFileAsString and calling inFile " << fname << std::endl;
00044         std::ifstream inFile( fname);
00045         if( !inFile)
00046         {
00047             throw ErrorClass(" Could not read the given file: " + fileName.str());
00048         }
00049         //std::cout << "Inside FileUtil:getFileAsString, file read put into ostringstream" << std::endl;
00050         //while((ch = inFile.get() ) != EOF){
00051         //      outStr << ch;
00052         //      std::cout << ch ;
00053         //}
00054 
00055         while( inFile.get( ch ) )
00056         {
00057             outStr << ch;
00058             //std::cout << ch ;
00059         }
00060 
00061         if( !inFile.eof() )
00062         {
00063             throw ErrorClass(" There was a problem reading the file: " + fileName.str() );
00064         }
00065         //std::cout << std::endl;
00066         //std::cout << "Inside FileUtil:getFileAsString, convert to a string" << std::endl;
00067         soutString = outStr.str();
00068         inFile.close();
00069         return soutString;
00070     }
00071     catch(const ErrorClass& eclass)
00072     {
00073         throw ErrorClass( eclass.errormsg) ;
00074     }
00075 } // end getFileAsString
00076 
00077 
00078 
00079 char* FileUtil::getFileAsChar(const  char* fname)
00080 {
00081     try
00082     {
00083 
00084         std::ostringstream fileName;
00085         fileName << fname << std::endl;
00086         std::filebuf *pbuf;
00087         long bufsize = 0;
00088         char *xml;
00089         char ch;
00090         std::ifstream inFile;
00091         std::cout << fname << std::endl;
00092         inFile.open( fname);
00093         if(!inFile)
00094         {
00095             throw ErrorClass(" Could not read the given file: " + fileName.str() );
00096         }
00097         // get the input file stream into the buffer
00098         pbuf = inFile.rdbuf();
00099         // now get the size
00100         bufsize = pbuf->pubseekoff(0,std::ios_base::end);
00101         // set back to zero
00102         pbuf ->pubseekpos(0, std::ios::in);
00103         // allocate the character array
00104         xml = new char[bufsize + 1];
00105         xml[ bufsize] =  '\0';
00106         bufsize = 0;
00107         while( inFile.get( ch ) )
00108         {
00109             xml[ bufsize] = ch;
00110             bufsize++;
00111         }
00112 
00113         if( !inFile.eof() )
00114         {
00115             throw ErrorClass(" There was a problem reading the file: " + fileName.str());
00116         }
00117         //while((ch = inFile.get()) != EOF ){
00118         //      xml[ bufsize] = ch;
00119         //      bufsize++;
00120         //}
00121         return xml;
00122     }
00123     catch(const ErrorClass& eclass)
00124     {
00125         throw ErrorClass( eclass.errormsg) ;
00126     }
00127 } // end getFileAsChar
00128 
00129 
00130 
00131 
00132 
00133 bool FileUtil::writeFileFromString(char* fname, std::string sname)
00134 {
00135     //std::ofstream outFile;
00136     //std::fstream outFile;
00137     //outFile.open( fname);
00138     //if(!outFile.is_open()){
00139     //  return false;
00140     //}
00141     //outFile << sname;
00142     //outFile.close();
00143     //return true;
00144 
00145     std::ostringstream fileName;
00146     fileName << fname << std::endl;
00147     FILE *ft ;
00148     try
00149     {
00150         ft = fopen ( fname, "w") ;
00151         if ( ft == NULL )
00152         {
00153             throw ErrorClass(" There was a problem opening the file: " + fileName.str());
00154         }
00155         char *cstr;
00156         cstr = new char [sname.size() + 1];
00157         strcpy (cstr, sname.c_str());
00158         size_t i;
00159         for(i = 0; i < sname.size() + 1; i++)
00160         {
00161             if(cstr[ i] != '\0')     fputc ( cstr[ i], ft )  ;
00162         }
00163 //          fputc( '\n', ft);
00164         fclose ( ft);
00165         delete[] cstr;
00166         cstr = NULL;
00167         return true;
00168     }
00169     catch(const ErrorClass& eclass)
00170     {
00171         throw ErrorClass( eclass.errormsg) ;
00172     }
00173 } // end writeFileFromString
00174 
00175 
00176 bool FileUtil::writeFileFromString(std::string  fname, std::string sname)
00177 {
00178 
00179     std::ostringstream fileName;
00180     fileName << fname << std::endl;
00181     FILE *ft ;
00182     try
00183     {
00184         ft = fopen ( fname.c_str(), "w") ;
00185         if ( ft == NULL )
00186         {
00187             throw ErrorClass(" There was a problem opening the file: " + fileName.str());
00188         }
00189         char *cstr;
00190         cstr = new char [sname.size() + 1];
00191         strcpy (cstr, sname.c_str());
00192         size_t i;
00193         for(i = 0; i < sname.size() + 1; i++)
00194         {
00195             if(cstr[ i] != '\0')     fputc ( cstr[ i], ft )  ;
00196         }
00197 //          fputc( '\n', ft);
00198         fclose ( ft);
00199         delete[] cstr;
00200         cstr = NULL;
00201         return true;
00202     }
00203     catch(const ErrorClass& eclass)
00204     {
00205         throw ErrorClass( eclass.errormsg) ;
00206     }
00207 } // end writeFileFromString
00208 
00209 bool FileUtil::writeFileFromChar(char* fname, char* ch)
00210 {
00211     //std::ofstream outFile;
00212     std::fstream outFile;
00213     outFile.open( fname);
00214     if(!outFile.is_open())
00215     {
00216         return false;
00217     }
00218     outFile << *ch;
00219     outFile.close();
00220     return true;
00221 } // end writeFileFromChar
00222 

Generated on Wed Nov 30 03:04:24 2011 by  doxygen 1.4.7