home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / Util / TemplateTranslator.cc < prev    next >
C/C++ Source or Header  |  2000-06-07  |  3KB  |  112 lines

  1. //
  2. //  $Id: TemplateTranslator.cc,v 1.2 2000/06/07 06:53:30 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include "TemplateTranslator.h"
  7. #include "InputStream.h"
  8. #include "OutputStream.h"
  9. #include "Assert.h"
  10.  
  11.  
  12. namespace Util
  13. {
  14.     // constants
  15.  
  16.     // Placeholder' brackets
  17.     static const char* BEGIN_MARK   = "${";
  18.     static const char* END_MARK     = "}$";
  19.  
  20.     // construction
  21.  
  22.     TemplateTranslator::TemplateTranslator(const TranslationTable& table):
  23.         _translationTable(table)
  24.     {}
  25.  
  26.     TemplateTranslator::~TemplateTranslator()
  27.     {}
  28.  
  29.     // operations
  30.  
  31.     void TemplateTranslator::translate(const char* in, OutputStream& out) const
  32.     {
  33.         processLine(in, out);
  34.     }
  35.  
  36.     // implementation
  37.  
  38.     void TemplateTranslator::processLine(const char* line, OutputStream& out) const
  39.     {
  40.         assert(line != 0);
  41.  
  42.         const char* ptr = line;
  43.         for (;;)
  44.         {
  45.             int off = findPlaceholder(ptr);
  46.             if (off == -1)
  47.                 break;
  48.  
  49.             out.writeData(ptr, off);
  50.  
  51.             ptr += off;
  52.             ptr += translatePlaceholder(ptr, out);
  53.         }
  54.  
  55.         out.writeData(ptr, StrLen(ptr));
  56.     }
  57.  
  58.     int TemplateTranslator::findPlaceholder(const char* line) const
  59.     {
  60.         assert(line != 0);
  61.  
  62.         char* beginMark = StrStr(line, BEGIN_MARK);
  63.         if (beginMark != 0)
  64.         {
  65.             char* endMark = StrStr(beginMark, END_MARK);
  66.             if (endMark != 0)
  67.                 return beginMark-line;
  68.         }
  69.  
  70.         return -1;
  71.     }
  72.  
  73.     int TemplateTranslator::translatePlaceholder(const char* placeholder, OutputStream& out) const
  74.     {
  75.         assert(placeholder != 0);
  76.  
  77.         char keyword[256];
  78.         int placeholderLen = getKeyword(placeholder, keyword, sizeof(keyword));
  79.  
  80.         const char* value = _translationTable.get(keyword);
  81.         if (value != 0)
  82.             out.writeData(value, StrLen(value));
  83.         else
  84.             out.writeData(placeholder, placeholderLen);
  85.  
  86.         return placeholderLen;
  87.     }
  88.  
  89.     int TemplateTranslator::getKeyword(const char* placeholder, char* result, int maxResultLen) const
  90.     {
  91.         assert(placeholder != 0);
  92.         assert(result != 0);
  93.  
  94.         const char* keyword = placeholder+StrLen(BEGIN_MARK);
  95.         const char* endMark = StrStr(keyword, END_MARK);
  96.  
  97.         copyKeyword(keyword, endMark-keyword, result, maxResultLen);
  98.  
  99.         return endMark-placeholder+StrLen(END_MARK);
  100.     }
  101.  
  102.     void TemplateTranslator::copyKeyword(const char* keyword, int keywordLen, char* result, int maxResultLen) const
  103.     {
  104.         if (keywordLen > maxResultLen-1)
  105.             keywordLen = maxResultLen-1;
  106.  
  107.         MemMove(result, keyword, keywordLen);
  108.         result[keywordLen] = 0;
  109.     }
  110. }
  111. // namespace Util
  112.