Commit 8358ecb9 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix i/o of floating point vector types

This ensures that vector types also don't suffer from data loss due to
representation change.
parent 1270f0fb
......@@ -47,10 +47,10 @@ using namespace graph_tool;
//
// String representation of individual data types. We have to take care
// specifically that no information is lost with floating point I/O.
//
namespace boost
{
template <>
string lexical_cast<string,uint8_t>(const uint8_t& val)
{
......@@ -73,7 +73,9 @@ template <>
string lexical_cast<string,double>(const double& val)
{
char* str = 0;
asprintf(&str, "%la", val);
int retval = asprintf(&str, "%la", val);
if (retval == -1)
throw bad_lexical_cast();
std::string ret = str;
free(str);
return ret;
......@@ -93,7 +95,9 @@ template <>
string lexical_cast<string,long double>(const long double& val)
{
char* str = 0;
asprintf(&str, "%La", val);
int retval = asprintf(&str, "%La", val);
if (retval == -1)
throw bad_lexical_cast();
std::string ret = str;
free(str);
return ret;
......@@ -108,10 +112,9 @@ long double lexical_cast<long double,string>(const string& val)
throw bad_lexical_cast();
return ret;
}
}
// vector io
// std::vector<> stream i/o
namespace std
{
// string vectors need special attention, since separators must be properly
......@@ -160,7 +163,8 @@ istream& operator>>(istream& in, vector<string>& vec)
}
return in;
}
}
} // std namespace
//
// Persistent IO of python::object types. All the magic is done in python,
......
......@@ -217,12 +217,40 @@ add_vertex(reverse_graph<Graph>& g)
} // namespace boost
namespace std
{
// STL omission?
inline bool max(const bool& a, const bool& b) { return a || b; }
}
//
// Data type string representation
// ===============================
//
// String representation of individual data types. We have to take care
// specifically that no information is lost with floating point I/O.
//
// These are implemented in graph_io.cc.
namespace boost
{
using namespace std;
template <>
string lexical_cast<string,uint8_t>(const uint8_t& val);
template <>
uint8_t lexical_cast<uint8_t,string>(const string& val);
template <>
string lexical_cast<string,double>(const double& val);
template <>
double lexical_cast<double,string>(const string& val);
template <>
string lexical_cast<string,long double>(const long double& val);
template <>
long double lexical_cast<long double,string>(const string& val);
}
// std::vector<> stream i/o
namespace std
{
......@@ -257,6 +285,8 @@ istream& operator>>(istream& in, vector<Type>& vec)
return in;
}
// string vectors need special attention, since separators must be properly
// escaped.
template <>
ostream& operator<<(ostream& out, const vector<string>& vec);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment