// Ryzom - MMORPG Framework
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
// VariableParserDlg.cpp : implementation file
//
#include "stdafx.h"
#include "variable_parser.h"
#include "variable_parserDlg.h"
/*#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif*/
using namespace NLMISC;
using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CVariableParserDlg dialog
CVariableParserDlg::CVariableParserDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVariableParserDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVariableParserDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CVariableParserDlg::~CVariableParserDlg()
{
m_nomVariables.clear();
for ( uint i=0; iSetWindowText( fileDlg.GetPathName() );
}
}
void CVariableParserDlg::OnTmplBrowse()
{
CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
if ( fileDlg.DoModal() == IDOK )
{
GetDlgItem( IDC_TMPL_FILE )->SetWindowText( fileDlg.GetPathName() );
}
}
void CVariableParserDlg::OnFootBrowse()
{
CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
if ( fileDlg.DoModal() == IDOK )
{
GetDlgItem( IDC_FOOT_FILE )->SetWindowText( fileDlg.GetPathName() );
}
}
void CVariableParserDlg::OnOutputBrowse()
{
CFileDialog fileDlg( FALSE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
if ( fileDlg.DoModal() == IDOK )
{
GetDlgItem( IDC_OUTPUT_FILE )->SetWindowText( fileDlg.GetPathName() );
}
}
void CVariableParserDlg::OnGenBrowse()
{
CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Comma Separated Values (*.csv)|*.csv||");
if ( fileDlg.DoModal() == IDOK )
{
GetDlgItem( IDC_GEN_FILE )->SetWindowText( fileDlg.GetPathName() );
}
}
void CVariableParserDlg::OnLUABrowse()
{
CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Lua files (*.lua)|*.lua||");
if ( fileDlg.DoModal() == IDOK )
{
GetDlgItem( IDC_LUA_FILE )->SetWindowText( fileDlg.GetPathName() );
}
}
void CleanString( CSString &str )
{
int i = str.size();
bool ok = false;
while ( !ok && ( i > 0 ) )
{
char c = str.c_str()[i-1];
if ( !isalpha(c) && !isdigit(c) && ( c != '_' ) && ( c != ')' ) )
{
i--;
}
else
ok = true;
}
str = str.left( i );
}
void CVariableParserDlg::ProcessGeneratorFile( const string& generatorFile )
{
CSString genData, ligne;
int colonneVar = 0;
genData.readFromFile( generatorFile );
ligne = genData.splitTo( "\n", true );
while ( ligne != "" )
{
CSString var = ligne.splitTo( ";", true );
CleanString( var );
if ( var != "" )
{
m_nomVariables.push_back( var ) ;
vector vec;
m_variables.push_back( vec );
}
}
while ( genData != "" )
{
ligne = genData.splitTo( "\n", true );
if ( ligne != "" )
{
for ( uint i=0;i= params.capacity() )
params.push_back( "" );
CString str = m_variables[colonne][ligne].c_str();
params[colonne] = str;
if ( colonne+1 == m_variables.size() )
{
ParseTemplate( params );
}
else
{
BuildParseParameters( params, ligne, colonne+1 );
}
}
else if ( left == "##" )
{
params.push_back( "" );
CString str = m_variables[colonne][0].c_str();
for ( uint j=m_nomVariables.size(); j>0; j-- )
{
str.Replace( toString( "C%d", j-1 ).c_str(), params[j-1].c_str() );
}
try
{
m_luaState.executeScript( str.GetBuffer( str.GetLength() ) );
}
catch (ELuaError &e)
{
MessageBox( toString( "%s", e.luaWhat().c_str() ).c_str() );
return;
}
m_luaState.push("VAL");
m_luaState.getTable(LUA_GLOBALSINDEX);
bool ok= false;
sint type= m_luaState.type();
if ( type == LUA_TBOOLEAN )
{
// get and pop
bool val= m_luaState.toBoolean();
m_luaState.pop();
// set result
if ( val )
str = "TRUE";
else
str = "FALSE";
ok = true;
}
else if ( type == LUA_TNUMBER )
{
// get and pop
double val= m_luaState.toNumber();
m_luaState.pop();
// set double or integer?
if ( val == floor( val ) )
str.Format( "%d", sint64( floor( val ) ) );
else
str.Format( "%.3f", val );
ok = true;
}
else if ( type == LUA_TSTRING )
{
// get and pop
std::string val;
m_luaState.toString(-1, val);
m_luaState.pop();
// set result
str = val.c_str();
ok = true;
}
if ( !ok )
{
MessageBox( toString( "VAL is not defined on column %d", colonne ).c_str() );
}
else
{
params[colonne] = str;
if ( colonne+1 == m_variables.size() )
{
ParseTemplate( params );
}
else
{
BuildParseParameters( params, ligne, colonne+1 );
}
}
}
else
{
for ( uint i=0; i= params.capacity() )
params.push_back( "" );
CString str;
str = m_variables[colonne][i].c_str();
params[colonne] = str;
if ( colonne+1 == m_variables.size() )
{
ParseTemplate( params );
}
else
{
BuildParseParameters( params, i, colonne+1 );
}
}
}
}
void CVariableParserDlg::ParseTemplate( const ParseParameters& params )
{
CString tmp = m_templateText.c_str();
for ( uint i=0; i< m_nomVariables.size(); i++ )
{
CSString toFind;
if ( CSString( m_nomVariables[ i ] ).left(2) == "@@" )
{
toFind = toString( "%s", m_nomVariables[i].c_str() );
}
else if ( CSString( m_nomVariables[ i ] ).left(2) == "##" )
{
toFind = toString( "%s", m_nomVariables[i].c_str() );
}
else
toFind = toString( "$$%s", m_nomVariables[i].c_str() );
tmp.Replace( toFind.c_str(), params[i].c_str() );
}
m_outputText += tmp;
}
void CVariableParserDlg::OnGenerate()
{
CString error = "";
CSString hdr, foot;
CString hdrFilename, templFileName, footFileName,
outputFileName, luaFileName;
GetDlgItem( IDC_HDR_FILE )->GetWindowText( hdrFilename );
if ( hdrFilename == "" )
error += "Header file name not set.\n";
GetDlgItem( IDC_FOOT_FILE )->GetWindowText( footFileName );
if ( footFileName == "" )
error += "Foot file name not set.\n";
GetDlgItem( IDC_TMPL_FILE )->GetWindowText( templFileName );
if ( templFileName == "" )
error += "Template file name not set.\n";
if ( m_varDefList.GetCount() == 0 )
error += "Generator file name not set.\n";
GetDlgItem( IDC_OUTPUT_FILE )->GetWindowText( outputFileName );
if ( outputFileName == "" )
error += "Output file name not set.";
if ( error != "" )
{
MessageBox( error, NULL, MB_OK );
}
else
{
GetDlgItem( IDC_LUA_FILE )->GetWindowText( luaFileName );
if ( luaFileName != "" )
{
m_luaState.executeFile( std::string( luaFileName ) );
}
hdr.readFromFile( std::string( hdrFilename ) );
foot.readFromFile( std::string( footFileName ) );
m_templateText.readFromFile( std::string( templFileName ) );
m_outputText = hdr;
for ( int i=0; i