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