From 3a55d5e218f76ce02a92e487fade8ca1abcac6f1 Mon Sep 17 00:00:00 2001 From: rail5 Date: Thu, 5 Oct 2023 11:27:39 +0800 Subject: [PATCH] No longer using namespace std by default --- edit/src/class_file.cpp | 76 +++++++++++++++++----------------- edit/src/class_instruction.cpp | 52 +++++++++++------------ edit/src/main.cpp | 36 ++++++++-------- edit/src/namespace_editor.h | 50 +++++++++++----------- 4 files changed, 103 insertions(+), 111 deletions(-) diff --git a/edit/src/class_file.cpp b/edit/src/class_file.cpp index bfde8e1..845dfaf 100644 --- a/edit/src/class_file.cpp +++ b/edit/src/class_file.cpp @@ -8,11 +8,9 @@ #include #endif -using namespace std; - -bool editor::file::set_file(string file_path) { +bool editor::file::set_file(std::string file_path) { /*** - bool set_file(string file_path): + bool set_file(std::string file_path): Initialize the file object Returns true if the file object is successfully initialized @@ -22,7 +20,7 @@ bool editor::file::set_file(string file_path) { /* Verify that the directory that the file is supposed to be in actually exists */ - string directory = isolate_path_from_filename(file_path); + std::string directory = isolate_path_from_filename(file_path); file_name = file_path; file_directory = directory; @@ -40,7 +38,7 @@ bool editor::file::set_file(string file_path) { Create the file if it doesn't already exist */ if (!file_exists(file_name)) { - ofstream new_file(file_name); + std::ofstream new_file(file_name); new_file.close(); /* @@ -67,12 +65,12 @@ bool editor::file::set_file(string file_path) { Initialize the file stream, set a POSIX file lock, and set file_length */ - file_stream = fstream(file_name, ios::binary | ios::out | ios::in); + file_stream = std::fstream(file_name, std::ios::binary | std::ios::out | std::ios::in); /* Obtain file_descriptor in order to lock the file Unfortunately, the only portable way to do this is to open a SECOND file stream with the C-type FILE* - rather than continuing with our luxury C++ fstream + rather than continuing with our luxury C++ std::fstream This FILE* will never be used, except to get the file descriptor so that we can lock the file during editing */ c_type_file = fopen64(file_name.c_str(), "a+"); @@ -84,7 +82,7 @@ bool editor::file::set_file(string file_path) { return initialized; } - file_length = filesystem::file_size(file_path); + file_length = std::filesystem::file_size(file_path); file_length_after_last_instruction = file_length; /* @@ -98,7 +96,7 @@ bool editor::file::set_file(string file_path) { error_message = ""; if (verbose) { - cout << "Set file to " << file_path << endl; + std::cout << "Set file to " << file_path << std::endl; } /* @@ -217,11 +215,11 @@ bool editor::file::is_initialized() { return initialized; } -string editor::file::get_file_name() { +std::string editor::file::get_file_name() { return file_name; } -string editor::file::get_file_directory() { +std::string editor::file::get_file_directory() { return file_directory; } @@ -233,17 +231,17 @@ int64_t editor::file::get_file_length() { return file_length; } -vector editor::file::get_instruction_set() { +std::vector editor::file::get_instruction_set() { return instruction_set; } -string editor::file::get_error_message() { +std::string editor::file::get_error_message() { return error_message; } -void editor::file::replace(int64_t start_position, string replacement_text) { +void editor::file::replace(int64_t start_position, std::string replacement_text) { /*** - void editor::file::replace(int64_t start_position, string replacement_text): + void editor::file::replace(int64_t start_position, std::string replacement_text): Execute a "REPLACE" instruction Opens a file stream & replaces text inside the file, starting from start_position, with replacement_text ***/ @@ -253,7 +251,7 @@ void editor::file::replace(int64_t start_position, string replacement_text) { } // Seek to start_position - file_stream.seekp(start_position, ios::beg); + file_stream.seekp(start_position, std::ios::beg); // Replace file_stream.write(replacement_text.c_str(), replacement_text.length()); @@ -262,13 +260,13 @@ void editor::file::replace(int64_t start_position, string replacement_text) { fflush(c_type_file); if (verbose) { - cout << "Executed REPLACE instruction (" << start_position << ", " << replacement_text << ")" << endl; + std::cout << "Executed REPLACE instruction (" << start_position << ", " << replacement_text << ")" << std::endl; } } -void editor::file::insert(int64_t start_position, string text_to_insert) { +void editor::file::insert(int64_t start_position, std::string text_to_insert) { /*** - void editor::file::insert(int64_t start_position, string text_to_insert): + void editor::file::insert(int64_t start_position, std::string text_to_insert): Execute an "INSERT" instruction Opens a file stream & inserts text_to_insert into the file at position start_position, without replacing ***/ @@ -290,7 +288,7 @@ void editor::file::insert(int64_t start_position, string text_to_insert) { // Writing TO EOF // Seek to EOF - file_stream.seekp(start_position, ios::beg); + file_stream.seekp(start_position, std::ios::beg); // Insert file_stream.write(text_to_insert.c_str(), text_to_insert.length()); @@ -301,7 +299,7 @@ void editor::file::insert(int64_t start_position, string text_to_insert) { } // Add a newline char - file_stream.seekp(new_file_length - 1, ios::beg); + file_stream.seekp(new_file_length - 1, std::ios::beg); file_stream.write("\n", 1); // Flush changes @@ -312,7 +310,7 @@ void editor::file::insert(int64_t start_position, string text_to_insert) { file_length = new_file_length; if (verbose) { - cout << "Executed INSERT instruction (" << start_position << ", " << text_to_insert << ")" << endl; + std::cout << "Executed INSERT instruction (" << start_position << ", " << text_to_insert << ")" << std::endl; } return; @@ -326,11 +324,11 @@ void editor::file::insert(int64_t start_position, string text_to_insert) { ftruncate(file_descriptor, new_file_length); if (verbose) { - cout << "Adjusted file length to " << new_file_length << endl; + std::cout << "Adjusted file length to " << new_file_length << std::endl; } // Add a newline char - file_stream.seekp(new_file_length - 1, ios::beg); + file_stream.seekp(new_file_length - 1, std::ios::beg); file_stream.write("\n", 1); for (int64_t i = (new_file_length - 1); i > start_position; i = (i - amount_to_store)) { @@ -362,25 +360,25 @@ void editor::file::insert(int64_t start_position, string text_to_insert) { char* temp_data_storage = new char[amount_to_store + 1]{0}; // Allocate memory // Store read portion into allocated memory - file_stream.seekg(copy_from_this_position, ios::beg); + file_stream.seekg(copy_from_this_position, std::ios::beg); file_stream.read(temp_data_storage, amount_to_store); // Add a NUL char to the end to terminate the string temp_data_storage[amount_to_store] = 0; // Copy it to its new proper place - file_stream.seekp(copy_to_this_position, ios::beg); + file_stream.seekp(copy_to_this_position, std::ios::beg); file_stream.write(temp_data_storage, amount_to_store); delete[] temp_data_storage; // Free memory if (verbose) { - cout << "Moved " << amount_to_store << " bytes to position #" << copy_to_this_position << " for INSERT instruction" << endl; + std::cout << "Moved " << amount_to_store << " bytes to position #" << copy_to_this_position << " for INSERT instruction" << std::endl; } } // Now, finally, insert the damn data (user inputted data) - file_stream.seekp(start_position, ios::beg); + file_stream.seekp(start_position, std::ios::beg); file_stream.write(text_to_insert.c_str(), text_to_insert.length()); // Flush changes @@ -391,7 +389,7 @@ void editor::file::insert(int64_t start_position, string text_to_insert) { file_length = new_file_length; if (verbose) { - cout << "Executed INSERT instruction (" << start_position << ", " << text_to_insert << ")" << endl; + std::cout << "Executed INSERT instruction (" << start_position << ", " << text_to_insert << ")" << std::endl; } } @@ -421,7 +419,7 @@ void editor::file::remove(int64_t start_position, int64_t end_position) { ftruncate(file_descriptor, new_file_length); // Add a newline char - file_stream.seekp(new_file_length - 1, ios::beg); + file_stream.seekp(new_file_length - 1, std::ios::beg); file_stream.write("\n", 1); // Flush changes @@ -432,7 +430,7 @@ void editor::file::remove(int64_t start_position, int64_t end_position) { file_length = new_file_length; if (verbose) { - cout << "Executed REMOVE instruction (" << start_position << ", " << end_position << ")" << endl; + std::cout << "Executed REMOVE instruction (" << start_position << ", " << end_position << ")" << std::endl; } return; @@ -455,20 +453,20 @@ void editor::file::remove(int64_t start_position, int64_t end_position) { char* temp_data_storage = new char[amount_to_store + 1]{0}; // Allocate memory // Store read portion into allocated memory - file_stream.seekg(copy_from_this_position, ios::beg); + file_stream.seekg(copy_from_this_position, std::ios::beg); file_stream.read(temp_data_storage, amount_to_store); // Add a NUL char to the end to terminate the string temp_data_storage[amount_to_store] = 0; // Copy it to its new proper place - file_stream.seekp(copy_to_this_position, ios::beg); + file_stream.seekp(copy_to_this_position, std::ios::beg); file_stream.write(temp_data_storage, amount_to_store); delete[] temp_data_storage; // Free memory if (verbose) { - cout << "Moved " << amount_to_store << " bytes to position #" << copy_to_this_position << " for REMOVE instruction" << endl; + std::cout << "Moved " << amount_to_store << " bytes to position #" << copy_to_this_position << " for REMOVE instruction" << std::endl; } } @@ -476,7 +474,7 @@ void editor::file::remove(int64_t start_position, int64_t end_position) { ftruncate(file_descriptor, new_file_length); // Add a newline char - file_stream.seekp(new_file_length - 1, ios::beg); + file_stream.seekp(new_file_length - 1, std::ios::beg); file_stream.write("\n", 1); // Flush changes @@ -487,7 +485,7 @@ void editor::file::remove(int64_t start_position, int64_t end_position) { file_length = new_file_length; if (verbose) { - cout << "Executed REMOVE instruction (" << start_position << ", " << end_position << ")" << endl; + std::cout << "Executed REMOVE instruction (" << start_position << ", " << end_position << ")" << std::endl; } } @@ -512,7 +510,7 @@ bool editor::file::execute_single_instruction(instruction instruction_to_execute } void editor::file::close() { - /* Close the fstream */ + /* Close the std::fstream */ file_stream.close(); /* Close the C-type FILE* */ fclose(c_type_file); @@ -521,7 +519,7 @@ void editor::file::close() { } -editor::file::file(string path, int blocksize, bool verbose_mode) { +editor::file::file(std::string path, int blocksize, bool verbose_mode) { block_size = blocksize; verbose = verbose_mode; set_file(path); diff --git a/edit/src/class_instruction.cpp b/edit/src/class_instruction.cpp index 0688bdc..94ddd77 100644 --- a/edit/src/class_instruction.cpp +++ b/edit/src/class_instruction.cpp @@ -1,5 +1,3 @@ -using namespace std; - void editor::instruction::clear_instruction() { /*** void clear_instruction(): @@ -28,7 +26,7 @@ void editor::instruction::process_special_chars() { } -bool editor::instruction::set_replace_instruction(int64_t start, string text) { +bool editor::instruction::set_replace_instruction(int64_t start, std::string text) { /*** bool set_replace_instruction(): - Sets the current instruction to type "replace" @@ -66,7 +64,7 @@ bool editor::instruction::set_replace_instruction(int64_t start, string text) { return true; } -bool editor::instruction::set_insert_instruction(int64_t start, string text) { +bool editor::instruction::set_insert_instruction(int64_t start, std::string text) { /*** bool set_insert_instruction(): - Sets the current instruction to type "insert" @@ -149,16 +147,16 @@ bool editor::instruction::set_remove_instruction(int64_t start, int64_t end) { return true; } -void editor::instruction::set_error_message(string message) { +void editor::instruction::set_error_message(std::string message) { /*** - void set_error_message(string message): + void set_error_message(std::string message): Set the instruction's "error_message" to the inputted string ***/ error_message = message; } -string editor::instruction::get_error_message() { +std::string editor::instruction::get_error_message() { return error_message; } @@ -186,11 +184,11 @@ int64_t editor::instruction::get_end_position() { return end_position; } -string editor::instruction::get_text() { +std::string editor::instruction::get_text() { return text_input; } -editor::instruction create_replace_instruction(int64_t start_position, string text) { +editor::instruction create_replace_instruction(int64_t start_position, std::string text) { editor::instruction new_instruction; new_instruction.set_replace_instruction(start_position, text); @@ -198,7 +196,7 @@ editor::instruction create_replace_instruction(int64_t start_position, string te return new_instruction; } -editor::instruction create_insert_instruction(int64_t start_position, string text) { +editor::instruction create_insert_instruction(int64_t start_position, std::string text) { editor::instruction new_instruction; new_instruction.set_insert_instruction(start_position, text); @@ -215,12 +213,12 @@ editor::instruction create_remove_instruction(int64_t start_position, int end_po } -editor::instruction parse_instruction_string(string instruction_string) { +editor::instruction parse_instruction_string(std::string instruction_string) { /* - instruction parse_instruction_string(string instruction_string): + instruction parse_instruction_string(std::string instruction_string): Create an 'instruction' object from a properly-formatted string - The string must be formatted in one of the following ways: + The std::string must be formatted in one of the following ways: 1. REPLACE 5 hello ( @@ -252,12 +250,12 @@ editor::instruction parse_instruction_string(string instruction_string) { New file: 0123456123 ) - That is, the string must be formatted with these rules: + That is, the std::string must be formatted with these rules: - Space-delimited - The first key is the instruction name (REPLACE, INSERT, or REMOVE) - The second key is the start position - The third key is either: - - A string (in the case of REPLACE or INSERT) + - A std::string (in the case of REPLACE or INSERT) - The end position (in the case of REMOVE) */ @@ -268,10 +266,10 @@ editor::instruction parse_instruction_string(string instruction_string) { instruction_string = remove_leading_whitespace(instruction_string); /* - Split the string into a vector delimited by spaces + Split the std::string into a std::vector delimited by spaces See: shared_functions/explode.cpp */ - vector instruction_vector = explode(instruction_string, ' ', 3); + std::vector instruction_vector = explode(instruction_string, ' ', 3); /* Set up an 'instruction' object marked invalid to return if there's a problem @@ -286,9 +284,9 @@ editor::instruction parse_instruction_string(string instruction_string) { return invalid_instruction; } - string first_element = ""; - string second_element = ""; - string third_element = ""; + std::string first_element = ""; + std::string second_element = ""; + std::string third_element = ""; bool second_element_is_end = false; bool third_element_is_end = false; @@ -387,10 +385,10 @@ editor::instruction parse_instruction_string(string instruction_string) { } -vector parse_instruction_set_string(string instruction_set_string) { +std::vector parse_instruction_set_string(std::string instruction_set_string) { /*** - vector parse_instruction_set_string(string instruction_set_string): - Create a vector of 'instruction' objects from a newline-delimited string of properly-formatted instructions + std::vector parse_instruction_set_string(std::string instruction_set_string): + Create a std::vector of 'instruction' objects from a newline-delimited std::string of properly-formatted instructions Example of ONE properly-formatted "instruction set" string: REPLACE 5 hello @@ -398,16 +396,16 @@ vector parse_instruction_set_string(string instruction_set_ REMOVE 7 10 Each line must follow the normal rules for properly-formatted instructions - See: parse_instruction_string(string instruction_string) + See: parse_instruction_string(std::string instruction_string) The "instruction set" itself must be newline-delimited That is, each individual instruction must be on its own line ***/ - vector output_instruction_set; + std::vector output_instruction_set; - vector instruction_strings = explode(instruction_set_string, '\n'); + std::vector instruction_strings = explode(instruction_set_string, '\n'); - for (string i : instruction_strings) { + for (std::string i : instruction_strings) { output_instruction_set.push_back( parse_instruction_string(i) ); } diff --git a/edit/src/main.cpp b/edit/src/main.cpp index ec0f136..e719737 100644 --- a/edit/src/main.cpp +++ b/edit/src/main.cpp @@ -10,13 +10,11 @@ #include #endif -using namespace std; - int main(int argc, char* argv[]) { - string program_name = "polonius-editor"; + std::string program_name = "polonius-editor"; - string help_string = + std::string help_string = program_name + " " + program_version + "\nCopyright (C) 2023 " + program_author + "\n\n" "This is free software (GNU GPL 3), and you are welcome to redistribute it under certain conditions.\n\n" "" @@ -80,20 +78,20 @@ int main(int argc, char* argv[]) { /* Necessary information for the program to do its job */ - string file_to_edit = ""; + std::string file_to_edit = ""; bool received_filename = false; - vector instructions_to_add; + std::vector instructions_to_add; int block_size = 10240; bool interpret_special_chars = false; bool verbose = false; /* - temp_instruction_set vector - -s / --add-instruction-set option fills this vector, moves its elements to instructions_to_add, and then clears this vector + temp_instruction_set std::vector + -s / --add-instruction-set option fills this std::vector, moves its elements to instructions_to_add, and then clears this std::vector */ - vector temp_instruction_set; + std::vector temp_instruction_set; /* @@ -128,7 +126,7 @@ int main(int argc, char* argv[]) { switch(c) { case 'i': if (received_filename) { - cerr << "polonius-editor: Error: Multiple files specified" << endl; + std::cerr << "polonius-editor: Error: Multiple files specified" << std::endl; return EXIT_BADFILE; } file_to_edit = optarg; @@ -147,7 +145,7 @@ int main(int argc, char* argv[]) { case 'b': block_size = parse_block_units(optarg); if (block_size == -1) { - cerr << program_name << ": Block size '" << optarg << "' is not understood" << endl << "Use -h for help" << endl; + std::cerr << program_name << ": Block size '" << optarg << "' is not understood" << std::endl << "Use -h for help" << std::endl; return EXIT_BADARG; } break; @@ -161,18 +159,18 @@ int main(int argc, char* argv[]) { break; case 'V': - cout << program_version << endl; + std::cout << program_version << std::endl; return EXIT_SUCCESS; break; case 'h': - cout << help_string; + std::cout << help_string; return EXIT_SUCCESS; break; case '?': if (optopt == 'i' || optopt == 's' || optopt == 'a' || optopt == 'b') { - cerr << program_name << ": Option -" << (char)optopt << " requires an argument" << endl << "Use -h for help" << endl; + std::cerr << program_name << ": Option -" << (char)optopt << " requires an argument" << std::endl << "Use -h for help" << std::endl; return EXIT_BADOPT; } break; @@ -181,7 +179,7 @@ int main(int argc, char* argv[]) { for (option_index = optind; option_index < argc; option_index++) { if (received_filename) { - cerr << "polonius-editor: Error: Multiple files specified" << endl; + std::cerr << "polonius-editor: Error: Multiple files specified" << std::endl; return EXIT_BADFILE; } file_to_edit = argv[option_index]; @@ -189,14 +187,14 @@ int main(int argc, char* argv[]) { } if (!received_filename) { - cerr << program_name << ": No input file given. Use -h for help" << endl; + std::cerr << program_name << ": No input file given. Use -h for help" << std::endl; return EXIT_BADFILE; } editor::file document(file_to_edit, block_size, verbose); if (!document.is_initialized()) { - cerr << program_name << ": " << document.get_error_message() << endl; + std::cerr << program_name << ": " << document.get_error_message() << std::endl; return EXIT_OTHER; } @@ -207,12 +205,12 @@ int main(int argc, char* argv[]) { } if (!document.add_instruction(instructions_to_add[i])) { - cerr << program_name << ": " << instructions_to_add[i].get_error_message() << endl; + std::cerr << program_name << ": " << instructions_to_add[i].get_error_message() << std::endl; return EXIT_BADARG; } } - vector instruction_set = document.get_instruction_set(); + std::vector instruction_set = document.get_instruction_set(); for (int i = 0; i < instruction_set.size(); i++) { document.execute_single_instruction(instruction_set[i]); diff --git a/edit/src/namespace_editor.h b/edit/src/namespace_editor.h index 12c61d3..fc0c006 100644 --- a/edit/src/namespace_editor.h +++ b/edit/src/namespace_editor.h @@ -1,5 +1,3 @@ -using namespace std; - namespace editor { enum operation_type { @@ -24,18 +22,18 @@ namespace editor { int64_t start_position = -1; int64_t end_position = -1; - string text_input = ""; + std::string text_input = ""; - string error_message = ""; + std::string error_message = ""; public: void clear_instruction(); void process_special_chars(); - bool set_replace_instruction(int64_t start, string text); - bool set_insert_instruction(int64_t start, string text); + 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(string message); + void set_error_message(std::string message); - string get_error_message(); + std::string get_error_message(); void update_start_position(int64_t start); void update_end_position(int64_t end); @@ -44,46 +42,46 @@ namespace editor { int get_operation_type(); int64_t get_start_position(); int64_t get_end_position(); - string get_text(); + std::string get_text(); }; - instruction create_replace_instruction(int64_t start_position, string text); - instruction create_insert_instruction(int64_t start_position, string text); + 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(string instruction_string); + instruction parse_instruction_string(std::string instruction_string); - vector parse_instruction_set_string(string instruction_set); + std::vector parse_instruction_set_string(std::string instruction_set); class file { private: bool initialized = false; - string file_name; - string file_directory; + std::string file_name; + std::string file_directory; int64_t file_length = 0; - fstream file_stream; + std::fstream file_stream; FILE* c_type_file; int file_descriptor; int block_size = 10240; - vector instruction_set; + std::vector instruction_set; int64_t file_length_after_last_instruction = 0; - string error_message = ""; + std::string error_message = ""; bool verbose = false; public: - bool set_file(string file_path); + bool set_file(std::string file_path); void set_block_size(int specified_blocksize); - void replace(int64_t start_position, string replacement_text); - void insert(int64_t start_position, string text_to_insert); + 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); @@ -94,23 +92,23 @@ namespace editor { bool is_initialized(); - string get_file_name(); - string get_file_directory(); + std::string get_file_name(); + std::string get_file_directory(); int get_block_size(); int64_t get_file_length(); - vector get_instruction_set(); + std::vector get_instruction_set(); - string get_error_message(); + std::string get_error_message(); void close(); /* Constructor */ - file(string path, int blocksize = 10240, bool verbose_mode = false); + file(std::string path, int blocksize = 10240, bool verbose_mode = false); }; }