Browse Source

Move zero argument check

master
Jake Wakeling 2 months ago
parent
commit
a40372a2fc
4 changed files with 64 additions and 61 deletions
  1. 11
    6
      src/arg.cpp
  2. 40
    42
      src/main.cpp
  3. 11
    11
      src/viewer.cpp
  4. 2
    2
      src/viewer.h

+ 11
- 6
src/arg.cpp View File

@@ -6,6 +6,7 @@
#include "arg.h"

// Parse program arguments and return result
// XVxA000: Error parsing arguments
cxxopts::ParseResult Arg::parse(int argc, char *argv[]) {
try {
cxxopts::Options options(PROGRAM_NAME, PROGRAM_DESC); // Create options object
@@ -22,24 +23,28 @@ cxxopts::ParseResult Arg::parse(int argc, char *argv[]) {

cxxopts::ParseResult result = options.parse(argc, argv);

if (result.arguments().size() == 0) { // Check for zero arguments
PrintUsage();
exit(0x0000);
}
if (result.count("help")) { // Check for help argument
PrintHelp(); // Show help
PrintHelp();
exit(0x0000);
}
if (result.count("version")) { // Check for version argument
PrintVersion(); // Show version information
PrintVersion();
exit(0x0000);
}

return result;
}
catch (const cxxopts::OptionException &exception) { // Catch option exceptions
fprintf(stderr, "XVx0A00: Error parsing arguments: %s\n", exception.what());
exit(0x0A00);
fprintf(stderr, "XVxA000: Error parsing arguments: %s\n", exception.what());
exit(0xA000);
}
}

// Print usage message
// Print usage information
void Arg::PrintUsage(void) {
printf("Usage: %s [-sew] file\n", PROGRAM_NAME);
printf("Try '%s --help' for more information\n", PROGRAM_NAME);
@@ -47,7 +52,7 @@ void Arg::PrintUsage(void) {
return;
}

// Print help message
// Print help information
void Arg::PrintHelp(void) {
printf("Usage: %s [-sew] file\n", PROGRAM_NAME);
printf("Examples:\n");

+ 40
- 42
src/main.cpp View File

@@ -6,56 +6,54 @@
#include "main.h"

int main(int argc, char *argv[]) {
if (argc > 1) {
cxxopts::ParseResult result = Arg::parse(argc, argv); // Parse arguments
size_t startByte = 0, endByte = 0;
int viewWidth = 16;
cxxopts::ParseResult result = Arg::parse(argc, argv); // Parse arguments
size_t startByte = 0, endByte = 0;
int viewWidth = 16;

if (result.count("start")) { // Check for start argument
if (result["start"].as<size_t>() <= 0) { // Check validity of width
fprintf(stderr, "XVx0A01: Start byte must be greater than zero\n");
return 0x0A01;
}
startByte = result["start"].as<size_t>(); // Set start byte
if (result.count("start")) { // Check for start argument
if (result["start"].as<size_t>() <= 0) { // Check validity of width
fprintf(stderr, "XVx0A01: Start byte must be greater than zero\n");
return 0x0A01;
}
if (result.count("end")) { // Check for end argument
if (result["end"].as<size_t>() <= 0) { // Check validity of width
fprintf(stderr, "XVx0A02: End byte must be greater than zero\n");
return 0x0A02;
}
endByte = result["end"].as<size_t>(); // Set end byte
startByte = result["start"].as<size_t>(); // Set start byte
}
if (result.count("end")) { // Check for end argument
if (result["end"].as<size_t>() <= 0) { // Check validity of width
fprintf(stderr, "XVx0A02: End byte must be greater than zero\n");
return 0x0A02;
}
if (result.count("width")) { // Check for width argument
if (result["width"].as<int>() <= 0) { // Check validity of width
fprintf(stderr, "XVx0A03: View width must be greater than zero\n");
return 0x0A03;
}
viewWidth = result["width"].as<int>(); // Set view width
endByte = result["end"].as<size_t>(); // Set end byte
}
if (result.count("width")) { // Check for width argument
if (result["width"].as<int>() <= 0) { // Check validity of width
fprintf(stderr, "XVx0A03: View width must be greater than zero\n");
return 0x0A03;
}
viewWidth = result["width"].as<int>(); // Set view width
}

if (result.count("file")) { // Check for file argument
std::string filePath = result["file"].as<std::string>();
if (result.count("file")) { // Check for file argument
std::string filePath = result["file"].as<std::string>();

std::ifstream file; // Open file for reading
file.open(filePath, std::ios::binary | std::ios::in);
if (!file) { // Check file open error
fprintf(stderr, "XVx0001: Failed to open %s for reading\n", filePath.c_str());
return 0x0001;
}
file.seekg(0, file.end);
size_t maxByte = file.tellg();
file.seekg(0, file.beg);
std::ifstream file; // Open file for reading
file.open(filePath, std::ios::binary | std::ios::in);
if (!file) { // Check file open error
fprintf(stderr, "XVx0001: Failed to open %s for reading\n", filePath.c_str());
return 0x0001;
}
file.seekg(0, file.end);
size_t maxByte = file.tellg();
file.seekg(0, file.beg);

if (endByte == 0 || endByte > maxByte) {
endByte = maxByte;
}
xv::Viewer::PrintHex(&file, viewWidth, startByte, endByte);
if (endByte == 0 || endByte > maxByte) {
endByte = maxByte;
}
else { Arg::PrintUsage(); }
xv::Viewer::PrintHex(&file, viewWidth, startByte, endByte);
}
else { Arg::PrintUsage(); } // Print help message
else { Arg::PrintUsage(); }

return 0x0000;
}

+ 11
- 11
src/viewer.cpp View File

@@ -7,19 +7,19 @@

using namespace xv;

Viewer::Viewer(void) {
InitWindowSize();
}
//Viewer::Viewer(void) {
// InitWindowSize();
//}

// Set the object columns and rows variables according to window size
void Viewer::InitWindowSize(void) {
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
this->columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
this->rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
#endif // WIN32
}
//void Viewer::InitWindowSize(void) {
//#ifdef WIN32
// CONSOLE_SCREEN_BUFFER_INFO csbi;
// GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
// this->columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
// this->rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
//#endif // WIN32
//}

// Print bytes within given range from file in hex format
void Viewer::PrintHex(std::ifstream *file, int viewWidth, size_t start, size_t end) {

+ 2
- 2
src/viewer.h View File

@@ -16,7 +16,7 @@
namespace xv {
class Viewer {
public:
Viewer::Viewer(void);
//Viewer::Viewer(void);

static void PrintHex(std::ifstream *hexFile, int viewWidth, size_t start, size_t end);

@@ -24,7 +24,7 @@ namespace xv {
size_t columns;
size_t rows;

void Viewer::InitWindowSize(void);
//void Viewer::InitWindowSize(void);

static void PrintByte(std::ifstream *file);
};

Loading…
Cancel
Save