NCBI C++ ToolKit
njn_ioutil.cpp
Go to the documentation of this file.
00001 /* $Id: njn_ioutil.cpp 44808 2010-02-18 16:10:58Z boratyng $
00002 * ===========================================================================
00003 *
00004 *                            PUBLIC DOMAIN NOTICE
00005 *               National Center for Biotechnology Information
00006 *
00007 *  This software/database is a "United States Government Work" under the
00008 *  terms of the United States Copyright Act.  It was written as part of
00009 *  the author's offical duties as a United States Government employee and
00010 *  thus cannot be copyrighted.  This software/database is freely available
00011 *  to the public for use. The National Library of Medicine and the U.S.
00012 *  Government have not placed any restriction on its use or reproduction.
00013 *
00014 *  Although all reasonable efforts have been taken to ensure the accuracy
00015 *  and reliability of the software and data, the NLM and the U.S.
00016 *  Government do not and cannot warrant the performance or results that
00017 *  may be obtained by using this software or data. The NLM and the U.S.
00018 *  Government disclaim all warranties, express or implied, including
00019 *  warranties of performance, merchantability or fitness for any particular
00020 *  purpose.
00021 *
00022 *  Please cite the author in any work or product based on this material.
00023 *
00024 * ===========================================================================*/
00025 
00026 /*****************************************************************************
00027 
00028 File name: njn_ioutil.cpp
00029 
00030 Author: John Spouge
00031 
00032 Contents: 
00033 
00034 ******************************************************************************/
00035 
00036 #include <ncbi_pch.hpp>
00037 
00038 #include <corelib/ncbistre.hpp>
00039 #include <assert.h>
00040 #include <math.h>
00041 #include <stdlib.h>
00042 
00043 #include "njn_ioutil.hpp"
00044 
00045 USING_NCBI_SCOPE;
00046 USING_SCOPE(blast);
00047 
00048 USING_SCOPE(Njn);
00049 USING_SCOPE(IoUtil);
00050 
00051 
00052    static const Format FORMAT = HUMAN;
00053    static Format format = HUMAN;
00054 
00055    static const char TERMINATOR = '!';
00056    static char terminator = TERMINATOR;
00057 
00058 
00059 Format IoUtil::getFormat ()
00060 {
00061    return format;
00062 }
00063 
00064 void IoUtil::setFormat (Format format_)
00065 {
00066    format = format_;
00067 }
00068 
00069 Format IoUtil::clearFormat ()
00070 {
00071    return format = FORMAT;
00072 }
00073 
00074 char IoUtil::getTerminator ()
00075 {
00076    return terminator;
00077 }
00078 
00079 void IoUtil::setTerminator (char t_)
00080 {
00081    terminator = t_;
00082 }
00083 
00084 char IoUtil::clearTerminator ()
00085 {
00086    return terminator = TERMINATOR;
00087 }
00088 
00089 void IoUtil::abort ()
00090 {
00091    exit (1);
00092 }
00093 
00094 void IoUtil::abort (const string &s_)
00095 {
00096    NcbiCout << s_ << endl;
00097    IoUtil::abort ();
00098 }
00099 
00100 istream &IoUtil::getLine ( 
00101 istream &in_, // input filestream
00102 string &str_, // string before '!' from next line
00103 const char t_) // termination character (could be '\n')
00104 { // behaves like getline (in_, str_) but throws away the string after first character t_ && skips lines of white space
00105 
00106    assert (t_ != '\0');
00107 
00108    if (! in_) return in_;
00109 
00110    const char *pbuf = 0;
00111 
00112    while (getline (in_, str_)) {
00113       for (pbuf = str_.c_str (); *pbuf != '\0' && isspace (*pbuf); pbuf++); // advance past white space
00114       if (*pbuf != '\0' && *pbuf != t_) break; // skip lines full of white space
00115    }
00116 
00117    if (t_ != '\n') {
00118       size_t pos = str_.find (t_, 0);
00119       if (pos < str_.size ()) str_.erase (pos, str_.size ());
00120    }
00121 
00122    return in_; // buf is empty and eof is reached 
00123 }
00124 
00125 istream &IoUtil::getLine ( 
00126 istream &in_, // input filestream
00127 stringstream &sstr_, // sstr_ contains the string before '!' from next line
00128 const char t_) // termination character (could be '\n')
00129 { // behaves like getline (in_, str_) but throws away the string after first character t_ && skips lines of white space
00130 
00131    string s;
00132 
00133    IoUtil::getLine (in_, s, t_);
00134    sstr_.clear ();
00135    sstr_.str ("");
00136    sstr_ << s;
00137    sstr_.clear ();
00138 
00139    return in_;
00140 }
00141 
00142 std::istream &IoUtil::in (
00143 std::istream &in_,
00144 double &x_)
00145 {
00146     string s;
00147     in_ >> s;
00148 
00149     for (string::iterator i = s.begin (); i != s.end (); i++) *i = tolower (*i);
00150 
00151     if (s == "1.#inf")
00152     {
00153         x_ = HUGE_VAL;   
00154     }
00155     else if (s == "nan")
00156     {
00157         x_ = HUGE_VAL;   
00158     }
00159     else
00160     {
00161         stringstream str (s);
00162         str >> x_;
00163 
00164         if (str.fail ()) in_.setstate (ios_base::failbit);
00165     }
00166 
00167     return in_;
00168 }
Modified on Wed May 23 13:06:29 2012 by modify_doxy.py rev. 337098