| 1 | 
tim | 
1765 | 
/* | 
| 2 | 
  | 
  | 
 * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 | 
  | 
  | 
 *  | 
| 4 | 
  | 
  | 
 * Contact: oopse@oopse.org | 
| 5 | 
  | 
  | 
 *  | 
| 6 | 
  | 
  | 
 * This program is free software; you can redistribute it and/or | 
| 7 | 
  | 
  | 
 * modify it under the terms of the GNU Lesser General Public License | 
| 8 | 
  | 
  | 
 * as published by the Free Software Foundation; either version 2.1 | 
| 9 | 
  | 
  | 
 * of the License, or (at your option) any later version. | 
| 10 | 
  | 
  | 
 * All we ask is that proper credit is given for our work, which includes | 
| 11 | 
  | 
  | 
 * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 | 
  | 
  | 
 * of your source code files, and to any copyright notice that you may distribute | 
| 13 | 
  | 
  | 
 * with programs based on this work. | 
| 14 | 
  | 
  | 
 *  | 
| 15 | 
  | 
  | 
 * This program is distributed in the hope that it will be useful, | 
| 16 | 
  | 
  | 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 | 
  | 
  | 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 | 
  | 
  | 
 * GNU Lesser General Public License for more details. | 
| 19 | 
  | 
  | 
 *  | 
| 20 | 
  | 
  | 
 * You should have received a copy of the GNU Lesser General Public License | 
| 21 | 
  | 
  | 
 * along with this program; if not, write to the Free Software | 
| 22 | 
  | 
  | 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 23 | 
  | 
  | 
 * | 
| 24 | 
  | 
  | 
 */ | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
#include "io/SectionParserManager.hpp" | 
| 27 | 
  | 
  | 
 | 
| 28 | 
  | 
  | 
namespace oopse { | 
| 29 | 
  | 
  | 
 | 
| 30 | 
  | 
  | 
SectionParserManager::~SectionParserManager() { | 
| 31 | 
  | 
  | 
    SectionParserManager::iterator i; | 
| 32 | 
  | 
  | 
    for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 33 | 
tim | 
1789 | 
        delete (i->sectionParser); | 
| 34 | 
tim | 
1765 | 
    } | 
| 35 | 
  | 
  | 
    sectionParsers_.clear(); | 
| 36 | 
  | 
  | 
} | 
| 37 | 
  | 
  | 
 | 
| 38 | 
tim | 
1789 | 
void SectionParserManager::parse(std::istream& input, ForceField& ff) { | 
| 39 | 
tim | 
1765 | 
 | 
| 40 | 
  | 
  | 
    //reset active flags | 
| 41 | 
  | 
  | 
    SectionParserManager::iterator i; | 
| 42 | 
  | 
  | 
    for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 43 | 
  | 
  | 
        i->isActive = false; | 
| 44 | 
  | 
  | 
    } | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
    const int bufferSize = 65535; | 
| 47 | 
  | 
  | 
    char buffer[bufferSize]; | 
| 48 | 
  | 
  | 
    int lineNo = 0; | 
| 49 | 
  | 
  | 
    //scan through the input stream and find section names         | 
| 50 | 
  | 
  | 
    while(input.getline(buffer, bufferSize)) { | 
| 51 | 
  | 
  | 
        ++lineNo; | 
| 52 | 
tim | 
1789 | 
        /**@todo implement trimLeft() */ | 
| 53 | 
  | 
  | 
        //std::string line = LeftTrim(buffer); | 
| 54 | 
  | 
  | 
        std::string line= buffer; | 
| 55 | 
tim | 
1765 | 
        //a line begins with "//" is comment | 
| 56 | 
  | 
  | 
        if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) { | 
| 57 | 
  | 
  | 
            continue; | 
| 58 | 
  | 
  | 
        } else {         | 
| 59 | 
  | 
  | 
            StringTokenizer tokenizer(line); | 
| 60 | 
  | 
  | 
            if (tokenizer.countTokens() < 2) { | 
| 61 | 
  | 
  | 
                continue; | 
| 62 | 
  | 
  | 
            } else { | 
| 63 | 
  | 
  | 
                std::string keyword = tokenizer.nextToken(); | 
| 64 | 
  | 
  | 
 | 
| 65 | 
  | 
  | 
                if (keyword != "begin") { | 
| 66 | 
  | 
  | 
                    continue; | 
| 67 | 
  | 
  | 
                } | 
| 68 | 
  | 
  | 
 | 
| 69 | 
  | 
  | 
                std::string section = tokenizer.nextToken(); | 
| 70 | 
  | 
  | 
 | 
| 71 | 
  | 
  | 
                i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), predict); | 
