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