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

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

Generated on Fri Jan 7 03:24:43 2011 by  doxygen 1.4.7