00001
00019 #include "OSFileUtil.h"
00020 #include "OSErrorClass.h"
00021 using std::cout;
00022 using std::endl;
00023
00024 FileUtil::FileUtil(){
00025 }
00026
00027
00028 FileUtil::~FileUtil(){
00029 }
00030
00031 std::string FileUtil::getFileAsString( const char* fname){
00032 try{
00033 std::ostringstream outStr;
00034 std::string soutString;
00035 char ch;
00036
00037 std::ifstream inFile( fname);
00038 if( !inFile){
00039 throw ErrorClass(" Could not read the given file");
00040 }
00041
00042
00043
00044
00045
00046
00047 while( inFile.get( ch ) ){
00048 outStr << ch;
00049
00050 }
00051
00052 if( !inFile.eof() ){
00053 throw ErrorClass(" There was a problem reading the file");
00054 }
00055
00056
00057 soutString = outStr.str();
00058 inFile.close();
00059 return soutString;
00060 }
00061 catch(const ErrorClass& eclass){
00062 throw ErrorClass( eclass.errormsg) ;
00063 }
00064 }
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
00081 pbuf = inFile.rdbuf();
00082
00083 bufsize = pbuf->pubseekoff(0,std::ios_base::end);
00084
00085 pbuf ->pubseekpos(0, std::ios::in);
00086
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
00099
00100
00101
00102 return xml;
00103 }
00104 catch(const ErrorClass& eclass){
00105 throw ErrorClass( eclass.errormsg) ;
00106 }
00107 }
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 }
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 }
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 }
00145