diff options
Diffstat (limited to 'graphics/asymptote/fileio.cc')
-rw-r--r-- | graphics/asymptote/fileio.cc | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/graphics/asymptote/fileio.cc b/graphics/asymptote/fileio.cc index 441a9590ec..b28269b2b1 100644 --- a/graphics/asymptote/fileio.cc +++ b/graphics/asymptote/fileio.cc @@ -75,6 +75,46 @@ void ifile::ignoreComment() } } +void ifile::Read(double& val) { + char c; + std::string str; + bool neg; + + while(isspace(c=stream->peek())) + stream->ignore(); + neg=stream->peek() == '-'; + // Try parsing the input as a number. + if(*stream >> val) + return; + + clear(); + + switch(stream->peek()) { + case 'I': case 'i': // inf + case 'N': case 'n': // NaN + for(Int i=0; i < 3 && stream->good(); i++) + str += stream->get(); + break; + default: + stream->setstate(std::ios_base::failbit); + return; + } + + if(strcasecmp(str.c_str(),"inf") == 0) + val=std::numeric_limits < double > ::infinity(); + else if(strcasecmp(str.c_str(),"nan") == 0) + val=std::numeric_limits < double > ::quiet_NaN(); + else { + for(auto it=str.rbegin(); it != str.rend(); ++it) + stream->putback(*it); + stream->setstate(std::ios_base::failbit); + return; + } + + if(neg) + val=-val; +} + bool ifile::eol() { int c; |