General code cleanup

This commit is contained in:
Andrew S. Rightenburg 2024-05-03 18:51:04 +08:00
parent f28931cc65
commit 35344263d3
Signed by: rail5
GPG Key ID: A0CB570AB6629159
24 changed files with 300 additions and 200 deletions

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef FILESYSTEM
#define FILESYSTEM
#include <filesystem>
@ -55,7 +59,6 @@ bool editor::file::set_file(std::string file_path) {
initialized = false;
return initialized;
}
}
/*
@ -160,7 +163,6 @@ bool editor::file::add_instruction(instruction &input_instruction) {
bool end_position_is_eof = (input_instruction.get_end_position() == -1);
if (start_position_is_eof) {
if (input_instruction.get_operation_type() == replace_operation) {
/*
REPLACE instructions are a special case with the 'END' keyword
@ -338,7 +340,6 @@ void editor::file::insert(int64_t start_position, std::string text_to_insert) {
file_stream.write("\n", 1);
for (int64_t i = (new_file_length - 1); i > start_position; i = (i - amount_to_store)) {
int64_t copy_to_this_position = (i - (amount_to_store - 1)) - 1;
int64_t copy_from_this_position = (copy_to_this_position - insert_length);
@ -351,7 +352,6 @@ void editor::file::insert(int64_t start_position, std::string text_to_insert) {
// (In the above example, 512 rather than 1024)
// And set i = start_position to make sure the loop doesn't run again after this
if (copy_from_this_position < start_position) {
int64_t difference = start_position - copy_from_this_position;
copy_from_this_position = start_position;
@ -444,7 +444,6 @@ void editor::file::remove(int64_t start_position, int64_t end_position) {
// Before EOF
for (int64_t i = start_position; i < (new_file_length - 1); i = (i + amount_to_store)) {
int64_t copy_to_this_position = i;
int64_t copy_from_this_position = (i + remove_length);
@ -496,7 +495,6 @@ void editor::file::remove(int64_t start_position, int64_t end_position) {
}
bool editor::file::execute_single_instruction(instruction instruction_to_execute) {
if (instruction_to_execute.get_operation_type() == replace_operation) {
replace(instruction_to_execute.get_start_position(), instruction_to_execute.get_text());
return true;

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
void editor::instruction::clear_instruction() {
/***
void clear_instruction():
@ -23,7 +27,6 @@ void editor::instruction::process_special_chars() {
text_input = process_bytecodes(text_input);
end_position = (start_position + text_input.length());
}
bool editor::instruction::set_replace_instruction(int64_t start, std::string text) {
@ -314,7 +317,7 @@ editor::instruction parse_instruction_string(std::string instruction_string) {
second_element_is_end = (second_element == "end");
if ( ! (is_number(second_element) || (second_element_is_end)) ) {
if ( !(is_number(second_element) || (second_element_is_end)) ) {
invalid_instruction.set_error_message("Invalid instruction: '" + instruction_vector[1] + "' is not a positive integer");
return invalid_instruction;
}
@ -327,7 +330,7 @@ editor::instruction parse_instruction_string(std::string instruction_string) {
third_element_is_end = (third_element == "end");
if ( ! (is_number(instruction_vector[2]) || (third_element_is_end) )) {
if ( !(is_number(instruction_vector[2]) || (third_element_is_end)) ) {
invalid_instruction.set_error_message("Invalid REMOVE instruction: '" + instruction_vector[2] + "' is not a positive integer");
return invalid_instruction;
}
@ -341,7 +344,6 @@ editor::instruction parse_instruction_string(std::string instruction_string) {
int64_t end_position;
if (is_replace_instruction) {
if (second_element_is_end) {
start_position = -1;
} else {
@ -352,7 +354,6 @@ editor::instruction parse_instruction_string(std::string instruction_string) {
}
if (is_insert_instruction) {
if (second_element_is_end) {
start_position = -1;
} else {
@ -363,7 +364,6 @@ editor::instruction parse_instruction_string(std::string instruction_string) {
}
if (is_remove_instruction) {
if (second_element_is_end) {
start_position = -1;
} else {
@ -406,7 +406,7 @@ std::vector<editor::instruction> parse_instruction_set_string(std::string instru
std::vector<std::string> instruction_strings = explode(instruction_set_string, '\n');
for (std::string i : instruction_strings) {
output_instruction_set.push_back( parse_instruction_string(i) );
output_instruction_set.push_back(parse_instruction_string(i));
}
return output_instruction_set;

View File

@ -1,3 +1,10 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef EDIT_SRC_INCLUDES_H_
#define EDIT_SRC_INCLUDES_H_
/* Needed for I/O */
#ifndef IOSTREAM
#define IOSTREAM
@ -128,3 +135,5 @@
#include "namespace_editor.h"
#include "class_instruction.cpp"
#include "class_file.cpp"
#endif // EDIT_SRC_INCLUDES_H_

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#include "includes.h"
#ifndef IOSTREAM
@ -11,7 +15,6 @@
#endif
int main(int argc, char* argv[]) {
std::string program_name = "polonius-editor";
std::string help_string =
@ -101,8 +104,7 @@ int main(int argc, char* argv[]) {
opterr = 0;
int option_index = 0;
static struct option long_options[] =
{
static struct option long_options[] = {
{"input", required_argument, 0, 'i'},
{"add-instruction-set", required_argument, 0, 's'},
@ -170,7 +172,7 @@ int main(int argc, char* argv[]) {
case '?':
if (optopt == 'i' || optopt == 's' || optopt == 'a' || optopt == 'b') {
std::cerr << program_name << ": Option -" << (char)optopt << " requires an argument" << std::endl << "Use -h for help" << std::endl;
std::cerr << program_name << ": Option -" << static_cast<char>(optopt) << " requires an argument" << std::endl << "Use -h for help" << std::endl;
return EXIT_BADOPT;
}
break;
@ -199,7 +201,6 @@ int main(int argc, char* argv[]) {
}
for (int i = 0; i < instructions_to_add.size(); i++) {
if (interpret_special_chars) {
instructions_to_add[i].process_special_chars();
}
@ -219,5 +220,4 @@ int main(int argc, char* argv[]) {
/* Close file */
document.close();
return EXIT_SUCCESS;
}

View File

@ -1,114 +1,123 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef EDIT_SRC_NAMESPACE_EDITOR_H_
#define EDIT_SRC_NAMESPACE_EDITOR_H_
namespace editor {
enum operation_type {
no_operation,
replace_operation,
insert_operation,
remove_operation
};
enum operation_type {
no_operation,
replace_operation,
insert_operation,
remove_operation
};
class instruction {
private:
bool initialized = false;
operation_type operation = no_operation;
/*
Operation types:
0 = none
1 = replace
2 = insert
3 = remove
*/
int64_t start_position = -1;
int64_t end_position = -1;
std::string text_input = "";
std::string error_message = "";
public:
void clear_instruction();
void process_special_chars();
bool set_replace_instruction(int64_t start, std::string text);
bool set_insert_instruction(int64_t start, std::string text);
bool set_remove_instruction(int64_t start, int64_t end);
void set_error_message(std::string message);
std::string get_error_message();
class instruction {
private:
bool initialized = false;
operation_type operation = no_operation;
/*
Operation types:
0 = none
1 = replace
2 = insert
3 = remove
*/
int64_t start_position = -1;
int64_t end_position = -1;
std::string text_input = "";
std::string error_message = "";
void update_start_position(int64_t start);
void update_end_position(int64_t end);
bool is_initialized();
int get_operation_type();
int64_t get_start_position();
int64_t get_end_position();
std::string get_text();
public:
void clear_instruction();
void process_special_chars();
bool set_replace_instruction(int64_t start, std::string text);
bool set_insert_instruction(int64_t start, std::string text);
bool set_remove_instruction(int64_t start, int64_t end);
void set_error_message(std::string message);
std::string get_error_message();
};
instruction create_replace_instruction(int64_t start_position, std::string text);
instruction create_insert_instruction(int64_t start_position, std::string text);
instruction create_remove_instruction(int64_t start_position, int64_t end_position);
instruction parse_instruction_string(std::string instruction_string);
std::vector<instruction> parse_instruction_set_string(std::string instruction_set);
void update_start_position(int64_t start);
void update_end_position(int64_t end);
bool is_initialized();
int get_operation_type();
int64_t get_start_position();
int64_t get_end_position();
std::string get_text();
};
class file {
private:
bool initialized = false;
std::string file_name;
std::string file_directory;
int64_t file_length = 0;
std::fstream file_stream;
FILE* c_type_file;
int file_descriptor;
int block_size = 10240;
std::vector<instruction> instruction_set;
int64_t file_length_after_last_instruction = 0;
std::string error_message = "";
bool verbose = false;
public:
bool set_file(std::string file_path);
void set_block_size(int specified_blocksize);
void replace(int64_t start_position, std::string replacement_text);
void insert(int64_t start_position, std::string text_to_insert);
void remove(int64_t start_position, int64_t end_position);
bool add_instruction(instruction &input_instruction);
bool execute_single_instruction(instruction instruction_to_execute);
bool execute_instructions();
bool is_initialized();
std::string get_file_name();
std::string get_file_directory();
int get_block_size();
int64_t get_file_length();
std::vector<instruction> get_instruction_set();
std::string get_error_message();
void close();
/*
Constructor
*/
file(std::string path, int blocksize = 10240, bool verbose_mode = false);
};
instruction create_replace_instruction(int64_t start_position, std::string text);
instruction create_insert_instruction(int64_t start_position, std::string text);
instruction create_remove_instruction(int64_t start_position, int64_t end_position);
}
instruction parse_instruction_string(std::string instruction_string);
std::vector<instruction> parse_instruction_set_string(std::string instruction_set);
class file {
private:
bool initialized = false;
std::string file_name;
std::string file_directory;
int64_t file_length = 0;
std::fstream file_stream;
FILE* c_type_file;
int file_descriptor;
int block_size = 10240;
std::vector<instruction> instruction_set;
int64_t file_length_after_last_instruction = 0;
std::string error_message = "";
bool verbose = false;
public:
bool set_file(std::string file_path);
void set_block_size(int specified_blocksize);
void replace(int64_t start_position, std::string replacement_text);
void insert(int64_t start_position, std::string text_to_insert);
void remove(int64_t start_position, int64_t end_position);
bool add_instruction(instruction &input_instruction);
bool execute_single_instruction(instruction instruction_to_execute);
bool execute_instructions();
bool is_initialized();
std::string get_file_name();
std::string get_file_directory();
int get_block_size();
int64_t get_file_length();
std::vector<instruction> get_instruction_set();
std::string get_error_message();
void close();
/*
Constructor
*/
explicit file(std::string path, int blocksize = 10240, bool verbose_mode = false);
};
} // namespace editor
#endif // EDIT_SRC_NAMESPACE_EDITOR_H_

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef FSTREAM
#define FSTREAM
#include <fstream>
@ -38,7 +42,6 @@ bool reader::file::init(std::string path) {
}
std::string reader::file::read(int64_t start_position, int64_t length) {
std::ifstream file_stream(file_name, std::ifstream::binary);
// allocate memory

View File

@ -1,3 +1,10 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef READ_SRC_INCLUDES_H_
#define READ_SRC_INCLUDES_H_
/* Needed for I/O */
#ifndef IOSTREAM
#define IOSTREAM
@ -103,3 +110,5 @@
/* Contains the 'meat' of the program */
#include "namespace_reader.h"
#include "class_file.cpp"
#endif // READ_SRC_INCLUDES_H_

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#include "includes.h"
#ifndef IOSTREAM
@ -11,7 +15,6 @@
#endif
int main(int argc, char* argv[]) {
std::string program_name = "polonius-reader";
std::string help_string =
@ -111,8 +114,7 @@ int main(int argc, char* argv[]) {
opterr = 0;
int option_index = 0;
static struct option long_options[] =
{
static struct option long_options[] = {
{"input", required_argument, 0, 'i'},
{"start", required_argument, 0, 's'},
{"length", required_argument, 0, 'l'},
@ -190,7 +192,7 @@ int main(int argc, char* argv[]) {
case '?':
if (optopt == 'i' || optopt == 's' || optopt == 'l') {
std::cerr << program_name << ": Option -" << (char)optopt << " requires an argument" << std::endl << "Use -h for help" << std::endl;
std::cerr << program_name << ": Option -" << static_cast<char>(optopt) << " requires an argument" << std::endl << "Use -h for help" << std::endl;
return EXIT_BADOPT;
}
break;

View File

@ -1,58 +1,66 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef READ_SRC_NAMESPACE_READER_H_
#define READ_SRC_NAMESPACE_READER_H_
namespace reader {
enum job_type {
read_job,
search_job
};
enum job_type {
read_job,
search_job
};
enum search_type {
t_normal_search,
t_regex_search
};
enum search_type {
t_normal_search,
t_regex_search
};
class file {
private:
bool initialized = false;
std::string file_name;
std::string init_error_message = "unknown";
int64_t file_length = 0;
bool just_outputting_positions = false;
int64_t start_position = 0;
int64_t amount_to_read = -1;
int64_t end_position = -1;
int block_size = 10240;
std::string search_query = "";
search_type query_type = t_normal_search;
job_type job = read_job;
bool do_read_job();
bool do_search_job();
bool do_normal_search();
bool do_regex_search();
class file {
private:
bool initialized = false;
std::string file_name;
std::string init_error_message = "unknown";
int64_t file_length = 0;
public:
bool init(std::string path);
std::string read(int64_t start_position, int64_t length);
std::string get_init_error_message();
int64_t get_file_length();
bool do_job();
void set_start_position(int64_t position);
void set_amount_to_read(int64_t amount);
void set_just_outputting_positions(bool flag);
void set_block_size(int size);
void set_search_query(std::string query);
void set_search_type(search_type normal_or_regex);
void set_job_type(job_type input_job);
};
}
bool just_outputting_positions = false;
int64_t start_position = 0;
int64_t amount_to_read = -1;
int64_t end_position = -1;
int block_size = 10240;
std::string search_query = "";
search_type query_type = t_normal_search;
job_type job = read_job;
bool do_read_job();
bool do_search_job();
bool do_normal_search();
bool do_regex_search();
public:
bool init(std::string path);
std::string read(int64_t start_position, int64_t length);
std::string get_init_error_message();
int64_t get_file_length();
bool do_job();
void set_start_position(int64_t position);
void set_amount_to_read(int64_t amount);
void set_just_outputting_positions(bool flag);
void set_block_size(int size);
void set_search_query(std::string query);
void set_search_type(search_type normal_or_regex);
void set_job_type(job_type input_job);
};
} // namespace reader
#endif // READ_SRC_NAMESPACE_READER_H_

View File

@ -1,3 +1,10 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef SHARED_DEFINITIONS_H_
#define SHARED_DEFINITIONS_H_
// Basic info:
#include "version.h"
#define program_author "rail5"
@ -20,3 +27,5 @@
// This might(?) mean that we can't open >2GB files on 32-bit systems which use clang/llvm
// Testing is needed
#endif
#endif // SHARED_DEFINITIONS_H_

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef SSTREAM
#define SSTREAM
#include <sstream>
@ -40,7 +44,6 @@ std::vector<std::string> explode(std::string const &input, char delimiter, int m
// If maximum_number_of_elements is set, we want to recombine any splits after the maximum
if (maximum_number_of_elements > 0) {
// Set the highest index (counting from zero) we want the vector to have
int last_permissible_element = maximum_number_of_elements - 1;

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef UNISTD_H
#define UNISTD_H
#include <unistd.h>

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef FILESYSTEM
#define FILESYSTEM
#include <filesystem>

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef SYS_STAT
#define SYS_STAT
#include <sys/stat.h>
@ -11,4 +15,4 @@ inline bool is_directory(std::string path) {
// Error
return false;
}
}
}

View File

@ -1,10 +1,13 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef ALGORITHM
#define ALGORITHM
#include <algorithm>
#endif
inline bool is_number(const std::string &s)
{
inline bool is_number(const std::string &s) {
return !s.empty() && std::find_if(s.begin(),
s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
}

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef FILESYSTEM
#define FILESYSTEM
#include <filesystem>

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef FN_IS_NUMBER
#define FN_IS_NUMBER
#include "is_number.cpp"
@ -21,12 +25,12 @@ inline int parse_block_units(const std::string &user_input) {
}
if (last_character == "K" || last_character == "k") {
int blocksize = (int)stoi(all_but_last_character);
int blocksize = stoi(all_but_last_character);
return (blocksize * 1024);
}
if (last_character == "M" || last_character == "m") {
int blocksize = (int)stoi(all_but_last_character);
int blocksize = stoi(all_but_last_character);
return (blocksize * 1024 * 1024);
}
@ -38,5 +42,5 @@ inline int parse_block_units(const std::string &user_input) {
if (!is_number(user_input)) {
return -1;
}
return (int)stol(user_input);
return static_cast<int>(stol(user_input));
}

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef REGEX
#define REGEX
#include <regex>
@ -32,7 +36,7 @@
caret = false; \
backslash_b = false; \
backslash_capital_b = false; \
or_operator = false;
or_operator = false;
std::vector<std::string> parse_regex(std::string expression) {
/***
@ -318,7 +322,7 @@ std::vector<std::string> parse_regex(std::string expression) {
// Push each of the characters we had been saving for the buffer
// Back into the output vector one by one
for (int i=0; i<curly_braces_buffer.size(); i++) {
for (int i=0; i < curly_braces_buffer.size(); i++) {
std::string bufferpart(1, curly_braces_buffer[i]);
if (multi_char_entry) {
@ -379,12 +383,12 @@ std::vector<std::string> create_sub_expressions(std::string expression) {
std::vector<std::string> output;
for (int i=0; i<components.size()-1; i++) {
for (int i = 0; i < components.size()-1; i++) {
int upper_limit = components.size() - i;
std::string sub_expression = "";
for (int j=0; j<upper_limit; j++) {
for (int j = 0; j < upper_limit; j++) {
if (string_starts_with(components[j], "{") && string_ends_with(components[j], "}")) {
std::string curly_braces_term = components[j];
@ -419,8 +423,7 @@ std::vector<std::string> create_sub_expressions(std::string expression) {
}
output.push_back(sub_expression);
}
return output;
}
}

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef REGEX
#define REGEX
#include <regex>
@ -33,4 +37,4 @@ std::string process_bytecodes(std::string input) {
}
return input;
}
}

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef VECTOR
#define VECTOR
#include <vector>
@ -22,4 +26,4 @@ std::string process_escapedchars(std::string input) {
}
return input;
}
}

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
std::string remove_leading_whitespace(const std::string &input) {
const std::string whitespace_chars = " \n\r\t\f\v";

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
inline bool string_ends_with(std::string const &haystack, std::string const &needle) {
/* Return true if 'haystack' ends with 'needle' */
if (haystack.length() >= needle.length()) {

View File

@ -1,3 +1,7 @@
/***
* Copyright (C) 2024 rail5
*/
inline bool string_starts_with(std::string const &haystack, std::string const &needle) {
/* Return true if 'haystack' starts with 'needle' */
return (!haystack.find(needle));

View File

@ -1,9 +1,13 @@
/***
* Copyright (C) 2024 rail5
*/
#ifndef ALGORITHM
#define ALGORITHM
#include <algorithm>
#endif
inline std::string to_lower(std::string &input) {
inline std::string to_lower(const std::string& input) {
std::string output = input;
std::transform(output.begin(), output.end(), output.begin(),
[](unsigned char c){ return std::tolower(c); });