Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromagician committed Apr 9, 2022
1 parent dad9f60 commit f627476
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 202 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vs/
Debug/
Release/
/project.aps
61 changes: 45 additions & 16 deletions MessageBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,54 @@
#include "UTL_Cmd.h"
#include "UTL_Conversion.h"

int _tmain(int argc, _TCHAR* argv[])
int _tmain(int _argc, _TCHAR* _pArgv[])
{
int iCorrectParameters = 0;
bool bHelp = false;
wstring title = _T("");
wstring message = _T("");
UTL_Cmd::Type type = UTL_Cmd::Type::Ok;
UTL_Cmd::Icon icon = UTL_Cmd::Icon::NoIcon;
UTL_Cmd::DefaultButton def = UTL_Cmd::DefaultButton::Def1;

if (UTL_Cmd::ParseCommandLinbe(argc, argv, iCorrectParameters, bHelp, title, message, type, icon, def) != 0)
bHelp = true;

if (bHelp || iCorrectParameters == 0) {
UTL_Cmd::Help();
return 0;
int correctParameters = 0;
bool help = false;
wstring title = _T("");
wstring message = _T("");
UTL_Cmd::Type type = UTL_Cmd::Type::Ok;
UTL_Cmd::Icon icon = UTL_Cmd::Icon::NoIcon;
UTL_Cmd::DefaultButton def = UTL_Cmd::DefaultButton::Def1;

map<wstring, UINT> string2icon{
{_T("noicon"), 0x00000000L},
{_T("information"), MB_ICONINFORMATION},
{_T("question"), MB_ICONQUESTION},
{_T("warning"), MB_ICONWARNING},
{_T("error"), MB_ICONERROR}
};

map<wstring, UINT> string2button{
{_T("ok"), MB_OK},
{_T("okcancel"), MB_OKCANCEL},
{_T("yesno"), MB_YESNO},
{_T("yesnocancel"), MB_YESNOCANCEL}
};

map<wstring, UINT> string2default{
{_T("1"), MB_DEFBUTTON1},
{_T("2"), MB_DEFBUTTON2},
{_T("3"), MB_DEFBUTTON3}
};

UTL_Cmd cmd;
cmd.Add(UTL_Cmd::_STRING, 2, _T("-title"), _T("-t"), _T("The 'xxx' argument specifies the title of the message box."), &title);
cmd.Add(UTL_Cmd::_STRING, 2, _T("-message"), _T("-m"), _T("The 'xxx' argument specifies the text of the message box. Newline can be embedded using \\n."), &message);
cmd.Add(UTL_Cmd::_TRUE, 3, _T("-help"), _T("-h"), _T("-?"), _T("To view help."), &help);
cmd.Add(UTL_Cmd::_ENUM, 2, _T("-icon"), _T("-i"), _T("The 'xxx' argument specifies the text of the message box. Allowed options: NoIcon|Information|Question|Warning|Error."), &icon, &string2icon);
cmd.Add(UTL_Cmd::_ENUM, 2, _T("-button"), _T("-b"), _T("The 'xxx' argument specifies the type of the message box. Allowed options: Ok|OkCancel|YesNo|YesNoCancel."), &type, &string2button);
cmd.Add(UTL_Cmd::_ENUM, 2, _T("-default"), _T("-d"), _T("The 'xxx' argument specifies the default button. Allowed options: 1|2|3."), &def, &string2default);

if (cmd.ParseCommandLine(_argc, _pArgv, correctParameters) != 0) {
cmd.Help();
return 1;
}

UTL_Conversion::StringReplaceAll(message, _T("\\n"), _T("\n"));
if (help || correctParameters == 0) {
cmd.Help();
return 0;
}

if (message.empty()) {
_tprintf(_T("Error - message is emnpty\n"));
Expand Down
6 changes: 5 additions & 1 deletion MessageBox.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>
</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
244 changes: 103 additions & 141 deletions UTL_Cmd.cpp
Original file line number Diff line number Diff line change
@@ -1,171 +1,133 @@
#include "UTL_Cmd.h"
#include "UTL_Conversion.h"

bool UTL_Cmd::IsHelp(wstring comm)
UTL_Cmd::UTL_Cmd()
{
comm = UTL_Conversion::ToLower(comm);
if (comm == _T("-?") || comm == _T("-help") || comm == _T("-h")) return true;
return false;
mArguments.clear();
}

bool UTL_Cmd::IsTitle(wstring comm)
UTL_Cmd::~UTL_Cmd()
{
comm = UTL_Conversion::ToLower(comm);
if (comm == _T("-title")) return true;
return false;
}

bool UTL_Cmd::IsMessage(wstring comm)
{
comm = UTL_Conversion::ToLower(comm);
if (comm == _T("-message")) return true;
return false;
}

bool UTL_Cmd::IsButton(wstring comm)
{
comm = UTL_Conversion::ToLower(comm);
if (comm == _T("-button")) return true;
return false;
}

bool UTL_Cmd::IsDefault(wstring comm)
{
comm = UTL_Conversion::ToLower(comm);
if (comm == _T("-default")) return true;
return false;
}

bool UTL_Cmd::IsIcon(wstring comm)
{
comm = UTL_Conversion::ToLower(comm);
if (comm == _T("-icon")) return true;
return false;
}

void UTL_Cmd::Help()
{
_tprintf(_T("MessageBox 0.1\n"));
_tprintf(_T("MessageBox 0.2\n"));
_tprintf(_T(" MessageBox for command line. Amiga Rulez!\n"));
_tprintf(_T("\nUsage:\n"));
_tprintf(_T(" MessageBox [OPTIONS]\n"));
_tprintf(_T("\nOptions:\n"));
_tprintf(_T(" -title TITLE\n"));
_tprintf(_T(" The TITLE argument specifies the title of the message box.\n"));
_tprintf(_T(" -message MESSAGE\n"));
_tprintf(_T(" The MESSAGE argument specifies the text of the message box. Newline can be embedded using \\n.\n"));
_tprintf(_T(" -icon ITYPE\n"));
_tprintf(_T(" The ITYPE argument specifies the text of the message box. Allowed options: NoIcon|Information|Question|Warning|Error.\n"));
_tprintf(_T(" -button BTYPE\n"));
_tprintf(_T(" The BTYPE argument specifies the type of the message box. Allowed options: Ok|OkCancel|YesNo|YesNoCancel.\n"));
_tprintf(_T(" -default N\n"));
_tprintf(_T(" The N argument specifies the default button. Allowed options: 1|2|3.\n"));
}

int UTL_Cmd::ParseCommandLinbe(int argc, _TCHAR* argv[], int& iCorrectParameters, bool& bHelp, wstring& title, wstring& message, Type& type, Icon& icon, DefaultButton& def)
{
iCorrectParameters = 0;

for (int i = 0; i < argc; i++) {
wstring tmp = argv[i];
tmp = UTL_Conversion::TrimWhiteChar(tmp);

if (UTL_Cmd::IsHelp(tmp)) {
bHelp = true;
iCorrectParameters++;
continue;
for (const auto& it : mArguments) {
for (const auto& text : it.text) {
_tprintf((_T(" ") + text + _T(" xxx\n")).c_str());
}
_tprintf((_T(" ") + it.help + _T("\n")).c_str());
}
}

if (UTL_Cmd::IsTitle(tmp)) {
i++;
if (i < argc) {
tmp = argv[i];
tmp = UTL_Conversion::TrimWhiteChar(tmp);
title = tmp;
iCorrectParameters++;
}
continue;
}
void UTL_Cmd::Add(ARGUMENT_TYPE _type, int _num, ...)
{
ARGUMENT arg;
arg.text.clear();
arg.type = _type;

int argCount = _num;
_num += 2; //help & data pointer
if (_type == _ENUM) _num++; //convert table

va_list arglist;
va_start(arglist, _num);
for (int x = 0; x < argCount; x++) {
LPCWSTR tmp = va_arg(arglist, LPCWSTR);
arg.text.push_back(tmp);
}
arg.help = va_arg(arglist, LPCWSTR);
arg.pVar = static_cast <void*> (va_arg(arglist, void*));
if (_type == _ENUM) arg.pTable = static_cast <map<wstring, UINT> *> (va_arg(arglist, void*));
else arg.pTable = nullptr;
va_end(arglist);

if (UTL_Cmd::IsMessage(tmp)) {
i++;
if (i < argc) {
tmp = argv[i];
tmp = UTL_Conversion::TrimWhiteChar(tmp);
message = tmp;
iCorrectParameters++;
}
continue;
}
mArguments.push_back(arg);
}

if (UTL_Cmd::IsButton(tmp)) {
i++;
if (i < argc) {
tmp = argv[i];
tmp = UTL_Conversion::TrimWhiteChar(tmp);
tmp = UTL_Conversion::ToLower(tmp);
if (tmp == _T("ok")) type = Ok;
else if (tmp == _T("okcancel")) type = OkCancel;
else if (tmp == _T("yesno")) type = YesNo;
else if (tmp == _T("yesnocancel")) type = YesNoCancel;
else {
_tprintf(_T("Error - unknown button\n"));
return 1;
int UTL_Cmd::ParseCommandLine(int _argc, _TCHAR* _pArgv[], int& _correctParameters)
{
_correctParameters = 0;

for (int i = 1; i < _argc; i++) {
bool unknown = true;
for (unsigned int a = 0; a < mArguments.size(); a++) {
if (find(mArguments[a].text.begin(), mArguments[a].text.end(), UTL_Conversion::TrimWhiteChar(_pArgv[i])) != mArguments[a].text.end()) {
if (mArguments[a].type == _STRING) {
i++;
if (i < _argc) {
wstring tmp = UTL_Conversion::TrimWhiteChar(_pArgv[i]);
UTL_Conversion::StringReplaceAll(tmp, _T("\\n"), _T("\n"));
*((wstring*)mArguments[a].pVar) = tmp;
_correctParameters++;
unknown = false;
}
break;
}
iCorrectParameters++;
}
else {
_tprintf(_T("Error - unknown button\n"));
return 1;
}
continue;
}

if (UTL_Cmd::IsIcon(tmp)) {
i++;
if (i < argc) {
tmp = argv[i];
tmp = UTL_Conversion::TrimWhiteChar(tmp);
tmp = UTL_Conversion::ToLower(tmp);
if (tmp == _T("noicon")) icon = NoIcon;
else if (tmp == _T("information")) icon = Information;
else if (tmp == _T("question")) icon = Question;
else if (tmp == _T("warning")) icon = Warning;
else if (tmp == _T("error")) icon = Error;
else {
_tprintf(_T("Error - unknown icon\n"));
return 1;
else if (mArguments[a].type == _BOOL) {
*((bool*)mArguments[a].pVar) = !*((bool*)mArguments[a].pVar);
_correctParameters++;
unknown = false;
break;
}
else if (mArguments[a].type == _TRUE) {
*((bool*)mArguments[a].pVar) = true;
_correctParameters++;
unknown = false;
break;
}
else if (mArguments[a].type == _INT) {
i++;
if (i < _argc) {
*((int*)mArguments[a].pVar) = UTL_Conversion::ToInt(UTL_Conversion::TrimWhiteChar(_pArgv[i]));
_correctParameters++;
unknown = false;
}
break;
}
else if (mArguments[a].type == _COLOR) {
i++;
if (i < _argc) {
((pair<bool, wstring>*)mArguments[a].pVar)->first = true;
((pair<bool, wstring>*)mArguments[a].pVar)->second = UTL_Conversion::TrimWhiteChar(_pArgv[i]);
_correctParameters++;
unknown = false;
}
break;
}
else if (mArguments[a].type == _ENUM) {
i++;
if (i < _argc) {
wstring key = UTL_Conversion::TrimWhiteChar(_pArgv[i]);
auto search = mArguments[a].pTable->find(key);
if (search != mArguments[a].pTable->end()) {
*((UINT*)mArguments[a].pVar) = search->second;
unknown = false;
}
else {
_tprintf(wstring(_T("Error - bad argument: ") + key + _T("\n")).c_str());
return 1;
}
_correctParameters++;
}
break;
}
iCorrectParameters++;
}
else {
_tprintf(_T("Error - unknown icon\n"));
return 1;
}
continue;
}

if (UTL_Cmd::IsDefault(tmp)) {
i++;
if (i < argc) {
tmp = argv[i];
tmp = UTL_Conversion::TrimWhiteChar(tmp);
if (tmp == _T("1")) def = Def1;
else if (tmp == _T("2")) def = Def2;
else if (tmp == _T("3")) def = Def3;
else {
_tprintf(_T("Error - unknown default button\n"));
_tprintf(_T("Error - unknown type\n"));
return 1;
}
iCorrectParameters++;
}
else {
_tprintf(_T("Error - unknown default button\n"));
return 1;
}
continue;
}
if (unknown) {
_tprintf(wstring(_T("Error - unknown argument: ") + (wstring)_pArgv[i] + _T("\n")).c_str());
return 1;
}
}

return 0;
}
Loading

0 comments on commit f627476

Please sign in to comment.