00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "cpsp/HCoreDatabaseFILE.hh"
00021 #include "cpsp/Exception.hh"
00022
00023 #include <algorithm>
00024 #include <functional>
00025
00026 #ifdef WIN32 // fuer systemspezifische verzeichnis-inhaltsauflistung
00027 # include <io.h>
00028 #else
00029 # include <dirent.h>
00030 #endif
00031
00032 #include <biu/util/Util_String.h>
00033
00034 #include <biu/LatticeDescriptorCUB.hh>
00035 #include <biu/LatticeDescriptorFCC.hh>
00036
00037 namespace cpsp
00038 {
00039
00041
00043
00044 const std::string ROOT_CUB = std::string("Cubic/");
00045 const std::string ROOT_FCC = std::string("Fcc/");
00046
00047 const std::string PREF_CUB = std::string("cub.");
00048 const std::string PREF_FCC = std::string("fcc.");
00049
00050 const std::string POSTFIX = std::string(".txt");
00051
00052
00053 HCoreDatabaseFILE::HCoreDatabaseFILE(const std::string& dbRootPath) :
00054 coreSize(0), minHH(0), maxHH(UINT_MAX-2),
00055
00056 DBROOTPATH(dbRootPath+(dbRootPath[dbRootPath.size()-1] == '/'?"":"/")),
00057 actFile(NULL), actFileNamePref(""), actFileHHcontInd(-1)
00058 {
00059 actHHcontacts.clear();
00060 }
00061
00062 HCoreDatabaseFILE::HCoreDatabaseFILE(const HCoreDatabaseFILE& toCopy) :
00063 coreSize(0), minHH(toCopy.minHH), maxHH(toCopy.maxHH),
00064 DBROOTPATH(toCopy.DBROOTPATH), actFile(NULL), actFileNamePref(""),
00065 actFileHHcontInd(-1)
00066 {
00067 }
00068
00069 HCoreDatabaseFILE::~HCoreDatabaseFILE()
00070 {
00071 if (actFile != NULL) {
00072 actFile->close();
00073 delete actFile;
00074 }
00075 actFile = NULL;
00076 }
00077
00078
00080
00082
00083 bool HCoreDatabaseFILE::initCoreAccess(const biu::LatticeDescriptor& latDescr, const unsigned int size,
00084 const unsigned int minHH_, const unsigned int maxHH_)
00085 {
00086
00087
00088 this->coreSize = size;
00089 this->minHH = minHH_;
00090 this->maxHH = maxHH_;
00091
00092 if (actFile != NULL) { delete actFile; actFile = NULL; }
00093 actFileHHcontInd = -1;
00094 actFileNamePref = "";
00095
00096
00097
00098
00099 std::string actDBdirName = DBROOTPATH, prefix = "";
00100
00101 if (latDescr == biu::LatticeDescriptorCUB()) {
00102 actDBdirName += ROOT_CUB; prefix += PREF_CUB;
00103 } else if (latDescr == biu::LatticeDescriptorFCC()) {
00104 actDBdirName += ROOT_FCC; prefix += PREF_FCC;
00105 } else {
00106 std::cerr <<"\n\tError: No core files for lattice type '"+latDescr.getName()+"' available!\n\n";
00107 return false;
00108 }
00109 actDBdirName += (biu::util::Util_String::int2str(size))+"/";
00110 prefix += (biu::util::Util_String::int2str(size)) + ".";
00111
00112 actHHcontacts.clear();
00113
00114 std::string filename;
00115
00116
00117 #ifdef WIN32
00118 long hFile;
00119 struct _finddata_t c_file;
00120 hFile = _findfirst ((actDBdirName+prefix+"*"+POSTFIX).c_str(), &c_file);
00121 if (hFile != -1L) {
00122
00123 do {
00124 filename = c_file.name;
00125 #else // linux
00126 DIR *actDBdir = opendir(actDBdirName.c_str());
00127 if(actDBdir != NULL)
00128 {
00129
00130 struct dirent *dirEntry;
00131
00132 while((dirEntry=readdir(actDBdir)))
00133 {
00134 filename = dirEntry->d_name;
00135 #endif
00136
00137 if (filename.substr(0,prefix.size()).compare(prefix) == 0) {
00138 filename = filename.erase(0,prefix.size());
00139 actHHcontacts.push_back(biu::util::Util_String::str2int(filename.erase(filename.find(POSTFIX))));
00140 }
00141 }
00142 #ifdef WIN32
00143 while( _findnext( hFile, &c_file ) == 0 );
00144 #endif
00145 } else {
00146 std::cerr <<"\n\tError: No core file for size '"<<size <<"' and\n\t lattice type '"
00147 <<latDescr.getName() <<"' available!\n\t==> no directory '"<<actDBdirName<<"'\n\n";
00148 return false;
00149 cpsp::Exception::errorCount++;
00150 }
00151
00152
00153 #ifdef WIN32
00154 _findclose( hFile );
00155 #else
00156 closedir(actDBdir);
00157 #endif
00158
00159
00160
00161 std::sort(actHHcontacts.begin(), actHHcontacts.end() , std::greater<int>());
00162
00163
00164
00165
00166 unsigned int i=0;
00167 while ( i<actHHcontacts.size() && actHHcontacts[i]>0 &&((unsigned int)actHHcontacts[i]>maxHH || (unsigned int)actHHcontacts[i]<minHH)) {
00168
00169 i++;
00170 }
00171
00172 if (i >= actHHcontacts.size()) {
00173
00174 return false;
00175 }
00176
00177 actFileHHcontInd = i;
00178 actFileNamePref = actDBdirName+prefix;
00179 if (actFile != NULL) {
00180 delete actFile; actFile = NULL;
00181 }
00182 actFile = new std::ifstream((actFileNamePref+(biu::util::Util_String::int2str(actHHcontacts[actFileHHcontInd]))+POSTFIX).c_str());
00183
00184 return true;
00185 }
00186
00187 bool HCoreDatabaseFILE::getNextCore(HCore& toFill) {
00188
00189 toFill.reset(INT_MAX, INT_MAX);
00190 if (actFile == NULL)
00191 return false;
00192
00193 std::string dataline = "";
00194 int i=0;
00195 int p[3];
00196
00197 do {
00198
00200 while (std::getline(*actFile, dataline, '#')) {
00201 if (!dataline.empty() && (dataline.substr(0,8).compare("COREDATA")==0)){
00202 try {
00203 std::istringstream iss(dataline.substr(9));
00204 toFill.reset(actHHcontacts[actFileHHcontInd], coreSize);
00205 for (i=0; i<(int)coreSize; i++) {
00206 p[2] = INT_MIN;
00207 iss >>p[0] >>p[1] >>p[2];
00208 if (p[2] == INT_MIN)
00209 throw ExcEOF();
00210 toFill.addPoint(HCore::CorePoint(p[0], p[1], p[2]));
00211 }
00212 } catch (ExcEOF)
00213 {
00214 toFill.reset(INT_MAX,INT_MAX);
00215 return false;
00216 }
00217 return toFill.isValid();
00218 }
00219 }
00220 actFile->close();
00221 delete actFile;
00222 actFile = NULL;
00223
00224
00225 if ((++actFileHHcontInd) < (int)actHHcontacts.size() && actHHcontacts[actFileHHcontInd]>=(int)minHH) {
00226
00227 actFile = new std::ifstream((actFileNamePref+(biu::util::Util_String::int2str(actHHcontacts[actFileHHcontInd]))+POSTFIX).c_str());
00228 }
00229 } while (actFile != NULL);
00230
00231 return false;
00232 }
00233
00234
00235 }