This commit is contained in:
Andrew S. Rightenburg 2023-03-27 00:41:31 -04:00
parent 5e703a84f5
commit 7891949dcc
3 changed files with 64 additions and 7 deletions

View file

@ -85,6 +85,7 @@ bool editor::file::set_file(string file_path) {
}
file_length = filesystem::file_size(file_path);
file_length_after_last_instruction = file_length;
/*
Set "initialized" to true if we've made it this far
@ -146,8 +147,8 @@ bool editor::file::add_instruction(instruction &input_instruction) {
/*
Now, make sure that the start position isn't further than the end of the file
*/
if ( (input_instruction.get_start_position() >= file_length && file_length > 0) ||
(input_instruction.get_start_position() > 0 && file_length == 0) ) {
if ( (input_instruction.get_start_position() >= file_length_after_last_instruction && file_length_after_last_instruction > 0) ||
(input_instruction.get_start_position() > 0 && file_length_after_last_instruction == 0) ) {
input_instruction.set_error_message("Invalid start position");
return false;
}
@ -156,17 +157,24 @@ bool editor::file::add_instruction(instruction &input_instruction) {
Next, make sure that the end position isn't further than the end of the file IF we're doing anything other than an insert (operation type #1, see namespace_editor.h for list of operation types)
*/
if (input_instruction.get_operation_type() != insert_operation) {
if (input_instruction.get_end_position() >= file_length) {
if (input_instruction.get_end_position() >= file_length_after_last_instruction) {
input_instruction.set_error_message("Invalid end position");
return false;
}
}
/*
Add the instruction to the set and return true
Add the instruction to the set, update file_length_after_last_instruction, and return true
*/
instruction_set.push_back(input_instruction);
if (input_instruction.get_operation_type() == insert_operation) {
file_length_after_last_instruction = (file_length_after_last_instruction + input_instruction.get_text().length());
} else if (input_instruction.get_operation_type() == remove_operation) {
long long int remove_length = (input_instruction.get_end_position() - input_instruction.get_start_position());
file_length_after_last_instruction = (file_length_after_last_instruction - remove_length);
}
return true;
}
@ -213,6 +221,10 @@ void editor::file::replace(long long int start_position, string replacement_text
file_stream.seekp(start_position, ios::beg);
// Replace
file_stream.write(replacement_text.c_str(), replacement_text.length());
// Flush changes
file_stream.flush();
fflush(c_type_file);
}
void editor::file::insert(long long int start_position, string text_to_insert) {
@ -253,6 +265,13 @@ void editor::file::insert(long long int start_position, string text_to_insert) {
file_stream.seekp(new_file_length - 1, ios::beg);
file_stream.write("\n", 1);
// Flush changes
file_stream.flush();
fflush(c_type_file);
// Update file_length for future instructions
file_length = new_file_length;
return;
}
@ -312,6 +331,13 @@ void editor::file::insert(long long int start_position, string text_to_insert) {
// Now, finally, insert the damn data (user inputted data)
file_stream.seekp(start_position, ios::beg);
file_stream.write(text_to_insert.c_str(), text_to_insert.length());
// Flush changes
file_stream.flush();
fflush(c_type_file);
// Update file_length for future instructions
file_length = new_file_length;
}
void editor::file::remove(long long int start_position, long long int end_position) {
@ -343,6 +369,13 @@ void editor::file::remove(long long int start_position, long long int end_positi
file_stream.seekp(new_file_length - 1, ios::beg);
file_stream.write("\n", 1);
// Flush changes
file_stream.flush();
fflush(c_type_file);
// Update file_length for future instructions
file_length = new_file_length;
return;
}
@ -382,6 +415,13 @@ void editor::file::remove(long long int start_position, long long int end_positi
// Add a newline char
file_stream.seekp(new_file_length - 1, ios::beg);
file_stream.write("\n", 1);
// Flush changes
file_stream.flush();
fflush(c_type_file);
// Update file_length for future instructions
file_length = new_file_length;
}
bool editor::file::execute_single_instruction(instruction instruction_to_execute) {

View file

@ -20,7 +20,7 @@ int main(int argc, char* argv[]) {
string program_author = "rail5";
string helpstring = program_name + " " + program_version + "\nCopyright (C) 2023 " + program_author +"\n\nThis is free software (GNU GPL 3), and you are welcome to redistribute it under certain conditions.\n\nUsage: " + program_name + " -i filename -a \"{INSTRUCTION}\"\n\nOptions:\n -i\n --input\n Specify input file to edit\n\n -a\n --add-instruction\n Instruct the program on how to edit your file\n Example instructions:\n REPLACE 5 hello world\n (Replaces text, starting from byte #5, with \"hello world\")\n INSERT 7 salut a tous\n (Inserts \"salut a tous\" at byte #7, shifting the rest of the file without replacing it)\n REMOVE 9 15\n (Removes bytes #9 to #15 from the file)\n\n -s\n --add-instruction-set\n Provide a set of multiple instructions for editing the file\n Each instruction in the set should be on its own line, as in the following example:\n --add-instruction-set \"REPLACE 20 hello world\n INSERT 50 hello again\n REMOVE 70 75\"\n\n -c\n --special-chars\n Interpret escaped character sequences (\\n, \\t and \\\\)\n\n -f\n --follow-logic\n Adjust the positions given by the instruction set as we go along\n For example, in this set:\n REMOVE 5 5\n REPLACE 10 hello world\n After \"Remove 5 5\", which removes the 5th byte from the file, \"Replace 10 hello world\" will be adjusted to \"Replace 9 hello world\", since removing an earlier byte caused the text which was originally at position #10 to move to position #9\n\n -b\n --block-size\n Specify the amount of data from the file we're willing to load into memory at any given time\n Example:\n -b 10M\n -b 200K\n (Default 1 kilobyte)\n\n -h\n --help\n Display this message\n\nExample:\n " + program_name + " --input ./file.txt --add-instruction \"REPLACE 20 hello \\n world\" --add-instruction \"REMOVE 10 12\" --block-size 10K --special-chars\n";
string helpstring = program_name + " " + program_version + "\nCopyright (C) 2023 " + program_author + "\n\nThis is free software (GNU GPL 3), and you are welcome to redistribute it under certain conditions.\n\nUsage: " + program_name + " -i filename -a \"{INSTRUCTION}\"\n\nOptions:\n -i\n --input\n Specify input file to edit\n\n -a\n --add-instruction\n Instruct the program on how to edit your file\n Example instructions:\n REPLACE 5 hello world\n (Replaces text, starting from byte #5, with \"hello world\")\n INSERT 7 salut a tous\n (Inserts \"salut a tous\" at byte #7, shifting the rest of the file without replacing it)\n REMOVE 9 15\n (Removes bytes #9 to #15 from the file)\n\n -s\n --add-instruction-set\n Provide a set of multiple instructions for editing the file\n Each instruction in the set should be on its own line, as in the following example:\n --add-instruction-set \"REPLACE 20 hello world\n INSERT 50 hello again\n REMOVE 70 75\"\n\n -c\n --special-chars\n Interpret escaped character sequences (\\n, \\t and \\\\)\n\n -f\n --follow-logic\n Adjust the positions given by the instruction set as we go along\n For example, in this set:\n REMOVE 5 5\n REPLACE 10 hello world\n After \"Remove 5 5\", which removes the 5th byte from the file, \"Replace 10 hello world\" will be adjusted to \"Replace 9 hello world\", since removing an earlier byte caused the text which was originally at position #10 to move to position #9\n\n -b\n --block-size\n Specify the amount of data from the file we're willing to load into memory at any given time\n Example:\n -b 10M\n -b 200K\n (Default 1 kilobyte)\n\n -h\n --help\n Display this message\n\nExample:\n " + program_name + " --input ./file.txt --add-instruction \"REPLACE 20 hello \\n world\" --add-instruction \"REMOVE 10 12\" --block-size 10K --special-chars\n";
/*

View file

@ -56,30 +56,47 @@ namespace editor {
class file {
private:
bool initialized = false;
string file_name;
string file_directory;
long long int file_length = 0;
int block_size = 1024;
vector<instruction> instruction_set;
fstream file_stream;
FILE* c_type_file;
int file_descriptor;
int block_size = 1024;
vector<instruction> instruction_set;
long long int file_length_after_last_instruction = 0;
bool follow_position_logic = false;
string error_message = "";
public:
bool set_file(string file_path);
void set_block_size(int specified_blocksize);
void replace(long long int start_position, string replacement_text);
void insert(long long int start_position, string text_to_insert);
void remove(long long int start_position, long long int end_position);
bool add_instruction(instruction &input_instruction);
bool execute_single_instruction(instruction instruction_to_execute);
bool execute_instructions();
bool is_initialized();
string get_file_name();
string get_file_directory();
int get_block_size();
long long int get_file_length();
vector<instruction> get_instruction_set();
string get_error_message();