| 1 |  | /********************************************************************** | 
| 2 |  |  | 
| 3 |  | This basic Periodic Table class was originally taken from the data.cpp | 
| 4 | < | file in OpenBabel. The code has been modified to match the OOPSE coding style. | 
| 4 | > | file in OpenBabel. The code has been modified to match the OpenMD coding style. | 
| 5 |  |  | 
| 6 |  | We have retained the OpenBabel copyright and GPL license on this class: | 
| 7 |  |  | 
| 30 |  | */ | 
| 31 |  |  | 
| 32 |  | #include "config.h" | 
| 33 | + |  | 
| 34 | + | #include <iostream> | 
| 35 | + | #include <cstdlib> | 
| 36 |  | #include <string> | 
| 37 |  | #include <fstream> | 
| 38 | + | #include <cstdlib> | 
| 39 |  | #include "utils/ElementsTable.hpp" | 
| 40 |  | #include "utils/simError.h" | 
| 41 | + | #include "io/ifstrstream.hpp" | 
| 42 |  |  | 
| 43 | < | #if !HAVE_STRNCASECMP | 
| 44 | < | extern "C" int strncasecmp(const char *s1, const char *s2, size_t n); | 
| 43 | > | #ifdef _MSC_VER | 
| 44 | > | #define strncasecmp _strnicmp | 
| 45 | > | #define strcasecmp _stricmp | 
| 46 |  | #endif | 
| 47 |  |  | 
| 48 | + |  | 
| 49 |  | #ifdef WIN32 | 
| 50 |  | #define FILE_SEP_CHAR "\\" | 
| 51 |  | #else | 
| 56 |  | #define BUFF_SIZE 32768 | 
| 57 |  | #endif | 
| 58 |  |  | 
| 59 | < | namespace oopse { | 
| 59 | > | namespace OpenMD { | 
| 60 |  |  | 
| 61 |  | ElementsTable etab; | 
| 62 |  |  | 
| 63 |  | ElementsTable::ElementsTable() { | 
| 64 |  | init_ = false; | 
| 65 | < | STR_DEFINE(dir_, FRC_PATH ); | 
| 65 | > | dir_ = std::string("TO_STRING(FRC_PATH)"); | 
| 66 |  | envvar_ = "FORCE_PARAM_PATH"; | 
| 67 |  | filename_ = "element.txt"; | 
| 68 |  | } | 
| 81 |  | RealType red, green, blue; | 
| 82 |  |  | 
| 83 |  | // skip comment line (at the top) | 
| 77 | – |  | 
| 84 |  | if (line[0] != '#')  { | 
| 85 |  | sscanf(line,"%d %5s %lf %*f %lf %d %lf %lf %lf %lf %lf %lf %lf %255s", | 
| 86 |  | &num, | 
| 101 |  | elNeg, ionize, elAffin, red, green, blue, | 
| 102 |  | name); | 
| 103 |  | elements_.push_back(ele); | 
| 104 | + |  | 
| 105 |  | } | 
| 106 |  | } | 
| 107 |  |  | 
| 112 |  | return elements_.size(); | 
| 113 |  | } | 
| 114 |  |  | 
| 115 | < | char *ElementsTable::GetSymbol(int atomicnum) { | 
| 115 | > | const char *ElementsTable::GetSymbol(int atomicnum) { | 
| 116 |  | if (!init_) | 
| 117 |  | Init(); | 
| 118 |  |  | 
| 291 |  | init_ = true; | 
| 292 |  |  | 
| 293 |  | std::string buffer, subbuffer; | 
| 294 | < | std::ifstream ifs1, ifs2, ifs3, ifs4, *ifsP; | 
| 294 | > | ifstrstream ifs1, ifs2, ifs3, ifs4, *ifsP; | 
| 295 |  | // First, look for an environment variable | 
| 296 |  | if (getenv(envvar_.c_str()) != NULL) { | 
| 297 |  | buffer = getenv(envvar_.c_str()); | 
| 298 |  | buffer += FILE_SEP_CHAR; | 
| 299 |  |  | 
| 300 | + |  | 
| 301 | + |  | 
| 302 | + |  | 
| 303 |  | if (!subdir_.empty()) { | 
| 304 |  | subbuffer = buffer; | 
| 305 |  | subbuffer += subdir_; | 
| 306 |  | subbuffer += FILE_SEP_CHAR; | 
| 307 |  | } | 
| 308 |  |  | 
| 309 | + |  | 
| 310 | + |  | 
| 311 |  | buffer += filename_; | 
| 312 |  | subbuffer += filename_; | 
| 313 | + |  | 
| 314 |  |  | 
| 315 |  | ifs1.open(subbuffer.c_str()); | 
| 316 |  | ifsP= &ifs1; | 
| 317 | < | if (!(*ifsP)) { | 
| 317 | > | if (!(ifsP->is_open())) { | 
| 318 |  | ifs2.open(buffer.c_str()); | 
| 319 |  | ifsP = &ifs2; | 
| 320 |  | } | 
| 321 | + |  | 
| 322 |  | } else { | 
| 323 |  | sprintf( painCave.errMsg, | 
| 324 |  | "ElementsTable error.\n" | 
| 331 |  | if ((*ifsP)) { | 
| 332 |  | while(ifsP->getline(charBuffer,BUFF_SIZE)) | 
| 333 |  | ParseLine(charBuffer); | 
| 334 | < |  | 
| 335 | < | if (ifs1) | 
| 336 | < | ifs1.close(); | 
| 337 | < | if (ifs2) | 
| 338 | < | ifs2.close(); | 
| 339 | < | if (ifs3) | 
| 340 | < | ifs3.close(); | 
| 341 | < | if (ifs4) | 
| 342 | < | ifs4.close(); | 
| 334 | > |  | 
| 335 | > | if (ifs1) | 
| 336 | > | ifs1.close(); | 
| 337 | > | if (ifs2) | 
| 338 | > | ifs2.close(); | 
| 339 | > | if (ifs3) | 
| 340 | > | ifs3.close(); | 
| 341 | > | if (ifs4) | 
| 342 | > | ifs4.close(); | 
| 343 | > |  | 
| 344 | > | if (GetSize() == 0) { | 
| 345 | > | sprintf( painCave.errMsg, | 
| 346 | > | "ElementsTable error.\n" | 
| 347 | > | "\tCannot initialize database %s \n", filename_.c_str()); | 
| 348 | > | painCave.isFatal = 0; | 
| 349 | > | simError(); | 
| 350 | > | } | 
| 351 |  |  | 
| 330 | – | if (GetSize() == 0) { | 
| 331 | – | sprintf( painCave.errMsg, | 
| 332 | – | "ElementsTable error.\n" | 
| 333 | – | "\tCannot initialize database %s \n", filename_.c_str()); | 
| 334 | – | painCave.isFatal = 0; | 
| 335 | – | simError(); | 
| 352 |  | } | 
| 337 | – |  | 
| 338 | – | } | 
| 353 |  |  | 
| 354 |  | } | 
| 355 |  | } |