Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision a9d2544724995d2401c4c0710adbfb2b1becee68)
+++ src/builder.cpp	(revision ca2b8351d36cdb8ecccc8ba4a307064e6a6fcdad)
@@ -258,8 +258,6 @@
 void CenterAtoms(molecule *mol)
 {
-  vector x;
+  vector x, y;
   char choice;  // menu choice char
-  double min[3];
-  int j;
   
   cout << Verbose(0) << "===========CENTER ATOMS=========================" << endl;
@@ -267,4 +265,5 @@
   cout << Verbose(0) << " b - on center of gravity" << endl;
   cout << Verbose(0) << " c - within box with additional boundary" << endl;
+  cout << Verbose(0) << " d - within given simulation box" << endl;
   cout << Verbose(0) << "all else - go back" << endl;
   cout << Verbose(0) << "===============================================" << endl;
@@ -288,16 +287,20 @@
       for (int i=0;i<3;i++) {
         cout << Verbose(0) << "Enter axis " << i << " boundary: ";
-        cin >> min[i];
+        cin >> y.x[i];
       }
       mol->CenterEdge((ofstream *)&cout, &x);  // make every coordinate positive
-      mol->SetBoxDimension(&x);  // update Box of atoms by boundary
-      // translate each coordinate by boundary
-      j=-1;
-      for (int i=0;i<NDIM;i++) {
-        j += i+1;
-        x.x[i] = min[i];
-        mol->cell_size[j] += x.x[i]*2.;
+      mol->Translate(&y); // translate by boundary
+      mol->SetBoxDimension(&(x+y*2));  // update Box of atoms by boundary
+      break;
+    case 'd':
+      cout << Verbose(1) << "Centering atoms in config file within given simulation box." << endl;
+      for (int i=0;i<3;i++) {
+        cout << Verbose(0) << "Enter axis " << i << " boundary: ";
+        cin >> x.x[i];
       }
-      mol->Translate(&x);
+      // center
+      mol->CenterInBox((ofstream *)&cout, &x);
+      // update Box of atoms by boundary
+      mol->SetBoxDimension(&x);
       break;
   }
@@ -696,42 +699,34 @@
 };
 
