Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision 52f5d5d2312e073ff5dc9a8561eacdf21c0b84b9)
+++ src/builder.cpp	(revision a8b9d61068f3f5cf8e1a42240f2a3128874cc073)
@@ -840,7 +840,11 @@
             break;
           case 'e':
-            cout << "Using " << argv[argptr] << " as elements database." << endl;
-            PathToDatabases = argv[argptr];
-            argptr+=1;
+            if ((argptr >= argc) || (argv[argptr][0] == '-')) {
+              cerr << "Not enough or invalid arguments for specifying element db: -e <db file>" << endl;
+            } else {
+              cout << "Using " << argv[argptr] << " as elements database." << endl;
+              PathToDatabases = argv[argptr];
+              argptr+=1;
+            }
             break;
           case 'n':
@@ -914,30 +918,38 @@
             case 'p':
               ExitFlag = 1;
-              SaveFlag = true;
-              cout << Verbose(1) << "Parsing xyz file for new atoms." << endl;
-              if (!mol->AddXYZFile(argv[argptr]))
-                cout << Verbose(2) << "File not found." << endl;
-              else {
-                cout << Verbose(2) << "File found and parsed." << endl;
-                config_present = present;
+              if ((argptr >= argc) || (argv[argptr][0] == '-')) {
+                cerr << "Not enough arguments for parsing: -p <xyz file>" << endl;
+              } else {
+                SaveFlag = true;
+                cout << Verbose(1) << "Parsing xyz file for new atoms." << endl;
+                if (!mol->AddXYZFile(argv[argptr]))
+                  cout << Verbose(2) << "File not found." << endl;
+                else {
+                  cout << Verbose(2) << "File found and parsed." << endl;
+                  config_present = present;
+                }
               }
               break;
             case 'a':
               ExitFlag = 1;
-              SaveFlag = true;
-              cout << Verbose(1) << "Adding new atom with element " << argv[argptr] << " at (" << argv[argptr+1] << "," << argv[argptr+2] << "," << argv[argptr+3] << "), ";
-              first = new atom;
-              first->type = periode->FindElement(atoi(argv[argptr]));
-              if (first->type != NULL)
-                cout << Verbose(2) << "found element " << first->type->name << endl;
-              for (int i=NDIM;i--;)
-                first->x.x[i] = atof(argv[argptr+1+i]);
-              if (first->type != NULL) {
-                mol->AddAtom(first);  // add to molecule
-                if ((config_present == empty) && (mol->AtomCount != 0))
-                  config_present = present;
-              } else
-                cerr << Verbose(1) << "Could not find the specified element." << endl; 
-              argptr+=4;
+              if ((argptr >= argc) || (argv[argptr][0] == '-') || (!IsValidNumber(argv[argptr+1]))) {
+                cerr << "Not enough or invalid arguments for adding atom: -a <element> <x> <y> <z>" << endl;
+              } else {
+                SaveFlag = true;
+                cout << Verbose(1) << "Adding new atom with element " << argv[argptr] << " at (" << argv[argptr+1] << "," << argv[argptr+2] << "," << argv[argptr+3] << "), ";
+                first = new atom;
+                first->type = periode->FindElement(atoi(argv[argptr]));
+                if (first->type != NULL)
+                  cout << Verbose(2) << "found element " << first->type->name << endl;
+                for (int i=NDIM;i--;)
+                  first->x.x[i] = atof(argv[argptr+1+i]);
+                if (first->type != NULL) {
+                  mol->AddAtom(first);  // add to molecule
+                  if ((config_present == empty) && (mol->AtomCount != 0))
+                    config_present = present;
+                } else
+                  cerr << Verbose(1) << "Could not find the specified element." << endl; 
+                argptr+=4;
+              }
               break;
             default:   // no match? Don't step on (this is done in next switch's default)
@@ -978,17 +990,23 @@
                 delete[](MinimumRingSize);
               }
-              argptr+=1;
+              //argptr+=1;
               break;
             case 'E':
               ExitFlag = 1;
