//////////////////////////////////////////////////////////////////////
// Task.cpp: implementation of the wxTask class.
//////////////////////////////////////////////////////////////////////
#include <wx/log.h>
#include <wx/filename.h>
#include "task.h"
#include "declarations.h"
#include "commonfunctions.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////
//// class wxTaskParameters //////////////////////////////
//////////////////////////////////////////////////////////
void TestArg(const wxString arg, int argc, int i)
{
if( ++i >= argc )
{
wxLogError(_U("ERR: Не задан параметр ключа %s"), arg.c_str());
exit(1);
}
}
void wxTaskParameters::ParseCommandLine(int argc, wxChar ** argv)
{
wxString DirName = _U("SRC");
wxString FileName = _U("1cv7.md");
bool PreserveDir = false;
if (argc <= 1) //no arguments at all
{
TaskType = help;
return;
}
for( int i=1; i < argc; i++ )
{
wxString arg;
arg = wxString(argv[i], wxConvUTF8);
if(!arg.Cmp(_U("-h")))
{
TaskType = help;
}
else if(!arg.Cmp(_U("--version")))
{
TaskType = show_version;
}
else if(!arg.Cmp(_U("-c")))
{
TaskType = compile;
FileName = _U("1cv7.new.md"); //на всякий случай
}
else if(!arg.Cmp(_U("-d")))
{
TaskType = decompile;
}
else if(!arg.Cmp(_U("-v")))
{
Verbose = 1;
}
else if(!arg.Cmp(_U("-vv")))
{
Verbose = 2;
}
else if(!arg.Cmp(_U("-vvv")))
{
Verbose = 3;
}
else if(!arg.Cmp(_U("-q")))
{
Verbose = -1;
}
else if(!arg.Cmp(_U("-D")))
{
TestArg(arg, argc, i);
arg.sprintf(_U("%s"), argv[++i]);
DirName = arg;
}
else if(!arg.Cmp(_U("-DD")))
{
TestArg(arg, argc, i);
arg.sprintf(_U("%s"), argv[++i]);
DirName = arg;
PreserveDir = true;
}
else if(!arg.Cmp(_U("-F")))
{
TestArg(arg, argc, i);
arg.sprintf(_U("%s"), argv[++i]);
FileName = arg;
}
else if(!arg.Cmp(_U("--external-report")))
{
CompoundType = external_report;
}
else if(!arg.Cmp(_U("--meta-data")))
{
CompoundType = meta_data;
}
else if(!arg.Cmp(_U("--no-order")))
{
NoOrdering = true;
}
else if(!arg.Cmp(_U("--no-profiles")))
{
NoProfiles = true;
}
else if(!arg.Cmp(_U("--no-empty-mxl")))
{
NoEmptyMxl = true;
}
else if(!arg.Cmp(_U("--truncate-mms")))
{
TruncateMMS = true;
}
else if(!arg.Cmp(_U("--eng")))
{
Language = lang_Eng;
}
else if(!arg.Cmp(_U("--translit")))
{
Language = lang_Translit;
}
else if(!arg.Cmp(_U("--no-defaults")))
{
NoDefaults = true;
}
else if(!arg.Cmp(_U("--no-version")))
{
SaveVersion = false;
}
else if(!arg.Cmp(_U("--filter")))
{
TestArg(arg, argc, i);
arg.sprintf(_U("%s"), argv[++i]);
MetadataPath = arg;
}
else if(!arg.Cmp(_U("--more-subfolders")))
{
MetadataBySubfolders = true;
}
else if(!arg.Cmp(_U("--no-empty-folders")))
{
NoEmptyFolders = true;
}
else if(!arg.Cmp(_U("--sort-rights")))
{
SortUserRightsObjects = true;
}
else if(!arg.Cmp(_U("--no-parse-dialogs")))
{
NotParseForms = true;
}
else if(!arg.Cmp(_U("--sort-form-controls")))
{
SortFormControls = true;
}
else if(!arg.Cmp(_U("--no-ini")))
{
CreateIniFile = false;
}
else if(!arg.Cmp(_U("--no-broken-links")))
{
NoBrokenLinks = true;
}
else if(!arg.Cmp(_U("--save-all-rights")))
{
MakeUserRightsInOneFile = true;
}
//Экспериментальная опция. Нужно будет убрать.
else if(!arg.Cmp(_U("--name-tr")))
{
TestArg(arg, argc, i);
arg.sprintf(_U("%s"), argv[++i]);
ObjectName = arg;
TaskType = name_translation;
}
else
{
wxLogError(_U("Invalid option '%s'"), arg.c_str());
TaskType = help;
return;
}
}
//Copy file and dir names into class variables
csCompoundFileName = FileName;
//Split file name on parts
wxFileName fname(FileName);
//Try to determine file type by extension
if( CompoundType == by_extension )
{
if(!fname.GetExt().CmpNoCase(_U(".ert")))
CompoundType = external_report;
else
CompoundType = meta_data;
}
//In case of external report add its name to directory.
//When directory specified explicitly leave its name unchanged
wxFileName dir(DirName);
if( (CompoundType == external_report) && !PreserveDir )
dir.SetName(fname.GetName());
csDirectoryName = dir.GetFullPath();
//Даже если пользователь и указал опцию, мы плюнем на неё если это не внешний отчет
if( (CompoundType != external_report) ) TruncateMMS = false;
DirectoryName = csDirectoryName;
CompoundFileName = csCompoundFileName;
}
//////////////////////////////////////////////////////////
//// class wxTask ///////////////////////////////////////
//////////////////////////////////////////////////////////
wxTask::wxTask(int argc, wxChar ** argv)
{
GComp_SetDefaults(&TaskParameters);
TaskParameters.ParseCommandLine(argc, argv);
}
void wxTask::Action()
{
switch( TaskParameters.TaskType )
{
case wxTaskParameters::show_version:
wxLogError(_U("GComp version %s"), GComp_GetVersion().c_str());
break;
case wxTaskParameters::help:
Help();
break;
case wxTaskParameters::compile:
Compile();
break;
case wxTaskParameters::decompile:
Decompile();
break;
//Экспериментальный код. Нужно будет убрать.
case wxTaskParameters::name_translation:
GComp_MakeNamePath(TaskParameters.DirectoryName, FALSE,
TaskParameters.ObjectName, 0);
break;
}
}
void wxTask::Decompile()
{
wxDateTime Time = wxDateTime::Now();
if( TaskParameters.Verbose > 1) Msg(1, _U("\nExtraction started: %s"), Time.Format(_U("%Y.%m.%d %H:%M:%S")).c_str());
GComp_ReadSettings(&TaskParameters);
if( !GComp_Decompile(&TaskParameters) )
exit(1);
if( TaskParameters.CreateIniFile )
GComp_SaveSettings(&TaskParameters);
if( TaskParameters.CompoundType != external_report || TaskParameters.Verbose > 1)
{
wxTimeSpan ExtractionTime = wxDateTime::Now() - Time;
Msg(1, _U("'%s' extracted into '%s' in %s"),
TaskParameters.CompoundFileName.c_str(), TaskParameters.DirectoryName.c_str(),
ExtractionTime.Format(_T("%H:%M:%S")).c_str());
}
}
void wxTask::Compile()
{
wxDateTime Time = wxDateTime::Now();
if( TaskParameters.Verbose > 1) wxLogMessage(_U("\nCompilation started: %s"), Time.Format(_U("%Y.%m.%d %H:%M:%S")).c_str());
GComp_ReadSettings(&TaskParameters);
if( !GComp_Compile(&TaskParameters) )
exit(1);
wxTimeSpan CompilationTime = wxDateTime::Now() - Time;
wxLogMessage(_U("Dir '%s' compiled into '%s' in %s"),
TaskParameters.DirectoryName.c_str(), TaskParameters.CompoundFileName.c_str(),
CompilationTime.Format(_U("%H:%M:%S")).c_str());
}