00001 #ifndef charSetTranslator_h_included 00002 #define charSetTranslator_h_included 00003 00004 #include <utility> 00005 #include <list> 00006 #include <string> 00007 00008 namespace cxxtls 00009 { 00010 00011 class CharSetTranslator 00049 { // CharSetTranslator 00050 char mapping_[256]; 00051 00052 00053 bool ok_; 00054 00055 char const *error_; 00056 00057 struct LhsSubrange 00060 { 00061 int offset_; // the character in the left hand side 00062 int mappedOffset_; // the index position in the right hand side 00063 00064 LhsSubrange(int lhsChar, int mappedRhsPosition) 00065 : offset_(lhsChar) 00066 , mappedOffset_(mappedRhsPosition) 00067 { 00068 } 00069 00070 }; 00071 00072 typedef std::list< LhsSubrange > LhsRange; 00073 00074 public: 00075 00076 bool ok() const { return ok_; } 00077 char const *error() const { return error_; } 00078 00079 CharSetTranslator(std::string const &inset, std::string const &outset) 00109 { 00110 00111 ok_ = true; 00112 error_ = ""; 00113 00114 LhsRange lhs; 00115 00116 char rhs[256]; 00117 00118 populateLhs(unEscape(inset).c_str(), lhs); 00119 populateRhs(unEscape(outset).c_str(), rhs); 00120 populateMapping(lhs, rhs); 00121 00122 } 00123 00124 char operator[] (unsigned char i) const { return mapping_[i]; } 00125 00126 00127 std::string translate(std::string const &s) const 00130 ; 00131 00132 private: 00133 00134 void populateLhs(char const *inset, LhsRange &lhs) 00137 ; 00138 00139 void populateRhs(char const *set, char *rhs) 00143 ; 00144 00145 void populateMapping(LhsRange const &lhs, char const *rhs) 00148 ; 00149 00150 public: 00151 00152 static std::string unEscape(std::string const &in) 00164 ; 00165 00166 00167 }; // CharSetTranslator 00168 00169 }; // namespace cxxlts 00170 00171 00172 #endif