Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

Utility.h

Go to the documentation of this file.
00001 // This file may be redistributed and modified under the terms of the
00002 // GNU Lesser General Public License (See COPYING for details).
00003 // Copyright (C) 2000-2001 Stefanus Du Toit, Michael Day
00004 
00005 #ifndef ATLAS_CODECS_UTILITY_H
00006 #define ATLAS_CODECS_UTILITY_H
00007 
00018 #include <cstdio>
00019 #include <string>
00020 #include <algorithm>
00021 
00022 namespace Atlas { namespace Codecs {
00023   
00025 inline const std::string charToHex(char c)
00026 {
00027     char hex[3];
00028 #ifdef WIN32
00029     _snprintf(hex, 3, "%x", c);
00030 #else
00031     snprintf(hex, 3, "%x", c);
00032 #endif
00033     return hex;
00034 }
00035 
00037 inline char hexToChar(const std::string& hex)
00038 {
00039     int c;
00040     sscanf(hex.c_str(), "%x", &c);
00041     return c;
00042 }
00043 
00053 inline const std::string hexEncode(const std::string& prefix,
00054         const std::string& special, const std::string& message)
00055 {
00056     std::string encoded;
00057 
00058     for (std::string::const_iterator i = message.begin();
00059          i != message.end(); ++i)
00060     {
00061         if (std::find(special.begin(), special.end(), *i) != special.end())
00062         {
00063             encoded += prefix;
00064             encoded += charToHex(*i);
00065         }
00066         else
00067         {
00068             encoded += *i;
00069         }
00070     }
00071 
00072     return encoded;
00073 }
00074 
00086 inline const std::string hexDecode(const std::string& prefix,
00087                                    const std::string& message)
00088 {
00089     std::string newMessage;
00090     std::string curFragment;
00091     
00092     for (size_t i = 0; i < message.size(); i++) {
00093         if (std::equal(prefix.begin(),prefix.begin() + curFragment.length() + 1,
00094                     (curFragment + message[i]).begin())) {
00095             curFragment += message[i];
00096         } else {
00097             newMessage += curFragment + message[i];
00098             curFragment = "";
00099         }
00100         if (curFragment == prefix) {
00101             std::string hex;
00102             hex += message[++i];
00103             hex += message[++i];
00104             newMessage += hexToChar(hex);
00105             curFragment = "";
00106         }
00107     }
00108 
00109     return newMessage;
00110 }
00111 
00112 } } // namespace Atlas::Codecs
00113 
00114 #endif

Copyright 2000 the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.