-              SaveFlag = true;
-              cout << Verbose(1) << "Changing atom " << argv[argptr] << " to element " << argv[argptr+1] << "." << endl;
-              first = mol->FindAtom(atoi(argv[argptr]));
-              first->type = periode->FindElement(atoi(argv[argptr+1]));
-              argptr+=2;
+              if ((argptr+1 >= argc) || (!IsValidNumber(argv[argptr])) || (argv[argptr+1][0] == '-')) {
+                cerr << "Not enough or invalid arguments given for changing element: -E <atom nr.> <element>" << endl;
+              } else {
+                SaveFlag = true;
+                cout << Verbose(1) << "Changing atom " << argv[argptr] << " to element " << argv[argptr+1] << "." << endl;
+                first = mol->FindAtom(atoi(argv[argptr]));
+                first->type = periode->FindElement(atoi(argv[argptr+1]));
+                argptr+=2;
+              }
               break;
             case 'T':
               ExitFlag = 1;
-              {
+              if ((argptr >= argc) || (argv[argptr][0] == '-')) {
+                cerr << "Not enough or invalid arguments given for storing tempature: -T <temperature file>" << endl;
+              } else {
                 cout << Verbose(1) << "Storing temperatures in " << argv[argptr] << "." << endl;
                 ofstream *output = new ofstream(argv[argptr], ios::trunc);
@@ -999,81 +1017,102 @@
                 output->close();
                 delete(output);
+                argptr+=1;
               }
-              argptr+=1;
               break;
             case 'P':
               ExitFlag = 1;
-              SaveFlag = true;
-              cout << Verbose(1) << "Parsing forces file and Verlet integrating." << endl;
-              if (!mol->VerletForceIntegration(argv[argptr], configuration.Deltat, configuration.GetIsAngstroem()))
-                cout << Verbose(2) << "File not found." << endl;
-              else
-                cout << Verbose(2) << "File found and parsed." << endl;
-              argptr+=1;
+              if ((argptr >= argc) || (argv[argptr][0] == '-')) {
+                cerr << "Not enough or invalid arguments given for parsing and integrating forces: -P <forces file>" << endl;
+              } else {
+                SaveFlag = true;
+                cout << Verbose(1) << "Parsing forces file and Verlet integrating." << endl;
+                if (!mol->VerletForceIntegration(argv[argptr], configuration.Deltat, configuration.GetIsAngstroem()))
+                  cout << Verbose(2) << "File not found." << endl;
+                else
+                  cout << Verbose(2) << "File found and parsed." << endl;
+                argptr+=1;
+              }
               break;
             case 't':
               ExitFlag = 1;
-              SaveFlag = true;
-              cout << Verbose(1) << "Translating all ions to new origin." << endl;
-              for (int i=NDIM;i--;)
-                x.x[i] = atof(argv[argptr+i]);
-              mol->Translate((const Vector *)&x);
-              argptr+=3;
+              if ((argptr+2 >= argc) || (!IsValidNumber(argv[argptr])) || (!IsValidNumber(argv[argptr+1])) || (!IsValidNumber(argv[argptr+2])) ) {
+                cerr << "Not enough or invalid arguments given for translation: -t <x> <y> <z>" << endl;
+              } else {
+                ExitFlag = 1;
+                SaveFlag = true;
+                cout << Verbose(1) << "Translating all ions to new origin." << endl;
+                for (int i=NDIM;i--;)
+                  x.x[i] = atof(argv[argptr+i]);
+                mol->Translate((const Vector *)&x);
+                argptr+=3;
+              }
               break;
             case 's':
               ExitFlag = 1;
-              SaveFlag = true;
-              j = -1;
-              cout << Verbose(1) << "Scaling all ion positions by factor." << endl;
-              factor = new double[NDIM];
-              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<NDIM;i++) {
-                j += i+1;
-                x.x[i] = atof(argv[NDIM+i]);
-                mol->cell_size[j]*=factor[i];
+              if ((argptr >= argc) || (!IsValidNumber(argv[argptr])) ) {
+                cerr << "Not enough or invalid arguments given for scaling: -s <factor/[factor_x]> [factor_y] [factor_z]" << endl;
+              } else {
+                SaveFlag = true;
+                j = -1;
+                cout << Verbose(1) << "Scaling all ion positions by factor." << endl;
+                factor = new double[NDIM];
+                factor[0] = atof(argv[argptr]);
+                if ((argptr < argc) && (IsValidNumber(argv[argptr])))
+                  argptr++;
+                factor[1] = atof(argv[argptr]);
+                if ((argptr < argc) && (IsValidNumber(argv[argptr])))
+                  argptr++;
+                factor[2] = atof(argv[argptr]);
+                mol->Scale(&factor);
+                for (int i=0;i<NDIM;i++) {
+                  j += i+1;
+                  x.x[i] = atof(argv[NDIM+i]);
+                  mol->cell_size[j]*=factor[i];
+                }
+                delete[](factor);
+                argptr+=1;
               }
-              delete[](factor);
-              argptr+=1;
               break;
             case 'b':
               ExitFlag = 1;
-              SaveFlag = true;
-              j = -1;
-              cout << Verbose(1) << "Centering atoms in config file within given simulation box." << endl;
-              j=-1;
-              for (int i=0;i<NDIM;i++) {
-                j += i+1;
-                x.x[i] = atof(argv[argptr++]);
-                mol->cell_size[j] += x.x[i]*2.;
+              if ((argptr+2 >= argc) || (!IsValidNumber(argv[argptr])) || (!IsValidNumber(argv[argptr+1])) || (!IsValidNumber(argv[argptr+2])) ) {
+                cerr << "Not enough or invalid arguments given for centering in box: -b <length_x> <length_y> <length_z>" << endl;
+              } else {
+                SaveFlag = true;
+                j = -1;
+                cout << Verbose(1) << "Centering atoms in config file within given simulation box." << endl;
+                j=-1;
+                for (int i=0;i<NDIM;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);
               }
-              // center
-              mol->CenterInBox((ofstream *)&cout, &x);
-              // update Box of atoms by boundary
-              mol->SetBoxDimension(&x);
               break;
             case 'c':
               ExitFlag = 1;
-              SaveFlag = true;
-              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<NDIM;i++) {
-                j += i+1;
-                x.x[i] = atof(argv[argptr++]);
-                mol->cell_size[j] += x.x[i]*2.;
+              if ((argptr+2 >= argc) || (!IsValidNumber(argv[argptr])) || (!IsValidNumber(argv[argptr+1])) || (!IsValidNumber(argv[argptr+2])) ) {
+                cerr << "Not enough or invalid arguments given for centering with boundary: -c <boundary_x> <boundary_y> <boundary_z>" << endl;
+              } else {
+                SaveFlag = true;
+                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<NDIM;i++) {
+                  j += i+1;
+                  x.x[i] = atof(argv[argptr++]);
+                  mol->cell_size[j] += x.x[i]*2.;
+                }
+                mol->Translate((const Vector *)&x);
               }
-              mol->Translate((const Vector *)&x);
               break;
             case 'O':
@@ -1092,6 +1131,8 @@
             case 'f':
               ExitFlag = 1;
-              cout << "Fragmenting molecule with bond distance " << argv[argptr] << " angstroem, order of " << argv[argptr+1] << "." << endl;
-              if (argc >= argptr+2) {
+              if ((argptr+1 >= argc) || (!IsValidNumber(argv[argptr])) || (!IsValidNumber(argv[argptr+1]))) {
+                cerr << "Not enough or invalid arguments for fragmentation: -f <max. bond distance> <bond order>" << endl;
+              } else {
+                cout << "Fragmenting molecule with bond distance " << argv[argptr] << " angstroem, order of " << argv[argptr+1] << "." << endl;
                 cout << Verbose(0) << "Creating connection matrix..." << endl;
                 start = clock();
@@ -1103,7 +1144,5 @@
                 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;
+                argptr+=2;
               }
               break;
@@ -1130,9 +1169,17 @@
             case 'U':
               ExitFlag = 1;
-              volume = atof(argv[argptr++]);
-              cout << Verbose(0) << "Using " << volume << " angstrom^3 as the volume instead of convex envelope one's." << endl;  
+              if ((argptr+1 >= argc) || (!IsValidNumber(argv[argptr])) || (!IsValidNumber(argv[argptr+1])) ) {
+                cerr << "Not enough or invalid arguments given for suspension with specified volume: -U <volume> <density>" << endl;
+                volume = -1; // for case 'u': don't print error again
+              } else {
+                volume = atof(argv[argptr++]);
+                cout << Verbose(0) << "Using " << volume << " angstrom^3 as the volume instead of convex envelope one's." << endl;
+              }
             case 'u':
               ExitFlag = 1;
-              {
+              if ((argptr >= argc) || (!IsValidNumber(argv[argptr])) ) {
+                if (volume != -1)
+                  cerr << "Not enough arguments given for suspension: -u <density>" << endl;
+              } else {
                 double density;
                 SaveFlag = true;
@@ -1149,58 +1196,62 @@
 //                  repetition[i] = 1;
 //                }
-                PrepareClustersinWater((ofstream *)&cout, &configuration, mol, volume, density);
+                PrepareClustersinWater((ofstream *)&cout, &configuration, mol, volume, density);  // if volume == 0, will calculate from ConvexEnvelope
               }
               break;
             case 'd':
               ExitFlag = 1;
-              SaveFlag = true;
-              for (int axis = 1; axis <= NDIM; axis++) {
-                int faktor = atoi(argv[argptr++]);
-                int count;
-                element ** Elements;
-                Vector ** vectors;
-                if (faktor < 1) {
-                  cerr << Verbose(0) << "ERROR: Repetition faktor mus be greater than 1!" << endl;
-                  faktor = 1;
-                }
-                mol->CountAtoms((ofstream *)&cout);  // recount atoms
-                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 = new element *[count];
-                  vectors = new Vector *[count];
-                  j = 0;
-                  first = mol->start;
-                  while (first->next != mol->end) {  // make a list of all atoms with coordinates and element
-                    first = first->next;
-                    Elements[j] = first->type;
-                    vectors[j] = &first->x;
-                    j++;
+              if ((argptr+2 >= argc) || (!IsValidNumber(argv[argptr])) || (!IsValidNumber(argv[argptr+1])) || (!IsValidNumber(argv[argptr+2])) ) {
+                cerr << "Not enough or invalid arguments given for repeating cells: -d <repeat_x> <repeat_y> <repeat_z>" << endl;
+              } else {
+                SaveFlag = true;
+                for (int axis = 1; axis <= NDIM; axis++) {
+                  int faktor = atoi(argv[argptr++]);
+                  int count;
+                  element ** Elements;
+                  Vector ** vectors;
+                  if (faktor < 1) {
+                    cerr << Verbose(0) << "ERROR: Repetition faktor mus be greater than 1!" << endl;
+                    faktor = 1;
                   }
-                  if (count != j)
-                    cout << Verbose(0) << "ERROR: AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl;
-                  x.Zero();
-                  y.Zero();
-                  y.x[abs(axis)-1] = mol->cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] * abs(axis)/axis; // last term is for sign, first is for magnitude
-                  for (int i=1;i<faktor;i++) {  // then add this list with respective translation factor times
-                    x.AddVector(&y); // per factor one cell width further
-                    for (int k=count;k--;) { // go through every atom of the original cell
-                      first = new atom(); // create a new body
-                      first->x.CopyVector(vectors[k]);  // use coordinate of original atom
-                      first->x.AddVector(&x);      // translate the coordinates
-                      first->type = Elements[k];  // insert original element
-                      mol->AddAtom(first);        // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
+                  mol->CountAtoms((ofstream *)&cout);  // recount atoms
+                  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 = new element *[count];
+                    vectors = new Vector *[count];
+                    j = 0;
+                    first = mol->start;
+                    while (first->next != mol->end) {  // make a list of all atoms with coordinates and element
+                      first = first->next;
+                      Elements[j] = first->type;
+                      vectors[j] = &first->x;
+                      j++;
                     }
+                    if (count != j)
+                      cout << Verbose(0) << "ERROR: AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl;
+                    x.Zero();
+                    y.Zero();
+                    y.x[abs(axis)-1] = mol->cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] * abs(axis)/axis; // last term is for sign, first is for magnitude
+                    for (int i=1;i<faktor;i++) {  // then add this list with respective translation factor times
+                      x.AddVector(&y); // per factor one cell width further
+                      for (int k=count;k--;) { // go through every atom of the original cell
+                        first = new atom(); // create a new body
+                        first->x.CopyVector(vectors[k]);  // use coordinate of original atom
+                        first->x.AddVector(&x);      // translate the coordinates
+                        first->type = Elements[k];  // insert original element
+                        mol->AddAtom(first);        // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
+                      }
+                    }
+                    // free memory
+                    delete[](Elements);
+                    delete[](vectors);
+                    // correct cell size
+                    if (axis < 0) { // if sign was negative, we have to translate everything
+                      x.Zero();
+                      x.AddVector(&y);
+                      x.Scale(-(faktor-1));
+                      mol->Translate(&x);
+                    }
+                    mol->cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] *= faktor; 
                   }
-                  // free memory
-                  delete[](Elements);
-                  delete[](vectors);
-                  // correct cell size
-                  if (axis < 0) { // if sign was negative, we have to translate everything
-                    x.Zero();
-                    x.AddVector(&y);
-                    x.Scale(-(faktor-1));
-                    mol->Translate(&x);
-                  }
-                  mol->cell_size[(abs(axis) == 2) ? 2 : ((abs(axis) == 3) ? 5 : 0)] *= faktor; 
                 }
               }
