Changeset 625

Show
Ignore:
Timestamp:
03/10/07 12:20:10 (18 months ago)
Author:
hyriand
Message:

Commented codesetmanager.cpp/h

Location:
branches/newnet/museekd/museekd
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/newnet/museekd/museekd/codesetmanager.cpp

    r611 r625  
    2929Museek::CodesetManager::~CodesetManager() 
    3030{ 
     31  /* Free all the stored iconv contexts. */ 
    3132  std::map<std::pair<std::string, std::string>, iconv_t>::iterator it, end = m_Contexts.end(); 
    3233  for(it = m_Contexts.begin(); it != end; ++it) 
     
    3738Museek::CodesetManager::convert(const std::string & from, const std::string & to, const std::string & str) 
    3839{ 
     40  /* No point in trying to convert an emtpy string. */ 
    3941  if(str.empty()) 
    4042    return str; 
    4143 
     44  /* Get our iconv conversion context. */ 
    4245  iconv_t context = getContext(from, to); 
     46  /* Guess and allocate a buffer. str.size() * 4 should be enough to hold a 
     47     converted string to any character set, even UTF32. */ 
    4348  size_t buf_len = str.size() * 4 + 1; 
    4449  char * out_buf = new char[buf_len]; 
     
    4853  while(1) 
    4954  { 
     55    /* Fetch pointer to the source data. */ 
    5056    const char * inbuf = str.data(); 
    5157    char * out_ptr = out_buf; 
     
    5359    out_left = buf_len; 
    5460 
     61    /* Try to convert the string. */ 
    5562    r = iconv(context, (ICONV_INPUT_TYPE)&inbuf, &in_left, &out_ptr, &out_left); 
    56     if(r == (size_t)-1 && errno == E2BIG) 
     63    if(r == (size_t)-1 && errno == E2BIG) // Output buffer not large enough 
    5764    { 
    5865      delete [] out_buf; 
    59       buf_len *= 2; 
     66      buf_len *= 2; // Try a buffer twice as large 
    6067      out_buf = new char[buf_len]; 
    6168      continue; 
    6269    } 
     70    /* FIXME: There should be additional error handling here, strings that 
     71              hold invalid characters will be truncated. */ 
    6372    break; 
    6473  } 
    6574 
     75  /* Create the result string. */ 
    6676  std::string ret; 
    6777  if(r != (size_t)-1 && in_left == 0) 
    6878    ret.assign(out_buf, buf_len - out_left); 
    6979 
     80  // Free the output buffer. 
    7081  delete [] out_buf; 
    7182  return ret; 
     
    7586Museek::CodesetManager::getNetworkCodeset(const std::string & domain, const std::string & key) const 
    7687{ 
     88  // Try to get the requested character set 
    7789  std::string codeset = museekd()->config()->get(domain, key); 
    78   if(codeset.empty()) 
     90  if(codeset.empty()) // Get the default network encoding 
    7991    codeset = museekd()->config()->get("encoding", "network"); 
    80   if(codeset.empty()) 
     92  if(codeset.empty()) // Fall back to UTF-8 
    8193    codeset = "UTF-8"; 
    8294  return codeset; 
     
    92104Museek::CodesetManager::fromRoomMap(const std::string & room, const std::map<std::string, std::string> & map) 
    93105{ 
     106  // Get the character set for the room. 
    94107  std::string codeset = getNetworkCodeset("encoding.rooms", room); 
    95108 
    96109  std::map<std::string, std::string> result; 
    97110 
     111  // Iterate over the map, convert values and store them in 'result' 
    98112  std::map<std::string, std::string>::const_iterator it, end = map.end(); 
    99113  for(it = map.begin(); it != end; ++it) 
     
    112126Museek::CodesetManager::getContext(const std::string & from, const std::string & to) 
    113127{ 
     128  // This is the context store key. 
    114129  std::pair<std::string, std::string> key(from, to); 
     130  // Check if we've already constructed this iconv context. 
    115131  std::map<std::pair<std::string, std::string>, iconv_t>::iterator it; 
    116132  it = m_Contexts.find(key); 
     
    118134    return (*it).second; 
    119135 
     136  // Create the iconv context for this conversion. 
    120137  iconv_t context = iconv_open(from.c_str(), to.c_str()); 
     138  // Currently, we don't handle invalid contexts very well. 
    121139  assert(context != (iconv_t)-1); 
     140  // Store the context for future use. 
    122141  m_Contexts[key] = context; 
    123142  return context; 
  • branches/newnet/museekd/museekd/codesetmanager.h

    r611 r625  
    3636    CodesetManager(Museekd * museekd); 
    3737 
     38    /* Frees all stored iconv contexts. */ 
    3839    ~CodesetManager(); 
    3940 
     41    /* Return pointer to museekd instance. */ 
    4042    Museekd * museekd() const 
    4143    { 
     
    4345    } 
    4446 
     47    /* Convert 'str' from character set 'from' to character set 'to' */ 
    4548    std::string convert(const std::string & from, const std::string & to, const std::string & str); 
     49    /* Convert 'str' from character set 'from' to UTF8 */ 
    4650    std::string toUtf8(const std::string & from, const std::string & str) 
    4751    { 
    4852      return convert(from, "UTF-8", str); 
    4953    } 
     54    /* Convert 'str' from UTF8 to character set 'to' */ 
    5055    std::string fromUtf8(const std::string & to, const std::string & str) 
    5156    { 
     
    5358    } 
    5459 
     60    /* Convert 'str' from character set set for room 'room' to UTF8 */ 
    5561    std::string fromRoom(const std::string & room, const std::string & str); 
     62    /* Convert values in map from character set set for room 'room' to UTF8 */ 
    5663    std::map<std::string, std::string> fromRoomMap(const std::string & room, const std::map<std::string, std::string> & map); 
     64    /* Convert 'str' from UTF8 to character set set for room 'room' */ 
    5765    std::string toRoom(const std::string & room, const std::string & str); 
    5866 
    5967  private: 
     68    /* Get the character set for an object from the configuration */ 
    6069    std::string getNetworkCodeset(const std::string & domain, const std::string & key) const; 
     70    /* Get an iconv conversion context from character set 'from' to 'to'. */ 
    6171    iconv_t getContext(const std::string & from, const std::string & to); 
    6272 
     73    /* Weak reference to museekd instance. */ 
    6374    NewNet::WeakRefPtr<Museekd> m_Museekd; 
     75    /* Iconv context cache. */ 
    6476    std::map<std::pair<std::string, std::string>, iconv_t> m_Contexts; 
    6577  };