No longer using namespace std by default

This commit is contained in:
Andrew S. Rightenburg 2023-10-05 11:15:51 +08:00
parent 5f6e25388d
commit cb930f9f34
Signed by: rail5
GPG Key ID: A0CB570AB6629159
3 changed files with 47 additions and 53 deletions

View File

@ -8,9 +8,7 @@
#include <iostream>
#endif
using namespace std;
bool reader::file::init(string path) {
bool reader::file::init(std::string path) {
if (!file_exists(path)) {
init_error_message = "File '" + path + "' does not exist";
return false;
@ -18,7 +16,7 @@ bool reader::file::init(string path) {
file_name = path;
ifstream file_stream(file_name, ifstream::binary);
std::ifstream file_stream(file_name, std::ifstream::binary);
if (!file_stream) {
init_error_message = "Could not open file '" + file_name + "' for reading";
@ -34,12 +32,12 @@ bool reader::file::init(string path) {
return true;
}
string reader::file::read(int64_t start_position, int64_t length) {
std::string reader::file::read(int64_t start_position, int64_t length) {
ifstream file_stream(file_name, ifstream::binary);
std::ifstream file_stream(file_name, std::ifstream::binary);
// allocate memory
string buffer(length, ' ');
std::string buffer(length, ' ');
// set position
file_stream.seekg(start_position);
@ -59,7 +57,7 @@ bool reader::file::do_read_job() {
Just output in the format "startposition endposition"
*/
if (just_outputting_positions) {
cout << start_position << " " << end_position-1 << endl;
std::cout << start_position << " " << end_position-1 << std::endl;
return true;
}
@ -70,7 +68,7 @@ bool reader::file::do_read_job() {
block_size = amount_left_in_file;
}
cout << read(i, block_size);
std::cout << read(i, block_size);
}
return true;
}
@ -100,7 +98,7 @@ bool reader::file::do_normal_search() {
shift_by_this_much = amount_left_in_file;
}
string block_data = read(i, block_size);
std::string block_data = read(i, block_size);
// Check if the WHOLE search query is in the block
// And if so, just output it
@ -120,11 +118,11 @@ bool reader::file::do_normal_search() {
match_end = (match_start + search_query_length);
if (just_outputting_positions) {
cout << match_start << " " << match_end-1 << endl;
std::cout << match_start << " " << match_end-1 << std::endl;
return true;
}
cout << block_data.substr(start_position, search_query_length) << endl;
std::cout << block_data.substr(start_position, search_query_length) << std::endl;
return true;
}
@ -133,7 +131,7 @@ bool reader::file::do_normal_search() {
bool reader::file::do_regex_search() {
/***
A regex search in Polonius should happen this way:
A std::regex search in Polonius should happen this way:
0. Validate the regular expression (TODO: expression currently not validated before running)
1. Parse the regular expression into its component parts
e.g.:
@ -162,7 +160,7 @@ bool reader::file::do_regex_search() {
Final:
Report the found match
One important restriction is that we will be limited to finding regex matches no longer than the user-specified block size
One important restriction is that we will be limited to finding std::regex matches no longer than the user-specified block size
(default 10KB)
TODO:
@ -188,7 +186,7 @@ bool reader::file::do_regex_search() {
int64_t match_start = 0;
int64_t match_end = 0;
vector<string> sub_expressions = create_sub_expressions(search_query);
std::vector<std::string> sub_expressions = create_sub_expressions(search_query);
for (int64_t current_index = start_position; current_index < end_position; (current_index = current_index + block_size)) {
regex_scan:
@ -198,16 +196,16 @@ bool reader::file::do_regex_search() {
block_size = amount_left_in_file;
}
string block_data = read(current_index, block_size);
smatch regex_search_result;
regex expression(search_query);
std::string block_data = read(current_index, block_size);
std::smatch regex_search_result;
std::regex expression(search_query);
bool full_match_found = regex_search(block_data, regex_search_result, expression);
if (!full_match_found) {
for (int64_t j = 0; j < sub_expressions.size(); j++) {
smatch sub_expression_search_result;
regex sub_expression(sub_expressions[j] + R"($)"); // 'R"($)"' signifies that the string must END with the match
std::smatch sub_expression_search_result;
std::regex sub_expression(sub_expressions[j] + R"($)"); // 'R"($)"' signifies that the std::string must END with the match
// Partial match found?
bool partial_match_found = regex_search(block_data, sub_expression_search_result, sub_expression);
@ -226,11 +224,11 @@ bool reader::file::do_regex_search() {
match_end = current_index + (block_size - regex_search_result.suffix().length());
if (just_outputting_positions) {
cout << match_start << " " << match_end-1 << endl;
std::cout << match_start << " " << match_end-1 << std::endl;
return true;
}
cout << regex_search_result[0] << endl;
std::cout << regex_search_result[0] << std::endl;
return true;
}
return false;
@ -247,7 +245,7 @@ bool reader::file::do_search_job() {
bool reader::file::do_job() {
if (!initialized) {
cout << "Error reading file" << endl;
std::cout << "Error reading file" << std::endl;
return false;
}
@ -280,7 +278,7 @@ bool reader::file::do_job() {
return false;
}
string reader::file::get_init_error_message() {
std::string reader::file::get_init_error_message() {
return init_error_message;
}
@ -304,7 +302,7 @@ void reader::file::set_block_size(int size) {
block_size = size;
}
void reader::file::set_search_query(string query) {
void reader::file::set_search_query(std::string query) {
search_query = query;
}

View File

@ -10,13 +10,11 @@
#include <getopt.h>
#endif
using namespace std;
int main(int argc, char* argv[]) {
string program_name = "polonius-reader";
std::string program_name = "polonius-reader";
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"
""
@ -46,7 +44,7 @@ int main(int argc, char* argv[]) {
" -f\n"
" --find\n"
" --search\n"
" Search for a string in the file\n"
" Search for a std::string in the file\n"
" If -s / --start is specified, the program will only search for matches after that start position\n"
" If -l / --length is specified, the program will only search for matches within that length from the start\n"
" Returns nothing (blank) if no matches were found\n\n"
@ -90,7 +88,7 @@ int main(int argc, char* argv[]) {
"amount_to_read" == -1 will result in reading from the start position to the end of the file
*/
bool received_filename = false;
string file_to_read = "";
std::string file_to_read = "";
int64_t start_position = 0;
int64_t amount_to_read = -1;
@ -98,7 +96,7 @@ int main(int argc, char* argv[]) {
int block_size = 10240;
reader::job_type job = reader::read_job;
string searching_for = "";
std::string searching_for = "";
bool output_position = false;
@ -132,7 +130,7 @@ int main(int argc, char* argv[]) {
switch(c) {
case 'i':
if (received_filename) {
cerr << program_name << ": Error: Multiple files specified" << endl;
std::cerr << program_name << ": Error: Multiple files specified" << std::endl;
return EXIT_BADFILE;
}
file_to_read = optarg;
@ -141,24 +139,24 @@ int main(int argc, char* argv[]) {
case 's':
if (!is_number(optarg)) {
cerr << program_name << ": '" << optarg << "' is not an integer" << endl << "Use -h for help" << endl;
std::cerr << program_name << ": '" << optarg << "' is not an integer" << std::endl << "Use -h for help" << std::endl;
return EXIT_BADARG;
}
start_position = (int64_t)stoll(optarg);
start_position = (int64_t)std::stoll(optarg);
break;
case 'l':
if (!is_number(optarg)) {
cerr << program_name << ": '" << optarg << "' is not an integer" << endl << "Use -h for help" << endl;
std::cerr << program_name << ": '" << optarg << "' is not an integer" << std::endl << "Use -h for help" << std::endl;
return EXIT_BADARG;
}
amount_to_read = (int64_t)stoll(optarg);
amount_to_read = (int64_t)std::stoll(optarg);
break;
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;
@ -181,18 +179,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 == 'l') {
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;
@ -201,7 +199,7 @@ int main(int argc, char* argv[]) {
for (option_index = optind; option_index < argc; option_index++) {
if (received_filename) {
cerr << program_name << ": Error: Multiple files specified" << endl;
std::cerr << program_name << ": Error: Multiple files specified" << std::endl;
return EXIT_BADFILE;
}
file_to_read = argv[option_index];
@ -210,14 +208,14 @@ int main(int argc, char* argv[]) {
// Make sure we got an input file
if (!received_filename) {
cerr << help_string;
std::cerr << help_string;
return EXIT_BADFILE;
}
reader::file the_file;
if (!the_file.init(file_to_read)) {
cerr << program_name << ": " << the_file.get_init_error_message() << endl;
std::cerr << program_name << ": " << the_file.get_init_error_message() << std::endl;
return EXIT_BADFILE;
}

View File

@ -1,5 +1,3 @@
using namespace std;
namespace reader {
enum job_type {
@ -16,8 +14,8 @@ namespace reader {
private:
bool initialized = false;
string file_name;
string init_error_message = "unknown";
std::string file_name;
std::string init_error_message = "unknown";
int64_t file_length = 0;
bool just_outputting_positions = false;
@ -28,7 +26,7 @@ namespace reader {
int block_size = 10240;
string search_query = "";
std::string search_query = "";
search_type query_type = t_normal_search;
job_type job = read_job;
@ -40,11 +38,11 @@ namespace reader {
bool do_regex_search();
public:
bool init(string path);
bool init(std::string path);
string read(int64_t start_position, int64_t length);
std::string read(int64_t start_position, int64_t length);
string get_init_error_message();
std::string get_init_error_message();
int64_t get_file_length();
bool do_job();
@ -53,7 +51,7 @@ namespace reader {
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(string query);
void set_search_query(std::string query);
void set_search_type(search_type normal_or_regex);
void set_job_type(job_type input_job);
};