make functions inline & handle input from stdout

This commit is contained in:
千住柱間 2024-05-17 14:21:11 -04:00
parent 0bcd58398d
commit cd14096781
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -6,16 +6,17 @@
#include <cctype> #include <cctype>
#include <xcdat.hpp> #include <xcdat.hpp>
#include <utility> #include <utility>
#include <unistd.h>
// Struct to hold the view and id of each entry // Struct to hold the view and id of each entry
struct Entry { struct Entry {
std::string_view decoded_view; std::string_view decoded_view;
uint64_t id; uint64_t id;
Entry(std::string_view decoded_view, uint64_t id) : decoded_view(decoded_view), id(id) {} inline Entry(std::string_view decoded_view, uint64_t id) : decoded_view(decoded_view), id(id) {}
}; };
// Function to remove one UTF-8 character and return the character along with the new string // Function to remove one UTF-8 character and return the character along with the new string
std::pair<std::string, std::string> remove_one_utf8_char(const std::string& str) { inline std::pair<std::string, std::string> remove_one_utf8_char(const std::string& str) {
if (str.empty()) { if (str.empty()) {
return {"", str}; return {"", str};
} }
@ -37,13 +38,41 @@ std::pair<std::string, std::string> remove_one_utf8_char(const std::string& str)
return {"", ""}; return {"", ""};
} }
int main(int argc, char* argv[]) { // Function to check input from command line or piped input
if (argc < 2) { inline std::string get_input(int argc, char* argv[]) {
std::cerr << "Usage: " << argv[0] << " <search_string> [--debug] [--dict <path_to_dictionary>]" << std::endl; std::string search_string;
return 1;
if (!isatty(fileno(stdin))) { // If input is piped
std::getline(std::cin, search_string);
} else { // If input is provided as command line argument
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <search_string>" << std::endl;
exit(1);
}
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--debug") == 0) {
// Handle debug mode
} else if (std::strcmp(argv[i], "--dict") == 0 && i + 1 < argc) {
// Skip the next argument as it's the dictionary file path
i++;
} else {
search_string = argv[i];
break;
}
}
} }
std::string search_string; if (search_string.empty()) {
std::cerr << "Search string not provided." << std::endl;
exit(1);
}
return search_string;
}
int main(int argc, char* argv[]) {
std::string search_string = get_input(argc, argv);
bool debug_mode = false; bool debug_mode = false;
std::string dict_file = "dict.bin"; // Default dictionary file path std::string dict_file = "dict.bin"; // Default dictionary file path
@ -54,16 +83,9 @@ int main(int argc, char* argv[]) {
} else if (std::strcmp(argv[i], "--dict") == 0 && i + 1 < argc) { } else if (std::strcmp(argv[i], "--dict") == 0 && i + 1 < argc) {
dict_file = argv[i + 1]; dict_file = argv[i + 1];
i++; // Skip the next argument as it's the dictionary file path i++; // Skip the next argument as it's the dictionary file path
} else {
search_string = argv[i];
} }
} }
if (search_string.empty()) {
std::cerr << "Search string not provided." << std::endl;
return 1;
}
std::string raw_output; std::string raw_output;
const auto trie = xcdat::load<xcdat::trie_15_type>(dict_file); const auto trie = xcdat::load<xcdat::trie_15_type>(dict_file);