Skip to content

Commit

Permalink
Portability changes: main/wmain
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed Apr 7, 2024
1 parent c0edc21 commit 5570cb0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
63 changes: 45 additions & 18 deletions bkdecmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
//////////////////////////////////////////////////////////////////////
// Preliminary function declarations

int wmain_impl(std::vector<std::wstring>& wargs);

void PrintWelcome();
void PrintUsage();
bool ParseCommandLine(int argc, wchar_t* argv[]);
bool ParseCommandLine(std::vector<std::wstring>& wargs);

bool DoDiskList();
bool DoDiskExtractFile();
Expand All @@ -31,9 +33,9 @@ bool DoDiskDeleteFile();
#define OPTIONSTR "-"
#endif

const wchar_t* g_sCommand = nullptr;
const wchar_t* g_sImageFileName = nullptr;
const wchar_t* g_sFileName = nullptr;
std::wstring g_sCommand;
std::wstring g_sImageFileName;
std::wstring g_sFileName;

enum CommandRequirements
{
Expand Down Expand Up @@ -83,12 +85,11 @@ void PrintUsage()
<< L" bkdecmd d <ImageFile> <FileName> - удалить файл" << std::endl;
}


bool ParseCommandLine(int argc, wchar_t* argv[])
bool ParseCommandLine(std::vector<std::wstring>& wargs)
{
for (int argn = 1; argn < argc; argn++)
for (auto warg : wargs)
{
const wchar_t* arg = argv[argn];
const wchar_t* arg = warg.c_str();
if (arg[0] == OPTIONCHAR)
{
{
Expand All @@ -98,11 +99,11 @@ bool ParseCommandLine(int argc, wchar_t* argv[])
}
else
{
if (g_sCommand == nullptr)
if (g_sCommand.empty())
g_sCommand = arg;
else if (g_sImageFileName == nullptr)
else if (g_sImageFileName.empty())
g_sImageFileName = arg;
else if (g_sFileName == nullptr)
else if (g_sFileName.empty())
g_sFileName = arg;
else
{
Expand All @@ -113,15 +114,15 @@ bool ParseCommandLine(int argc, wchar_t* argv[])
}

// Parsed options validation
if (g_sCommand == nullptr)
if (g_sCommand.empty())
{
std::wcout << L"Не указана команда." << std::endl;
return false;
}
CommandInfo* pcinfo = nullptr;
for (int i = 0; i < g_CommandInfos_count; i++)
{
if (wcscmp(g_sCommand, g_CommandInfos[i].command) == 0)
if (wcscmp(g_sCommand.c_str(), g_CommandInfos[i].command) == 0)
{
pcinfo = g_CommandInfos + i;
break;
Expand All @@ -135,12 +136,12 @@ bool ParseCommandLine(int argc, wchar_t* argv[])
g_pCommand = pcinfo;

// More pre-checks based on command requirements
if (g_sImageFileName == nullptr)
if (g_sImageFileName.empty())
{
std::wcout << L"Файл образа не указан." << std::endl;
return false;
}
if ((pcinfo->requirements & CMDR_PARAM_FILENAME) != 0 && g_sFileName == nullptr)
if ((pcinfo->requirements & CMDR_PARAM_FILENAME) != 0 && g_sFileName.empty())
{
std::wcout << L"Ожидалось имя файла." << std::endl;
return false;
Expand All @@ -154,18 +155,44 @@ bool ParseCommandLine(int argc, wchar_t* argv[])
return true;
}

#ifdef _WIN32
int wmain(int argc, wchar_t* argv[])
{
#ifdef _WIN32
// Console output mode
_setmode(_fileno(stdout), _O_U16TEXT);
#else // #D

std::vector<std::wstring> wargs;
for (int argn = 1; argn < argc; argn++)
{
wargs.push_back(std::wstring(argv[argn]));
}

return wmain_impl(wargs);
}
#else
int main(int argc, char* argv[])
{
// Console output mode
std::locale::global(std::locale(""));
std::wcout.imbue(std::locale());

std::vector<std::wstring> wargs;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
for (int argn = 1; argn < argc; argn++)
{
std::wstring warg = converter.from_bytes(argv[argn]);
wargs.push_back(warg);
}

return main_impl(wargs);
}
#endif

int wmain_impl(std::vector<std::wstring>& wargs)
{
PrintWelcome();

if (!ParseCommandLine(argc, argv))
if (!ParseCommandLine(wargs))
{
PrintUsage();
return 255;
Expand Down
2 changes: 1 addition & 1 deletion imgos/BKFloppyImage_ANDos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void CBKFloppyImage_ANDos::ConvertAbstractToRealRecord(BKDirDataItem *pFR, bool
биты 9-15 – год, считая от 1980 г. («эпоха MS-DOS»), возможны значения от 0 до 127 включительно, т.е. 1980-2107 гг.
*/
tm ctm {};
#ifdef WIN32
#ifdef _WIN32
gmtime_s(&ctm, &pFR->timeCreation);
#else
gmtime_r(&pFR->timeCreation, &ctm);
Expand Down
2 changes: 1 addition & 1 deletion imgos/BKFloppyImage_HCDos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void CBKFloppyImage_HCDos::ConvertAbstractToRealRecord(BKDirDataItem *pFR, bool
{
pRec->length = pFR->nSize;
tm ctm;
#ifdef WIN32
#ifdef _WIN32
gmtime_s(&ctm, &pFR->timeCreation);
#else
gmtime_r(&pFR->timeCreation, &ctm);
Expand Down
2 changes: 1 addition & 1 deletion imgos/BKFloppyImage_RT11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void CBKFloppyImage_RT11::ConvertAbstractToRealRecord(BKDirDataItem *pFR, bool b
{
pRec->nBlkSize = ByteSizeToBlockSize_l(pFR->nSize); // размер проги в блоках
tm ctm {};
#ifdef WIN32
#ifdef _WIN32
gmtime_s(&ctm, &pFR->timeCreation);
#else
gmtime_r(&pFR->timeCreation, &ctm);
Expand Down
7 changes: 5 additions & 2 deletions pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@

// Добавьте сюда заголовочные файлы для предварительной компиляции

#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <vector>
#include <filesystem>
#include <locale>
#include <codecvt>

#include <string.h>

#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#else
#include <locale>
#endif

#ifdef _WIN32
Expand Down

0 comments on commit 5570cb0

Please sign in to comment.