00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_FileWriter_cpp)
00056 #define ALIZE_FileWriter_cpp
00057
00058 #if defined(_WIN32)
00059 #define _CRT_SECURE_NO_WARNINGS
00060 #endif
00061
00062 #include "FileWriter.h"
00063 #include "Exception.h"
00064
00065 using namespace alize;
00066
00067
00068 FileWriter::FileWriter(const FileName& f)
00069 :Object(), _pFileStruct(NULL) , _fileName(f), _swap(false){}
00070
00071 bool FileWriter::isClosed() const { return _pFileStruct == NULL; }
00072
00073 bool FileWriter::isOpen() const { return _pFileStruct != NULL; }
00074
00075 void FileWriter::open()
00076 {
00077 if (isOpen())
00078 close();
00079 if (_fileName.isEmpty())
00080 throw Exception("empty file name", __FILE__, __LINE__);
00081 _pFileStruct = ::fopen(_fileName.c_str(),"wb");
00082 if (_pFileStruct == NULL)
00083 throw IOException("Cannot create new file", __FILE__, __LINE__,
00084 _fileName);
00085 }
00086
00087 void FileWriter::close()
00088 {
00089 if (isOpen())
00090 if (::fclose(_pFileStruct) == EOF)
00091 throw IOException("Cannot close file", __FILE__, __LINE__,
00092 _fileName);
00093 _pFileStruct = NULL;
00094 }
00095
00096 void FileWriter::writeUInt4(unsigned long v)
00097 {
00098 assert(_pFileStruct != NULL);
00099 if (sizeof(unsigned int) == 4)
00100 {
00101 if (::fwrite(&v, sizeof(unsigned int), 1, _pFileStruct) != 1)
00102 throw IOException("Cannot write in file", __FILE__, __LINE__,
00103 _fileName);
00104 }
00105 else if (sizeof(unsigned long) == 4)
00106 {
00107 if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00108 throw IOException("Cannot write in file", __FILE__, __LINE__,
00109 _fileName);
00110 }
00111 else
00112 return;
00113 }
00114
00115 void FileWriter::writeDouble(double v)
00116 {
00117 assert(_pFileStruct != NULL);
00118 if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00119 throw IOException("Cannot write in file", __FILE__, __LINE__,
00120 _fileName);
00121 }
00122
00123 void FileWriter::writeFloat(float v)
00124 {
00125 assert(_pFileStruct != NULL);
00126 if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00127 throw IOException("Cannot write in file", __FILE__, __LINE__,
00128 _fileName);
00129 }
00130
00131 void FileWriter::writeShort(short v)
00132 {
00133 assert(_pFileStruct != NULL);
00134 if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00135 throw IOException("Cannot write in file", __FILE__, __LINE__,
00136 _fileName);
00137 }
00138
00139 void FileWriter::writeChar(char v)
00140 {
00141 assert(_pFileStruct != NULL);
00142 if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00143 throw IOException("Cannot write in file", __FILE__, __LINE__,
00144 _fileName);
00145 }
00146
00147 void FileWriter::writeString(const String& string)
00148 {
00149 if (string.isEmpty())
00150 return;
00151 assert(_pFileStruct != NULL);
00152 if (::fwrite(string.c_str(), string.length(), 1, _pFileStruct) != 1)
00153 throw IOException("Cannot write in file", __FILE__, __LINE__,
00154 _fileName);
00155 }
00156
00157 void FileWriter::writeAttribute(const String& name, const String& value)
00158 {
00159
00160 writeString(" " + name + "=\"" + value + "\"");
00161 }
00162
00163 void FileWriter::writeAttribute(const String& name, unsigned long value)
00164 { writeString(" " + name + "=\"" + String::valueOf(value) + "\""); }
00165
00166 void FileWriter::writeAttribute(const String& name, double value)
00167 { writeString(" " + name + "=\"" + String::valueOf(value) + "\""); }
00168
00169 String FileWriter::toString() const
00170 {
00171 return Object::toString()
00172 + "\n file name = '" + _fileName;
00173 }
00174
00175 void FileWriter::swap2Bytes(void *src, void *dest)
00176 {
00177 char *p1,*p2;
00178 char tmp;
00179
00180 p1 = (char*)src;
00181 p2 = (char*)dest;
00182 tmp = p1[1];
00183 p2[1] = p1[0];
00184 p2[0] = tmp;
00185 }
00186
00187 void FileWriter::swap4Bytes(void *src, void *dest)
00188 {
00189 char *p1,*p2;
00190 char tmp;
00191
00192 p1 = (char*)src;
00193 p2 = (char*)dest;
00194 tmp = p1[3];
00195 p2[3] = p1[0];
00196 p2[0] = tmp;
00197 tmp = p1[2];
00198 p2[2] = p1[1];
00199 p2[1] = tmp;
00200 }
00201
00202 void FileWriter::swap8Bytes(void *src, void *dest)
00203 {
00204 char *p1,*p2;
00205 char tmp;
00206 int i;
00207
00208 p1 = (char*)src;
00209 p2 = (char*)dest;
00210 for (i=0; i<4; i++)
00211 {
00212 tmp = p1[i];
00213 p1[i] = p2[7-i];
00214 p2[7-i] = tmp;
00215 }
00216 }
00217
00218 String FileWriter::getClassName() const { return "FileWriter"; }
00219
00220 FileWriter::~FileWriter() { close(); }
00221
00222
00223 #endif // !defined(ALIZE_FileWriter_cpp)
00224