libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
textstream.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdio>
11#include <cstdint>
13#include <promeki/core/string.h>
15
17
18class IODevice;
19class Buffer;
20
55 public:
62
69
76
86
95 explicit TextStream(Buffer *buffer);
96
105 explicit TextStream(String *string);
106
116 explicit TextStream(FILE *file);
117
120
121 // ============================================================
122 // Status
123 // ============================================================
124
129 Status status() const { return _status; }
130
134 void resetStatus() { _status = Ok; }
135
140 bool atEnd() const;
141
149 void flush();
150
155 IODevice *device() const { return _device; }
156
157 // ============================================================
158 // Encoding
159 // ============================================================
160
169
174 String encoding() const { return _encoding; }
175
176 // ============================================================
177 // Formatting controls
178 // ============================================================
179
189 void setFieldWidth(int width) { _fieldWidth = width; }
190
195 int fieldWidth() const { return _fieldWidth; }
196
201 void setFieldAlignment(FieldAlignment align) { _fieldAlignment = align; }
202
207 FieldAlignment fieldAlignment() const { return _fieldAlignment; }
208
213 void setPadChar(char c) { _padChar = c; }
214
219 char padChar() const { return _padChar; }
220
228 void setIntegerBase(int base) { _integerBase = base; }
229
234 int integerBase() const { return _integerBase; }
235
240 void setRealNumberPrecision(int precision) { _realNumberPrecision = precision; }
241
246 int realNumberPrecision() const { return _realNumberPrecision; }
247
253
258 RealNumberNotation realNumberNotation() const { return _realNumberNotation; }
259
260 // ============================================================
261 // Write operators
262 // ============================================================
263
267 TextStream &operator<<(const char *val);
273 TextStream &operator<<(unsigned int val);
286
289
290 // ============================================================
291 // Read operators
292 // ============================================================
293
304
305 // ============================================================
306 // Read methods
307 // ============================================================
308
317
323
330
331 private:
336 void writeString(const String &str);
337
343 String applyPadding(const String &str);
344
350 bool readChar(char &ch);
351
360 void unreadChar(char ch);
361
365 void skipWhitespace();
366
371 String readToken();
372
373 IODevice *_device = nullptr;
374 IODevice *_ownedDevice = nullptr;
375
376 // Encoding
377 String _encoding{"UTF-8"};
378
379 // Formatting state
380 int _fieldWidth = 0;
381 FieldAlignment _fieldAlignment = Right;
382 char _padChar = ' ';
383 int _integerBase = 10;
384 int _realNumberPrecision = 6;
385 RealNumberNotation _realNumberNotation = SmartNotation;
386
387 // Status
388 Status _status = Ok;
389};
390
391// ============================================================================
392// Manipulators
393// ============================================================================
394
397 s << '\n';
398 s.flush();
399 return s;
400}
401
404 s.flush();
405 return s;
406}
407
410 s.setIntegerBase(16);
411 return s;
412}
413
416 s.setIntegerBase(10);
417 return s;
418}
419
422 s.setIntegerBase(8);
423 return s;
424}
425
428 s.setIntegerBase(2);
429 return s;
430}
431
434 s.setRealNumberNotation(TextStream::Fixed);
435 return s;
436}
437
440 s.setRealNumberNotation(TextStream::Scientific);
441 return s;
442}
443
446 s.setFieldAlignment(TextStream::Left);
447 return s;
448}
449
452 s.setFieldAlignment(TextStream::Right);
453 return s;
454}
455
458 s.setFieldAlignment(TextStream::Center);
459 return s;
460}
461
Generic memory buffer descriptor with alignment and memory space support.
Definition buffer.h:85
Abstract base class for all I/O devices.
Definition iodevice.h:29
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Formatted text I/O with encoding awareness.
Definition textstream.h:54
TextStream & operator<<(bool val)
Writes "true" or "false".
void setFieldWidth(int width)
Sets the minimum field width for formatted output.
Definition textstream.h:189
void resetStatus()
Resets the stream status to Ok.
Definition textstream.h:134
String encoding() const
Returns the current encoding name.
Definition textstream.h:174
RealNumberNotation
Floating-point notation styles.
Definition textstream.h:71
@ SmartNotation
Use fixed or scientific as appropriate (default).
Definition textstream.h:72
@ Fixed
Always use fixed-point notation.
Definition textstream.h:73
@ Scientific
Always use scientific notation.
Definition textstream.h:74
TextStream & operator>>(int64_t &val)
Reads an int64_t from text.
TextStream(Buffer *buffer)
Constructs a TextStream backed by a Buffer.
void setRealNumberNotation(RealNumberNotation notation)
Sets the floating-point notation style.
Definition textstream.h:252
RealNumberNotation realNumberNotation() const
Returns the current real number notation.
Definition textstream.h:258
int fieldWidth() const
Returns the current field width.
Definition textstream.h:195
String read(size_t maxLength)
Reads up to maxLength characters.
TextStream & operator<<(char val)
Writes a single character.
TextStream & operator<<(double val)
Writes a double as formatted text.
int realNumberPrecision() const
Returns the current real number precision.
Definition textstream.h:246
TextStream & operator>>(double &val)
Reads a double from text.
TextStream & operator<<(TextStream &(*manip)(TextStream &))
Writes a manipulator function.
TextStream & operator<<(const String &val)
Writes a String.
void setIntegerBase(int base)
Sets the integer base for formatted output.
Definition textstream.h:228
FieldAlignment
Field alignment for padded output.
Definition textstream.h:64
@ Center
Center-aligned (pad both sides).
Definition textstream.h:67
@ Right
Right-aligned (pad on left, default).
Definition textstream.h:66
@ Left
Left-aligned (pad on right).
Definition textstream.h:65
String readAll()
Reads all remaining text.
TextStream(IODevice *device)
Constructs a TextStream on an IODevice.
void flush()
Flushes any buffered output to the underlying device.
TextStream & operator<<(int val)
Writes an int as formatted decimal text.
TextStream & operator>>(int &val)
Reads an int from text.
TextStream & operator<<(uint64_t val)
Writes a uint64_t as formatted text.
TextStream & operator<<(unsigned int val)
Writes an unsigned int as formatted decimal text.
TextStream & operator<<(int64_t val)
Writes an int64_t as formatted text.
IODevice * device() const
Returns the underlying IODevice.
Definition textstream.h:155
void setPadChar(char c)
Sets the padding character used for field width.
Definition textstream.h:213
Status status() const
Returns the current stream status.
Definition textstream.h:129
void setEncoding(const String &encoding)
Sets the text encoding for read/write operations.
bool atEnd() const
Returns true if at the end of the stream.
int integerBase() const
Returns the current integer base.
Definition textstream.h:234
~TextStream()
Destructor.
FieldAlignment fieldAlignment() const
Returns the current field alignment.
Definition textstream.h:207
TextStream & operator<<(float val)
Writes a float as formatted text.
TextStream & operator<<(const Variant &val)
Writes a Variant using its toString() representation.
TextStream & operator>>(String &val)
Reads a whitespace-delimited token into a String.
TextStream(FILE *file)
Constructs a TextStream wrapping a C stdio FILE.
TextStream & operator>>(char &val)
Reads a single character.
String readLine()
Reads one line of text, consuming the trailing newline.
char padChar() const
Returns the current pad character.
Definition textstream.h:219
Status
Stream status codes.
Definition textstream.h:57
@ ReadPastEnd
Attempted to read beyond available data.
Definition textstream.h:59
@ WriteFailed
A write operation failed.
Definition textstream.h:60
@ Ok
No error.
Definition textstream.h:58
void setFieldAlignment(FieldAlignment align)
Sets the field alignment.
Definition textstream.h:201
TextStream(String *string)
Constructs a TextStream backed by a String.
TextStream & operator<<(const char *val)
Writes a C string.
void setRealNumberPrecision(int precision)
Sets the number of decimal places for float/double output.
Definition textstream.h:240
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
TextStream & endl(TextStream &s)
Writes a newline and flushes the stream.
Definition textstream.h:396
TextStream & right(TextStream &s)
Sets field alignment to Right.
Definition textstream.h:451
TextStream & oct(TextStream &s)
Sets integer base to 8 (octal).
Definition textstream.h:421
TextStream & fixed(TextStream &s)
Sets real number notation to Fixed.
Definition textstream.h:433
TextStream & left(TextStream &s)
Sets field alignment to Left.
Definition textstream.h:445
TextStream & center(TextStream &s)
Sets field alignment to Center.
Definition textstream.h:457
TextStream & scientific(TextStream &s)
Sets real number notation to Scientific.
Definition textstream.h:439
TextStream & hex(TextStream &s)
Sets integer base to 16 (hexadecimal).
Definition textstream.h:409
TextStream & dec(TextStream &s)
Sets integer base to 10 (decimal).
Definition textstream.h:415
TextStream & bin(TextStream &s)
Sets integer base to 2 (binary).
Definition textstream.h:427
TextStream & flush(TextStream &s)
Flushes the stream.
Definition textstream.h:403