Stringio.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: Stringio.cxx 23428 2008-04-23 09:50:35Z brun $
00002 // Author: Fons Rademakers   04/08/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TString Input/Output functions, put here so the linker will include  //
00015 // them only if I/O is done.                                            //
00016 //                                                                      //
00017 //////////////////////////////////////////////////////////////////////////
00018 
00019 #include <ctype.h>              // Looking for isspace()
00020 
00021 #include "Riostream.h"
00022 #include "TString.h"
00023 
00024 
00025 //______________________________________________________________________________
00026 istream& TString::ReadFile(istream& strm)
00027 {
00028    // Replace string with the contents of strm, stopping at an EOF.
00029 
00030    Clobber(GetInitialCapacity());
00031 
00032    while(1) {
00033       strm.read(fData+Length(), Capacity()-Length());
00034       Pref()->fNchars += strm.gcount();
00035 
00036       if (!strm.good())
00037          break;                    // EOF encountered
00038 
00039       // If we got here, the read must have stopped because
00040       // the buffer was going to overflow. Resize and keep
00041       // going.
00042       Capacity(Length() + GetResizeIncrement());
00043    }
00044 
00045    fData[Length()] = '\0';         // Add null terminator
00046 
00047    if (Capacity()-Length() > GetMaxWaste())
00048       Capacity(AdjustCapacity(Capacity()));
00049 
00050    return strm;
00051 }
00052 
00053 //______________________________________________________________________________
00054 istream& TString::ReadLine(istream& strm, Bool_t skipWhite)
00055 {
00056    // Read a line from stream upto newline skipping any whitespace.
00057 
00058    if (skipWhite)
00059       strm >> ws;
00060 
00061    return ReadToDelim(strm, '\n');
00062 }
00063 
00064 //______________________________________________________________________________
00065 istream& TString::ReadString(istream& strm)
00066 {
00067    // Read a line from stream upto \0, including any newline.
00068 
00069    return ReadToDelim(strm, '\0');
00070 }
00071 
00072 //______________________________________________________________________________
00073 istream& TString::ReadToDelim(istream& strm, char delim)
00074 {
00075    // Read up to an EOF, or a delimiting character, whichever comes
00076    // first.  The delimiter is not stored in the string,
00077    // but is removed from the input stream.
00078    // Because we don't know how big a string to expect, we first read
00079    // as much as we can and then, if the EOF or null hasn't been
00080    // encountered, do a resize and keep reading.
00081 
00082    Clobber(GetInitialCapacity());
00083    int p = strm.peek();             // Check if we are already at delim
00084    if (p == delim) {
00085       strm.get();                    // eat the delimiter, and return \0.
00086    } else {
00087       while (1) {
00088          strm.get(fData+Length(),            // Address of next byte
00089                   Capacity()-Length()+1,     // Space available (+1 for terminator)
00090                   delim);                    // Delimiter
00091          Pref()->fNchars += strm.gcount();
00092          if (!strm.good()) break;            // Check for EOF or stream failure
00093          p = strm.peek();
00094          if (p == delim) {                   // Check for delimiter
00095             strm.get();                      // eat the delimiter.
00096             break;
00097          }
00098          // Delimiter not seen.  Resize and keep going:
00099          Capacity(Length() + GetResizeIncrement());
00100       }
00101    }
00102 
00103    fData[Length()] = '\0';                // Add null terminator
00104 
00105    if (Capacity()-Length() > GetMaxWaste())
00106       Capacity(AdjustCapacity(Capacity()));
00107 
00108    return strm;
00109 }
00110 
00111 //______________________________________________________________________________
00112 istream& TString::ReadToken(istream& strm)
00113 {
00114    // Read a token, delimited by whitespace, from the input stream.
00115 
00116    Clobber(GetInitialCapacity());
00117 
00118    strm >> ws;                                   // Eat whitespace
00119 
00120    UInt_t wid = strm.width(0);
00121    char c;
00122    Int_t hitSpace = 0;
00123    while ((wid == 0 || Pref()->fNchars < (Int_t)wid) &&
00124           strm.get(c).good() && (hitSpace = isspace((Int_t)c)) == 0) {
00125       // Check for overflow:
00126       if (Length() == Capacity())
00127          Capacity(Length() + GetResizeIncrement());
00128 
00129       fData[Pref()->fNchars++] = c;
00130    }
00131    if (hitSpace)
00132       strm.putback(c);
00133 
00134    fData[Length()] = '\0';                       // Add null terminator
00135 
00136    if (Capacity()-Length() > GetMaxWaste())
00137       Capacity(AdjustCapacity(Capacity()));
00138 
00139    return strm;
00140 }
00141 
00142 //______________________________________________________________________________
00143 istream& operator>>(istream& strm, TString& s)
00144 {
00145    // Read string from stream.
00146 
00147    return s.ReadToken(strm);
00148 }
00149 
00150 //______________________________________________________________________________
00151 ostream& operator<<(ostream& os, const TString& s)
00152 {
00153    // Write string to stream.
00154 
00155    if (os.good()) {
00156       if (os.tie()) os.tie()->flush(); // instead of opfx
00157       UInt_t len = s.Length();
00158       UInt_t wid = os.width();
00159       wid = (len < wid) ? wid - len : 0;
00160       os.width(wid);
00161       long flags = os.flags();
00162       if (wid && !(flags & ios::left))
00163          os << "";  // let the ostream fill
00164       os.write((char*)s.Data(), s.Length());
00165       if (wid && (flags & ios::left))
00166          os << "";  // let the ostream fill
00167    }
00168    // instead of os.osfx();
00169    if (os.flags() & ios::unitbuf)
00170       os.flush();
00171    return os;
00172 }
00173 
00174 // ------------------- C I/O ------------------------------------
00175 
00176 //______________________________________________________________________________
00177 Bool_t TString::Gets(FILE *fp, Bool_t chop)
00178 {
00179    // Read one line from the stream, including the \n, or until EOF.
00180    // Remove the trailing \n if chop is true. Returns kTRUE if data was read.
00181 
00182    char buf[256];
00183    Bool_t r = kFALSE;
00184 
00185    Clobber(GetInitialCapacity());
00186 
00187    do {
00188       if (fgets(buf, sizeof(buf), fp) == 0) break;
00189       *this += buf;
00190       r = kTRUE;
00191    } while (!ferror(fp) && !feof(fp) && strchr(buf,'\n') == 0);
00192 
00193    if (chop && EndsWith("\n")) Chop();
00194 
00195    return r;
00196 }
00197 
00198 //______________________________________________________________________________
00199 void TString::Puts(FILE *fp)
00200 {
00201    // Write string to the stream.
00202 
00203    fputs(Data(), fp);
00204 }

Generated on Tue Jul 5 14:11:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1