Solution46
CStreamOut.cpp
[詳解]
1 //=============================================================================
2 /// @file
3 /// ストリーム出力クラス実装ファイル
4 ///
5 /// ストリーム出力クラス実装ファイルです。
6 ///
7 /// $Id: CStreamOut.cpp 245 2019-03-20 15:03:41Z admin $
8 /// $Date: 2019-03-21 00:03:41 +0900 (2019/03/21 (木)) $
9 /// $Author: admin $
10 ///
11 /// @attention なし
12 
13 #pragma managed( push, off )
14 
15 //=============================================================================
16 // インクルードファイル
17 #include <CStreamOut.h>
18 #include <LibUtility.h>
19 #include <stdio.h>
20 
21 //=============================================================================
22 // 共通ライブラリ名前空間
23 namespace LibCommon {
24  //=========================================================================
25  // ストリーム出力クラス
26  //=========================================================================
27  // 構築子と解体子
28  //-------------------------------------------------------------------------
29  // コンストラクタ
31  // メンバ変数初期化
32  : m_nLine( 0 )
33  , m_nIndent( 0 )
34  {}
35 
36  //-------------------------------------------------------------------------
37  // デストラクタ
39  }
40 
41  //=========================================================================
42  // 公開関数
43  //-------------------------------------------------------------------------
44  // 1文字出力関数
45  bool CStreamOut::OutputChar( wchar_t ch ) noexcept {
46  // 処理ブロック
47  bool result = false;
48  do {
49  // 文字列を出力する
50  wchar_t szBuffer[ 2 ] = { ch, L'\0' };
51  if ( !OutputString( szBuffer ) ) {
52  // 失敗!
53  break;
54  }
55 
56  // 成功!
57  result = true;
58  } while ( false );
59 
60  // 実行結果を返す
61  return result;
62  }
63 
64  //-------------------------------------------------------------------------
65  // 書式設定文字列出力関数
66  bool CStreamOut::OutputFormat( wchar_t const* pszFormat, ... ) noexcept {
67  // 処理ブロック
68  bool result = false;
69  do {
70  // 引数リストを作成する
71  va_list vaArgs;
72  va_start( vaArgs, pszFormat );
73 
74  // 処理ブロック
75  do {
76  // 引数リスト文字列を出力する
77  if ( !OutputArgs( pszFormat, vaArgs ) ) {
78  // 失敗!
79  break;
80  }
81 
82  // 成功!
83  result = true;
84  } while ( false );
85 
86  // 引数リストを解放する
87  va_end( vaArgs );
88  } while ( false );
89 
90  // 実行結果を返す
91  return result;
92  }
93 
94  //-------------------------------------------------------------------------
95  // 引数リスト文字列出力関数
96  bool CStreamOut::OutputArgs( wchar_t const* pszFormat, va_list vaArgs ) noexcept {
97  // 処理ブロック
98  bool result = false;
99  do {
100  // 書式設定文字列ポインタを調べる
101  if ( nullptr == pszFormat ) {
102  // 失敗!
103  break;
104  }
105 
106  // 書式設定文字列を展開する
107  wchar_t szBuffer[ FORMAT_BUFFER_SIZE + 1 ];
108  if ( !ExpandFormatArgs( szBuffer, ( FORMAT_BUFFER_SIZE + 1 ), pszFormat, vaArgs ) ) {
109  // 失敗!
110  break;
111  }
112  // 文字列を出力する
113  else if ( !OutputString( szBuffer ) ) {
114  // 失敗!
115  break;
116  }
117 
118  // 成功!
119  result = true;
120  } while ( false );
121 
122  // 実行結果を返す
123  return result;
124  }
125 
126  //-------------------------------------------------------------------------
127  // 改行出力関数
128  bool CStreamOut::OutputNewLine() noexcept {
129  // 処理ブロック
130  bool result = false;
131  do {
132  // 改行文字列を出力する
133  if ( !OutputString( L"\n" ) ) {
134  // 失敗!
135  break;
136  }
137 
138  // 成功!
139  result = true;
140  } while ( false );
141 
142  // 実行結果を返す
143  return result;
144  }
145 
146  //-------------------------------------------------------------------------
147  // 行ヘッダー出力関数
149  // 処理ブロック
150  bool result = false;
151  do {
152  // 行番号を更新する
154 
155  // ヘッダ文字列を出力する
156  SYSTEMTIME sSysTime;
157  ::GetLocalTime( &sSysTime );
158  if ( !OutputFormat( L"%06lu:[%04X:%04X]%02u%02u%02u-%02u%02u%02u.%03u ",
159  GetLineNumber(),
160  ::GetCurrentProcessId(),
161  ::GetCurrentThreadId(),
162  ( sSysTime.wYear % 100 ),
163  sSysTime.wMonth,
164  sSysTime.wDay,
165  sSysTime.wHour,
166  sSysTime.wMinute,
167  sSysTime.wSecond,
168  sSysTime.wMilliseconds ) ) {
169  // 失敗!
170  break;
171  }
172 
173  // 成功!
174  result = true;
175  } while ( false );
176 
177  // 実行結果を返す
178  return result;
179  }
180 
181  //-------------------------------------------------------------------------
182  // インデント出力関数
183  bool CStreamOut::OutputIndent() noexcept {
184  // 処理ブロック
185  bool result = false;
186  do {
187  // 巡回する
188  for ( int uCount = GetIndentCount();; --uCount ) {
189  // 残りループ回数を調べる
190  if ( 0 == uCount ) {
191  // 成功!
192  result = true;
193  break;
194  }
195  // インデント文字列を出力する
196  else if ( !OutputString( L" " ) ) {
197  // 失敗!
198  break;
199  }
200  }
201  } while ( false );
202 
203  // 実行結果を返す
204  return result;
205  }
206 
207  //-------------------------------------------------------------------------
208  // インデント関数
209  bool CStreamOut::Indent() noexcept {
210  // インデントカウントをインクリメントする
211  ++m_nIndent;
212 
213  // 成功!
214  return true;
215  }
216 
217  //-------------------------------------------------------------------------
218  // アンインデント関数
219  bool CStreamOut::Unindent() noexcept {
220  // 処理ブロック
221  bool result = false;
222  do {
223  // インデントカウントを調べる
224  if ( 0 >= m_nIndent ) {
225  // 致命的エラー!
227  break;
228  }
229 
230  // インデントカウントをデクリメントする
231  --m_nIndent;
232 
233  // 成功!
234  result = true;
235  } while ( false );
236 
237  // 実行結果を返す
238  return result;
239  }
240 
241  //-------------------------------------------------------------------------
242  // 文字列行出力関数
243  bool CStreamOut::OutputLineString( wchar_t const* pszString, bool bHeader, bool bIndent ) noexcept {
244  // 処理ブロック
245  bool result = false;
246  do {
247  // 行ヘッダを出力する
248  if ( bHeader && !OutputLineHeader() ) {
249  // 失敗!
250  break;
251  }
252  // インデントを出力する
253  else if ( bIndent && !OutputIndent() ) {
254  // 失敗!
255  break;
256  }
257  // 文字列を出力する
258  else if ( !OutputString( pszString ) ) {
259  // 失敗!
260  break;
261  }
262  // 改行を出力する
263  else if ( !OutputNewLine() ) {
264  // 失敗!
265  break;
266  }
267 
268  // 成功!
269  result = true;
270  } while ( false );
271 
272  // 実行結果を返す
273  return result;
274  }
275 
276  //-------------------------------------------------------------------------
277  // 書式設定文字列行出力関数
278  bool CStreamOut::OutputLineFormat( wchar_t const* pszFormat, ... ) noexcept {
279  // 処理ブロック
280  bool result = false;
281  do {
282  // 引数リストを作成する
283  va_list vaArgs;
284  va_start( vaArgs, pszFormat );
285 
286  // 処理ブロック
287  do {
288  // 引数リスト文字列を行出力する
289  if ( !OutputLineArgs( pszFormat, vaArgs ) ) {
290  // 失敗!
291  break;
292  }
293 
294  // 成功!
295  result = true;
296  } while ( false );
297 
298  // 引数リストを解放する
299  va_end( vaArgs );
300  } while ( false );
301 
302  // 実行結果を返す
303  return result;
304  }
305 
306  //-------------------------------------------------------------------------
307  // 引数リスト文字列行出力関数
308  bool CStreamOut::OutputLineArgs( wchar_t const* pszFormat, va_list vaArgs ) noexcept {
309  // 拡張引数リスト文字列を行出力する
310  return ExOutputLineArgs( true, true, pszFormat, vaArgs );
311  }
312 
313  //-------------------------------------------------------------------------
314  // 拡張書式設定文字列行出力関数
315  bool CStreamOut::ExOutputLineFormat( bool bHeader, bool bIndent, wchar_t const* pszFormat, ... ) noexcept {
316  // 処理ブロック
317  bool result = false;
318  do {
319  // 引数リストを作成する
320  va_list vaArgs;
321  va_start( vaArgs, pszFormat );
322 
323  // 処理ブロック
324  do {
325  // 引数リスト文字列を行出力する
326  if ( !ExOutputLineArgs( bHeader, bIndent, pszFormat, vaArgs ) ) {
327  // 失敗!
328  break;
329  }
330 
331  // 成功!
332  result = true;
333  } while ( false );
334 
335  // 引数リストを解放する
336  va_end( vaArgs );
337  } while ( false );
338 
339  // 実行結果を返す
340  return result;
341  }
342 
343  //-------------------------------------------------------------------------
344  // 拡張引数リスト文字列行出力関数
345  bool CStreamOut::ExOutputLineArgs( bool bHeader, bool bIndent, wchar_t const* pszFormat, va_list vaArgs ) noexcept {
346  // 処理ブロック
347  bool result = false;
348  do {
349  // 書式設定文字列ポインタを調べる
350  if ( nullptr == pszFormat ) {
351  // 失敗!
352  break;
353  }
354 
355  // 書式設定文字列を展開する
356  wchar_t szBuffer[ FORMAT_BUFFER_SIZE + 1 ];
357  if ( !ExpandFormatArgs( szBuffer, ( FORMAT_BUFFER_SIZE + 1 ), pszFormat, vaArgs ) ) {
358  // 失敗!
359  break;
360  }
361  // 文字列を行出力する
362  else if ( !OutputLineString( szBuffer, bHeader, bIndent ) ) {
363  // 失敗!
364  break;
365  }
366 
367  // 成功!
368  result = true;
369  } while ( false );
370 
371  // 実行結果を返す
372  return result;
373  }
374 }
375 
376 #pragma managed( pop )
#define NotifyFatalError()
致命的エラー発生通知マクロ
Definition: LibUtility.h:22
virtual bool UpdateLineNumber() noexcept
行番号更新関数
Definition: CStreamOut.h:150
ストリーム出力クラスヘッダファイル
int m_nIndent
インデントカウント
Definition: CStreamOut.h:297
LIB_COMMON_API bool ExpandFormatArgs(wchar_t *pszBuffer, size_t uSize, wchar_t const *pszFormat, va_list vaArgs) noexcept
書式設定文字列展開関数
Definition: LibUtility.cpp:195
virtual bool OutputNewLine() noexcept
改行出力関数
Definition: CStreamOut.cpp:128
共通ライブラリ名前空間
Definition: CArray.h:23
virtual bool OutputLineHeader() noexcept
行ヘッダー出力関数
Definition: CStreamOut.cpp:148
virtual int GetLineNumber() noexcept
行番号取得関数
Definition: CStreamOut.h:161
virtual bool OutputArgs(wchar_t const *pszFormat, va_list vaArgs) noexcept
引数リスト文字列出力関数
Definition: CStreamOut.cpp:96
virtual bool OutputLineFormat(wchar_t const *pszFormat,...) noexcept
書式設定文字列行出力関数
Definition: CStreamOut.cpp:278
virtual bool OutputChar(wchar_t ch) noexcept
1文字出力関数
Definition: CStreamOut.cpp:45
virtual bool OutputLineString(wchar_t const *pszString, bool bHeader=true, bool bIndent=true) noexcept
文字列行出力関数
Definition: CStreamOut.cpp:243
virtual bool OutputFormat(wchar_t const *pszFormat,...) noexcept
書式設定文字列出力関数
Definition: CStreamOut.cpp:66
virtual bool OutputString(wchar_t const *pszString) noexcept=0
文字列出力関数
virtual bool OutputLineArgs(wchar_t const *pszFormat, va_list vaArgs) noexcept
引数リスト文字列行出力関数
Definition: CStreamOut.cpp:308
CStreamOut() noexcept
コンストラクタ
Definition: CStreamOut.cpp:30
virtual bool OutputIndent() noexcept
インデント出力関数
Definition: CStreamOut.cpp:183
virtual ~CStreamOut() noexcept
デストラクタ
Definition: CStreamOut.cpp:38
virtual bool Indent() noexcept
インデント関数
Definition: CStreamOut.cpp:209
virtual bool ExOutputLineArgs(bool bHeader, bool bIndent, wchar_t const *pszFormat, va_list vaArgs) noexcept
拡張引数リスト文字列行出力関数
Definition: CStreamOut.cpp:345
static int const FORMAT_BUFFER_SIZE
書式設定文字列バッファサイズ
Definition: CStreamOut.h:291
virtual bool ExOutputLineFormat(bool bHeader, bool bIndent, wchar_t const *pszFormat,...) noexcept
拡張書式設定文字列行出力関数
Definition: CStreamOut.cpp:315
virtual int GetIndentCount() noexcept
インデントカウント取得関数
Definition: CStreamOut.h:172
ユーティリティライブラリヘッダファイル
virtual bool Unindent() noexcept
アンインデント関数
Definition: CStreamOut.cpp:219