-/********************************************** Main routine **************************************/
-
-int main(int argc, char **argv)
+/** Parses the command line options.
+ * \param argc argument count
+ * \param **argv arguments array
+ * \param *mol molecule structure
+ * \param *periode elements structure
+ * \param configuration config file structure
+ * \param *ConfigFileName pointer to config file name in **argv
+ * \param *ElementsFileName pointer to elements db file name in **argv
+ * \return exit code (0 - successful, all else - something's wrong)
+ */
+int ParseCommandLineOptions(int argc, char **argv, molecule *&mol, periodentafel *&periode, config& configuration, char *&ConfigFileName, char *&ElementsFileName)
 {
-  periodentafel *periode = new periodentafel; // and a period table of all elements
-  molecule *mol = new molecule(periode);    // first we need an empty molecule
-  config configuration;
-  double tmp1;
-  double bond, min_bond;
-  atom *first, *second;
   element *finder;
-  char choice;  // menu choice char
   vector x,y,z,n;  // coordinates for absolute point in cell volume
-	double *factor;	// unit factor if desired
-  bool valid; // flag if input was valid or not
+  double *factor; // unit factor if desired
   ifstream test;
   ofstream output;
   string line;
-  char filename[MAXSTRINGSIZE];
-  char *ConfigFileName = NULL;
-  char *ElementsFileName = NULL;
+  atom *first;
   int ExitFlag = 0;
-  int Z;
-  int j, axis, count, faktor;
-  int *MinimumRingSize = NULL;
+  int j;
   enum ConfigStatus config_present = absent;
-  MoleculeLeafClass *Subgraphs = NULL;
   clock_t start,end;
-  element **Elements;
-  vector **Vectors;
   int argptr;
-
-  // =========================== PARSE COMMAND LINE OPTIONS ====================================
+  
   if (argc > 1) { // config file specified as option
     // 1. : Parse options that just set variables or print help
-  	argptr = 1;
-  	do {
-  	  if (argv[argptr][0] == '-') {
+    argptr = 1;
+    do {
+      if (argv[argptr][0] == '-') {
         cout << Verbose(0) << "Recognized command line argument: " << argv[argptr][1] << ".\n";
         argptr++;
@@ -744,4 +739,5 @@
             cout << "or simply " << argv[0] << " without arguments for interactive session." << endl;
             cout << "\t-a Z x1 x2 x3\tAdd new atom of element Z at coordinates (x1,x2,x3)." << endl;
+            cout << "\t-b x1 x2 x3\tCenter atoms in domain with given edge lengths of (x1,x2,x3)." << endl;
             cout << "\t-c x1 x2 x3\tCenter atoms in domain with a minimum distance to boundary of (x1,x2,x3)." << endl;
             cout << "\t-e <file>\tSets the element database to be parsed from this file (default: elements.db in same dir as " << argv[0] << ")." << endl;
@@ -756,5 +752,5 @@
             delete(mol);
             delete(periode);
-            return (0);
+            return (1);
             break;
           case 'v':
@@ -764,5 +760,5 @@
             delete(mol);
             delete(periode);
-            return (0);
+            return (1);
             break;
           case 'e':
@@ -775,9 +771,9 @@
             break;
         }
-  	  } else
-  	    argptr++;
+      } else
+        argptr++;
     } while (argptr < argc);
-  	
-  	// 2. Parse the element database
+    
+    // 2. Parse the element database
     if (periode->LoadPeriodentafel(ElementsFileName))
       cout << Verbose(0) << "Element list loaded successfully." << endl;
@@ -785,5 +781,5 @@
       cout << Verbose(0) << "Element list loading failed." << endl;
     
-  	// 3. Find config file name and parse if possible
+    // 3. Find config file name and parse if possible
     if (argv[1][0] != '-') {
       cout << Verbose(0) << "Config file given." << endl;
@@ -827,4 +823,5 @@
     argptr = 1;
     do {
+      cout << "Current Command line argument: " << argv[argptr] << "." << endl;
       if (argv[argptr][0] == '-') {
         argptr++;
@@ -834,5 +831,5 @@
               ExitFlag = 1;
               cout << Verbose(1) << "Parsing xyz file for new atoms." << endl;
-              if (!mol->AddXYZFile(argv[argptr++]))
+              if (!mol->AddXYZFile(argv[argptr]))
                 cout << Verbose(2) << "File not found." << endl;
               else
@@ -844,100 +841,113 @@
           }
         }
-        if (config_present != empty) { 
-          if (config_present == present) {
-            switch(argv[argptr-1][1]) {
-              case 't':
-                ExitFlag = 1;
-                cout << Verbose(1) << "Translating all ions to new origin." << endl;
-                for (int i=0;i<3;i++)
-                  x.x[i] = atof(argv[argptr+i]);
-                mol->Translate((const vector *)&x);
-                argptr+=3;
-                break;
-              case 'a':
-                ExitFlag = 1;
-                cout << Verbose(1) << "Adding new atom." << endl;
-                first = new atom;
-                for (int i=0;i<3;i++)
-                  first->x.x[i] = atof(argv[argptr+1+i]);
-                finder = periode->start;
-                while (finder != periode->end) {
-                  finder = finder->next;
-                  if (strncmp(finder->symbol,argv[argptr+1],3) == 0) {
-                    first->type = finder;
-                    break;
-                  }
+        if (config_present == present) {
+          switch(argv[argptr-1][1]) {
+            case 't':
+              ExitFlag = 1;
+              cout << Verbose(1) << "Translating all ions to new origin." << endl;
+              for (int i=0;i<3;i++)
+                x.x[i] = atof(argv[argptr+i]);
+              mol->Translate((const vector *)&x);
+              argptr+=3;
+              break;
+            case 'a':
+              ExitFlag = 1;
+              cout << Verbose(1) << "Adding new atom." << endl;
+              first = new atom;
+              for (int i=0;i<3;i++)
+                first->x.x[i] = atof(argv[argptr+1+i]);
+              finder = periode->start;
+              while (finder != periode->end) {
+                finder = finder->next;
+                if (strncmp(finder->symbol,argv[argptr+1],3) == 0) {
+                  first->type = finder;
+                  break;
                 }
-                mol->AddAtom(first);  // add to molecule
-                argptr+=4;
-                break;
-              case 's':
-                ExitFlag = 1;
-                j = -1;
-                cout << Verbose(1) << "Scaling all ion positions by factor." << endl;
-                factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor");
-                factor[0] = atof(argv[argptr]);
-                if (argc > argptr+1)
-                  argptr++;
-                factor[1] = atof(argv[argptr]);
-                if (argc > argptr+1)
-                  argptr++;
-                factor[2] = atof(argv[argptr]);
-                mol->Scale(&factor);
-                for (int i=0;i<3;i++) {
-                  j += i+1;
-                  x.x[i] = atof(argv[3+i]);
-                  mol->cell_size[j]*=factor[i];
+              }
+              mol->AddAtom(first);  // add to molecule
+              argptr+=4;
+              break;
+            case 's':
+              ExitFlag = 1;
+              j = -1;
+              cout << Verbose(1) << "Scaling all ion positions by factor." << endl;
+              factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor");
+              factor[0] = atof(argv[argptr]);
+              if (argc > argptr+1)
+                argptr++;
+              factor[1] = atof(argv[argptr]);
+              if (argc > argptr+1)
+                argptr++;
+              factor[2] = atof(argv[argptr]);
+              mol->Scale(&factor);
+              for (int i=0;i<3;i++) {
+                j += i+1;
+                x.x[i] = atof(argv[3+i]);
+                mol->cell_size[j]*=factor[i];
+              }
+              Free((void **)&factor, "main: *factor");
+              argptr+=1;
+              break;
+            case 'b':
+              ExitFlag = 1;
+              j = -1;
+              cout << Verbose(1) << "Centering atoms in config file within given simulation box." << endl;
+              j=-1;
+              for (int i=0;i<3;i++) {
+                j += i+1;
+                x.x[i] = atof(argv[argptr++]);
+                mol->cell_size[j] += x.x[i]*2.;
+              }
+              // center
+              mol->CenterInBox((ofstream *)&cout, &x);
+              // update Box of atoms by boundary
+              mol->SetBoxDimension(&x);
+              break;
+            case 'c':
+              ExitFlag = 1;
+              j = -1;
+              cout << Verbose(1) << "Centering atoms in config file within given additional boundary." << endl;
+              // make every coordinate positive
+              mol->CenterEdge((ofstream *)&cout, &x);
+              // update Box of atoms by boundary
+              mol->SetBoxDimension(&x);
+              // translate each coordinate by boundary
+              j=-1;
+              for (int i=0;i<3;i++) {
+                j += i+1;
+                x.x[i] = atof(argv[argptr++]);
+                mol->cell_size[j] += x.x[i]*2.;
+              }
+              mol->Translate((const vector *)&x);
+              break;
+            case 'r':
+              ExitFlag = 1;
+              cout << Verbose(1) << "Converting config file from supposed old to new syntax." << endl;
+              break;
+            case 'f':
+              if (ExitFlag ==0) ExitFlag = 2;  // only set if not already by other command line switch
+              cout << "Fragmenting molecule with bond distance " << argv[argptr] << " angstroem, order of " << argv[argptr+1] << "." << endl;
+              if (argc >= argptr+2) {
+                cout << Verbose(0) << "Creating connection matrix..." << endl;
+                start = clock();
+                mol->CreateAdjacencyList((ofstream *)&cout, atof(argv[argptr++]));
+                cout << Verbose(0) << "Fragmenting molecule with current connection matrix ..." << endl;
+                if (mol->first->next != mol->last) {
+                  mol->FragmentMolecule((ofstream *)&cout, atoi(argv[argptr]), &configuration);
                 }
-                Free((void **)&factor, "main: *factor");
+                end = clock();
+                cout << Verbose(0) << "Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s." << endl;
                 argptr+=1;
-                break;
-              case 'c':
-                ExitFlag = 1;
-                j = -1;
-                cout << Verbose(1) << "Centering atoms in config file within given additional boundary." << endl;
-                // make every coordinate positive
-                mol->CenterEdge((ofstream *)&cout, &x);
-                // update Box of atoms by boundary
-                mol->SetBoxDimension(&x);
-                // translate each coordinate by boundary
-                j=-1;
-                for (int i=0;i<3;i++) {
-                  j += i+1;
-                  x.x[i] = atof(argv[argptr++]);
-                  mol->cell_size[j] += x.x[i]*2.;
-                }
-                mol->Translate((const vector *)&x);
-                break;
-              case 'r':
-                ExitFlag = 1;
-                cout << Verbose(1) << "Converting config file from supposed old to new syntax." << endl;
-                break;
-              case 'f':
-                if (ExitFlag ==0) ExitFlag = 2;  // only set if not already by other command line switch
-                cout << "Fragmenting molecule with bond distance " << argv[argptr] << " angstroem, order of " << argv[argptr+1] << "." << endl;
-                if (argc >= argptr+2) {
-                  cout << Verbose(0) << "Creating connection matrix..." << endl;
-                  start = clock();
-                  mol->CreateAdjacencyList((ofstream *)&cout, atof(argv[argptr++]));
-                  cout << Verbose(0) << "Fragmenting molecule with current connection matrix ..." << endl;
-                  if (mol->first->next != mol->last) {
-                    mol->FragmentMolecule((ofstream *)&cout, atoi(argv[argptr]), &configuration);
-                  }
-                  end = clock();
-                  cout << Verbose(0) << "Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s." << endl;
-                  argptr+=1;
-                } else {
-                  cerr << "Not enough arguments for fragmentation: -f <max. bond distance> <bond order>" << endl;
-                }
-                break;
-              default:   // no match? Step on
-                argptr++;
-                break;
-            }
+              } else {
+                cerr << "Not enough arguments for fragmentation: -f <max. bond distance> <bond order>" << endl;
+              }
+              break;
+            default:   // no match? Step on
+              argptr++;
+              break;
           }
         }
-  	  } else argptr++;
-  	} while (argptr < argc);
+      } else argptr++;
+    } while (argptr < argc);
     if (ExitFlag == 1) // 1 means save and exit
       SaveConfig(ConfigFileName, &configuration, periode, mol);
@@ -954,5 +964,38 @@
     configuration.RetrieveConfigPathAndName("main_pcp_linux");
   }
-
+  return(0);
+};
+
+/********************************************** Main routine **************************************/
+
+int main(int argc, char **argv)
+{
+  periodentafel *periode = new periodentafel; // and a period table of all elements
+  molecule *mol = new molecule(periode);    // first we need an empty molecule
+  config configuration;
+  double tmp1;
+  double bond, min_bond;
+  atom *first, *second;
+  char choice;  // menu choice char
+  vector x,y,z,n;  // coordinates for absolute point in cell volume
+	double *factor;	// unit factor if desired
+  bool valid; // flag if input was valid or not
+  ifstream test;
+  ofstream output;
+  string line;
+  char filename[MAXSTRINGSIZE];
+  char *ConfigFileName = NULL;
+  char *ElementsFileName = NULL;
+  int Z;
+  int j, axis, count, faktor;
+  int *MinimumRingSize = NULL;
+  MoleculeLeafClass *Subgraphs = NULL;
+  clock_t start,end;
+  element **Elements;
+  vector **Vectors;
+
+  // =========================== PARSE COMMAND LINE OPTIONS ====================================
+  j = ParseCommandLineOptions(argc, argv, mol, periode, configuration, ConfigFileName, ElementsFileName);
+  if (j) return j;  // something went wrong
   
   // General stuff
@@ -1003,7 +1046,4 @@
     switch (choice) {
       default:
-      case 'q': // quit 
-        break;
-        
       case 'a': // add atom
         AddAtoms(periode, mol);
@@ -1011,4 +1051,34 @@
         break;
       
+      case 'b': // scale a bond
+        cout << Verbose(0) << "Scaling bond length between two atoms." << endl;
+        first = mol->AskAtom("Enter first (fixed) atom: ");
+        second = mol->AskAtom("Enter second (shifting) atom: ");
+        min_bond = 0.;
+        for (int i=0;i<3;i++)
+          min_bond += (first->x.x[i]-second->x.x[i])*(first->x.x[i] - second->x.x[i]);
+        min_bond = sqrt(min_bond);
+        cout << Verbose(0) << "Current Bond length between " << first->type->name << " Atom " << first->nr << " and " << second->type->name << " Atom " << second->nr << ": " << min_bond << " a.u." << endl;
+        cout << Verbose(0) << "Enter new bond length [a.u.]: ";
+        cin >> bond;
+        for (int i=0;i<3;i++) {
+          second->x.x[i] -= (second->x.x[i]-first->x.x[i])/min_bond*(min_bond-bond);
+        }
+        //cout << Verbose(0) << "New coordinates of Atom " << second->nr << " are: ";
+        //second->Output(second->type->No, 1, (ofstream *)&cout);       
+        break;
+
+      case 'c': // unit scaling of the metric 
+       cout << Verbose(0) << "Angstroem -> Bohrradius: 1.8897261\t\tBohrradius -> Angstroem: 0.52917721" << endl;
+       cout << Verbose(0) << "Enter three factors: ";
+       factor = new double[NDIM];
+       cin >> factor[0];
+       cin >> factor[1];
+       cin >> factor[2];
+       valid = true;
+       mol->Scale(&factor);
+       delete[](factor);
+       break;
+       
       case 'd': // duplicate the periodic cell along a given axis, given times
         cout << Verbose(0) << "State the axis [(+-)123]: ";
@@ -1020,6 +1090,6 @@
         if (mol->AtomCount != 0) {  // if there is more than none
           count = mol->AtomCount;   // is changed becausing of adding, thus has to be stored away beforehand
-          Elements = (element **) Malloc(sizeof(element *)*count, "main: duplicateCell - **Elements");
-          Vectors = (vector **) Malloc(sizeof(vector *)*count, "main: duplicateCell - **Vectors");
+          Elements = new element *[count];
+          Vectors = new vector *[count];
           j = 0;
           first = mol->start;
@@ -1048,6 +1118,6 @@
             mol->CreateAdjacencyList((ofstream *)&cout, mol->BondDistance);
           // free memory
-          Free((void **)&Elements, "main: duplicateCell - **Elements");
-          Free((void **)&Vectors, "main: duplicateCell - **Vectors");
+          delete[](Elements);
+          delete[](Vectors);
           // correct cell size
           if (axis < 0) { // if sign was negative, we have to translate everything
@@ -1061,72 +1131,28 @@
         break;
       
-      case 'g': // center the atoms
-        CenterAtoms(mol);
-        break;
-        
-      case 'b': // scale a bond
-        cout << Verbose(0) << "Scaling bond length between two atoms." << endl;
-        first = mol->AskAtom("Enter first (fixed) atom: ");
-        second = mol->AskAtom("Enter second (shifting) atom: ");
-        min_bond = 0.;
-        for (int i=0;i<3;i++)
-          min_bond += (first->x.x[i]-second->x.x[i])*(first->x.x[i] - second->x.x[i]);
-        min_bond = sqrt(min_bond);
-        cout << Verbose(0) << "Current Bond length between " << first->type->name << " Atom " << first->nr << " and " << second->type->name << " Atom " << second->nr << ": " << min_bond << " a.u." << endl;
-        cout << Verbose(0) << "Enter new bond length [a.u.]: ";
-        cin >> bond;
-        for (int i=0;i<3;i++) {
-          second->x.x[i] -= (second->x.x[i]-first->x.x[i])/min_bond*(min_bond-bond);
-        }
-        //cout << Verbose(0) << "New coordinates of Atom " << second->nr << " are: ";
-        //second->Output(second->type->No, 1, (ofstream *)&cout);       
-        break;
-
-      case 'i': // align all atoms 
-        AlignAtoms(periode, mol);
-        break;
-
-      case 'm': // mirror atoms along a given axis
-        MirrorAtoms(mol);
-        break;
-       
-      case 't': // translate all atoms
-	     cout << Verbose(0) << "Enter translation vector." << endl;       
-	     x.AskPosition(mol->cell_size,0);
-       mol->Translate((const vector *)&x);
-	     break;
-  
       case 'e': // edit each field of the configuration
        configuration.Edit(mol);
        break;
-	
-      case 'c': // unit scaling of the metric 
-       cout << Verbose(0) << "Angstroem -> Bohrradius: 1.8897261\t\tBohrradius -> Angstroem: 0.52917721" << endl;
-    	 cout << Verbose(0) << "Enter three factors: ";
-       factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor");
-       cin >> factor[0];
-       cin >> factor[1];
-       cin >> factor[2];
-       valid = true;
-       mol->Scale(&factor);
-	     Free((void **)&factor, "main: *factor");
-       break;
-			 
-      case 'r': // remove atom
-        RemoveAtoms(mol);        
+  
+      case 'f':
+        FragmentAtoms(mol, &configuration);
         break;
         
+      case 'g': // center the atoms
+        CenterAtoms(mol);
+        break;
+        
+      case 'i': // align all atoms 
+        AlignAtoms(periode, mol);
+        break;
+
       case 'l': // measure distances or angles
         MeasureAtoms(periode, mol);
         break;
 
-      case 'p': // parse and XYZ file
-        cout << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
-        do {
-          cout << Verbose(0) << "Enter file name: ";
-          cin >> filename;
-        } while (!mol->AddXYZFile(filename));
-        break;
-
+      case 'm': // mirror atoms along a given axis
+        MirrorAtoms(mol);
+        break;
+       
       case 'o': // create the connection matrix
         cout << Verbose(0) << "What's the maximum bond distance: ";
@@ -1147,8 +1173,33 @@
         break;
         
-      case 'f':
-        FragmentAtoms(mol, &configuration);
-      	break;
+      case 'p': // parse and XYZ file
+        cout << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl;
+        do {
+          cout << Verbose(0) << "Enter file name: ";
+          cin >> filename;
+        } while (!mol->AddXYZFile(filename));
+        break;
+
+      case 'q': // quit 
+        break;
         
+      case 'r': // remove atom
+        RemoveAtoms(mol);        
+        break;
+        
+      case 's': // save to config file
+        SaveConfig(ConfigFileName, &configuration, periode, mol);
+        break;
+
+      case 't': // translate all atoms
+       cout << Verbose(0) << "Enter translation vector." << endl;       
+       x.AskPosition(mol->cell_size,0);
+       mol->Translate((const vector *)&x);
+       break;
+  
+      case 'T':
+        testroutine(mol);
+        break;
+      
       case 'u': // change an atom's element
         first = NULL;
@@ -1161,12 +1212,4 @@
         first->type = periode->FindElement(Z);
         cout << Verbose(0) << "Atom " << first->nr << "'s element is " << first->type->name << "." << endl;    
-        break;
-        
-      case 'T':
-        testroutine(mol);
-        break;
-      
-      case 's': // save to config file
-        SaveConfig(ConfigFileName, &configuration, periode, mol);
         break;
     };
