00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <ctype.h>
00020
00021 #include "Riostream.h"
00022 #include "TString.h"
00023
00024
00025
00026 istream& TString::ReadFile(istream& strm)
00027 {
00028
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;
00038
00039
00040
00041
00042 Capacity(Length() + GetResizeIncrement());
00043 }
00044
00045 fData[Length()] = '\0';
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
00057
00058 if (skipWhite)
00059 strm >> ws;
00060
00061 return ReadToDelim(strm, '\n');
00062 }
00063
00064
00065 istream& TString::ReadString(istream& strm)
00066 {
00067
00068
00069 return ReadToDelim(strm, '\0');
00070 }
00071
00072
00073 istream& TString::ReadToDelim(istream& strm, char delim)
00074 {
00075
00076
00077
00078
00079
00080
00081
00082 Clobber(GetInitialCapacity());
00083 int p = strm.peek();
00084 if (p == delim) {
00085 strm.get();
00086 } else {
00087 while (1) {
00088 strm.get(fData+Length(),
00089 Capacity()-Length()+1,
00090 delim);
00091 Pref()->fNchars += strm.gcount();
00092 if (!strm.good()) break;
00093 p = strm.peek();
00094 if (p == delim) {
00095 strm.get();
00096 break;
00097 }
00098
00099 Capacity(Length() + GetResizeIncrement());
00100 }
00101 }
00102
00103 fData[Length()] = '\0';
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
00115
00116 Clobber(GetInitialCapacity());
00117
00118 strm >> ws;
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
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';
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
00146
00147 return s.ReadToken(strm);
00148 }
00149
00150
00151 ostream& operator<<(ostream& os, const TString& s)
00152 {
00153
00154
00155 if (os.good()) {
00156 if (os.tie()) os.tie()->flush();
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 << "";
00164 os.write((char*)s.Data(), s.Length());
00165 if (wid && (flags & ios::left))
00166 os << "";
00167 }
00168
00169 if (os.flags() & ios::unitbuf)
00170 os.flush();
00171 return os;
00172 }
00173
00174
00175
00176
00177 Bool_t TString::Gets(FILE *fp, Bool_t chop)
00178 {
00179
00180
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
00202
00203 fputs(Data(), fp);
00204 }