Ejemplo n.º 1
0
void catfile(istream& infile, const string& filename, 
             str_str_map& list) {
   static string colons{": "};
   int line_num = 0;
   for(;;) {
      string line;
      getline (infile, line);
      if(infile.eof()) break;
      cout << filename << colons << ++line_num 
           << colons << line << endl;
      
      line = trim(line);
      if(line.size() == 0 or line[0] == '#')
         continue;
      size_t pos = line.find_first_of("=");
      if(pos == string::npos) {
         auto itor = list.find(line);
         if(itor != list.end())
            cout << itor->first << " = " << itor->second << endl;
         else
            cout << line << ": key not found" << endl;
      } else {
         string key = trim(line.substr(0, pos == 0 ? 0 : pos));
         string value = trim(line.substr(pos + 1));
         if(!key.empty() && !value.empty()) {
            list.insert({key, value});
            cout << key << " = " << value << endl;
         } else if(!key.empty() && value.empty()) {
            auto itor = list.find(key);
            if(itor != list.end())
               list.erase(itor);
         } else if(key.empty() && !value.empty()) {
            for(auto itor = list.begin(); itor != list.end(); ++itor) {
               if(itor->second == value)
                  cout << itor->first << " = " << itor->second << endl;
            }
         } else {
            for(auto itor = list.begin(); itor != list.end(); ++itor) {
               cout << itor->first << " = " << itor->second << endl;
            }
         }
      }
   }
}
Ejemplo n.º 2
0
void read(istream& in, str_str_map& map, string filename="-"){
   string line; int line_number{1};
   string key, value; bool equals;
   while(getline(in,line)){
      cout << filename << ": " << line_number++
      << ": " << line << endl;
      trim_space(line);
      if (line.length() == 0 || line[0] == '#') continue;
      key = trim_key(line); value = trim_value(line);
      equals = !(line == key);
      if (key.length() == 0 and value.length() == 0){
         for (auto iter = map.begin(); iter != map.end(); ++iter){
            cout << (*iter).first << " = " << (*iter).second << endl;
         }
      } else if(key.length() > 0 && value.length() > 0){
         cout << key << " = " << value << endl;
         map.insert(str_str_pair(key,value));
      } else if (key.length() > 0){
         if (equals){
            auto locate = map.find(str_str_pair(key,value));
            if (locate != map.end()) { map.erase(locate); continue; }}
         else{
            auto locate = map.find(str_str_pair(key,value));
            if (locate != map.end()){
               cout << (*locate).first << " = "
               << (*locate).second << endl;
               continue;
            }
            cout << key << ": key not found" << endl;
         }
      } else if (value.length() > 0 and equals){
         str_str_map::iterator search = map.begin();
         for(;search != map.end(); ++search){
            if(search->second == value){
               cout << (*search).first << " = "
               << (*search).second << endl;
            }
         }
      }
   }
 }