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

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

Generated on Mon Aug 3 03:02:25 2009 by  doxygen 1.4.7