Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Formula.cpp

    r426f2a ree86a0  
    5555
    5656void Formula::fromString(const std::string &formula) throw(ParseError){
    57   // make this transactional, in case an error is thrown
    58   Formula res;
    59   string::const_iterator begin = formula.begin();
    60   string::const_iterator end = formula.end();
    61   res.parseFromString(begin,end,static_cast<char>(0));
    62   (*this)=res;
    63 }
    64 
    65 int Formula::parseMaybeNumber(string::const_iterator &it,string::const_iterator &end) throw(ParseError){
    66   static const range<char> Numbers = makeRange('0',static_cast<char>('9'+1));
    67   int count = 0;
    68   while(it!=end && Numbers.isInRange(*it))
    69     count = (count*10) + ((*it++)-Numbers.first);
    70   // one is implicit
    71   count = (count!=0)?count:1;
    72   return count;
    73 }
    74 
    75 void Formula::parseFromString(string::const_iterator &it,string::const_iterator &end,char delimiter) throw(ParseError){
    7657  // some constants needed for parsing... Assumes ASCII, change if other encodings are used
    7758  static const range<char> CapitalLetters = makeRange('A',static_cast<char>('Z'+1));
    7859  static const range<char> SmallLetters = makeRange('a',static_cast<char>('z'+1));
    79   map<char,char> delimiters;
    80   delimiters['('] = ')';
    81   delimiters['['] = ']';
     60  static const range<char> Numbers = makeRange('0',static_cast<char>('9'+1));
    8261  // clean the formula
    8362  clear();
    84   for(/*send from above*/;it!=end && *it!=delimiter;/*updated in loop*/){
    85     // we might have a sub formula
    86     if(delimiters.count(*it)){
    87       Formula sub;
    88       char nextdelim=delimiters[*it];
    89       sub.parseFromString(++it,end,nextdelim);
    90       if(!sub.getElementCount()){
    91         throw(ParseError(__FILE__,__LINE__));
    92       }
    93       int count = parseMaybeNumber(++it,end);
    94       addFormula(sub,count);
    95       continue;
    96     }
     63  string::const_iterator end = formula.end(); // will be used frequently
     64  for(string::const_iterator it=formula.begin();it!=end;){
    9765    string shorthand;
    9866    // Atom names start with a capital letter
     
    10371    while(it!=end && SmallLetters.isInRange(*it))
    10472      shorthand+=(*it++);
    105     int count = parseMaybeNumber(it,end);
     73    // now we can count the occurences
     74    int count = 0;
     75    while(it!=end && Numbers.isInRange(*it))
     76      count = (count*10) + ((*it++)-Numbers.first);
     77    // one is implicit
     78    count = (count!=0)?count:1;
    10679    // test if the shorthand exists
    10780    if(!World::getInstance().getPeriode()->FindElement(shorthand))
     
    11083    addElements(shorthand,count);
    11184  }
    112   if(it==end && delimiter!=0){
    113     throw(ParseError(__FILE__,__LINE__));
    114   }
    11585}
    11686
     
    12393    *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
    12494    for(const_iterator iter=begin(); iter!=end();++iter){
     95      (*iter).first->No = No;
    12596      result = result && (*iter).first->Checkout(output, No++, (*iter).second);
    12697    }
     
    224195}
    225196
    226 void Formula::addFormula(const Formula &formula,unsigned int n){
    227   for(Formula::const_iterator iter=formula.begin();iter!=formula.end();++iter){
    228     this->addElements(iter->first,iter->second*n);
    229   }
    230 }
    231 
    232 enumeration<Formula::key_type> Formula::enumerateElements() const{
    233   enumeration<key_type> res(1);
    234   for(Formula::const_iterator iter=begin();iter!=end();++iter){
    235     res.add(iter->first);
    236   }
    237   return res;
    238 }
    239 
    240197const unsigned int Formula::operator[](const element *element) const{
    241198  ASSERT(element,"Invalid pointer in access of Formula");
Note: See TracChangeset for help on using the changeset viewer.