My Project
OSFileUtil.cpp
Go to the documentation of this file.
1/* $Id$ */
18#include <cstring>
19#include "OSFileUtil.h"
20#include "OSErrorClass.h"
21#include "OSOutput.h"
22
23using std::endl;
24
25
27{
28} // end constructor
29
30
32{
33} // end destructor
34
35std::string FileUtil::getFileAsString( const char* fname)
36{
37 try
38 {
39 std::ostringstream outStr;
40 std::ostringstream fileName;
41 fileName << fname << std::endl;
42 std::string soutString;
43 char ch;
44 std::ifstream inFile( fname);
45 if( !inFile)
46 {
47 throw ErrorClass(" Could not open the given file: " + fileName.str());
48 }
49
50 while( inFile.get( ch ) )
51 {
52 outStr << ch;
53 }
54
55 if( !inFile.eof() )
56 {
57 throw ErrorClass(" There was a problem reading the file: " + fileName.str() );
58 }
59 soutString = outStr.str();
60 inFile.close();
61 return soutString;
62 }
63 catch(const ErrorClass& eclass)
64 {
65 throw ErrorClass( eclass.errormsg) ;
66 }
67} // end getFileAsString
68
69
70char* FileUtil::getFileAsChar(const char* fname)
71{
72 try
73 {
74 std::ostringstream fileName;
75 fileName << fname << std::endl;
76 std::filebuf *pbuf;
77 long bufsize = 0;
78 char *xml;
79 char ch;
80 std::ifstream inFile;
82 inFile.open( fname);
83 if(!inFile)
84 {
85 throw ErrorClass(" Could not read the given file: " + fileName.str() );
86 }
87 // get the input file stream into the buffer
88 pbuf = inFile.rdbuf();
89 // now get the size
90 bufsize = pbuf->pubseekoff(0,std::ios_base::end);
91 // set back to zero
92 pbuf ->pubseekpos(0, std::ios::in);
93 // allocate the character array
94 xml = new char[bufsize + 1];
95 xml[ bufsize] = '\0';
96 bufsize = 0;
97 while( inFile.get( ch ) )
98 {
99 xml[ bufsize] = ch;
100 bufsize++;
101 }
102
103 if( !inFile.eof() )
104 {
105 throw ErrorClass(" There was a problem reading the file: " + fileName.str());
106 }
107 return xml;
108 }
109 catch(const ErrorClass& eclass)
110 {
111 throw ErrorClass( eclass.errormsg) ;
112 }
113} // end getFileAsChar
114
115
116bool FileUtil::writeFileFromString(char* fname, std::string sname)
117{
118 std::ostringstream fileName;
119 fileName << fname << std::endl;
120 FILE *ft ;
121 try
122 {
123 ft = fopen ( fname, "w") ;
124 if ( ft == NULL )
125 {
126 throw ErrorClass(" There was a problem opening the file: " + fileName.str());
127 }
128 char *cstr;
129 cstr = new char [sname.size() + 1];
130 strcpy (cstr, sname.c_str());
131 size_t i;
132 for(i = 0; i < sname.size() + 1; i++)
133 {
134 if(cstr[ i] != '\0') fputc ( cstr[ i], ft ) ;
135 }
136 fclose ( ft);
137 delete[] cstr;
138 cstr = NULL;
139 return true;
140 }
141 catch(const ErrorClass& eclass)
142 {
143 throw ErrorClass( eclass.errormsg) ;
144 }
145} // end writeFileFromString
146
147
148bool FileUtil::writeFileFromString(std::string fname, std::string sname)
149{
150 std::ostringstream fileName;
151 fileName << fname << std::endl;
152 FILE *ft ;
153 try
154 {
155 ft = fopen ( fname.c_str(), "w") ;
156 if ( ft == NULL )
157 {
158 throw ErrorClass(" There was a problem opening the file: " + fileName.str());
159 }
160 char *cstr;
161 cstr = new char [sname.size() + 1];
162 strcpy (cstr, sname.c_str());
163 size_t i;
164 for(i = 0; i < sname.size() + 1; i++)
165 {
166 if(cstr[ i] != '\0') fputc ( cstr[ i], ft ) ;
167 }
168 fclose ( ft);
169 delete[] cstr;
170 cstr = NULL;
171 return true;
172 }
173 catch(const ErrorClass& eclass)
174 {
175 throw ErrorClass( eclass.errormsg) ;
176 }
177} // end writeFileFromString
178
179
180bool FileUtil::writeFileFromChar(char* fname, char* ch)
181{
182 //std::ofstream outFile;
183 std::fstream outFile;
184 outFile.open( fname);
185 if(!outFile.is_open())
186 {
187 return false;
188 }
189 outFile << *ch;
190 outFile.close();
191 return true;
192} // end writeFileFromChar
193
const OSSmartPtr< OSOutput > osoutput
Definition OSOutput.cpp:39
used for throwing exceptions.
std::string errormsg
errormsg is the error that is causing the exception to be thrown
bool writeFileFromString(char *fname, std::string thestring)
write a file from an input string.
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
bool writeFileFromChar(char *fname, char *ch)
write a file from an input char pointer.
~FileUtil()
the class destructor
char * getFileAsChar(const char *fname)
read a file and return contents as a char pointer.
FileUtil()
the class constructor
@ ENUM_OUTPUT_LEVEL_info
@ ENUM_OUTPUT_AREA_OSUtils