From cd140967811741d54d067f918edd64c93c2d0436 Mon Sep 17 00:00:00 2001 From: hashirama Date: Fri, 17 May 2024 14:21:11 -0400 Subject: [PATCH] make functions inline & handle input from stdout --- src/main.cc | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main.cc b/src/main.cc index 609d085..c277fa8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,16 +6,17 @@ #include #include #include +#include // Struct to hold the view and id of each entry struct Entry { std::string_view decoded_view; 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 -std::pair remove_one_utf8_char(const std::string& str) { +inline std::pair remove_one_utf8_char(const std::string& str) { if (str.empty()) { return {"", str}; } @@ -37,13 +38,41 @@ std::pair remove_one_utf8_char(const std::string& str) return {"", ""}; } -int main(int argc, char* argv[]) { - if (argc < 2) { - std::cerr << "Usage: " << argv[0] << " [--debug] [--dict ]" << std::endl; - return 1; - } - +// Function to check input from command line or piped input +inline std::string get_input(int argc, char* argv[]) { std::string search_string; + + 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] << " " << 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; + } + } + } + + 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; 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) { dict_file = argv[i + 1]; 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; const auto trie = xcdat::load(dict_file);