/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OS/OS/src/OSUtils/OSFileUtil.cpp

Go to the documentation of this file.
00001 
00019 #include "OSFileUtil.h"
00020 #include "OSErrorClass.h"
00021 using std::cout;
00022 using std::endl;
00023 
00024 FileUtil::FileUtil(){
00025 } // end constructor
00026 
00027 
00028 FileUtil::~FileUtil(){  
00029 } // end destructor
00030 
00031 std::string FileUtil::getFileAsString( const char* fname){
00032         try{
00033                 std::ostringstream outStr;
00034                 std::string soutString;
00035                 char ch;
00036                 //std::cout << "Inside FileUtil:getFileAsString and calling inFile" << std::endl;
00037                 std::ifstream inFile( fname);
00038                 if( !inFile){
00039                         throw ErrorClass(" Could not read the given file");
00040                 }
00041                 //std::cout << "Inside FileUtil:getFileAsString, file read put into ostringstream" << std::endl;
00042                 //while((ch = inFile.get() ) != EOF){
00043                 //      outStr << ch;
00044                 //      std::cout << ch ;
00045                 //}
00046                 
00047                 while( inFile.get( ch ) ){
00048                         outStr << ch;
00049                         //std::cout << ch ;
00050                 }
00051                 
00052                 if( !inFile.eof() ){
00053                         throw ErrorClass(" There was a problem reading the file");
00054                 }
00055                 //std::cout << std::endl;
00056                 //std::cout << "Inside FileUtil:getFileAsString, convert to a string" << std::endl;
00057                 soutString = outStr.str();
00058                 inFile.close();
00059                 return soutString;
00060         }
00061         catch(const ErrorClass& eclass){
00062                 throw ErrorClass( eclass.errormsg) ;
00063         }
00064 } // end getFileAsString
00065 
00066 
00067 
00068 char* FileUtil::getFileAsChar(const  char* fname){
00069         try{
00070                 std::filebuf *pbuf;
00071                 long bufsize = 0;
00072                 char *xml;
00073                 char ch;
00074                 std::ifstream inFile;
00075                 std::cout << fname << std::endl;
00076                 inFile.open( fname);
00077                 if(!inFile){
00078                         throw ErrorClass(" Could not read the given file");
00079                 }
00080                 // get the input file stream into the buffer
00081                 pbuf = inFile.rdbuf();
00082                 // now get the size
00083                 bufsize = pbuf->pubseekoff(0,std::ios_base::end);
00084                 // set back to zero
00085                 pbuf ->pubseekpos(0, std::ios::in);
00086                 // allocate the character array
00087                 xml = new char[bufsize + 1];
00088                 xml[ bufsize] =  '\0';
00089                 bufsize = 0;
00090                 while( inFile.get( ch ) ){
00091                         xml[ bufsize] = ch;
00092                         bufsize++;
00093                 }
00094                 
00095                 if( !inFile.eof() ){
00096                         throw ErrorClass(" There was a problem reading the file");
00097                 }
00098                 //while((ch = inFile.get()) != EOF ){
00099                 //      xml[ bufsize] = ch;
00100                 //      bufsize++;
00101                 //}
00102                 return xml;
00103         }
00104         catch(const ErrorClass& eclass){
00105                 throw ErrorClass( eclass.errormsg) ;
00106         }
00107 } // end getFileAsChar
00108 
00109 
00110 
00111 
00112 
00113 bool FileUtil::writeFileFromString(char* fname, std::string sname){
00114         std::ofstream outFile;
00115         outFile.open( fname);
00116         if(!outFile.is_open()){
00117                 return false;
00118         }
00119         outFile << sname;
00120         outFile.close();
00121         return true;
00122 } // end writeFileFromString
00123 
00124 bool FileUtil::writeFileFromString(std::string  fname, std::string sname){
00125         std::ofstream outFile;
00126         outFile.open( fname.c_str()  );
00127         if(!outFile.is_open()){
00128                 return false;
00129         }
00130         outFile << sname;
00131         outFile.close();
00132         return true;
00133 } // end writeFileFromString
00134 
00135 bool FileUtil::writeFileFromChar(char* fname, char* ch){
00136         std::ofstream outFile;
00137         outFile.open( fname);
00138         if(!outFile.is_open()){
00139                 return false;
00140         }
00141         outFile << *ch;
00142         outFile.close();
00143         return true;
00144 } // end writeFileFromChar
00145  

Generated on Sat Mar 29 22:38:03 2008 by  doxygen 1.5.3