502571e982
- migliorata in Lua EgtOutBox - aggiunta in Lua EgtFileDialog - aggiunta in Lua EgtGetCalcToolDirFromaAngles.
555 lines
16 KiB
C++
555 lines
16 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2014-2014
|
|
//----------------------------------------------------------------------------
|
|
// File : LUA_General.cpp Data : 27.09.14 Versione : 1.5i5
|
|
// Contenuto : Funzioni generali per LUA.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 27.09.14 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "EXE.h"
|
|
#include "LUA.h"
|
|
#include "GenTools.h"
|
|
#include "/EgtDev/Include/ExeExecutor.h"
|
|
#include "/EgtDev/Include/EGkLuaAux.h"
|
|
#include "/EgtDev/Include/EGnStringUtils.h"
|
|
#include "/EgtDev/Include/EGnFileUtils.h"
|
|
#include "/EgtDev/Include/EGnFileCompare.h"
|
|
#include "/EgtDev/Include/EgtPerfCounter.h"
|
|
#include "/EgtDev/Include/EgtIniFile.h"
|
|
#include "/EgtDev/Include/EgtStringConverter.h"
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
static PerformanceCounter s_Counter ;
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
MyPrint( lua_State* L)
|
|
{
|
|
string sOut ;
|
|
int n = lua_gettop( L) ; /* number of arguments */
|
|
lua_getglobal( L, "tostring") ;
|
|
for ( int i = 1 ; i <= n ; ++ i) {
|
|
const char* s ;
|
|
lua_pushvalue( L, -1) ; /* function to be called */
|
|
lua_pushvalue( L, i) ; /* value to print */
|
|
lua_call( L, 1, 1) ;
|
|
s = lua_tostring( L, -1) ; /* get result */
|
|
if ( s == nullptr)
|
|
return luaL_error( L,
|
|
LUA_QL("tostring") " must return a string to " LUA_QL("print")) ;
|
|
if ( i > 1)
|
|
sOut += "\t" ;
|
|
sOut += s ;
|
|
lua_pop( L, 1) ; /* pop result */
|
|
}
|
|
if ( ! sOut.empty())
|
|
LOG_INFO( GetLogger(), sOut.c_str())
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaEvalNumExpr( lua_State* L)
|
|
{
|
|
// un solo parametro stringa : sExpr
|
|
string sExpr ;
|
|
LuaCheckParam( L, 1, sExpr)
|
|
LuaClearStack( L) ;
|
|
// valuto l'espressione
|
|
double dVal ;
|
|
bool bOk = ExeLuaEvalNumExpr( sExpr, &dVal) ;
|
|
// restituisco il risultato
|
|
if ( bOk)
|
|
LuaSetParam( L, dVal) ;
|
|
else
|
|
LuaSetParam( L) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaPause( lua_State* L)
|
|
{
|
|
// 1 parametro : numero di millisecondi
|
|
int nTime ;
|
|
LuaCheckParam( L, 1, nTime)
|
|
LuaClearStack( L) ;
|
|
// controllo la durata della pausa e la eseguo
|
|
const int MIN_TIME = 0 ;
|
|
const int MAX_TIME = 10000 ;
|
|
if ( nTime < MIN_TIME)
|
|
nTime = MIN_TIME ;
|
|
else if ( nTime > MAX_TIME)
|
|
nTime = MAX_TIME ;
|
|
Sleep( nTime) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, true) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaStartCounter( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// avvio il contatore
|
|
s_Counter.Start() ;
|
|
return 0 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaStopCounter( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// fermo il contatore
|
|
double dTime = s_Counter.Stop() ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, dTime) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaNumToString( lua_State* L)
|
|
{
|
|
// 1 o 2 parametri : dVal [, nDec]
|
|
double dVal ;
|
|
LuaCheckParam( L, 1, dVal)
|
|
int nDec = 3 ;
|
|
LuaGetParam( L, 2, nDec) ;
|
|
LuaClearStack( L) ;
|
|
// costruisco la stringa
|
|
string sVal = ToString( dVal, nDec) ;
|
|
// ritorno il risultato
|
|
LuaSetParam( L, sVal) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaSplitString( lua_State* L)
|
|
{
|
|
// 1 o 2 parametri : sVal [, sSep]
|
|
string sVal ;
|
|
string sSep = "," ;
|
|
bool bFound = LuaGetParam( L, 1, sVal) ;
|
|
LuaGetParam( L, 2, sSep) ;
|
|
LuaClearStack( L) ;
|
|
// se ricevuta stringa, la divido in parti separate da ','
|
|
if ( bFound) {
|
|
STRVECTOR vsVal ;
|
|
Tokenize( sVal, sSep, vsVal) ;
|
|
// ritorno il risultato
|
|
LuaSetParam( L, vsVal) ;
|
|
}
|
|
// altrimenti restituisco nil
|
|
else
|
|
LuaSetParam( L) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaSplitStringPlus( lua_State* L)
|
|
{
|
|
// 2 parametri : sVal, sHea
|
|
string sVal ;
|
|
string sHea ;
|
|
bool bFound = LuaGetParam( L, 1, sVal) ;
|
|
LuaCheckParam( L, 2, sHea)
|
|
LuaClearStack( L) ;
|
|
// se ricevuta stringa, la divido in parti conservandone le intestazioni
|
|
if ( bFound) {
|
|
STRVECTOR vsVal ;
|
|
TokenizePlus( sVal, sHea, vsVal) ;
|
|
// ritorno il risultato
|
|
LuaSetParam( L, vsVal) ;
|
|
}
|
|
// altrimenti restituisco nil
|
|
else
|
|
LuaSetParam( L) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaExecTsc( lua_State* L)
|
|
{
|
|
// 1 parametro : file
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
LuaClearStack( L) ;
|
|
// eseguo lo script TSC
|
|
bool bOk = ExeTscExecFile( sFile) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaOutLog( lua_State* L)
|
|
{
|
|
// 1 parametro : stringa da emettere
|
|
string sOut ;
|
|
LuaCheckParam( L, 1, sOut)
|
|
LuaClearStack( L) ;
|
|
// accodo il messaggio nel file di log
|
|
LOG_INFO( GetLogger(), sOut.c_str())
|
|
// non c'è risultato
|
|
return 0 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaOutBox( lua_State* L)
|
|
{
|
|
// 3 o 4 parametri : stringa, titolo, icona [, bModal]
|
|
string sOut ;
|
|
LuaCheckParam( L, 1, sOut)
|
|
string sTitle ;
|
|
LuaCheckParam( L, 2, sTitle)
|
|
string sIcon ;
|
|
LuaCheckParam( L, 3, sIcon)
|
|
int nIcon = MB_ICONINFORMATION ;
|
|
ToUpper( sIcon) ;
|
|
if ( sIcon == "ERROR")
|
|
nIcon = MB_ICONERROR ;
|
|
else if ( sIcon == "WARNING")
|
|
nIcon = MB_ICONWARNING ;
|
|
bool bModal = true ;
|
|
LuaGetParam( L, 4, bModal) ;
|
|
LuaClearStack( L) ;
|
|
// emetto la finestra di dialogo
|
|
int nRes = MessageBox( FindTopWindow(), stringtoW( sOut), stringtoW( sTitle),
|
|
MB_OKCANCEL | nIcon | ( bModal ? MB_TASKMODAL : MB_SYSTEMMODAL)) ;
|
|
// risultato (Ok->true, Cancel->false)
|
|
LuaSetParam( L, ( nRes == IDOK)) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaProcessEvents( lua_State* L)
|
|
{
|
|
// 2 parametri : nProg, nPause
|
|
int nProg ;
|
|
LuaCheckParam( L, 1, nProg)
|
|
int nPause ;
|
|
LuaCheckParam( L, 2, nPause)
|
|
LuaClearStack( L) ;
|
|
// lancio aggiornamento interfaccia
|
|
int nRes = ExeProcessEvents( nProg, nPause) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, nRes) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaOutText( lua_State* L)
|
|
{
|
|
// 1 parametro : sText
|
|
string sText ;
|
|
LuaCheckParam( L, 1, sText)
|
|
LuaClearStack( L) ;
|
|
// lancio emissione testo
|
|
bool bOk = ExeOutText( sText) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaFileDialog( lua_State* L)
|
|
{
|
|
// 3 parametri : bOpenVsSave, sFile, sFilter
|
|
bool bOpenVsSave ;
|
|
LuaCheckParam( L, 1, bOpenVsSave)
|
|
string sFile ;
|
|
LuaCheckParam( L, 2, sFile)
|
|
string sFilter ;
|
|
LuaCheckParam( L, 3, sFilter)
|
|
LuaClearStack( L) ;
|
|
// Converto i parametri nel formato wide e cambio L'|' in L'\0' nel filtro
|
|
AtoWEX<MAX_PATH> wsFileName( sFile.c_str()) ;
|
|
AtoW wsFilter( sFilter.c_str()) ;
|
|
for ( wchar_t* pC = wsFilter.m_psz ; *pC != L'\0' ; ++ pC) {
|
|
if ( *pC == L'|')
|
|
*pC = L'\0' ;
|
|
}
|
|
// Riempio la struttura dati per il dialogo
|
|
OPENFILENAME ofn ;
|
|
ZeroMemory( &ofn, sizeof( ofn)) ;
|
|
ofn.lStructSize = sizeof( ofn) ;
|
|
ofn.hwndOwner = FindTopWindow() ;
|
|
ofn.lpstrFilter = LPWSTR( wsFilter) ;
|
|
ofn.lpstrFile = LPWSTR( wsFileName) ;
|
|
ofn.nMaxFile = MAX_PATH ;
|
|
ofn.Flags = OFN_DONTADDTORECENT | OFN_PATHMUSTEXIST ;
|
|
// Visualizzo il dialogo
|
|
if ( bOpenVsSave) {
|
|
ofn.Flags |= OFN_FILEMUSTEXIST ;
|
|
if ( GetOpenFileName( &ofn) != FALSE)
|
|
LuaSetParam( L, wstrztoA( wsFileName)) ;
|
|
else
|
|
LuaSetParam( L) ;
|
|
}
|
|
else {
|
|
ofn.Flags |= OFN_OVERWRITEPROMPT ;
|
|
if ( GetSaveFileName( &ofn) != FALSE)
|
|
LuaSetParam( L, wstrztoA( wsFileName)) ;
|
|
else
|
|
LuaSetParam( L) ;
|
|
}
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaExistsFile( lua_State* L)
|
|
{
|
|
// 1 parametro : sFile
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
LuaClearStack( L) ;
|
|
// verifico esistenza file
|
|
bool bOk = ExistsFile( sFile) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaCopyFile( lua_State* L)
|
|
{
|
|
// 2 parametri : sFile, sNewFile
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
string sNewFile ;
|
|
LuaCheckParam( L, 2, sNewFile)
|
|
LuaClearStack( L) ;
|
|
// copio il file
|
|
bool bOk = CopyFileEgt( sFile, sNewFile) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRenameFile( lua_State* L)
|
|
{
|
|
// 2 parametri : sFile, sNewFile
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
string sNewFile ;
|
|
LuaCheckParam( L, 2, sNewFile)
|
|
LuaClearStack( L) ;
|
|
// rinomino il file
|
|
bool bOk = RenameFile( sFile, sNewFile) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaEraseFile( lua_State* L)
|
|
{
|
|
// 1 parametro : file
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
LuaClearStack( L) ;
|
|
// cancello il file
|
|
bool bOk = EraseFile( sFile) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaEmptyDirectory( lua_State* L)
|
|
{
|
|
// 1 parametro : dir
|
|
string sDir ;
|
|
LuaCheckParam( L, 1, sDir)
|
|
LuaClearStack( L) ;
|
|
// svuoto il direttorio
|
|
bool bOk = EmptyDirectory( sDir) ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaTextFileCompare( lua_State* L)
|
|
{
|
|
// 4 parametri : file1, file2, inizio commento, file diff
|
|
string sFile1 ;
|
|
LuaCheckParam( L, 1, sFile1)
|
|
string sFile2 ;
|
|
LuaCheckParam( L, 2, sFile2)
|
|
string sRemStart ;
|
|
LuaCheckParam( L, 3, sRemStart)
|
|
string sFileDiff ;
|
|
LuaCheckParam( L, 4, sFileDiff)
|
|
LuaClearStack( L) ;
|
|
// eseguo il confronto
|
|
bool bOk = TextFileCompare( sFile1, sFile2, sRemStart, sFileDiff) ;
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaBinaryFileCompare( lua_State* L)
|
|
{
|
|
// 3 parametri : file1, file2, file diff
|
|
string sFile1 ;
|
|
LuaCheckParam( L, 1, sFile1)
|
|
string sFile2 ;
|
|
LuaCheckParam( L, 2, sFile2)
|
|
string sFileDiff ;
|
|
LuaCheckParam( L, 3, sFileDiff)
|
|
LuaClearStack( L) ;
|
|
// eseguo il confronto
|
|
bool bOk = BinaryFileCompare( sFile1, sFile2, sFileDiff) ;
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaGetStringFromIni( lua_State* L)
|
|
{
|
|
// 4 parametri : sSec, sKey, sDef, sIniFile
|
|
string sSec ;
|
|
LuaCheckParam( L, 1, sSec)
|
|
string sKey ;
|
|
LuaCheckParam( L, 2, sKey)
|
|
string sDef ;
|
|
LuaCheckParam( L, 3, sDef)
|
|
string sIniFile ;
|
|
LuaCheckParam( L, 4, sIniFile)
|
|
LuaClearStack( L) ;
|
|
// leggo da file INI
|
|
string sVal = GetPrivateProfileStringUtf8( sSec.c_str(), sKey.c_str(), sDef.c_str(), sIniFile.c_str()) ;
|
|
LuaSetParam( L, sVal) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaWriteStringToIni( lua_State* L)
|
|
{
|
|
// 4 parametri : sSec, sKey, sVal, sIniFile
|
|
string sSec ;
|
|
LuaCheckParam( L, 1, sSec)
|
|
string sKey ;
|
|
LuaCheckParam( L, 2, sKey)
|
|
string sVal ;
|
|
LuaCheckParam( L, 3, sVal)
|
|
string sIniFile ;
|
|
LuaCheckParam( L, 4, sIniFile)
|
|
LuaClearStack( L) ;
|
|
// scrivo su file INI
|
|
bool bOk = WritePrivateProfileStringUtf8( sSec.c_str(), sKey.c_str(), sVal.c_str(), sIniFile.c_str()) ;
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaGetVersion( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// costruisco la stringa con le versioni
|
|
string sVer ;
|
|
if ( ! ExeGetVersionInfo( sVer, "\n"))
|
|
sVer = "VersionInfo error" ;
|
|
// restituisco il risultato
|
|
LuaSetParam( L, sVer) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaIs64bit( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// restituisco il risultato
|
|
#if defined( _WIN64)
|
|
bool bRes = true ;
|
|
#elif defined( _WIN32)
|
|
bool bRes = false ;
|
|
#endif
|
|
LuaSetParam( L, bRes) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaGetLanguage( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// recupero il nome della lingua corrente
|
|
string sLang = ExeGetLanguage() ;
|
|
LuaSetParam( L, sLang) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
bool
|
|
LuaInstallGeneral( LuaMgr& luaMgr)
|
|
{
|
|
bool bOk = ( &luaMgr != nullptr) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "print", MyPrint) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtEvalNumExpr", LuaEvalNumExpr) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtPause", LuaPause) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtStartCounter", LuaStartCounter) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtStopCounter", LuaStopCounter) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtNumToString", LuaNumToString) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtSplitString", LuaSplitString) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtSplitStringPlus", LuaSplitStringPlus) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtExecTsc", LuaExecTsc) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtOutLog", LuaOutLog) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtOutBox", LuaOutBox) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtProcessEvents", LuaProcessEvents) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtOutText", LuaOutText) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtFileDialog", LuaFileDialog) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtExistsFile", LuaExistsFile) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtCopyFile", LuaCopyFile) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRenameFile", LuaRenameFile) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtEraseFile", LuaEraseFile) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtEmptyDirectory", LuaEmptyDirectory) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtTextFileCompare", LuaTextFileCompare) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtBinaryFileCompare", LuaBinaryFileCompare) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtGetStringFromIni", LuaGetStringFromIni) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtWriteStringToIni", LuaWriteStringToIni) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtGetVersion", LuaGetVersion) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtIs64bit", LuaIs64bit) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtGetLanguage", LuaGetLanguage) ;
|
|
return bOk ;
|
|
}
|