CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
common_FixedString.h
1 /*--------------------------------------------------------------------------------*
2  Copyright (C)Nintendo All rights reserved.
3 
4  These coded instructions, statements, and computer programs contain proprietary
5  information of Nintendo and/or its licensed developers and are protected by
6  national and international copyright laws. They may not be disclosed to third
7  parties or copied or duplicated in any form, in whole or in part, without the
8  prior written consent of Nintendo.
9 
10  The content herein is highly confidential and should be handled accordingly.
11  *--------------------------------------------------------------------------------*/
12 
13 
14 #pragma once
15 
16 #include <nn/pia/common/common_Definitions.h>
17 #include <nn/pia/common/common_String.h>
18 
19 namespace nn
20 {
21 namespace pia
22 {
23 namespace common
24 {
25 /*!
26  @brief 固定長のバッファを持つ文字列を表すクラスです。
27  */
28 template <uint32_t N>
30 {
31 public:
32  /*!
33  @brief デフォルトコンストラクタです。
34  */
36  {
37  m_BufferSize = N * MultiByteCharMaxSize;
38  Clear();
39  }
40 
41  /*!
42  @brief デストラクタです。
43  */
44  virtual ~FixedString()
45  {
46  }
47 
48  /*!
49  @brief UTF-8 の文字列から構築するコンストラクタです。
50 
51  @param[in] pStr 文字列のポインタです。
52  */
53  FixedString(const char* pStr)
54  {
55  m_BufferSize = N * MultiByteCharMaxSize;
56  SetString(pStr, CalcBytes(pStr, EncodingType_Utf8), EncodingType_Utf8);
57  }
58 
59  /*!
60  @brief UTF-16 の文字列から構築するコンストラクタです。
61 
62  @param[in] pStr 文字列のポインタです。
63  */
64  FixedString(const charUtf16_t* pStr)
65  {
66  m_BufferSize = N * MultiByteCharMaxSize;
67  SetString(pStr, CalcBytes(pStr, EncodingType_Utf16), EncodingType_Utf16);
68  }
69 
70  /*!
71  @brief 文字列バッファのサイズを取得します。
72 
73  @return 文字列バッファのサイズを返します。
74  */
75  static uint32_t GetBufferSize()
76  {
77  return N * MultiByteCharMaxSize;
78  }
79 
80  /*!
81  @brief 文字列をクリアします。
82  */
83  virtual void Clear()
84  {
85  for (uint32_t i = 0; i < N * MultiByteCharMaxSize + 1; i++)
86  {
87  m_Buffer[i] = 0;
88  }
89  m_pBuffer = m_Buffer;
90  m_Bytes = 0;
91  m_EncodingType = EncodingType_Unknown;
92  }
93 
94 private:
95  virtual Result SetString(const void* str, uint32_t byteSize, EncodingType type)
96  {
97  if (!PIA_IS_VALID_POINTER(str))
98  {
99  PIA_RETURN_RESULT_WITH_REPORT(ResultInvalidArgument, "String is NULL.");
100  }
101  if (byteSize > m_BufferSize)
102  {
103  PIA_RETURN_RESULT_WITH_REPORT(ResultInvalidArgument, "String is too long.");
104  }
105  if (type == EncodingType_Utf16)
106  {
107  if (byteSize > m_BufferSize / MultiByteCharMaxSize * 2)
108  {
109  PIA_RETURN_RESULT_WITH_REPORT(ResultInvalidArgument, "String is too long.");
110  }
111  }
112  else if (type == EncodingType_Utf8)
113  {
114  const char* p = static_cast<const char*>(str);
115  uint32_t length = 0;
116  while (*p != 0)
117  {
118  uint32_t bytes = 0;
119  uint8_t ccc = static_cast<uint8_t>(*p);
120  if (ccc > 0x00 && ccc <= 0x7f)
121  {
122  bytes = 1;
123  }
124  else if (ccc >= 0xc2 && ccc <= 0xdf)
125  {
126  bytes = 2;
127  }
128  else if (ccc >= 0xe0 && ccc <= 0xef)
129  {
130  bytes = 3;
131  }
132  else if (ccc >= 0xf0 && ccc <= 0xf7)
133  {
134  bytes = 4;
135  }
136  else if (ccc >= 0xf8 && ccc <= 0xfb)
137  {
138  bytes = 5;
139  }
140  else if (ccc >= 0xfc && ccc <= 0xfd)
141  {
142  bytes = 6;
143  }
144  if (bytes > 0)
145  {
146  ++length;
147  p += bytes;
148  }
149  else
150  {
151  // ここにくるのはおかしい
152  ++p;
153  PIA_ASSERT(false);
154  }
155  }
156  if (length > m_BufferSize / MultiByteCharMaxSize)
157  {
158  PIA_RETURN_RESULT_WITH_REPORT(ResultInvalidArgument, "String is too long.");
159  }
160  }
161  else
162  {
163  PIA_RETURN_RESULT_WITH_REPORT(ResultInvalidArgument, "String encoding type is not supported.");
164  }
165 
166  for (uint32_t i = 0; i < m_BufferSize; i++)
167  {
168  m_Buffer[i] = 0;
169  }
170 
171  uint32_t c = 0;
172  if (type == EncodingType_Utf8)
173  {
174  for (const char* p = static_cast<const char*>(str); *p != 0x00 && c < m_BufferSize; ++p)
175  {
176  m_Buffer[c] = *p;
177  ++c;
178  }
179  m_Buffer[c] = 0x00;
180  }
181  else if (type == EncodingType_Utf16)
182  {
183  for (const charUtf16_t* p = static_cast<const charUtf16_t*>(str); *p != 0x0000 && c < m_BufferSize / MultiByteCharMaxSize * 2; ++p)
184  {
185  charUtf16_t* d = reinterpret_cast<charUtf16_t*>(&m_Buffer[c]);
186  *d = *p;
187  c += 2;
188  }
189  m_Buffer[c] = 0x00;
190  m_Buffer[c + 1] = 0x00;
191  }
192  m_pBuffer = m_Buffer;
193  m_Bytes = c;
194  m_EncodingType = type;
195 
196  return ResultSuccess();
197  }
198 
199  // NULL文字分 +1 しておく
200  char m_Buffer[N * MultiByteCharMaxSize + 1];
201 };
202 }
203 }
204 } // end of namespace nn::pia::common