00001
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 }
00029
00030
00031 FileUtil::~FileUtil(){
00032 }
00033
00034 std::string FileUtil::getFileAsString( const char* fname){
00035 try{
00036 std::ostringstream outStr;
00037 std::string soutString;
00038 char ch;
00039
00040 std::ifstream inFile( fname);
00041 if( !inFile){
00042 throw ErrorClass(" Could not read the given file");
00043 }
00044
00045
00046
00047
00048
00049
00050 while( inFile.get( ch ) ){
00051 outStr << ch;
00052
00053 }
00054
00055 if( !inFile.eof() ){
00056 throw ErrorClass(" There was a problem reading the file");
00057 }
00058
00059
00060 soutString = outStr.str();
00061 inFile.close();
00062 return soutString;
00063 }
00064 catch(const ErrorClass& eclass){
00065 throw ErrorClass( eclass.errormsg) ;
00066 }
00067 }
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
00084 pbuf = inFile.rdbuf();
00085
00086 bufsize = pbuf->pubseekoff(0,std::ios_base::end);
00087
00088 pbuf ->pubseekpos(0, std::ios::in);
00089
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
00102
00103
00104
00105 return xml;
00106 }
00107 catch(const ErrorClass& eclass){
00108 throw ErrorClass( eclass.errormsg) ;
00109 }
00110 }
00111
00112
00113
00114
00115
00116 bool FileUtil::writeFileFromString(char* fname, std::string sname){
00117
00118
00119
00120
00121
00122
00123
00124
00125
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
00141 fclose ( ft);
00142 return true;
00143 }
00144 catch(const ErrorClass& eclass){
00145 throw ErrorClass( eclass.errormsg) ;
00146 }
00147 }
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
00166 fclose ( ft);
00167 return true;
00168 }
00169 catch(const ErrorClass& eclass){
00170 throw ErrorClass( eclass.errormsg) ;
00171 }
00172 }
00173
00174 bool FileUtil::writeFileFromChar(char* fname, char* ch){
00175
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 }
00185