Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Stringio.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Fons Rademakers 04/08/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//////////////////////////////////////////////////////////////////////////
13// //
14// TString Input/Output functions, put here so the linker will include //
15// them only if I/O is done. //
16// //
17//////////////////////////////////////////////////////////////////////////
18
19#include <cctype> // Looking for isspace()
20#include <iostream>
21
22#include "TString.h"
23
24
25////////////////////////////////////////////////////////////////////////////////
26/// Replace string with the contents of strm, stopping at an EOF.
27
28std::istream& TString::ReadFile(std::istream& strm)
29{
30 // get file size
31 Ssiz_t end, cur = strm.tellg();
32 strm.seekg(0, std::ios::end);
33 end = strm.tellg();
34 strm.seekg(cur);
35
36 // any positive number of reasonable size for a file
37 const Ssiz_t incr = 256;
38
39 Clobber(end-cur);
40
41 while(1) {
42 Ssiz_t len = Length();
43 Ssiz_t cap = Capacity();
44 strm.read(GetPointer()+len, cap-len);
45 SetSize(len + strm.gcount());
46
47 if (!strm.good())
48 break; // EOF encountered
49
50 // If we got here, the read must have stopped because
51 // the buffer was going to overflow. Resize and keep
52 // going.
53 cap = AdjustCapacity(cap, cap+incr);
54 Capacity(cap);
55 }
56
57 GetPointer()[Length()] = '\0'; // Add null terminator
58
59 return strm;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Read a line from stream upto newline skipping any whitespace.
64
65std::istream& TString::ReadLine(std::istream& strm, Bool_t skipWhite)
66{
67 if (skipWhite)
68 strm >> std::ws;
69
70 return ReadToDelim(strm, '\n');
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// Read a line from stream upto \0, including any newline.
75
76std::istream& TString::ReadString(std::istream& strm)
77{
78 return ReadToDelim(strm, '\0');
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Read up to an EOF, or a delimiting character, whichever comes
83/// first. The delimiter is not stored in the string,
84/// but is removed from the input stream.
85/// Because we don't know how big a string to expect, we first read
86/// as much as we can and then, if the EOF or null hasn't been
87/// encountered, do a resize and keep reading.
88
89std::istream& TString::ReadToDelim(std::istream& strm, char delim)
90{
91 // any positive number of reasonable size for a string
92 const Ssiz_t incr = 32;
93
94 Clobber(incr);
95
96 int p = strm.peek(); // Check if we are already at delim
97 if (p == delim) {
98 strm.get(); // eat the delimiter, and return \0.
99 } else {
100 while (1) {
101 Ssiz_t len = Length();
102 Ssiz_t cap = Capacity();
103 strm.get(GetPointer()+len, // Address of next byte
104 cap-len+1, // Space available (+1 for terminator)
105 delim); // Delimiter
106 SetSize(len + strm.gcount());
107 if (!strm.good()) break; // Check for EOF or stream failure
108 p = strm.peek();
109 if (p == delim) { // Check for delimiter
110 strm.get(); // eat the delimiter.
111 break;
112 }
113 // Delimiter not seen. Resize and keep going:
114 cap = AdjustCapacity(cap, cap+incr);
115 Capacity(cap);
116 }
117 }
118
119 GetPointer()[Length()] = '\0'; // Add null terminator
120
121 return strm;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Read a token, delimited by whitespace, from the input stream.
126
127std::istream& TString::ReadToken(std::istream& strm)
128{
129 // any positive number of reasonable size for a token
130 const Ssiz_t incr = 16;
131
132 Clobber(incr);
133
134 strm >> std::ws; // Eat whitespace
135
136 UInt_t wid = strm.width(0);
137 char c='\0';
138 Int_t hitSpace = 0;
139 while ((wid == 0 || Length() < (Int_t)wid) &&
140 strm.get(c).good() && (hitSpace = isspace((Int_t)c)) == 0) {
141 // Check for overflow:
142 Ssiz_t len = Length();
143 Ssiz_t cap = Capacity();
144 if (len == cap) {
145 cap = AdjustCapacity(cap, cap+incr);
146 Capacity(cap);
147 }
148 GetPointer()[len] = c;
149 len++;
150 SetSize(len);
151 }
152 if (hitSpace)
153 strm.putback(c);
154
155 GetPointer()[Length()] = '\0'; // Add null terminator
156
157 return strm;
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Read string from stream.
162
163std::istream& operator>>(std::istream& strm, TString& s)
164{
165 return s.ReadToken(strm);
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Write string to stream.
170
171std::ostream& operator<<(std::ostream& os, const TString& s)
172{
173 if (os.good()) {
174 if (os.tie()) os.tie()->flush(); // instead of opfx
175 UInt_t len = s.Length();
176 UInt_t wid = os.width();
177 wid = (len < wid) ? wid - len : 0;
178 os.width(wid);
179 long flags = os.flags();
180 if (wid && !(flags & std::ios::left))
181 os << ""; // let the std::ostream fill
182 os.write((char*)s.Data(), s.Length());
183 if (wid && (flags & std::ios::left))
184 os << ""; // let the std::ostream fill
185 }
186 // instead of os.osfx();
187 if (os.flags() & std::ios::unitbuf)
188 os.flush();
189 return os;
190}
191
192// ------------------- C I/O ------------------------------------
193
194////////////////////////////////////////////////////////////////////////////////
195/// Read one line from the stream, including the `\n`, or until EOF.
196/// Remove the trailing `[\r]\n` if chop is true. Returns kTRUE if data was read.
197
199{
200 char buf[256];
201 Bool_t r = kFALSE;
202
203 Clobber(256);
204
205 do {
206 if (fgets(buf, sizeof(buf), fp) == nullptr) break;
207 *this += buf;
208 r = kTRUE;
209 } while (!ferror(fp) && !feof(fp) && strchr(buf,'\n') == nullptr);
210
211 if (chop && EndsWith("\n")) {
212 Chop();
213 if (EndsWith("\r"))
214 Chop();
215 }
216
217 return r;
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Write string to the stream.
222
223void TString::Puts(FILE *fp)
224{
225 fputs(GetPointer(), fp);
226}
#define c(i)
Definition RSha256.hxx:101
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
std::istream & operator>>(std::istream &strm, TString &s)
Read string from stream.
Definition Stringio.cxx:163
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize wid
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
Bool_t Gets(FILE *fp, Bool_t chop=kTRUE)
Read one line from the stream, including the \n, or until EOF.
Definition Stringio.cxx:198
std::istream & ReadToDelim(std::istream &str, char delim='\n')
Read up to an EOF, or a delimiting character, whichever comes first.
Definition Stringio.cxx:89
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2222
const char * Data() const
Definition TString.h:380
static TString * ReadString(TBuffer &b, const TClass *clReq)
Read TString object from buffer.
Definition TString.cxx:1340
TString & Chop()
Definition TString.h:691
Ssiz_t Capacity() const
Definition TString.h:368
static Ssiz_t AdjustCapacity(Ssiz_t oldCap, Ssiz_t newCap)
Calculate a nice capacity greater than or equal to newCap.
Definition TString.cxx:1206
void Puts(FILE *fp)
Write string to the stream.
Definition Stringio.cxx:223
std::istream & ReadFile(std::istream &str)
Replace string with the contents of strm, stopping at an EOF.
Definition Stringio.cxx:28
void SetSize(Ssiz_t s)
Definition TString.h:250
std::istream & ReadToken(std::istream &str)
Read a token, delimited by whitespace, from the input stream.
Definition Stringio.cxx:127
char * GetPointer()
Definition TString.h:258
void Clobber(Ssiz_t nc)
Clear string and make sure it has a capacity of nc.
Definition TString.cxx:1229
std::istream & ReadLine(std::istream &str, Bool_t skipWhite=kTRUE)
Read a line from stream upto newline skipping any whitespace.
Definition Stringio.cxx:65