str_repr.hh 5.25 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2013 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef STR_REPR_HH
#define STR_REPR_HH

#include <iostream>
22
#include <clocale>
Tiago Peixoto's avatar
Tiago Peixoto committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <boost/lexical_cast.hpp>

//
// Data type string representation
// ===============================
//
// String representation of individual data types. Among other things, we have
// to take care specifically that no information is lost with floating point
// I/O.
//

namespace boost
{

//
// "chars" should be printed as numbers, since they can be non-printable
//

template <>
string lexical_cast<string,uint8_t>(const uint8_t& val)
{
    return lexical_cast<std::string>(int(val));
}

template <>
uint8_t lexical_cast<uint8_t,string>(const string& val)
{
    return uint8_t(lexical_cast<int>(val));
}

// float, double and long double should be printed in hexadecimal format to
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
// preserve internal representation. (we also need to make sure the
// representation is locale-independent).

int print_float_dispatch(char*& str, float val)
{
    return asprintf(&str, "%a", val);
}

int print_float_dispatch(char*& str, double val)
{
    return asprintf(&str, "%la", val);
}

int print_float_dispatch(char*& str, long double val)
{
    return asprintf(&str, "%La", val);
}

template <class Val>
73
int print_float(char*& str, Val val)
74 75 76
{
    char* locale = setlocale(LC_NUMERIC, NULL);
    setlocale(LC_NUMERIC, "C");
77
    int retval = print_float_dispatch(str, val);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    setlocale(LC_NUMERIC, locale);
    return retval;
}

int scan_float_dispatch(const char* str, float& val)
{
    return sscanf(str, "%a", &val);
}

int scan_float_dispatch(const char* str, double& val)
{
    return sscanf(str, "%la", &val);
}

int scan_float_dispatch(const char* str, long double& val)
{
    return sscanf(str, "%La", &val);
}
Tiago Peixoto's avatar
Tiago Peixoto committed
96

97 98 99 100 101 102 103 104 105 106 107
template <class Val>
int scan_float(const char* str, Val& val)
{
    char* locale = setlocale(LC_NUMERIC, NULL);
    setlocale(LC_NUMERIC, "C");
    int retval = scan_float_dispatch(str, val);
    setlocale(LC_NUMERIC, locale);
    return retval;
}


Tiago Peixoto's avatar
Tiago Peixoto committed
108 109 110 111
template <>
string lexical_cast<string,float>(const float& val)
{
    char* str = 0;
112
    int retval = print_float(str, val);
Tiago Peixoto's avatar
Tiago Peixoto committed
113 114 115 116 117 118 119 120 121 122 123
    if (retval == -1)
        throw bad_lexical_cast();
    std::string ret = str;
    free(str);
    return ret;
}

template <>
float lexical_cast<float,string>(const string& val)
{
    float ret;
124
    int nc = scan_float(val.c_str(), ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
125 126 127 128 129 130 131 132 133
    if (nc != 1)
        throw bad_lexical_cast();
    return ret;
}

template <>
string lexical_cast<string,double>(const double& val)
{
    char* str = 0;
134
    int retval = print_float(str, val);
Tiago Peixoto's avatar
Tiago Peixoto committed
135 136 137 138 139 140 141 142 143 144 145
    if (retval == -1)
        throw bad_lexical_cast();
    std::string ret = str;
    free(str);
    return ret;
}

template <>
double lexical_cast<double,string>(const string& val)
{
    double ret;
146
    int nc = scan_float(val.c_str(), ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
147 148 149 150 151 152 153 154 155
    if (nc != 1)
        throw bad_lexical_cast();
    return ret;
}

template <>
string lexical_cast<string,long double>(const long double& val)
{
    char* str = 0;
156
    int retval = print_float(str, val);
Tiago Peixoto's avatar
Tiago Peixoto committed
157 158 159 160 161 162 163 164 165 166 167
    if (retval == -1)
        throw bad_lexical_cast();
    std::string ret = str;
    free(str);
    return ret;
}

template <>
long double lexical_cast<long double,string>(const string& val)
{
    long double ret;
168
    int nc = scan_float(val.c_str(), ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    if (nc != 1)
        throw bad_lexical_cast();
    return ret;
}
} // namespace boost

//
// stream i/o of std::vector<>
//

namespace std
{

// string vectors need special attention, since separators must be properly
// escaped.
template <>
ostream& operator<<(ostream& out, const vector<string>& vec)
{
    for (size_t i = 0; i < vec.size(); ++i)
    {
        string s = vec[i];
        // escape separators
        boost::replace_all(s, "\\", "\\\\");
        boost::replace_all(s, ", ", ",\\ ");

        out << s;
        if (i < vec.size() - 1)
            out << ", ";
    }
    return out;
}

template <>
istream& operator>>(istream& in, vector<string>& vec)
{
    using namespace boost;
    using namespace boost::algorithm;
    using namespace boost::xpressive;

    vec.clear();
    string data;
    while (in.good())
    {
        string line;
        getline(in, line);
        data += line;
    }

    if (data == "")
        return in; // empty string is OK

    sregex re = sregex::compile(", ");
    sregex_token_iterator iter(data.begin(), data.end(), re, -1), end;
    for (; iter != end; ++iter)
    {
        vec.push_back(*iter);
        // un-escape separators
        boost::replace_all(vec.back(), ",\\ ", ", ");
        boost::replace_all(vec.back(), "\\\\", "\\");
    }
    return in;
}

} // std namespace

#endif // STR_REPR_HH