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_FileInfo_cpp)
00056 #define ALIZE_FileInfo_cpp
00057
00058 #if defined(_WIN32)
00059 #pragma warning( disable : 4127 4702)
00060 #endif
00061
00062 #include "FileInfo.h"
00063 #include "Exception.h"
00064
00065 using namespace alize;
00066
00067
00068 FileInfo::FileInfo(const FileName & f):Object(), _pFileStruct(NULL),
00069 _fileName(f), _swap(false)
00070 {
00071 }
00072
00073
00074 bool FileInfo::isClosed() const
00075 {
00076 return _pFileStruct == NULL;
00077 }
00078
00079
00080 bool FileInfo::isOpen() const
00081 {
00082 return _pFileStruct != NULL;
00083 }
00084
00085
00086 void FileInfo::open(int mode)
00087 {
00088 if (isOpen())
00089 close();
00090 if (_fileName.isEmpty())
00091 throw Exception("empty file name", __FILE__, __LINE__);
00092 if (mode == 1)
00093 _pFileStruct =::fopen(_fileName.c_str(), "wb");
00094 else if (mode == 2)
00095 _pFileStruct =::fopen(_fileName.c_str(), "rb");
00096 if (_pFileStruct == NULL)
00097 throw IOException("Cannot create new file", __FILE__, __LINE__,
00098 _fileName);
00099 }
00100
00101
00102 void FileInfo::close()
00103 {
00104 if (isOpen())
00105 if (::fclose(_pFileStruct) == EOF)
00106 throw IOException("Cannot close file", __FILE__, __LINE__, _fileName);
00107 _pFileStruct = NULL;
00108 }
00109
00110
00111 void FileInfo::writeTopInfo(const LKVector & v, Config & c)
00112 {
00113 if (isClosed())
00114 open(1);
00115 assert(_pFileStruct != NULL);
00116 for (int i = 0; i < c.getParam("topDistribsCount").toLong(); i++)
00117 if (::fwrite(&(v.getArray()[i].idx), sizeof(v.getArray()[i].idx), 1,
00118 _pFileStruct) != 1)
00119 throw IOException("Cannot write in file", __FILE__, __LINE__,
00120 _fileName);
00121
00122 if (::fwrite(&v.sumNonTopDistribLK, sizeof(v.sumNonTopDistribLK), 1,
00123 _pFileStruct) != 1)
00124 throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00125
00126 if (::fwrite(&v.sumNonTopDistribWeights, sizeof(v.sumNonTopDistribWeights),
00127 1, _pFileStruct) != 1)
00128 throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00129
00130 }
00131
00132
00133 void FileInfo::writeTopInfo(RealVector <real_t> & v, Config & c)
00134 {
00135 if (isClosed())
00136 open(1);
00137 assert(_pFileStruct != NULL);
00138
00139 for (unsigned long i = 0; i < v.size(); i++){
00140 if (i<c.getParam("topDistribsCount").toULong()){
00141 unsigned long tmp=(unsigned long)v[i];
00142 if (::fwrite(&tmp, sizeof(tmp), 1,_pFileStruct) != 1)
00143 throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00144 }
00145 else {
00146 if (::fwrite(&v[i], sizeof(v[i]), 1,_pFileStruct) != 1)
00147 throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00148 }
00149
00150 }
00151
00152 }
00153
00154
00155 void FileInfo::loadTopInfo(StatServer & ss, unsigned long &numLigne,
00156 Config & c)
00157 {
00158 ULongVector index;
00159 double sumNonSelectedWeights;
00160 double sumNonSelectedLLK;
00161 unsigned long s;
00162 if (isClosed())
00163 {
00164 open(2);
00165 }
00166
00167 assert(_pFileStruct != NULL);
00168 unsigned long pos = numLigne * (c.getParam("topDistribsCount").toLong() * sizeof(unsigned long) + 2 * sizeof(real_t));
00169
00170
00171 if (::fseek(_pFileStruct, pos, SEEK_SET) != 0)
00172 throw IOException("seek out of bounds", __FILE__, __LINE__, _fileName);
00173
00174 for (int i = 0; i < c.getParam("topDistribsCount").toLong(); i++)
00175 {
00176 ::fread(&s, 1, sizeof(unsigned long), _pFileStruct);
00177 index.addValue(s);
00178 }
00179
00180 ::fread(&sumNonSelectedLLK, 1, sizeof(double), _pFileStruct);
00181
00182 ::fread(&sumNonSelectedWeights, 1, sizeof(double), _pFileStruct);
00183 ss.setTopDistribIndexVector(index, sumNonSelectedWeights, sumNonSelectedLLK);
00184
00185
00186
00187 }
00188
00189
00190 String FileInfo::toString() const
00191 {
00192 return Object::toString() + "\n file name = '" + _fileName;
00193 }
00194
00195
00196 String FileInfo::getClassName() const
00197 {
00198 return "FileInfo";
00199 }
00200
00201
00202 FileInfo::~FileInfo()
00203 {
00204 close();
00205 }
00206
00207
00208
00209 #endif // !defined(ALIZE_FileInfo_cpp)