1 /* $Id: $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's offical duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================*/
25 
26 /*****************************************************************************
27 
28 File name: njn_ioutil.cpp
29 
30 Author: John Spouge
31 
32 Contents:
33 
34 ******************************************************************************/
35 
36 #include <math.h>
37 
38 #include <assert.h>
39 #include "njn_ioutil.hpp"
40 #include <iostream>
41 #include <fstream>
42 
43 
44 using namespace std;
45 using namespace Njn;
46 using namespace IoUtil;
47 
48 
49 
50    const Format FORMAT = HUMAN;
51    Format format1 = HUMAN;
52 
53    const char TERMINATOR = '!';
54    char terminator = TERMINATOR;
55 
56 
getFormat()57 Format IoUtil::getFormat ()
58 {
59    return format1;
60 }
61 
setFormat(Format format_)62 void IoUtil::setFormat (Format format_)
63 {
64    format1 = format_;
65 }
66 
clearFormat()67 Format IoUtil::clearFormat ()
68 {
69    return format1 = FORMAT;
70 }
71 
getTerminator()72 char IoUtil::getTerminator ()
73 {
74    return terminator;
75 }
76 
setTerminator(char t_)77 void IoUtil::setTerminator (char t_)
78 {
79    terminator = t_;
80 }
81 
clearTerminator()82 char IoUtil::clearTerminator ()
83 {
84    return terminator = TERMINATOR;
85 }
86 
abort()87 void IoUtil::abort ()
88 {
89    exit (1);
90 }
91 
abort(const string & s_)92 void IoUtil::abort (const string &s_)
93 {
94    cerr << s_ << endl;
95    IoUtil::abort ();
96 }
97 
getLine(istream & in_,string & str_,const char t_)98 istream &IoUtil::getLine (
99 istream &in_, // input filestream
100 string &str_, // string before '!' from next line
101 const char t_) // termination character (could be '\n')
102 { // behaves like getline (in_, str_) but throws away the string after first character t_ && skips lines of white space
103 
104    assert (t_ != '\0');
105 
106    if (! in_) return in_;
107 
108    const char *pbuf = 0;
109 
110    while (getline (in_, str_)) {
111       for (pbuf = str_.c_str (); *pbuf != '\0' && isspace (*pbuf); pbuf++); // advance past white space
112       if (*pbuf != '\0' && *pbuf != t_) break; // skip lines full of white space
113    }
114 
115    if (t_ != '\n') {
116       size_t pos = str_.find (t_, 0);
117       if (pos < str_.size ()) str_.erase (pos, str_.size ());
118    }
119 
120    return in_; // buf is empty and eof is reached
121 }
122 
getLine(istream & in_,stringstream & sstr_,const char t_)123 istream &IoUtil::getLine (
124 istream &in_, // input filestream
125 stringstream &sstr_, // sstr_ contains the string before '!' from next line
126 const char t_) // termination character (could be '\n')
127 { // behaves like getline (in_, str_) but throws away the string after first character t_ && skips lines of white space
128 
129    string s;
130 
131    IoUtil::getLine (in_, s, t_);
132    sstr_.clear ();
133    sstr_.str ("");
134    sstr_ << s;
135    sstr_.clear ();
136 
137    return in_;
138 }
139 
in(std::istream & in_,double & x_)140 std::istream &IoUtil::in (
141 std::istream &in_,
142 double &x_)
143 {
144     string s;
145     in_ >> s;
146 
147     for (string::iterator i = s.begin (); i != s.end (); i++) *i = /*sls added (char)*/(char) tolower (*i);
148 
149     if (s == "1.#inf")
150     {
151         x_ = HUGE_VAL;
152     }
153     else if (s == "nan")
154     {
155         x_ = HUGE_VAL;
156     }
157     else
158     {
159         stringstream str (s);
160         str >> x_;
161 
162         if (str.fail ()) in_.setstate (ios_base::failbit);
163     }
164 
165     return in_;
166 }
167 
168