Thea
TextOutputStream.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2013
12 //
13 //============================================================================
14 
15 /*
16  ORIGINAL HEADER
17 
18  @file TextOutputStream.h
19 
20  @maintainer Morgan McGuire, http://graphics.cs.williams.edu
21  @created 2004-06-21
22  @edited 2006-10-24
23 
24  Copyright 2000-2007, Morgan McGuire.
25  All rights reserved.
26 */
27 
28 #ifndef __Thea_TextOutputStream_hpp__
29 #define __Thea_TextOutputStream_hpp__
30 
31 #include "Common.hpp"
32 #include "Array.hpp"
33 #include "NamedObject.hpp"
34 #include "Noncopyable.hpp"
35 #include <cstdarg>
36 
37 namespace Thea {
38 
56 class THEA_API TextOutputStream : public NamedObject, private Noncopyable
57 {
58  public:
69  struct THEA_API WordWrap
70  {
72  enum Value
73  {
74  NONE,
75  WITHOUT_BREAKING,
76  ALWAYS
77  };
78 
79  THEA_ENUM_CLASS_BODY(WordWrap)
80  };
81 
83  struct THEA_API NewlineStyle
84  {
85  enum Value
86  {
87  WINDOWS,
88  UNIX
89  };
90 
91  THEA_ENUM_CLASS_BODY(NewlineStyle);
92  };
93 
95  class THEA_API Settings
96  {
97  public:
100 
103 
106 
109 
115 
118 
120  std::string trueSymbol;
121 
123  std::string falseSymbol;
124 
126  Settings();
127 
129  static Settings const & defaults() { static Settings const s; return s; }
130 
131  }; // class Settings
132 
133  private:
139  bool startingNewLine;
140 
142  int currentColumn;
143 
145  bool inDQuote;
146 
148  std::string path;
149 
151  Array<char> data;
152 
154  int indentLevel;
155 
157  int indentSpaces;
158 
160  std::string newline;
161 
163  bool m_ok;
164 
166  bool changed;
167 
169  Settings options;
170 
172  void setOptions(Settings const & _opt);
173 
175  void convertNewlines(std::string const & in, std::string & out);
176 
178  void wordWrapIndentAppend(std::string const & str);
179 
181  void setIndentLevel(int i);
182 
184  void indentAppend(char c);
185 
187  bool _commit(bool flush, bool force);
188 
189  public:
190  THEA_DECL_SMART_POINTERS(TextOutputStream)
191 
192 
197  explicit TextOutputStream(std::string const & path_, Settings const & settings_ = Settings::defaults());
198 
200  explicit TextOutputStream(Settings const & settings_ = Settings::defaults());
201 
203  ~TextOutputStream();
204 
206  bool ok() const;
207 
209  std::string getPath() const
210  {
211  return path;
212  }
213 
222  bool commit(bool flush = true);
223 
225  void commitToString(std::string & str);
226 
228  std::string commitToString();
229 
235  void pushIndent();
236 
242  void popIndent();
243 
248  void writeString(std::string const & str);
249 
251  void writeBoolean(bool b);
252 
257  void writeNumber(double n);
258 
260  void writeNewline();
261 
263  void writeNewlines(int numLines);
264 
270  void writeSymbol(std::string const & str);
271 
276  void writeSymbols(
277  std::string const & a,
278  std::string const & b = "",
279  std::string const & c = "",
280  std::string const & d = "",
281  std::string const & e = "",
282  std::string const & f = "");
283 
288  void THEA_CDECL printf(char const * fmt, ...) THEA_CHECK_MEMBER_PRINTF_ARGS;
289 
294  void THEA_CDECL vprintf(char const * fmt, va_list arg_list) THEA_CHECK_MEMBER_VPRINTF_ARGS;
295 
296 }; // class TextOutputStream
297 
298 } // namespace Thea
299 
300 #endif
int spacesPerIndent
Number of spaces in each indent.
A base class for objects that should never be copied.
Definition: Noncopyable.hpp:28
Root namespace for the Thea library.
WordWrap wordWrap
Word wrap mode.
An object wrapping a name string.
Definition: NamedObject.hpp:58
std::string falseSymbol
The symbol used to represent false.
std::string trueSymbol
The symbol used to represent true.
Convenient formatting of ASCII text written to a file.
Output configuration optionss.
int numColumns
Number of columns for word wrapping.
bool allowWordWrapInsideDoubleQuotes
Is word-wrapping allowed to insert newlines inside double quotes? Default: false. ...
bool convertNewlines
If true, all newlines are converted to newlineStyle regardless of how they start out.
std::string getPath() const
Get the path to the current file being written ("<memory>" for memory streams).
Word-wrap settings (enum class).
Newline style (enum class).
std::vector< T, Alloc > Array
Dynamically resizable array.
Definition: Array.hpp:25
NewlineStyle newlineStyle
Style of newline used by word wrapping and by (optionsal) conversion.
static Settings const & defaults()
Get the default settings.