/home/coin/SVN-release/OS-1.0.0/OS/src/OSUtils/FileUtil.cpp

Go to the documentation of this file.
00001 
00019 #include "FileUtil.h"
00020 
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(  char* fname){
00032         std::ostringstream outStr;
00033         std::string soutString;
00034         char ch;
00035         std::ifstream inFile( fname);
00036         if(!inFile){
00037                 cout << "Could not open file" << endl; 
00038                 return "";
00039         }
00040         while((ch = inFile.get() ) != EOF){
00041                 outStr << ch;
00042         }
00043         soutString = outStr.str();
00044         inFile.close();
00045         return soutString;
00046 
00047 } // end getFileAsString
00048 
00049 
00050 
00051 char* FileUtil::getFileAsChar(  char* fname){
00052         std::filebuf *pbuf;
00053         long bufsize = 0;
00054         char *xml;
00055         char ch;
00056         std::ifstream inFile;
00057         std::cout << fname << std::endl;
00058         inFile.open( fname);
00059         // get the input file stream into the buffer
00060         pbuf = inFile.rdbuf();
00061         // now get the size
00062         bufsize = pbuf->pubseekoff(0,std::ios_base::end);
00063         // set back to zero
00064         pbuf ->pubseekpos(0, std::ios::in);
00065         // allocate the character array
00066         xml = new char[bufsize + 1];
00067         xml[ bufsize] =  '\0';
00068         bufsize = 0;
00069         while((ch = inFile.get()) != EOF ){
00070                 xml[ bufsize] = ch;
00071                 bufsize++;
00072         }
00073         return xml;
00074 } // end getFileAsChar
00075 
00076 
00077 
00078 
00079 
00080 bool FileUtil::writeFileFromString(char* fname, std::string sname){
00081         std::ofstream outFile;
00082         outFile.open( fname);
00083         if(!outFile.is_open()){
00084                 return false;
00085         }
00086         outFile << sname;
00087         outFile.close();
00088         return true;
00089 } // end writeFileFromString
00090 
00091 bool FileUtil::writeFileFromString(std::string  fname, std::string sname){
00092         std::ofstream outFile;
00093         outFile.open( &fname[ 0] );
00094         if(!outFile.is_open()){
00095                 return false;
00096         }
00097         outFile << sname;
00098         outFile.close();
00099         return true;
00100 } // end writeFileFromString
00101 
00102 bool FileUtil::writeFileFromChar(char* fname, char* ch){
00103         std::ofstream outFile;
00104         outFile.open( fname);
00105         if(!outFile.is_open()){
00106                 return false;
00107         }
00108         outFile << *ch;
00109         outFile.close();
00110         return true;
00111 } // end writeFileFromChar
00112  

Generated on Thu May 15 22:15:05 2008 by  doxygen 1.4.7