opennurbs_textlog.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(ON_TEXTLOG_INC_)
18 #define ON_TEXTLOG_INC_
19 
20 class ON_CLASS ON_TextLog
21 {
22 public:
23  /*
24  Description:
25  Create a text log that dumps to the virtual function
26  void ON_TextLog::AppendText().
27  */
28  ON_TextLog();
29 
30  /*
31  Description:
32  Create a text log that dumps to an ASCII file.
33  Parameters:
34  fp - [in] Pointer to an open ASCII text file. The file
35  pointer must remain valid as long as the text
36  log is in use.
37  */
38  ON_TextLog( FILE* fp); // dump to open ASCII text file
39 
40  /*
41  Description:
42  Create a text log that dumps to a string.
43  Parameters:
44  s - [in] String that must exist as long as
45  the text log is in use.
46  */
47  ON_TextLog( ON_wString& s );
48 
49  /*
50  Description:
51  ON_TextLog::Null is a silent text log and can be used when no output
52  is desired but an ON_TextLog parameter is required.
53  */
54  static ON_TextLog Null;
55 
56  bool IsTextHash() const;
57 
58 public:
59  virtual ~ON_TextLog();
60 
61  void SetDoubleFormat( const char* ); // default is %g
62  void GetDoubleFormat( ON_String& ) const;
63 
64  void SetFloatFormat( const char* ); // default is %g
65  void GetFloatFormat( ON_String& ) const;
66 
67  void PushIndent();
68  void PopIndent();
69  int IndentSize() const; // 0: one tab per indent
70  // >0: number of spaces per indent
71  void SetIndentSize(int);
72 
73  /*
74  Returns:
75  Current indentation count
76  */
77  int IndentCount();
78  /*
79  Description:
80  Set indentation count.
81  */
82  void SetIndentCount(
83  int indent_count
84  );
85 
86  void PrintWrappedText( const char*, int = 60 ); // last arg is maximum line length
87  void PrintWrappedText( const wchar_t*, int = 60 ); // last arg is maximum line length
88 
89  /*
90  Description:
91  Print a formatted ASCII string of up to 2000 characters.
92  Parameters:
93  format - [in] nullptr terminated format control string
94  Remarks:
95  To print strings longer than 2000 characters, you must
96  use ON_TextLog::PrintString.
97  See Also:
98  ON_TextLog::PrintString
99  */
100  void ON_VARGS_FUNC_CDECL Print(const char* format, ...);
101 
102  /*
103  Description:
104  Print a formatted INICODE string of up to 2000 characters.
105  Parameters:
106  format - [in] nullptr terminated format control string
107  Remarks:
108  To print strings longer than 2000 characters, you must
109  use ON_TextLog::PrintString.
110  See Also:
111  ON_TextLog::PrintString
112  */
113  void ON_VARGS_FUNC_CDECL Print(const wchar_t* format, ...);
114 
115  void Print( float );
116  void Print( double );
117  void Print( const ON_2dPoint& );
118  void Print( const ON_3dPoint& );
119  void Print( const ON_4dPoint& );
120  void Print( const ON_2dVector& );
121  void Print( const ON_3dVector& );
122  void Print( const ON_Xform& );
123  void Print( const ON_UUID& );
124  void Print( const ON_COMPONENT_INDEX& );
125 
126  /*
127  Description:
128  Print an unformatted wide char string of any length.
129  Parameters:
130  string - [in]
131  */
132  void Print( const ON_wString& string );
133 
134  /*
135  Description:
136  Print an unformatted UTF-8 string of any length.
137  Parameters:
138  string - [in]
139  */
140  void Print( const ON_String& string );
141 
142  void Print( const ON_3dPointArray&, const char* = nullptr );
143  void Print(
144  const ON_Matrix&,
145  const char* = nullptr, // optional preamble
146  int = 0 // optional number precision
147  );
148 
149  // printing utilities
150  /*
151  Description:
152  Same as calling Print("\n");
153  */
154  void PrintNewLine();
155 
156  /*
157  Description:
158  Print an unformatted ASCII string of any length.
159  Parameters:
160  s - [in] nullptr terminated ASCII string.
161  */
162  void PrintString( const char* s );
163 
164  /*
165  Description:
166  Print an unformatted UNICODE string of any length.
167  Parameters:
168  s - [in] nullptr terminated UNICODE string.
169  */
170  void PrintString( const wchar_t* s );
171 
172  void PrintRGB( const ON_Color& );
173 
174  void PrintTime( const struct tm& );
175 
176  void PrintPointList(
177  int, // dim
178  bool, // true for rational points
179  int, // count
180  int, // stride
181  const double*, // point[] array
182  const char* = nullptr // optional preabmle
183  );
184 
185  void PrintPointGrid(
186  int, // dim
187  bool, // true for rational points
188  int, int, // point_count0, point_count1
189  int, int, // point_stride0, point_stride1
190  const double*, // point[] array
191  const char* = nullptr // optional preabmle
192  );
193 
194  void PrintKnotVector(
195  int, // order
196  int, // cv_count
197  const double* // knot[] array
198  );
199 
200  ON_TextLog& operator<<( const char* );
201  ON_TextLog& operator<<( char );
202  ON_TextLog& operator<<( short );
203  ON_TextLog& operator<<( int );
204  ON_TextLog& operator<<( float );
205  ON_TextLog& operator<<( double );
206  ON_TextLog& operator<<( const ON_2dPoint& );
207  ON_TextLog& operator<<( const ON_3dPoint& );
208  ON_TextLog& operator<<( const ON_4dPoint& );
209  ON_TextLog& operator<<( const ON_2dVector& );
210  ON_TextLog& operator<<( const ON_3dVector& );
211  ON_TextLog& operator<<( const ON_Xform& );
212 
213 protected:
214  friend class ON_TextHash;
215 
216  FILE* m_pFile;
218 
219 
220  /*
221  Description:
222  If the ON_TextLog(ON_wString& wstr) constructor was used, the
223  default appends s to wstr. If the ON_TextLog(FILE* fp)
224  constructor was used, the default calls fputs( fp, s).
225  In all other cases, the default calls printf("%s",s).
226  Parameters:
227  s - [in];
228  */
229  virtual
230  void AppendText(
231  const char* s
232  );
233 
234  /*
235  Description:
236  If the ON_TextLog(ON_wString& wstr) constructor was used, the
237  default appends s to wstr. In all other cases, the default
238  converts the string to an ON_String and calls the ASCII
239  version AppendText(const char*).
240  Parameters:
241  s - [in];
242  */
243  virtual
244  void AppendText(
245  const wchar_t* s
246  );
247 
248 private:
249  ON_String m_indent;
250  ON_String m_double_format;
251  ON_String m_double2_format;
252  ON_String m_double3_format;
253  ON_String m_double4_format;
254  ON_String m_float_format;
255  ON_String m_float2_format;
256  ON_String m_float3_format;
257  ON_String m_float4_format;
258 
259  ON_String m_line;
260 
261  int m_beginning_of_line = 0; // 0
262 
263  // size of a single indentation
264  int m_indent_size = 0; // 0 use tabs, > 0 = number of spaces per indent level
265 
266  // Number of indentations at the start of a new line
267  int m_indent_count = 0;
268 
269 private:
270  ON_TextLog( const ON_TextLog& ) = delete;
271  ON_TextLog& operator=( const ON_TextLog& ) = delete;
272 };
273 
274 /*
275 Description:
276  ON_TextLogIndent is a class used with ON_TextLog to
277  push and pop indentation.
278 */
279 class ON_CLASS ON_TextLogIndent
280 {
281 public:
282  // The constructor calls text_log.PushIndent()
283  // and the destuctor calls text_log.PopIndent()
285  class ON_TextLog& text_log
286  );
287 
288  // If bEnabled is true, the constructor calls text_log.PushIndent()
289  // and the destuctor calls text_log.PopIndent()
291  class ON_TextLog& text_log,
292  bool bEnabled
293  );
294 
295  ~ON_TextLogIndent();
296 
297 private:
298  class ON_TextLog& m_text_log;
299  bool m_bEnabled;
300 
301  // prevent use of copy construction and operator=
302  // (no implementations)
304  ON_TextLogIndent& operator=(const ON_TextLogIndent&);
305 };
306 
307 class ON_CLASS ON_TextHash : public ON_TextLog
308 {
309 public:
310  ON_TextHash() = default;
311  ~ON_TextHash() = default;
312 
313 private:
314  ON_TextHash(const ON_TextHash&) = delete;
315  ON_TextHash& operator= (const ON_TextHash&) = delete;
316 
317 public:
318 
319  ON_StringMapType StringMapType() const;
320 
321  const class ON_Locale& StringMapLocale() const;
322 
323  void SetStringMap(
324  const class ON_Locale& locale,
325  ON_StringMapType map_type
326  );
327 
328  void SetStringMap(
329  ON_StringMapOrdinalType map_type
330  );
331 
332  /*
333  Parameters:
334  bEnableIdRemap - [in]
335  if bEnableIdRemap is true, the sequences of
336  code points that match the format
337  XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
338  where X is a hexadecimal digit (0-9, a-f, or A-F)
339  and the hyphen is literal.
340  will be replaced with an id created by
341  ON_NextNotUniqueId().
342  This is used for comparing code that generates streams
343  containg new uuids.
344  */
345  void SetIdRemap(
346  bool bEnableIdRemap
347  );
348 
349  /*
350  Returns:
351  True if id remap is available.
352  */
353  bool IdRemap() const;
354 
355  /*
356  Description:
357  In some testing situations, the output text log can be set
358  when it is necessary to see the text used to compute the
359  SHA-1 hash. The has can be caluculate which no output text
360  log.
361 
362  Parameters:
363  output_text_log - [in]
364  Destination text log for the mtext used to calculate the hash.
365  */
366  void SetOutputTextLog(
367  ON_TextLog* output_text_log
368  );
369 
370  ON_TextLog* OutputTextLog() const;
371 
372  /*
373  Returns:
374  Total number of bytes that have passed through this text log.
375  */
376  ON__UINT64 ByteCount() const;
377 
378  /*
379  Returns:
380  SHA-1 hash value of the text sent to this text log.
381  */
382  ON_SHA1_Hash Hash() const;
383 
384 private:
385  void AppendText(const char* s) override;
386  void AppendText(const wchar_t* s) override;
387 
388 private:
389  bool m_bApplyStringMap = false;
390  bool m_bApplyIdRemap = false;
391 
392  ON_UUID m_remap_id = ON_nil_uuid;
393  ON_UuidPairList m_remap_id_list;
394 
395  ON_StringMapType m_string_map_type = ON_StringMapType::Identity;
396  ON_StringMapOrdinalType m_string_map_ordinal_type = ON_StringMapOrdinalType::Identity;
397  ON_Locale m_string_map_local = ON_Locale::InvariantCulture;
398 
399  ON_TextLog* m_output_text_log = nullptr;
400 
401  static const char* Internal_ParseId(
402  const char* s,
403  ON_UUID* id
404  );
405 
406  static bool Internal_IsHexDigit(char c);
407 
408  ON_SHA1 m_sha1;
409 };
410 
411 
412 #endif
static const ON_Locale InvariantCulture
Definition: opennurbs_locale.h:103
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
ON_wString * m_pString
Definition: opennurbs_textlog.h:196
Definition: opennurbs_sha1.h:19
Definition: opennurbs_string.h:2020
Definition: opennurbs_color.h:24
Definition: opennurbs_point.h:277
FILE * m_pFile
Definition: opennurbs_textlog.h:195
Definition: opennurbs_point.h:2018
friend class ON_TextHash
Definition: opennurbs_textlog.h:193
Definition: opennurbs_point.h:648
Definition: opennurbs_string.h:852
Definition: opennurbs_xform.h:28
virtual void AppendText(const char *s)
If the ON_TextLog(ON_wString& wstr) constructor was used, the default appends s to wstr...
ON_SHA1 is a small class for calculating the SHA-1 hash of a sequence of bytes. It may be use increme...
Definition: opennurbs_sha1.h:242
ON_TextLogIndent is a class used with ON_TextLog to push and pop indentation.
Definition: opennurbs_textlog.h:253
ON_TextLog()
Create a text log that dumps to the virtual function void ON_TextLog::AppendText().
Definition: opennurbs_matrix.h:22
Definition: opennurbs_textlog.h:20
The ON_UuidPairList class provides a tool to efficiently maintain a list of uuid pairs and determine ...
Definition: opennurbs_array.h:1214
Definition: opennurbs_textlog.h:281
Definition: opennurbs_point.h:460
Definition: opennurbs_locale.h:32
Definition: opennurbs_point.h:839
Definition: opennurbs_point.h:1152