NCBI C++ ToolKit
njn_ioutil.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: njn_ioutil.cpp 44808 2010-02-18 16:10:58Z boratyng $
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 <ncbi_pch.hpp>
37 
38 #include <corelib/ncbistre.hpp>
39 #include <assert.h>
40 #include <math.h>
41 #include <stdlib.h>
42 
43 #include "njn_ioutil.hpp"
44 
46 USING_SCOPE(blast);
47 
49 USING_SCOPE(IoUtil);
50 
51 
52  static const Format FORMAT = HUMAN;
53  static Format format = HUMAN;
54 
55  static const char TERMINATOR = '!';
56  static char terminator = TERMINATOR;
57 
58 
60 {
61  return format;
62 }
63 
64 void IoUtil::setFormat (Format format_)
65 {
66  format = format_;
67 }
68 
70 {
71  return format = FORMAT;
72 }
73 
75 {
76  return terminator;
77 }
78 
79 void IoUtil::setTerminator (char t_)
80 {
81  terminator = t_;
82 }
83 
85 {
86  return terminator = TERMINATOR;
87 }
88 
89 void IoUtil::abort ()
90 {
91  exit (1);
92 }
93 
94 void IoUtil::abort (const string &s_)
95 {
96  NcbiCout << s_ << endl;
97  IoUtil::abort ();
98 }
99 
100 istream &IoUtil::getLine (
101 istream &in_, // input filestream
102 string &str_, // string before '!' from next line
103 const char t_) // termination character (could be '\n')
104 { // behaves like getline (in_, str_) but throws away the string after first character t_ && skips lines of white space
105 
106  assert (t_ != '\0');
107 
108  if (! in_) return in_;
109 
110  const char *pbuf = 0;
111 
112  while (getline (in_, str_)) {
113  for (pbuf = str_.c_str (); *pbuf != '\0' && isspace (*pbuf); pbuf++); // advance past white space
114  if (*pbuf != '\0' && *pbuf != t_) break; // skip lines full of white space
115  }
116 
117  if (t_ != '\n') {
118  size_t pos = str_.find (t_, 0);
119  if (pos < str_.size ()) str_.erase (pos, str_.size ());
120  }
121 
122  return in_; // buf is empty and eof is reached
123 }
124 
125 istream &IoUtil::getLine (
126 istream &in_, // input filestream
127 stringstream &sstr_, // sstr_ contains the string before '!' from next line
128 const char t_) // termination character (could be '\n')
129 { // behaves like getline (in_, str_) but throws away the string after first character t_ && skips lines of white space
130 
131  string s;
132 
133  IoUtil::getLine (in_, s, t_);
134  sstr_.clear ();
135  sstr_.str ("");
136  sstr_ << s;
137  sstr_.clear ();
138 
139  return in_;
140 }
141 
142 std::istream &IoUtil::in (
143 std::istream &in_,
144 double &x_)
145 {
146  string s;
147  in_ >> s;
148 
149  for (string::iterator i = s.begin (); i != s.end (); i++) *i = tolower (*i);
150 
151  if (s == "1.#inf")
152  {
153  x_ = HUGE_VAL;
154  }
155  else if (s == "nan")
156  {
157  x_ = HUGE_VAL;
158  }
159  else
160  {
161  stringstream str (s);
162  str >> x_;
163 
164  if (str.fail ()) in_.setstate (ios_base::failbit);
165  }
166 
167  return in_;
168 }
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NcbiCout
Definition: ncbistre.hpp:543
exit(2)
int i
int isspace(Uchar c)
Definition: ncbictype.hpp:69
int tolower(Uchar c)
Definition: ncbictype.hpp:72
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
static char terminator
Definition: njn_ioutil.cpp:56
USING_SCOPE(blast)
static Format format
Definition: njn_ioutil.cpp:53
static const Format FORMAT
Definition: njn_ioutil.cpp:52
static const char TERMINATOR
Definition: njn_ioutil.cpp:55
USING_NCBI_SCOPE
Definition: njn_ioutil.cpp:45
char getTerminator()
Definition: njn_ioutil.cpp:74
void setFormat(Format format_)
Definition: njn_ioutil.cpp:64
char clearTerminator()
Definition: njn_ioutil.cpp:84
Format clearFormat()
Definition: njn_ioutil.cpp:69
std::istream & in(std::istream &in_, double &x_)
std::istream & getLine(std::istream &in_, std::string &str_, const char t_=getTerminator())
Format
Definition: njn_ioutil.hpp:52
@ HUMAN
Definition: njn_ioutil.hpp:52
void setTerminator(char t_)
Definition: njn_ioutil.cpp:79
void abort()
Format getFormat()
Definition: njn_ioutil.cpp:59
#define assert(x)
Definition: srv_diag.hpp:58
Modified on Mon May 27 04:36:35 2024 by modify_doxy.py rev. 669887