| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * ElementsTextQuery.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Oct 25, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <iostream>
 | 
|---|
| 23 | #include <boost/lexical_cast.hpp>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "TextUI/Query/TextQuery.hpp"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "Helpers/Log.hpp"
 | 
|---|
| 28 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 29 | #include "element.hpp"
 | 
|---|
| 30 | #include "periodentafel.hpp"
 | 
|---|
| 31 | #include "World.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | using boost::lexical_cast;
 | 
|---|
| 34 | using boost::bad_lexical_cast;
 | 
|---|
| 35 | 
 | 
|---|
| 36 | 
 | 
|---|
| 37 | TextDialog::ElementsTextQuery::ElementsTextQuery(std::string title, std::string _description) :
 | 
|---|
| 38 |     Dialog::ElementsQuery(title,_description)
 | 
|---|
| 39 | {}
 | 
|---|
| 40 | 
 | 
|---|
| 41 | TextDialog::ElementsTextQuery::~ElementsTextQuery()
 | 
|---|
| 42 | {}
 | 
|---|
| 43 | 
 | 
|---|
| 44 | bool TextDialog::ElementsTextQuery::handle() {
 | 
|---|
| 45 |   std::string shorthand;
 | 
|---|
| 46 |   int Z=-1;
 | 
|---|
| 47 |   Log() << Verbose(0) << getTitle();
 | 
|---|
| 48 |   std::string line;
 | 
|---|
| 49 |   getline(cin,line);
 | 
|---|
| 50 |   // dissect by " "
 | 
|---|
| 51 |   std::string::iterator olditer = line.begin();
 | 
|---|
| 52 |   for(string::iterator iter = line.begin(); iter != line.end(); ++iter) {
 | 
|---|
| 53 |     if (*iter == ' ') {
 | 
|---|
| 54 |       std::istringstream stream(string(iter, olditer));
 | 
|---|
| 55 |       stream >> shorthand;
 | 
|---|
| 56 |       try {
 | 
|---|
| 57 |         Z = lexical_cast<int>(shorthand);
 | 
|---|
| 58 |         temp = World::getInstance().getPeriode()->FindElement(Z);
 | 
|---|
| 59 |       } catch (bad_lexical_cast) {
 | 
|---|
| 60 |         temp = World::getInstance().getPeriode()->FindElement(shorthand.c_str());
 | 
|---|
| 61 |       };
 | 
|---|
| 62 |       if(!temp && Z!=-1){
 | 
|---|
| 63 |         Log() << Verbose(0) << "Invalid Element" << shorthand << endl;
 | 
|---|
| 64 |         break;
 | 
|---|
| 65 |       }
 | 
|---|
| 66 |       tmp.push_back(temp);
 | 
|---|
| 67 |       olditer = iter;
 | 
|---|
| 68 |     }
 | 
|---|
| 69 |   }
 | 
|---|
| 70 |   if (olditer != line.begin()) { // insert last part also
 | 
|---|
| 71 |     std::istringstream stream(string(olditer, line.end()));
 | 
|---|
| 72 |     stream >> shorthand;
 | 
|---|
| 73 |     try {
 | 
|---|
| 74 |       Z = lexical_cast<int>(shorthand);
 | 
|---|
| 75 |       temp = World::getInstance().getPeriode()->FindElement(Z);
 | 
|---|
| 76 |     } catch (bad_lexical_cast) {
 | 
|---|
| 77 |       temp = World::getInstance().getPeriode()->FindElement(shorthand.c_str());
 | 
|---|
| 78 |     };
 | 
|---|
| 79 |     if(!temp && Z!=-1) {
 | 
|---|
| 80 |       Log() << Verbose(0) << "Invalid Element" << shorthand << endl;
 | 
|---|
| 81 |       tmp.push_back(temp);
 | 
|---|
| 82 |     }
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   return (Z!=-1);
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|