1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // VariableParserDlg.cpp : implementation file
21 #include "variable_parser.h"
22 #include "variable_parserDlg.h"
27 static char THIS_FILE[] = __FILE__;
30 using namespace NLMISC
;
33 /////////////////////////////////////////////////////////////////////////////
34 // CVariableParserDlg dialog
36 CVariableParserDlg::CVariableParserDlg(CWnd
* pParent
/*=NULL*/)
37 : CDialog(CVariableParserDlg::IDD
, pParent
)
39 //{{AFX_DATA_INIT(CVariableParserDlg)
40 // NOTE: the ClassWizard will add member initialization here
42 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
43 m_hIcon
= AfxGetApp()->LoadIcon(IDR_MAINFRAME
);
46 CVariableParserDlg::~CVariableParserDlg()
48 m_nomVariables
.clear();
49 for ( uint i
=0; i
<m_variables
.size(); i
++ )
51 m_variables
[i
].clear();
57 void CVariableParserDlg::DoDataExchange(CDataExchange
* pDX
)
59 CDialog::DoDataExchange(pDX
);
60 //{{AFX_DATA_MAP(CVariableParserDlg)
61 DDX_Control(pDX
, IDC_VARDEF
, m_varDefList
);
65 BEGIN_MESSAGE_MAP(CVariableParserDlg
, CDialog
)
66 //{{AFX_MSG_MAP(CVariableParserDlg)
69 ON_BN_CLICKED(IDC_HDR_BROWSE
, OnHdrBrowse
)
70 ON_BN_CLICKED(IDC_TMPL_BROWSE
, OnTmplBrowse
)
71 ON_BN_CLICKED(IDC_FOOT_BROWSE
, OnFootBrowse
)
72 ON_BN_CLICKED(IDC_OUTPUT_BROWSE
, OnOutputBrowse
)
73 ON_BN_CLICKED(IDC_GENERATE
, OnGenerate
)
74 ON_BN_CLICKED(IDC_GEN_BROWSE
, OnGenBrowse
)
75 ON_BN_CLICKED(IDC_LUA_BROWSE
, OnLUABrowse
)
76 ON_BN_CLICKED(IDC_ADDVARDEF
, OnAddvardef
)
77 ON_BN_CLICKED(IDC_REMVARDEF
, OnRemvardef
)
81 /////////////////////////////////////////////////////////////////////////////
82 // CVariableParserDlg message handlers
84 BOOL
CVariableParserDlg::OnInitDialog()
86 CDialog::OnInitDialog();
88 // Set the icon for this dialog. The framework does this automatically
89 // when the application's main window is not a dialog
90 SetIcon(m_hIcon
, TRUE
); // Set big icon
91 SetIcon(m_hIcon
, FALSE
); // Set small icon
93 // TODO: Add extra initialization here
95 return TRUE
; // return TRUE unless you set the focus to a control
98 // If you add a minimize button to your dialog, you will need the code below
99 // to draw the icon. For MFC applications using the document/view model,
100 // this is automatically done for you by the framework.
102 void CVariableParserDlg::OnPaint()
106 CPaintDC
dc(this); // device context for painting
108 SendMessage(WM_ICONERASEBKGND
, (WPARAM
) dc
.GetSafeHdc(), 0);
110 // Center icon in client rectangle
111 int cxIcon
= GetSystemMetrics(SM_CXICON
);
112 int cyIcon
= GetSystemMetrics(SM_CYICON
);
114 GetClientRect(&rect
);
115 int x
= (rect
.Width() - cxIcon
+ 1) / 2;
116 int y
= (rect
.Height() - cyIcon
+ 1) / 2;
119 dc
.DrawIcon(x
, y
, m_hIcon
);
127 // The system calls this to obtain the cursor to display while the user drags
128 // the minimized window.
129 HCURSOR
CVariableParserDlg::OnQueryDragIcon()
131 return (HCURSOR
) m_hIcon
;
135 void CVariableParserDlg::OnHdrBrowse()
137 CFileDialog
fileDlg( TRUE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
, "Primitive file (*.primitive)|*.primitive||");
139 if ( fileDlg
.DoModal() == IDOK
)
141 GetDlgItem( IDC_HDR_FILE
)->SetWindowText( fileDlg
.GetPathName() );
146 void CVariableParserDlg::OnTmplBrowse()
148 CFileDialog
fileDlg( TRUE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
, "Primitive file (*.primitive)|*.primitive||");
150 if ( fileDlg
.DoModal() == IDOK
)
152 GetDlgItem( IDC_TMPL_FILE
)->SetWindowText( fileDlg
.GetPathName() );
157 void CVariableParserDlg::OnFootBrowse()
159 CFileDialog
fileDlg( TRUE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
, "Primitive file (*.primitive)|*.primitive||");
161 if ( fileDlg
.DoModal() == IDOK
)
163 GetDlgItem( IDC_FOOT_FILE
)->SetWindowText( fileDlg
.GetPathName() );
168 void CVariableParserDlg::OnOutputBrowse()
170 CFileDialog
fileDlg( FALSE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
, "Primitive file (*.primitive)|*.primitive||");
172 if ( fileDlg
.DoModal() == IDOK
)
174 GetDlgItem( IDC_OUTPUT_FILE
)->SetWindowText( fileDlg
.GetPathName() );
179 void CVariableParserDlg::OnGenBrowse()
181 CFileDialog
fileDlg( TRUE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
, "Comma Separated Values (*.csv)|*.csv||");
183 if ( fileDlg
.DoModal() == IDOK
)
185 GetDlgItem( IDC_GEN_FILE
)->SetWindowText( fileDlg
.GetPathName() );
189 void CVariableParserDlg::OnLUABrowse()
191 CFileDialog
fileDlg( TRUE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
, "Lua files (*.lua)|*.lua||");
193 if ( fileDlg
.DoModal() == IDOK
)
195 GetDlgItem( IDC_LUA_FILE
)->SetWindowText( fileDlg
.GetPathName() );
199 void CleanString( CSString
&str
)
201 int i
= (int)str
.size();
204 while ( !ok
&& ( i
> 0 ) )
206 char c
= str
.c_str()[i
-1];
207 if ( !isalpha(c
) && !isdigit(c
) && ( c
!= '_' ) && ( c
!= ')' ) )
218 void CVariableParserDlg::ProcessGeneratorFile( const string
& generatorFile
)
220 CSString genData
, ligne
;
223 genData
.readFromFile( generatorFile
);
225 ligne
= genData
.splitTo( "\n", true );
227 while ( ligne
!= "" )
229 CSString var
= ligne
.splitTo( ";", true );
234 m_nomVariables
.push_back( var
) ;
236 m_variables
.push_back( vec
);
240 while ( genData
!= "" )
242 ligne
= genData
.splitTo( "\n", true );
246 for ( uint i
=0;i
<m_variables
.size(); i
++)
248 CSString var
= ligne
.splitTo( ";", true );
253 m_variables
[i
].push_back( var
);
260 ParseParameters params
;
261 BuildParseParameters( params
, 0, 0 );
265 void CVariableParserDlg::BuildParseParameters( ParseParameters
& params
, uint ligne
, uint colonne
)
267 CSString left
= CSString( m_nomVariables
[ colonne
] ).left(2);
271 //if ( colonne+1 >= params.capacity() )
272 params
.push_back( "" );
274 CString str
= m_variables
[colonne
][ligne
].c_str();
275 params
[colonne
] = str
;
277 if ( colonne
+1 == m_variables
.size() )
279 ParseTemplate( params
);
283 BuildParseParameters( params
, ligne
, colonne
+1 );
286 else if ( left
== "##" )
288 params
.push_back( "" );
289 CString str
= m_variables
[colonne
][0].c_str();
291 for ( uint j
=(uint
)m_nomVariables
.size(); j
>0; j
-- )
293 str
.Replace( toString( "C%d", j
-1 ).c_str(), params
[j
-1].c_str() );
298 m_luaState
.executeScript( str
.GetBuffer( str
.GetLength() ) );
302 MessageBox( toString( "%s", e
.luaWhat().c_str() ).c_str() );
307 m_luaState
.push("VAL");
308 m_luaState
.getTable(LUA_GLOBALSINDEX
);
310 sint type
= m_luaState
.type();
312 if ( type
== LUA_TBOOLEAN
)
315 bool val
= m_luaState
.toBoolean();
324 else if ( type
== LUA_TNUMBER
)
327 double val
= m_luaState
.toNumber();
329 // set double or integer?
330 if ( val
== floor( val
) )
331 str
.Format( "%d", sint64( floor( val
) ) );
333 str
.Format( "%.3f", val
);
336 else if ( type
== LUA_TSTRING
)
340 m_luaState
.toString(-1, val
);
349 MessageBox( toString( "VAL is not defined on column %d", colonne
).c_str() );
353 params
[colonne
] = str
;
354 if ( colonne
+1 == m_variables
.size() )
356 ParseTemplate( params
);
360 BuildParseParameters( params
, ligne
, colonne
+1 );
366 for ( uint i
=0; i
<m_variables
[colonne
].size(); i
++ )
368 //if ( colonne+1 >= params.capacity() )
369 params
.push_back( "" );
373 str
= m_variables
[colonne
][i
].c_str();
375 params
[colonne
] = str
;
377 if ( colonne
+1 == m_variables
.size() )
379 ParseTemplate( params
);
383 BuildParseParameters( params
, i
, colonne
+1 );
390 void CVariableParserDlg::ParseTemplate( const ParseParameters
& params
)
392 CString tmp
= m_templateText
.c_str();
394 for ( uint i
=0; i
< m_nomVariables
.size(); i
++ )
397 if ( CSString( m_nomVariables
[ i
] ).left(2) == "@@" )
399 toFind
= toString( "%s", m_nomVariables
[i
].c_str() );
401 else if ( CSString( m_nomVariables
[ i
] ).left(2) == "##" )
403 toFind
= toString( "%s", m_nomVariables
[i
].c_str() );
406 toFind
= toString( "$$%s", m_nomVariables
[i
].c_str() );
408 tmp
.Replace( toFind
.c_str(), params
[i
].c_str() );
414 void CVariableParserDlg::OnGenerate()
419 CString hdrFilename
, templFileName
, footFileName
,
420 outputFileName
, luaFileName
;
423 GetDlgItem( IDC_HDR_FILE
)->GetWindowText( hdrFilename
);
424 if ( hdrFilename
== "" )
425 error
+= "Header file name not set.\n";
427 GetDlgItem( IDC_FOOT_FILE
)->GetWindowText( footFileName
);
428 if ( footFileName
== "" )
429 error
+= "Foot file name not set.\n";
431 GetDlgItem( IDC_TMPL_FILE
)->GetWindowText( templFileName
);
432 if ( templFileName
== "" )
433 error
+= "Template file name not set.\n";
435 if ( m_varDefList
.GetCount() == 0 )
436 error
+= "Generator file name not set.\n";
438 GetDlgItem( IDC_OUTPUT_FILE
)->GetWindowText( outputFileName
);
439 if ( outputFileName
== "" )
440 error
+= "Output file name not set.";
444 MessageBox( error
, NULL
, MB_OK
);
448 GetDlgItem( IDC_LUA_FILE
)->GetWindowText( luaFileName
);
449 if ( luaFileName
!= "" )
451 m_luaState
.executeFile( std::string( luaFileName
) );
454 hdr
.readFromFile( std::string( hdrFilename
) );
455 foot
.readFromFile( std::string( footFileName
) );
456 m_templateText
.readFromFile( std::string( templFileName
) );
459 for ( int i
=0; i
<m_varDefList
.GetCount(); i
++ )
462 m_varDefList
.GetText( i
, str
);
463 ProcessGeneratorFile( std::string( str
) );
467 m_outputText
+= foot
;
468 m_outputText
.writeToFile( std::string( outputFileName
) );
470 MessageBox( "Done !" );
474 void CVariableParserDlg::OnAddvardef()
476 CFileDialog
fileDlg( TRUE
, NULL
, NULL
, OFN_OVERWRITEPROMPT
|OFN_ALLOWMULTISELECT
,
477 "Comma Separated Values (*.csv)|*.csv||");
479 if ( fileDlg
.DoModal() == IDOK
)
481 for ( POSITION pos
= fileDlg
.GetStartPosition(); pos
!= NULL
; )
483 CString str
= fileDlg
.GetNextPathName(pos
);
484 int index
= m_varDefList
.FindStringExact( 0, str
);
485 if ( index
== LB_ERR
)
487 m_varDefList
.SetCurSel( m_varDefList
.AddString( str
) );
490 m_varDefList
.SetCurSel( index
);
495 void CVariableParserDlg::OnRemvardef()
497 int index
= m_varDefList
.GetCurSel();
498 if ( index
!= LB_ERR
)
499 m_varDefList
.DeleteString( index
);