Replacing tokens in a text file with replaced text This code parses a text file, replacing special tokens marked as $(token) with some replaced text.  The $ symbol can be escaped by entering a double $$.  In the example a user enters a text file containing tokens to replace and a separate mapping file with, for instance, colour:red to denote how to replace a token.

Some remarks I have is that is is not using any OO really.  Does it need to be?

#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;map&gt;
#include &lt;string&gt;

static void readToken(std::istream&amp; strm, std::vector&lt;char&gt;&amp; token)
{
   while(strm) {
      int c (strm.get());
      token.push_back(c);
      if(')' == c) break;
   }
}

static void lookup_replacement(const std::map&lt;std::string,std::string&gt;&amp; token_map, 
                               const std::vector&lt;char&gt;&amp; key, std::vector&lt;char&gt;&amp; value) {
    if(key.size() &gt; 3) {  //3 chars min are '$', '(' and ')'                      
        std::map&lt;std::string,std::string&gt;::const_iterator it = token_map.find(std::string(key.begin()+2, key.end()-1));
        if(it != token_map.end())
           value.insert(value.end(), it-&gt;second.begin(), it-&gt;second.end());
    }
}

static bool readTo(std::istream&amp; strm, std::vector&lt;char&gt;&amp; buf, const char term[])
{
   bool ret(false);
   while(strm) {
      int c (strm.peek());
      if(strchr(term, c)) {
         strm.get();
         int c2(strm.peek());
         if(c != c2) {
           strm.unget();
           ret = true;
           break;
         }
      }
      strm.get();
      buf.push_back(c);
   }
   return ret;
}

static void create_mapping(std::ifstream&amp; strm, std::map&lt;std::string,std::string&gt;&amp; tokens) {
   if(strm.good()) {
      std::string line;
      while(std::getline(strm, line)) {
         std::string::size_type pos = line.find_first_of(':');
         if(pos != std::string::npos)
            tokens.insert(std::make_pair&lt;std::string, std::string&gt;(line.substr(0, pos), line.substr(pos+1)));
      }
   } 
}

int main() {
   std::map&lt;std::string,std::string&gt; token_map;

   typedef std::map&lt;std::string,std::string&gt;::iterator map_iter;

   std::cout &lt;&lt; "Enter path to text file to replace\nTokens to be replaced should be of form: $(token)\n";
   std::string file;
   std::cin &gt;&gt; file;

   std::cout &lt;&lt; "Enter path to file with target token &lt;-&gt; replacement token in format:\n" 
                "&lt;toreplace1&gt;:&lt;replacement1&gt;\n&lt;toreplace2&gt;:&lt;replacement2&gt;\netc\n";
   std::string mapfile;
   std::cin &gt;&gt; mapfile;

   std::ifstream strmmap(mapfile.c_str());
   create_mapping(strmmap, token_map);

   // replaced text
   std::vector&lt;char&gt; replaced;

   std::ifstream strm(file.c_str());
   if(!strm.bad()) {
      while(!strm.eof()) {
         if(readTo(strm, replaced, "$")) {
            std::vector&lt;char&gt; token;
            readToken(strm, token);
            std::vector&lt;char&gt; replace_tok;
            lookup_replacement(token_map, token, replace_tok);
            if(!replace_tok.empty())
                replaced.insert(replaced.end(), replace_tok.begin(), replace_tok.end());
         }
      }
   }

   //print out replaced text
   std::cout &lt;&lt; "your replaced text file\n";
   std::vector&lt;char&gt;::iterator pit = replaced.begin();
   while(pit != replaced.end())
      std::cout &lt;&lt; *pit++;

   return 0;
}


The example input file I was using:


  Once upon a $(place1) there was a $(adjective1) $(colour) $(animal).
  
  Do you have a $$
  
  You have enemies? Good. That means you've stood up for something,
  sometime in your life. by $(author) on $(date)
  
  A fanatic is one who can't change his mind and won't change the
  subject. By $(author)


And the mapping file:




place1:time 
adjective1:cunning 
colour:brown 
animal:fox 
author:Winston Churchill 
date:06/06/2013
Its a little heavy on the code side.

Your definition of a token still leaves possibilities that you have not defined.

$$          :  Replace with $                                         Fine
$(&lt;X&gt;)      :  Replace with the mapped `&lt;X&gt;`                          Fine.

$(&lt;stuff&gt;\n : No closing ')'                                          Error
$()         : What happens with no identifier.                        Error
$&lt;X&gt;        : Some other character after '$' that is not '(' or '$'   Error


Especially your stream handling.
Its usually bad practice to test for .good(), .bad() or .eof() during normal processing. You want to test these after things go wrong to generate the appropriate error message or not. Usually stream code looks like this:

while(std::getline(stream, line))
{
    // correctly read a line
}

// or

while(stream &gt;&gt; value &gt;&gt; value2 &gt;&gt; value3 &gt;&gt; etc)
{
    // correctly read a value from the stream
}


You can use OO to compartmentalize your mapping.
Personally I would combine your mapper and mapped class into a single class.

class Mapper
{
    // Class contains a map from tokens =&gt; value
    // used via replace() to replace all tokens $(&lt;X&gt;) in string
    // with the values contained in the map.

    std::map&lt;std::string, std::string&gt;  replaceMap;
    public:
        Mapper(std::string const&amp; fileName)
        {
            // Read all the mapping values from the file.

            std::ifstream   mapFile(fileName.c_str());
            std::string     line;

            while(std::getline(mapFile, line))
            {
                // Each line in the file contains a token/value mapping
                std::stringstream   linestream(line);

                std::string         token;
                std::string         value;

                std::getline(linestream, token, ':');
                std::getline(linestream, value);

                // Save the token and value
                replaceMap[token]  = value;
            }
        }

        std::string replace(std::string const&amp; line)
        {
            std::string result;
            std::size_t last = 0;

            // Search for the '$' char repeatedly.
            for(std::size_t find = line.find('$');find != std::string::npos;last=find, find=line.find('$', find))
            {
                // Copy the inert text from line
                // into the result
                result  += line.substr(last, (find-last));

                // If the next character is a '(' then search for the closing ')'
                //
                // If we don't find a token `$(&lt;X&gt;)` then ignore the '$' and treat
                // it like a normal character. Note If &lt;X&gt; has no mapping then
                // we replace it with the empty string. Note if &lt;X&gt; is empty it is
                // is still value.

                bool hit = false 
                if ((find + 1 &lt; line.size()) &amp;&amp; line[find + 1] == '(')
                {
                    std::size_t end = line.find(')', find);
                    if (end != std::string::npos)
                    {
                        std::string key = line.substr(find+2, end-find-2);
                        result += replaceMap[key];
                        find = end + 1;
                        hit  = true;
                    }
                }

                if (!hit)
                {
                    // Token was not found.
                    // Put the '$' on the output and move on.
                    result  += '$';
                    find++;
                }
            }
            // Add the rest of the inert string to the output
            result  += line.substr(last);

            return result;
        }
};


This makes it easy to use:

int main(int argc, char* argv[])
{
    Mapper  mapper(argv[1]);

    std::ifstream   input(argv[2]);
    std::string     line;
    while(std::getline(input, line))
    {
        std::string result = mapper.replace(line);
        std::cout &lt;&lt; result &lt;&lt; "\n";
    }
}