aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2025-01-27 11:17:57 +0100
committerJán Sučan <jan@jansucan.com>2025-01-27 15:22:36 +0100
commit7b2955b20ba51e27a72a881bcf7abdec73d7942c (patch)
tree52a7573a2d676629b557666d3f7cd48b910cd644
parent1a157599b63627d04a968f6251a3cde9e0cfeb7e (diff)
Put the code for options to a namespace
-rw-r--r--src/create.cpp2
-rw-r--r--src/create.h2
-rw-r--r--src/main.cpp20
-rw-r--r--src/options.cpp90
-rw-r--r--src/options.h31
-rw-r--r--src/restore.cpp2
-rw-r--r--src/restore.h2
7 files changed, 78 insertions, 71 deletions
diff --git a/src/create.cpp b/src/create.cpp
index 460bdd1..1956d5b 100644
--- a/src/create.cpp
+++ b/src/create.cpp
@@ -350,7 +350,7 @@ class DiffFinder
};
void
-create(const OptionsCreate &opts)
+create(const Options::Create &opts)
{
std::ifstream in_istream{opts.getInFilePath(),
std::ifstream::in | std::ifstream::binary};
diff --git a/src/create.h b/src/create.h
index bbfc374..6c1002f 100644
--- a/src/create.h
+++ b/src/create.h
@@ -35,4 +35,4 @@ class CreateError : public DiffddError
explicit CreateError(const std::string &message) : DiffddError(message) {}
};
-void create(const OptionsCreate &opts);
+void create(const Options::Create &opts);
diff --git a/src/main.cpp b/src/main.cpp
index c265805..f837d50 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -42,20 +42,20 @@ int
main(int argc, char **argv)
{
try {
- if (OptionParser::isHelp(argc, argv)) {
- OptionsPrintUsage();
- } else if (OptionParser::isVersion(argc, argv)) {
+ if (Options::Parser::isHelp(argc, argv)) {
+ Options::printUsage();
+ } else if (Options::Parser::isVersion(argc, argv)) {
print_version();
- } else if (OptionParser::isCreate(argc, argv)) {
- create(OptionParser::parseCreate(argc, argv));
- } else if (OptionParser::isRestore(argc, argv)) {
- restore(OptionParser::parseRestore(argc, argv));
+ } else if (Options::Parser::isCreate(argc, argv)) {
+ create(Options::Parser::parseCreate(argc, argv));
+ } else if (Options::Parser::isRestore(argc, argv)) {
+ restore(Options::Parser::parseRestore(argc, argv));
} else {
- OptionsPrintUsage();
+ Options::printUsage();
exit(1);
}
- } catch (const OptionError &e) {
- OptionsPrintUsage();
+ } catch (const Options::Error &e) {
+ Options::printUsage();
std::cerr << "ERROR: " << e.what() << std::endl;
exit(1);
} catch (const DiffddError &e) {
diff --git a/src/options.cpp b/src/options.cpp
index baa779d..a9dd284 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -35,8 +35,11 @@
*/
#include "program_info.h"
+namespace Options
+{
+
void
-OptionsPrintUsage()
+printUsage()
{
std::cout << "Usage: " << PROGRAM_NAME_STR << " create";
std::cout << " [-B BUFFER_SIZE] -i INFILE -b BASEFILE -o OUTFILE"
@@ -50,80 +53,80 @@ OptionsPrintUsage()
std::cout << " Or: " << PROGRAM_NAME_STR << " help" << std::endl;
}
-OptionsCreate::OptionsCreate() : buffer_size{OPTIONS_DEFAULT_BUFFER_SIZE} {}
+Create::Create() : buffer_size{Options::DEFAULT_BUFFER_SIZE} {}
uint32_t
-OptionsCreate::getBufferSize() const
+Create::getBufferSize() const
{
return buffer_size;
}
std::filesystem::path
-OptionsCreate::getInFilePath() const
+Create::getInFilePath() const
{
return in_file_path;
}
std::filesystem::path
-OptionsCreate::getBaseFilePath() const
+Create::getBaseFilePath() const
{
return base_file_path;
}
std::filesystem::path
-OptionsCreate::getOutFilePath() const
+Create::getOutFilePath() const
{
return out_file_path;
}
-OptionsRestore::OptionsRestore() : buffer_size{OPTIONS_DEFAULT_BUFFER_SIZE} {}
+Restore::Restore() : buffer_size{Options::DEFAULT_BUFFER_SIZE} {}
uint32_t
-OptionsRestore::getBufferSize() const
+Restore::getBufferSize() const
{
return buffer_size;
}
std::filesystem::path
-OptionsRestore::getDiffFilePath() const
+Restore::getDiffFilePath() const
{
return diff_file_path;
}
std::filesystem::path
-OptionsRestore::getOutFilePath() const
+Restore::getOutFilePath() const
{
return out_file_path;
}
bool
-OptionParser::isHelp(int argc, char **argv)
+Parser::isHelp(int argc, char **argv)
{
return isOperation(argc, argv, "help");
}
bool
-OptionParser::isVersion(int argc, char **argv)
+Parser::isVersion(int argc, char **argv)
{
return isOperation(argc, argv, "version");
}
bool
-OptionParser::isCreate(int argc, char **argv)
+Parser::isCreate(int argc, char **argv)
{
return isOperation(argc, argv, "create");
}
bool
-OptionParser::isRestore(int argc, char **argv)
+Parser::isRestore(int argc, char **argv)
{
return isOperation(argc, argv, "restore");
}
-OptionsCreate
-OptionParser::parseCreate(int argc, char **argv)
+Create
+Parser::parseCreate(int argc, char **argv)
{
- OptionsCreate opts;
+ Create opts;
// Skip the executable name. Do not skip the operation name. getopt expects
// to start at an argument immediately preceding the possible options.
@@ -155,11 +158,10 @@ OptionParser::parseCreate(int argc, char **argv)
break;
case ':':
- throw OptionError("missing argument for option '-" +
- std::string(1, optopt) + "'");
+ throw Error("missing argument for option '-" +
+ std::string(1, optopt) + "'");
default:
- throw OptionError("unknown option '-" + std::string(1, optopt) +
- "'");
+ throw Error("unknown option '-" + std::string(1, optopt) + "'");
}
}
@@ -168,19 +170,19 @@ OptionParser::parseCreate(int argc, char **argv)
/* Convert numbers in the arguments */
if ((arg_buffer_size != NULL) &&
parse_unsigned(arg_buffer_size, &(opts.buffer_size))) {
- throw OptionError("incorrect buffer size");
+ throw Error("incorrect buffer size");
} else if (opts.buffer_size == 0) {
- throw OptionError("buffer size cannot be 0");
+ throw Error("buffer size cannot be 0");
}
if (arg_input_file == NULL) {
- throw OptionError("missing input file");
+ throw Error("missing input file");
} else if (arg_base_file == NULL) {
- throw OptionError("missing base file");
+ throw Error("missing base file");
} else if (arg_output_file == NULL) {
- throw OptionError("missing output file");
+ throw Error("missing output file");
} else if (argc != 0) {
- throw OptionError("too many arguments");
+ throw Error("too many arguments");
}
opts.in_file_path = arg_input_file;
@@ -190,10 +192,10 @@ OptionParser::parseCreate(int argc, char **argv)
return opts;
}
-OptionsRestore
-OptionParser::parseRestore(int argc, char **argv)
+Restore
+Parser::parseRestore(int argc, char **argv)
{
- OptionsRestore opts;
+ Restore opts;
argc -= 1;
argv += 1;
@@ -218,11 +220,10 @@ OptionParser::parseRestore(int argc, char **argv)
break;
case ':':
- throw OptionError("missing argument for option '-" +
- std::string(1, optopt) + "'");
+ throw Error("missing argument for option '-" +
+ std::string(1, optopt) + "'");
default:
- throw OptionError("unknown option '-" + std::string(1, optopt) +
- "'");
+ throw Error("unknown option '-" + std::string(1, optopt) + "'");
}
}
@@ -231,17 +232,17 @@ OptionParser::parseRestore(int argc, char **argv)
/* Convert numbers in the arguments */
if ((arg_buffer_size != NULL) &&
parse_unsigned(arg_buffer_size, &(opts.buffer_size))) {
- throw OptionError("incorrect buffer size");
+ throw Error("incorrect buffer size");
} else if (opts.buffer_size == 0) {
- throw OptionError("buffer size cannot be 0");
+ throw Error("buffer size cannot be 0");
}
if (arg_diff_file == NULL) {
- throw OptionError("missing diff file");
+ throw Error("missing diff file");
} else if (arg_output_file == NULL) {
- throw OptionError("missing output file");
+ throw Error("missing output file");
} else if (argc != 0) {
- throw OptionError("too many arguments");
+ throw Error("too many arguments");
}
opts.diff_file_path = arg_diff_file;
@@ -251,15 +252,14 @@ OptionParser::parseRestore(int argc, char **argv)
}
bool
-OptionParser::isOperation(int argc, char **argv, std::string_view operationName)
+Parser::isOperation(int argc, char **argv, std::string_view operationName)
{
- return ((argc >= 2) &&
- (strncmp(argv[1], operationName.data(),
- OptionParser::MAX_OPERATION_NAME_LENGTH) == 0));
+ return ((argc >= 2) && (strncmp(argv[1], operationName.data(),
+ Parser::MAX_OPERATION_NAME_LENGTH) == 0));
}
int
-OptionParser::parse_unsigned(const char *const arg, uint32_t *const value)
+Parser::parse_unsigned(const char *const arg, uint32_t *const value)
{
char *end;
@@ -269,3 +269,5 @@ OptionParser::parse_unsigned(const char *const arg, uint32_t *const value)
return ((*end != '\0') || (errno != 0)) ? -1 : 0;
}
+
+} // namespace Options
diff --git a/src/options.h b/src/options.h
index ac9fdfd..0ea11d8 100644
--- a/src/options.h
+++ b/src/options.h
@@ -31,22 +31,25 @@
#include <cstdint>
#include <filesystem>
-class OptionError : public DiffddError
+namespace Options
+{
+
+class Error : public DiffddError
{
public:
- explicit OptionError(const std::string &message) : DiffddError(message) {}
+ explicit Error(const std::string &message) : DiffddError(message) {}
};
-const inline int OPTIONS_DEFAULT_BUFFER_SIZE{4 * 1024 * 1024};
+const inline int DEFAULT_BUFFER_SIZE{4 * 1024 * 1024};
-void OptionsPrintUsage();
+void printUsage();
-class OptionsCreate
+class Create
{
- friend class OptionParser;
+ friend class Parser;
public:
- OptionsCreate();
+ Create();
uint32_t getBufferSize() const;
std::filesystem::path getInFilePath() const;
@@ -60,12 +63,12 @@ class OptionsCreate
std::filesystem::path out_file_path;
};
-class OptionsRestore
+class Restore
{
- friend class OptionParser;
+ friend class Parser;
public:
- OptionsRestore();
+ Restore();
uint32_t getBufferSize() const;
std::filesystem::path getDiffFilePath() const;
@@ -77,7 +80,7 @@ class OptionsRestore
std::filesystem::path out_file_path;
};
-class OptionParser
+class Parser
{
public:
static bool isHelp(int argc, char **argv);
@@ -85,8 +88,8 @@ class OptionParser
static bool isCreate(int argc, char **argv);
static bool isRestore(int argc, char **argv);
- static OptionsCreate parseCreate(int argc, char **argv);
- static OptionsRestore parseRestore(int argc, char **argv);
+ static Create parseCreate(int argc, char **argv);
+ static Restore parseRestore(int argc, char **argv);
private:
static const size_t MAX_OPERATION_NAME_LENGTH{8};
@@ -95,3 +98,5 @@ class OptionParser
std::string_view operationName);
static int parse_unsigned(const char *const arg, uint32_t *const value);
};
+
+} // namespace Options
diff --git a/src/restore.cpp b/src/restore.cpp
index 5b490d9..75d2af7 100644
--- a/src/restore.cpp
+++ b/src/restore.cpp
@@ -32,7 +32,7 @@
#include <vector>
void
-restore(const OptionsRestore &opts)
+restore(const Options::Restore &opts)
{
std::fstream diff_stream;
diff_stream.open(opts.getDiffFilePath(),
diff --git a/src/restore.h b/src/restore.h
index ac98517..c5cde3c 100644
--- a/src/restore.h
+++ b/src/restore.h
@@ -35,4 +35,4 @@ class RestoreError : public DiffddError
explicit RestoreError(const std::string &message) : DiffddError(message) {}
};
-void restore(const OptionsRestore &opts);
+void restore(const Options::Restore &opts);