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