Index: src/periodentafel.cpp
===================================================================
--- src/periodentafel.cpp	(revision 5f612eea044e919bffc18a83cd7ddd35f981c765)
+++ src/periodentafel.cpp	(revision 4eb4fe704b5f87bedd7f4eec06861f2f98bff9df)
@@ -8,9 +8,11 @@
 
 #include <iomanip>
+#include <iostream>
 #include <fstream>
 #include <cstring>
-#include <cassert>
-
+
+#include "Helpers/Assert.hpp"
 #include "element.hpp"
+#include "elements_db.hpp"
 #include "helpers.hpp"
 #include "lists.hpp"
@@ -27,5 +29,11 @@
  */
 periodentafel::periodentafel()
-{};
+{
+  ASSERT(LoadElementsDatabase(new stringstream(elementsDB,ios_base::in)), "General element initialization failed");
+  ASSERT(LoadValenceDatabase(new stringstream(valenceDB,ios_base::in)), "Valence entry of element initialization failed");
+  ASSERT(LoadOrbitalsDatabase(new stringstream(orbitalsDB,ios_base::in)), "Orbitals entry of element initialization failed");
+  ASSERT(LoadHBondAngleDatabase(new stringstream(HbondangleDB,ios_base::in)), "HBond angle entry of element initialization failed");
+  ASSERT(LoadHBondLengthsDatabase(new stringstream(HbonddistanceDB,ios_base::in)), "HBond distance entry of element initialization failed");
+};
 
 /** destructor for class periodentafel
@@ -39,10 +47,10 @@
 /** Adds element to period table list
  * \param *pointer element to be added
- * \return true - succeeded, false - does not occur
+ * \return iterator to added element
  */
 periodentafel::iterator periodentafel::AddElement(element * const pointer)
 {
   atomicNumber_t Z = pointer->getNumber();
-  assert(!elements.count(Z));
+  ASSERT(!elements.count(Z), "Element is already present.");
   pointer->sort = &pointer->Z;
   if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
@@ -54,14 +62,19 @@
 /** Removes element from list.
  * \param *pointer element to be removed
- * \return true - succeeded, false - element not found
  */
 void periodentafel::RemoveElement(element * const pointer)
 {
-  atomicNumber_t Z = pointer->getNumber();
+  RemoveElement(pointer->getNumber());
+};
+
+/** Removes element from list.
+ * \param Z element to be removed
+ */
+void periodentafel::RemoveElement(atomicNumber_t Z)
+{
   elements.erase(Z);
 };
 
 /** Removes every element from the period table.
- * \return true - succeeded, false - does not occur
  */
 void periodentafel::CleanupPeriodtable()
@@ -78,5 +91,5 @@
  * \return pointer to element or NULL if not found
  */
-const element * periodentafel::FindElement(atomicNumber_t Z) const
+element * const periodentafel::FindElement(atomicNumber_t Z) const
 {
   const_iterator res = elements.find(Z);
@@ -89,5 +102,5 @@
  * \return pointer to element
  */
-const element * periodentafel::FindElement(const char * const shorthand) const
+element * const periodentafel::FindElement(const char * const shorthand) const
 {
   element *res = 0;
@@ -102,8 +115,9 @@
 
 /** Asks for element number and returns pointer to element
- */
-const element * periodentafel::AskElement() const
-{
-  const element *walker = NULL;
+ * \return desired element or NULL
+ */
+element * const periodentafel::AskElement() const
+{
+  element * walker = NULL;
   int Z;
   do {
@@ -118,16 +132,14 @@
  * \return pointer to either present or newly created element
  */
-const element * periodentafel::EnterElement()
-{
-  const element *res = NULL;
+element * const periodentafel::EnterElement()
+{
   atomicNumber_t Z = 0;
   DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
   cin >> Z;
-  res = FindElement(Z);
+  element * const res = FindElement(Z);
   if (!res) {
     // TODO: make this using the constructor
-    element *tmp;
     DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
-    tmp = new element;
+    element *tmp = new element;
     tmp->Z = Z;
     DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
@@ -138,5 +150,5 @@
     cin >> tmp->symbol;
     AddElement(tmp);
-    res = tmp;
+    return tmp;
   }
   return res;
@@ -204,7 +216,5 @@
 bool periodentafel::LoadPeriodentafel(const char *path)
 {
-  ifstream infile;
-  element *ptr;
-  map<atomicNumber_t,element*> parsedElems;
+  ifstream input;
   bool status = true;
   bool otherstatus = true;
@@ -215,34 +225,83 @@
   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
-  infile.open(filename);
-  if (infile != NULL) {
-    infile.getline(header1, MAXSTRINGSIZE);
-    infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
+  input.open(filename);
+  status = status && LoadElementsDatabase(&input);
+
+  // fill valence DB per element
+  strncpy(filename, path, MAXSTRINGSIZE);
+  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
+  strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
+  input.open(filename);
+  otherstatus = otherstatus && LoadValenceDatabase(&input);
+
+  // fill orbitals DB per element
+  strncpy(filename, path, MAXSTRINGSIZE);
+  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
+  strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
+  input.open(filename);
+  otherstatus = otherstatus && LoadOrbitalsDatabase(&input);
+
+  // fill H-BondAngle DB per element
+  strncpy(filename, path, MAXSTRINGSIZE);
+  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
+  strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
+  input.open(filename);
+  otherstatus = otherstatus && LoadHBondAngleDatabase(&input);
+
+  // fill H-BondDistance DB per element
+  strncpy(filename, path, MAXSTRINGSIZE);
+  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
+  strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
+  input.open(filename);
+  otherstatus = otherstatus && LoadHBondLengthsDatabase(&input);
+
+  if (!otherstatus){
+    DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
+  }
+
+  return status;
+};
+
+/** load the element info.
+ * \param *input stream to parse from
+ * \return true - parsing successful, false - something went wrong
+ */
+bool periodentafel::LoadElementsDatabase(istream *input)
+{
+  if ((*input) != NULL) {
+    (*input).getline(header1, MAXSTRINGSIZE);
+    (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
     DoLog(0) && (Log() << Verbose(0) <<  "Parsed elements:");
-    while (!infile.eof()) {
+    while (!(*input).eof()) {
       element *neues = new element;
-      infile >> neues->name;
-      //infile >> ws;
-      infile >> neues->symbol;
-      //infile >> ws;
-      infile >> neues->period;
-      //infile >> ws;
-      infile >> neues->group;
-      //infile >> ws;
-      infile >> neues->block;
-      //infile >> ws;
-      infile >> neues->Z;
-      //infile >> ws;
-      infile >> neues->mass;
-      //infile >> ws;
-      infile >> neues->CovalentRadius;
-      //infile >> ws;
-      infile >> neues->VanDerWaalsRadius;
-      //infile >> ws;
-      infile >> ws;
+      (*input) >> neues->name;
+      //(*input) >> ws;
+      (*input) >> neues->symbol;
+      //(*input) >> ws;
+      (*input) >> neues->period;
+      //(*input) >> ws;
+      (*input) >> neues->group;
+      //(*input) >> ws;
+      (*input) >> neues->block;
+      //(*input) >> ws;
+      (*input) >> neues->Z;
+      //(*input) >> ws;
+      (*input) >> neues->mass;
+      //(*input) >> ws;
+      (*input) >> neues->CovalentRadius;
+      //(*input) >> ws;
+      (*input) >> neues->VanDerWaalsRadius;
+      //(*input) >> ws;
+      (*input) >> ws;
       DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
+      if (elements.count(neues->Z)) {// if element already present, remove and delete it
+        element * const Elemental = FindElement(neues->Z);
+        ASSERT(Elemental != NULL, "element should be present but is not??");
+        RemoveElement(Elemental);
+        delete(Elemental);
+      }
       //neues->Output((ofstream *)&cout);
       if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
-        parsedElems[neues->getNumber()] = neues;
+        elements[neues->getNumber()] = neues;
       else {
         DoLog(0) && (Log() << Verbose(0) << "Could not parse element: ");
@@ -252,106 +311,104 @@
     }
     DoLog(0) && (Log() << Verbose(0) << endl);
-    infile.close();
-    infile.clear();
-  } else
-    status = false;
-
-  // fill valence DB per element
-  strncpy(filename, path, MAXSTRINGSIZE);
-  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
-  strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
-  infile.open(filename);
-  if (infile != NULL) {
-    while (!infile.eof()) {
+    return true;
+  } else
+    return false;
+}
+
+/** load the valence info.
+ * \param *input stream to parse from
+ * \return true - parsing successful, false - something went wrong
+ */
+bool periodentafel::LoadValenceDatabase(istream *input)
+{
+  char dummy[MAXSTRINGSIZE];
+  if ((*input) != NULL) {
+    (*input).getline(dummy, MAXSTRINGSIZE);
+    while (!(*input).eof()) {
       atomicNumber_t Z;
-      infile >> Z;
-      infile >> ws;
-      infile >> parsedElems[Z]->Valence;
-      infile >> ws;
+      (*input) >> Z;
+      ASSERT(elements.count(Z), "Element not present");
+      (*input) >> ws;
+      (*input) >> elements[Z]->Valence;
+      (*input) >> ws;
       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
     }
-    infile.close();
-    infile.clear();
-  } else
-    otherstatus = false;
-
-  // fill valence DB per element
-  strncpy(filename, path, MAXSTRINGSIZE);
-  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
-  strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
-  infile.open(filename);
-  if (infile != NULL) {
-    while (!infile.eof()) {
+    return true;
+  } else
+		return false;
+}
+
+/** load the orbitals info.
+ * \param *input stream to parse from
+ * \return true - parsing successful, false - something went wrong
+ */
+bool periodentafel::LoadOrbitalsDatabase(istream *input)
+{
+  char dummy[MAXSTRINGSIZE];
+  if ((*input) != NULL) {
+    (*input).getline(dummy, MAXSTRINGSIZE);
+    while (!(*input).eof()) {
       atomicNumber_t Z;
-      infile >> Z;
-      infile >> ws;
-      infile >> parsedElems[Z]->NoValenceOrbitals;
-      infile >> ws;
+      (*input) >> Z;
+      ASSERT(elements.count(Z), "Element not present");
+      (*input) >> ws;
+      (*input) >> elements[Z]->NoValenceOrbitals;
+      (*input) >> ws;
       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
     }
-    infile.close();
-    infile.clear();
-  } else
-    otherstatus = false;
-
-  // fill H-BondDistance DB per element
-  strncpy(filename, path, MAXSTRINGSIZE);
-  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
-  strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
-  infile.open(filename);
-  if (infile != NULL) {
-    while (!infile.eof()) {
+    return true;
+  } else
+    return false;
+}
+
+/** load the hbond angles info.
+ * \param *input stream to parse from
+ * \return true - parsing successful, false - something went wrong
+ */
+bool periodentafel::LoadHBondAngleDatabase(istream *input)
+{
+  char dummy[MAXSTRINGSIZE];
+  if ((*input) != NULL) {
+    (*input).getline(dummy, MAXSTRINGSIZE);
+    while (!(*input).eof()) {
       atomicNumber_t Z;
-      infile >> Z;
-      ptr = parsedElems[Z];
-      infile >> ws;
-      infile >> ptr->HBondDistance[0];
-      infile >> ptr->HBondDistance[1];
-      infile >> ptr->HBondDistance[2];
-      infile >> ws;
+      (*input) >> Z;
+      ASSERT(elements.count(Z), "Element not present");
+      (*input) >> ws;
+      (*input) >> elements[Z]->HBondAngle[0];
+      (*input) >> elements[Z]->HBondAngle[1];
+      (*input) >> elements[Z]->HBondAngle[2];
+      (*input) >> ws;
+      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondAngle[0] << ", " << FindElement((int)tmp)->HBondAngle[1] << ", " << FindElement((int)tmp)->HBondAngle[2] << " degrees bond angle for one, two, three connected hydrogens." << endl;
+    }
+    return true;
+  } else
+		return false;
+}
+
+/** load the hbond lengths info.
+ * \param *input stream to parse from
+ * \return true - parsing successful, false - something went wrong
+ */
+bool periodentafel::LoadHBondLengthsDatabase(istream *input)
+{
+  char dummy[MAXSTRINGSIZE];
+  if ((*input) != NULL) {
+    (*input).getline(dummy, MAXSTRINGSIZE);
+    while (!(*input).eof()) {
+      atomicNumber_t Z;
+      (*input) >> Z;
+      ASSERT(elements.count(Z), "Element not present");
+      (*input) >> ws;
+      (*input) >> elements[Z]->HBondDistance[0];
+      (*input) >> elements[Z]->HBondDistance[1];
+      (*input) >> elements[Z]->HBondDistance[2];
+      (*input) >> ws;
       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
     }
-    infile.close();
-    infile.clear();
-  } else
-    otherstatus = false;
-
-  // fill H-BondAngle DB per element
-  strncpy(filename, path, MAXSTRINGSIZE);
-  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
-  strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
-  infile.open(filename);
-  if (infile != NULL) {
-    while (!infile.eof()) {
-      atomicNumber_t Z;
-      infile >> Z;
-      ptr = parsedElems[Z];
-      infile >> ws;
-      infile >> ptr->HBondAngle[0];
-      infile >> ptr->HBondAngle[1];
-      infile >> ptr->HBondAngle[2];
-      infile >> ws;
-      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondAngle[0] << ", " << FindElement((int)tmp)->HBondAngle[1] << ", " << FindElement((int)tmp)->HBondAngle[2] << " degrees bond angle for one, two, three connected hydrogens." << endl;
-    }
-    infile.close();
-  } else
-    otherstatus = false;
-
-  if (otherstatus){
-    map<atomicNumber_t,element*>::iterator iter;
-    for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
-      AddElement((*iter).second);
-    }
-  }
-  else{
-    DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
-    map<atomicNumber_t,element*>::iterator iter;
-    for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
-      AddElement((*iter).second);
-    }
-  }
-
-  return status;
-};
+    return true;
+  } else
+		return false;
+}
 
 /** Stores element list to file.
@@ -374,6 +431,6 @@
     }
     f.close();
-  } else
-    result = false;
-  return result;
-};
+    return true;
+  } else
+    return result;
+};
