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