| 72 | 
  | 
  | 
                if (i == sectionParsers_.end()){ | 
| 73 | 
  | 
  | 
                    //can not find corresponding section parser | 
| 74 | 
  | 
  | 
                    std::cerr << "Can not find corresponding section parser for section: " << section << std::endl; | 
| 75 | 
  | 
  | 
                } else { | 
| 76 | 
  | 
  | 
                    i->isActive = true; | 
| 77 | 
  | 
  | 
                    i->lineNo = lineNo; | 
| 78 | 
tim | 
1790 | 
                    i->offset = input.tellg(); | 
| 79 | 
tim | 
1765 | 
                } | 
| 80 | 
  | 
  | 
                 | 
| 81 | 
  | 
  | 
            } | 
| 82 | 
  | 
  | 
        } | 
| 83 | 
  | 
  | 
 | 
| 84 | 
  | 
  | 
         | 
| 85 | 
  | 
  | 
    } | 
| 86 | 
  | 
  | 
 | 
| 87 | 
  | 
  | 
    //invoke parser | 
| 88 | 
  | 
  | 
    for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 89 | 
  | 
  | 
        if (i->isActive) { | 
| 90 | 
tim | 
1790 | 
            input.seekg(i->offset); | 
| 91 | 
tim | 
1765 | 
            (i->sectionParser)->parse(input, ff, i->lineNo); | 
| 92 | 
  | 
  | 
        } | 
| 93 | 
  | 
  | 
    } | 
| 94 | 
  | 
  | 
     | 
| 95 | 
  | 
  | 
} | 
| 96 | 
  | 
  | 
 | 
| 97 | 
  | 
  | 
void SectionParserManager::push_front(SectionParser* sp) { | 
| 98 | 
  | 
  | 
    SectionParserManager::iterator i; | 
| 99 | 
  | 
  | 
    i = findSectionParser(sp->getSectionName()); | 
| 100 | 
  | 
  | 
    if (i != sectionParsers_.end()) { | 
| 101 | 
  | 
  | 
        std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; | 
| 102 | 
  | 
  | 
        return; | 
| 103 | 
  | 
  | 
    } | 
| 104 | 
  | 
  | 
     | 
| 105 | 
  | 
  | 
    SectionParserContext context; | 
| 106 | 
  | 
  | 
     | 
| 107 | 
  | 
  | 
    if (sectionParsers_.empty()) { | 
| 108 | 
  | 
  | 
        context.priority = beginPriority_; | 
| 109 | 
  | 
  | 
    } else { | 
| 110 | 
  | 
  | 
        context.priority = sectionParsers_.front().priority - priorityDifference_; | 
| 111 | 
  | 
  | 
    } | 
| 112 | 
  | 
  | 
 | 
| 113 | 
  | 
  | 
    context.sectionParser = sp; | 
| 114 | 
  | 
  | 
    context.lineNo = 0; | 
| 115 | 
  | 
  | 
    context.offset = 0; | 
| 116 | 
  | 
  | 
    context.isActive = false; | 
| 117 | 
  | 
  | 
 | 
| 118 | 
  | 
  | 
} | 
| 119 | 
  | 
  | 
 | 
| 120 | 
  | 
  | 
