1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     SmInitialFileParser.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef SM_INITIAL_FILE_PARSER_H_
19 #define SM_INITIAL_FILE_PARSER_H_
20 
21 #include <ctype.h>
22 #include <stdlib.h>
23 
24 #include <nn/gx.h>
25 #include "../include/SmBase.h"
26 #include "../include/SmFile.h"
27 
28 
29 
30 //------------------------------------------------------------------------------
31 //! 4タイプの値を管理するパラメータクラスです。
32 class SmParam
33 {
34 public:
35     enum
36     {
37         SM_PARAM_TYPE_U32       = 0,
38         SM_PARAM_TYPE_S32       = 1,
39         SM_PARAM_TYPE_F32       = 2,
40         SM_PARAM_TYPE_STRING    = 3
41     };
42     typedef u32 SmParamType;
43 
44     //! コンストラクタです
SmParam()45     SmParam()
46     : m_Type( SM_PARAM_TYPE_U32 ),
47       m_u32Param( 0 )
48     {
49         m_String = "";
50     }
51 
52     //! パラメータタイプを取得します。
GetType()53     SmParamType GetType() const { return m_Type; }
54 
55     //! パラメータを取得します。
GetU32()56     u32 GetU32() const
57     {
58         switch( m_Type )
59         {
60         case SM_PARAM_TYPE_U32:
61             {
62                 return m_u32Param;
63             }
64         case SM_PARAM_TYPE_S32:
65             {
66                 return static_cast<u32>(m_s32Param);
67             }
68         case SM_PARAM_TYPE_F32:
69             {
70                 return static_cast<u32>(m_f32Param);
71             }
72         case SM_PARAM_TYPE_STRING:
73             {
74                 return static_cast<u32>(atoi(m_String.c_str()));
75             }
76 
77         }
78         return 0;
79     }
80 
GetS32()81     s32 GetS32() const
82     {
83         switch( m_Type )
84         {
85         case SM_PARAM_TYPE_U32:
86             {
87                 return static_cast<s32>(m_u32Param);
88             }
89         case SM_PARAM_TYPE_S32:
90             {
91                 return m_s32Param;
92             }
93         case SM_PARAM_TYPE_F32:
94             {
95                 return static_cast<s32>(m_f32Param);
96             }
97         case SM_PARAM_TYPE_STRING:
98             {
99                 return static_cast<s32>(atoi(m_String.c_str()));
100             }
101         }
102         return 0;
103     }
104 
GetF32()105     f32 GetF32() const
106     {
107         switch( m_Type )
108         {
109         case SM_PARAM_TYPE_U32:
110             {
111                 return static_cast<f32>(m_u32Param);
112             }
113         case SM_PARAM_TYPE_S32:
114             {
115                 return static_cast<f32>(m_s32Param);
116             }
117         case SM_PARAM_TYPE_F32:
118             {
119                 return m_f32Param;
120             }
121         case SM_PARAM_TYPE_STRING:
122             {
123                 return static_cast<f32>(atof(m_String.c_str()));
124             }
125         }
126         return 0;
127     }
128 
GetString()129     const char* GetString() const
130     {
131         switch( m_Type )
132         {
133         case SM_PARAM_TYPE_U32:
134         case SM_PARAM_TYPE_S32:
135         case SM_PARAM_TYPE_F32:
136             {
137 //                m_String.Format( "%d", m_u32Param );
138 //                return m_String.c_str();
139                 NW_FATAL_ERROR( "no support!" );
140             }
141 
142         case SM_PARAM_TYPE_STRING:
143             {
144                 return m_String.c_str();
145             }
146         }
147 
148         return NULL;
149     }
150 
151     //! パラメータを設定します。
SetParam(u32 param)152     void SetParam( u32 param )
153     {
154         m_Type      = SM_PARAM_TYPE_U32;
155         m_u32Param  = param;
156     }
157 
SetParam(s32 param)158     void SetParam( s32 param )
159     {
160         m_Type      = SM_PARAM_TYPE_S32;
161         m_s32Param  = param;
162     }
163 
SetParam(f32 param)164     void SetParam( f32 param )
165     {
166         m_Type      = SM_PARAM_TYPE_F32;
167         m_f32Param  = param;
168     }
169 
SetParam(const char * param)170     void SetParam( const char* param )
171     {
172         m_Type      = SM_PARAM_TYPE_STRING;
173         m_String    = param;
174     }
175 
176 private:
177     SmParamType m_Type;
178     union
179     {
180         u32 m_u32Param;
181         s32 m_s32Param;
182         f32 m_f32Param;
183     };
184     std::string m_String;
185 };
186 
187 
188 //---------------------------------------------------------------------------
189 //! 実行される1つのコマンドです。
190 class SmCmd : public SmBase
191 {
192 friend class SmInitialFileParser;
193 
194 private:
195     //! 名前と値を組み合わせたデータです。
196     struct SmData
197     {
198         std::string name;       // 名前
199         SmParam     param;      // 値
200     };
201 
202 public:
203     //! コンストラクタです。
SmCmd()204     SmCmd()
205     : m_Parent( NULL ),
206       m_Child( NULL ),
207       m_Next( NULL )
208     {
209         Init();
210     }
211 
212     //! データの初期化を行います。
Init()213     void Init()
214     {
215         m_Command = "NULL";
216 
217         for ( int i=0; i<16; ++i )
218         {
219             m_Data[i].name = "null";
220             m_Data[i].param.SetParam( 0 );
221         }
222     }
223 
224     //! エレメントを取得します。
GetCommand()225     const char* GetCommand() const
226     {
227         return m_Command.c_str();
228     }
229 
230     //! 名前を指定して値を取得します。
GetF32(const char * strName)231     f32 GetF32( const char* strName ) const
232     {
233         for ( int i=0; i<16; ++i )
234         {
235             const char* name = m_Data[i].name.c_str();
236             if ( strcmp( name, strName ) == 0 )
237             {
238                 return m_Data[i].param.GetF32();
239             }
240         }
241 
242         return 0.f;
243     }
244 
245 private:
246     SmCmd*          m_Parent;
247     SmCmd*          m_Child;
248     SmCmd*          m_Next;
249 
250     std::string     m_Command;     // <XXXX
251     SmData          m_Data[16];    // tx = 10  (16個固定)
252 };
253 
254 
255 //---------------------------------------------------------------------------
256 //! イニシャルファイルを解釈するクラスです。
257 class SmInitialFileParser : public  SmBase
258 {
259     typedef nw::ut::FixedSizeArray<SmCmd*, 64> SmCmdArray;
260 
261 public:
262     SmInitialFileParser();
263     ~SmInitialFileParser();
264 
265     //! 取得可能か問い合わせます。
266     bool    IsGettable();
267 
268     //! コマンドを取得します。
269     SmCmd   GetCmd();
270 
271     //! 初期化ファイルをロードします。
272     bool Load( const wchar_t* configFileName );
273 
274     //! コマンド数を取得します。
GetCmdNum()275     u32 GetCmdNum() const { return m_CmdArray.size(); }
276 
277     //! コマンドを取得します。
GetCmd(u32 no)278     const SmCmd* GetCmd( u32 no ) { return m_CmdArray[no]; }
279 
280 private:
281     //! コマンドツリーを生成します。
282     void _createCmdTree();
283 
284 private:
285     SmFile          m_File;
286 
287     char*           m_Top;
288     char*           m_Current;
289 
290     SmCmdArray      m_CmdArray;
291 };
292 
293 
294 
295 #endif // SM_INITIAL_FILE_PARSER_H_
296