Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
qx-textstreamwriter.h
1#ifndef QX_TEXTSTREAMWRITER_H
2#define QX_TEXTSTREAMWRITER_H
3
4// Shared Lib Support
5#include "qx/io/qx_io_export.h"
6
7// Qt Includes
8#include <QTextStream>
9#include <QFile>
10
11// Intra-component Includes
12#include "qx/io/qx-common-io.h"
13#include "qx/io/qx-ioopreport.h"
14
15namespace Qx
16{
17
18class QX_IO_EXPORT TextStreamWriter
19{
20//-Class Variables------------------------------------------------------------------------------------------------
21private:
22 static inline const IoOpReport NULL_FILE_REPORT = IoOpReport(IO_OP_WRITE, IO_ERR_NULL, static_cast<QFile*>(nullptr));
23
24//-Instance Variables------------------------------------------------------------------------------------------------
25private:
26 QFile* mFile;
27 QTextStream mStreamWriter;
28 WriteMode mWriteMode;
29 WriteOptions mWriteOptions;
30 bool mAtLineStart;
31 IoOpReport mStatus;
32
33//-Constructor-------------------------------------------------------------------------------------------------------
34public:
35 TextStreamWriter(WriteMode writeMode = Append, WriteOptions writeOptions = NoWriteOptions);
36 TextStreamWriter(const QString& filePath, WriteMode writeMode = Append, WriteOptions writeOptions = NoWriteOptions);
37
38//-Destructor-------------------------------------------------------------------------------------------------------
39public:
41
42//-Instance Functions------------------------------------------------------------------------------------------------
43private:
44 IoOpReport statusFromNative();
45 IoOpReport preWriteErrorCheck();
46 void setFile(const QString& filePath);
47 void unsetFile();
48
49public:
50 // Stock functions
51 QStringConverter::Encoding encoding() const;
52 QTextStream::FieldAlignment fieldAlignment() const;
53 int fieldWidth() const;
54 const QFile* file() const;
55 void flush();
56 bool generateByteOrderMark() const;
57 int integerBase() const;
58 QLocale locale() const;
59 QTextStream::NumberFlags numberFlags() const;
60 QChar padChar() const;
61 QTextStream::RealNumberNotation realNumberNotation() const;
62 int realNumberPrecision() const;
63 void reset();
64 void resetStatus();
65 void setEncoding(QStringConverter::Encoding encoding);
66 void setFieldAlignment(QTextStream::FieldAlignment mode);
67 void setFieldWidth(int width);
68 void setGenerateByteOrderMark(bool generate);
69 void setIntegerBase(int base);
70 void setLocale(const QLocale& locale);
71 void setNumberFlags(QTextStream::NumberFlags flags);
72 void setPadChar(QChar ch);
73 void setRealNumberNotation(QTextStream::RealNumberNotation notation);
74 void setRealNumberPrecision(int precision);
75 IoOpReport status() const;
76
77 template<typename T>
80 {
81 IoOpReport check = preWriteErrorCheck();
82
83 if(!check.isFailure())
84 {
85 mStreamWriter << d;
86 mStatus = statusFromNative();
87 }
88
89 return *this;
90 }
91
92 QString filePath() const;
93 void setFilePath(const QString& filePath);
94
95 // New functions
96 bool hasError() const;
97 IoOpReport writeLine(QString line, bool ensureLineStart = true);
98 IoOpReport writeText(QString text);
99 IoOpReport openFile();
100 void closeFile();
101 bool fileIsOpen() const;
102};
103
104}
105
106#endif // QX_TEXTSTREAMWRITER_H
The IoOpReport class is a container for details regarding the outcome of an IO operation.
Definition qx-ioopreport.h:53
bool isFailure() const
Definition qx-ioopreport.cpp:390
The TextStreamWriter class is a specialized wrapper for QTextStream that narrows and simplifies its u...
Definition qx-textstreamwriter.h:19
TextStreamWriter & operator<<(T d)
Definition qx-textstreamwriter.h:79
Specifies that a type defines a left shift operator for another type.
Definition qx-concepts.h:390
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
@ NoWriteOptions
Definition qx-common-io.h:30
WriteMode
Definition qx-common-io.h:27
@ Append
Definition qx-common-io.h:27
@ IO_ERR_NULL
Definition qx-ioopreport.h:40
@ IO_OP_WRITE
Definition qx-ioopreport.h:20
typedef NumberFlags
The qx-common-io header file provides various types, variables, and functions related to file IO.