~ubuntu-branches/ubuntu/trusty/atlas-cpp/trusty-proposed

« back to all changes in this revision

Viewing changes to Atlas/Codecs/Utility.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2005-12-19 22:43:03 UTC
  • mfrom: (3.1.2 breezy)
  • Revision ID: james.westby@ubuntu.com-20051219224303-kxrjuvih8vhdjchb
Tags: 0.5.98-3
Renamed the binary packages again to use the correct name scheme for the
C+ABI transition and add the correct conflicts (Closes #341673).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// This file may be redistributed and modified under the terms of the
2
2
// GNU Lesser General Public License (See COPYING for details).
3
 
// Copyright (C) 2000-2001 Stefanus Du Toit, Michael Day
 
3
// Copyright (C) 2000 Stefanus Du Toit, Michael Day
4
4
 
5
5
#ifndef ATLAS_CODECS_UTILITY_H
6
6
#define ATLAS_CODECS_UTILITY_H
17
17
 
18
18
#include <cstdio>
19
19
#include <string>
20
 
#include <algorithm>
21
20
 
22
21
namespace Atlas { namespace Codecs {
23
 
  
 
22
 
24
23
/// Convert an ASCII char to its hexadecimal value
25
24
inline const std::string charToHex(char c)
26
25
{
27
26
    char hex[3];
28
 
#ifdef WIN32
 
27
#ifdef __MINGW32__
 
28
    // Perhaps this should #ifdef _WIN32 instead?    
29
29
    _snprintf(hex, 3, "%x", c);
30
30
#else
31
31
    snprintf(hex, 3, "%x", c);
37
37
inline char hexToChar(const std::string& hex)
38
38
{
39
39
    int c;
40
 
    sscanf(hex.c_str(), "%x", &c);
41
 
    return c;
42
 
}
43
 
 
44
 
/** Escape a string by converting certain characters to their hexadecimal
45
 
 * value.
46
 
 *
47
 
 * @return The escaped message.
48
 
 * @param prefix The string that is to be prepended to the hexadecimal value.
49
 
 * @param special The characters that are to be escaped.
50
 
 * @param message The message that is to be escaped.
51
 
 * @see hexDecode
52
 
 */
53
 
inline const std::string hexEncode(const std::string& prefix,
54
 
        const std::string& special, const std::string& message)
55
 
{
56
 
    std::string encoded;
57
 
 
58
 
    for (std::string::const_iterator i = message.begin();
59
 
         i != message.end(); ++i)
60
 
    {
61
 
        if (std::find(special.begin(), special.end(), *i) != special.end())
62
 
        {
63
 
            encoded += prefix;
64
 
            encoded += charToHex(*i);
65
 
        }
66
 
        else
67
 
        {
68
 
            encoded += *i;
69
 
        }
70
 
    }
71
 
 
72
 
    return encoded;
73
 
}
74
 
 
75
 
/** Parse a message and replace hexadecimal 'escaped' values with their ASCII
76
 
 * counterparts.
77
 
 *
78
 
 * This function does the opposite to hexDecode - it takes a message that was
79
 
 * prepared with hexDecode, searches for occurences of prefix and replaces the
80
 
 * following hexadecimal values with their ASCII counterparts.
81
 
 *
82
 
 * @return The unescaped string.
83
 
 * @param prefix The string that is followed by the escaped characters
84
 
 * @param message The escaped message.
85
 
 */
86
 
inline const std::string hexDecode(const std::string& prefix,
87
 
                                   const std::string& message)
88
 
{
89
 
    std::string newMessage;
90
 
    std::string curFragment;
91
 
    
92
 
    for (size_t i = 0; i < message.size(); i++) {
93
 
        if (std::equal(prefix.begin(),prefix.begin() + curFragment.length() + 1,
94
 
                    (curFragment + message[i]).begin())) {
95
 
            curFragment += message[i];
96
 
        } else {
97
 
            newMessage += curFragment + message[i];
98
 
            curFragment = "";
99
 
        }
100
 
        if (curFragment == prefix) {
101
 
            std::string hex;
102
 
            hex += message[++i];
103
 
            hex += message[++i];
104
 
            newMessage += hexToChar(hex);
105
 
            curFragment = "";
106
 
        }
107
 
    }
108
 
 
109
 
    return newMessage;
110
 
}
 
40
    if (sscanf(hex.c_str(), "%x", &c) == 1) {
 
41
        return (char) c;
 
42
    } else {
 
43
        return (char) 0;
 
44
    }
 
45
}
 
46
 
 
47
const std::string hexEncodeWithPrefix(const std::string& prefix,
 
48
                                      const std::string& special,
 
49
                                      const std::string& message);
 
50
 
 
51
const std::string hexDecodeWithPrefix(const std::string& prefix,
 
52
                                      const std::string& message);
111
53
 
112
54
} } // namespace Atlas::Codecs
113
55
 
114
 
#endif
 
56
#endif // ATLAS_CODECS_UTILITY_H