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#include "TError.h"
24
25
26////////////////////////////////////////////////////////////////////////////////
27/// Replace string with the contents of strm, stopping at an EOF.
28
29std::istream& TString::ReadFile(std::istream& strm)
30{
31 // get file size
32 auto cur = strm.tellg();
33 strm.seekg(0, std::ios::end);
34 auto end = strm.tellg();
35 strm.seekg(cur);
36
37 // any positive number of reasonable size for a file
38 const Ssiz_t incr = 256;
39
40 Long64_t fileSize = end - cur;
41 if(fileSize > MaxSize()) {
42 Error("TString::ReadFile", "file size too large (%lld, max = %d), clamping", fileSize, MaxSize());
43 fileSize = MaxSize();
44 }
45 Clobber(fileSize);
46
47 while(1) {
48 Ssiz_t len = Length();
49 Ssiz_t cap = Capacity();
50 strm.read(GetPointer()+len, cap-len);
51 SetSize(len + strm.gcount());
52
53 if (!strm.good())
54 break; // EOF encountered
55
56 // If we got here, the read must have stopped because
57 // the buffer was going to overflow. Resize and keep
58 // going.
59 cap = AdjustCapacity(cap, cap+incr);
60 Capacity(cap);
61 }
62
63 GetPointer()[Length()] = '\0'; // Add null terminator
64
65 return strm;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Read a line from stream upto newline skipping any whitespace.
70
71std::istream& TString::ReadLine(std::istream& strm, Bool_t skipWhite)
72{
73 if (skipWhite)
74 strm >> std::ws;
75
76 return ReadToDelim(strm, '\n');
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Read a line from stream upto \0, including any newline.
81
82std::istream& TString::ReadString(std::istream& strm)
83{
84 return ReadToDelim(strm, '\0');
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Read up to an EOF, or a delimiting character, whichever comes
89/// first. The delimiter is not stored in the string,
90/// but is removed from the input stream.
91/// Because we don't know how big a string to expect, we first read
92/// as much as we can and then, if the EOF or null hasn't been
93/// encountered, do a resize and keep reading.
94
95std::istream& TString::ReadToDelim(std::istream& strm, char delim)
96{
97 // any positive number of reasonable size for a string
98 const Ssiz_t incr = 32;
99
100 Clobber(incr);
101
102 int p = strm.peek(); // Check if we are already at delim
103 if (p == delim) {
104 strm.get(); // eat the delimiter, and return \0.
105 } else {
106 while (1) {
107 Ssiz_t len = Length();
108 Ssiz_t cap = Capacity();
109 strm.get(GetPointer()+len, // Address of next byte
110 cap-len+1, // Space available (+1 for terminator)
111 delim); // Delimiter
112 SetSize(len + strm.gcount());
113 if (!strm.good()) break; // Check for EOF or stream failure
114 p = strm.peek();
115 if (p == delim) { // Check for delimiter
116 strm.get(); // eat the delimiter.
117 break;
118 }
119 // Delimiter not seen. Resize and keep going:
120 cap = AdjustCapacity(cap, cap+incr);
121 Capacity(cap);
122 }
123 }
124
125 GetPointer()[Length()] = '\0'; // Add null terminator
126
127 return strm;
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Read a token, delimited by whitespace, from the input stream.
132
133std::istream& TString::ReadToken(std::istream& strm)
134{
135 // any positive number of reasonable size for a token
136 const Ssiz_t incr = 16;
137
138 Clobber(incr);
139
140 strm >> std::ws; // Eat whitespace
141
142 UInt_t wid = strm.width(0);
143 char c='\0';
144 Int_t hitSpace = 0;
145 while ((wid == 0 || Length() < (Int_t)wid) && strm.get(c).good() &&
146 (hitSpace = isspace(static_cast<unsigned char>(c))) == 0) {
147 // Check for overflow:
148 Ssiz_t len = Length();
149 Ssiz_t cap = Capacity();
150 if (len == cap) {
151 cap = AdjustCapacity(cap, cap+incr);
152 Capacity(cap);
153 }
154 GetPointer()[len] = c;
155 len++;
156 SetSize(len);
157 }
158 if (hitSpace)
159 strm.putback(c);
160
161 GetPointer()[Length()] = '\0'; // Add null terminator
162
163 return strm;
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Read string from stream.
168
169std::istream& operator>>(std::istream& strm, TString& s)
170{
171 return s.ReadToken(strm);
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// Write string to stream.
176
177std::ostream& operator<<(std::ostream& os, const TString& s)
178{
179 if (os.good()) {
180 if (os.tie()) os.tie()->flush(); // instead of opfx
181 UInt_t len = s.Length();
182 UInt_t wid = os.width();
183 wid = (len < wid) ? wid - len : 0;
184 os.width(wid);
185 long flags = os.flags();
186 if (wid && !(flags & std::ios::left))
187 os << ""; // let the std::ostream fill
188 os.write((char*)s.Data(), s.Length());
189 if (wid && (flags & std::ios::left))
190 os << ""; // let the std::ostream fill
191 }
192 // instead of os.osfx();
193 if (os.flags() & std::ios::unitbuf)
194 os.flush();
195 return os;
196}
197
198// ------------------- C I/O ------------------------------------
199
200////////////////////////////////////////////////////////////////////////////////
201/// Read one line from the stream, including the `\n`, or until EOF.
202/// Remove the trailing `[\r]\n` if chop is true. Returns kTRUE if data was read.
203
205{
206 char buf[256];
207 Bool_t r = kFALSE;
208
209 Clobber(256);
210
211 do {
212 if (fgets(buf, sizeof(buf), fp) == nullptr) break;
213 *this += buf;
214 r = kTRUE;
215 } while (!ferror(fp) && !feof(fp) && strchr(buf,'\n') == nullptr);
216
217 if (chop && EndsWith("\n")) {
218 Chop();
219 if (EndsWith("\r"))
220 Chop();
221 }
222
223 return r;
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Write string to the stream.
228
229void TString::Puts(FILE *fp)
230{
231 fputs(GetPointer(), fp);
232}
#define c(i)
Definition RSha256.hxx:101
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:397
std::istream & operator>>(std::istream &strm, TString &s)
Read string from stream.
Definition Stringio.cxx:169
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
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:417
Bool_t Gets(FILE *fp, Bool_t chop=kTRUE)
Read one line from the stream, including the \n, or until EOF.
Definition Stringio.cxx:204
std::istream & ReadToDelim(std::istream &str, char delim='\n')
Read up to an EOF, or a delimiting character, whichever comes first.
Definition Stringio.cxx:95
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2244
const char * Data() const
Definition TString.h:376
static TString * ReadString(TBuffer &b, const TClass *clReq)
Read TString object from buffer.
Definition TString.cxx:1362
TString & Chop()
Definition TString.h:691
Ssiz_t Capacity() const
Definition TString.h:364
static Ssiz_t AdjustCapacity(Ssiz_t oldCap, Ssiz_t newCap)
Calculate a nice capacity greater than or equal to newCap.
Definition TString.cxx:1220
void Puts(FILE *fp)
Write string to the stream.
Definition Stringio.cxx:229
std::istream & ReadFile(std::istream &str)
Replace string with the contents of strm, stopping at an EOF.
Definition Stringio.cxx:29
static Ssiz_t MaxSize()
Definition TString.h:261
Ssiz_t Clobber(Ssiz_t nc)
Clear string and make sure it has a capacity of nc.
Definition TString.cxx:1246
void SetSize(Ssiz_t s)
Definition TString.h:248
std::istream & ReadToken(std::istream &str)
Read a token, delimited by whitespace, from the input stream.
Definition Stringio.cxx:133
char * GetPointer()
Definition TString.h:256
std::istream & ReadLine(std::istream &str, Bool_t skipWhite=kTRUE)
Read a line from stream upto newline skipping any whitespace.
Definition Stringio.cxx:71