void SectionParserManager::push_back(SectionParser* sp) { | 
| 121 | 
  | 
  | 
    SectionParserManager::iterator i; | 
| 122 | 
  | 
  | 
    i = findSectionParser(sp->getSectionName()); | 
| 123 | 
  | 
  | 
    if (i != sectionParsers_.end()) { | 
| 124 | 
  | 
  | 
        std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; | 
| 125 | 
  | 
  | 
        return; | 
| 126 | 
  | 
  | 
    } | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
    SectionParserContext context;     | 
| 129 | 
  | 
  | 
    if (sectionParsers_.empty()) { | 
| 130 | 
  | 
  | 
        context.priority = beginPriority_; | 
| 131 | 
  | 
  | 
    } else { | 
| 132 | 
  | 
  | 
        context.priority = sectionParsers_.back().priority + priorityDifference_; | 
| 133 | 
  | 
  | 
    } | 
| 134 | 
  | 
  | 
 | 
| 135 | 
  | 
  | 
    context.sectionParser = sp; | 
| 136 | 
  | 
  | 
    context.lineNo = 0; | 
| 137 | 
  | 
  | 
    context.offset = 0; | 
| 138 | 
  | 
  | 
    context.isActive = false; | 
| 139 | 
  | 
  | 
 | 
| 140 | 
  | 
  | 
} | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
void SectionParserManager::insert(SectionParser* sp, int priority) { | 
| 143 | 
  | 
  | 
    SectionParserManager::iterator i; | 
| 144 | 
  | 
  | 
    i = findSectionParser(sp->getSectionName()); | 
| 145 | 
  | 
  | 
    if (i != sectionParsers_.end()) { | 
| 146 | 
  | 
  | 
        std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; | 
| 147 | 
  | 
  | 
    } | 
| 148 | 
  | 
  | 
 | 
| 149 | 
  | 
  | 
    SectionParserContext context;     | 
| 150 | 
  | 
  | 
    context.priority = priority; | 
| 151 | 
  | 
  | 
    context.sectionParser = sp; | 
| 152 | 
  | 
  | 
    context.lineNo = 0; | 
| 153 | 
  | 
  | 
    context.offset = 0; | 
| 154 | 
  | 
  | 
    context.isActive = false; | 
| 155 | 
  | 
  | 
 | 
| 156 | 
  | 
  | 
    if (sectionParsers_.empty()) { | 
| 157 | 
  | 
  | 
        sectionParsers_.push_back(context); | 
| 158 | 
  | 
  | 
    } else { | 
| 159 | 
  | 
  | 
 | 
| 160 | 
  | 
  | 
        for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 161 | 
  | 
  | 
            if (i->priority == priority) { | 
| 162 | 
  | 
  | 
                 std::cerr << "Priority " << priority << " already used" << std::endl; | 
| 163 | 
  | 
  | 
                 return; | 
| 164 | 
  | 
  | 
            } else if (i->priority > priority) { | 
| 165 | 
  | 
  | 
                sectionParsers_.insert(i, context); | 
| 166 | 
  | 
  | 
                break; | 
| 167 | 
  | 
  | 
            } | 
| 168 | 
  | 
  | 
             | 
| 169 | 
  | 
  | 
        } | 
| 170 | 
  | 
  | 
    } | 
| 171 | 
  | 
  | 
 | 
| 172 | 
  | 
  | 
} | 
| 173 | 
  | 
  | 
 | 
| 174 | 
  | 
  | 
 | 
| 175 | 
tim | 
1789 | 
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { | 
| 176 | 
tim | 
1765 | 
    SectionParserManager::iterator i; | 
| 177 | 
  | 
  | 
    for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 178 | 
  | 
  | 
        if (i->sectionParser->getSectionName() == sectionName) { | 
| 179 | 
  | 
  | 
            break; | 
| 180 | 
  | 
  | 
        } | 
| 181 | 
  | 
  | 
    } | 
| 182 | 
  | 
  | 
 | 
| 183 | 
  | 
  | 
    return i; | 
| 184 | 
  | 
  | 
} | 
| 185 | 
  | 
  | 
 | 
| 186 | 
  | 
  | 
} |