diff --git a/main.cpp b/main.cpp index 5c48f830..4a6a2894 100644 --- a/main.cpp +++ b/main.cpp @@ -6,12 +6,14 @@ #define SIMPLECPP_TOKENLIST_ALLOW_PTR 0 #include "simplecpp.h" +#include #include #include #include #include -#include +#include #include +#include #include #include @@ -28,7 +30,12 @@ int main(int argc, char **argv) { bool error = false; const char *filename = nullptr; - bool use_istream = false; + enum : std::uint8_t { + File, + Fstream, + Sstream, + CharBuffer + } toklist_inf = File; bool fail_on_error = false; bool linenrs = false; @@ -86,9 +93,28 @@ int main(int argc, char **argv) break; } dui.includes.emplace_back(std::move(value)); - } else if (std::strcmp(arg, "-is")==0) { + } + else if (std::strncmp(arg, "-input=",7)==0) { found = true; - use_istream = true; + const std::string input = arg + 7; + if (input.empty()) { + std::cout << "error: option -inout with no value." << std::endl; + error = true; + break; + } + if (input == "file") { + toklist_inf = File; + } else if (input == "fstream") { + toklist_inf = Fstream; + } else if (input == "sstream") { + toklist_inf = Sstream; + } else if (input == "buffer") { + toklist_inf = CharBuffer; + } else { + std::cout << "error: unknown input type '" << input << "'." << std::endl; + error = true; + break; + } } break; case 's': @@ -117,8 +143,8 @@ int main(int argc, char **argv) break; case 'f': if (std::strcmp(arg, "-f")==0) { - found = true; fail_on_error = true; + found = true; } break; case 'l': @@ -157,7 +183,7 @@ int main(int argc, char **argv) std::cout << " -UNAME Undefine NAME." << std::endl; std::cout << " -std=STD Specify standard." << std::endl; std::cout << " -q Quiet mode (no output)." << std::endl; - std::cout << " -is Use std::istream interface." << std::endl; + std::cout << " -input=INPUT Specify input type - file (default), fstream, sstream, buffer." << std::endl; std::cout << " -e Output errors only." << std::endl; std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl; std::cout << " -l Print lines numbers." << std::endl; @@ -197,8 +223,21 @@ int main(int argc, char **argv) simplecpp::TokenList outputTokens(files); { simplecpp::TokenList *rawtokens; - if (use_istream) { - rawtokens = new simplecpp::TokenList(f, files,filename,&outputList); + if (toklist_inf == Fstream) { + rawtokens = new simplecpp::TokenList(f,files,filename,&outputList); + } + else if (toklist_inf == Sstream || toklist_inf == CharBuffer) { + std::ostringstream oss; + oss << f.rdbuf(); + f.close(); + const std::string s = oss.str(); + if (toklist_inf == Sstream) { + std::istringstream iss(s); + rawtokens = new simplecpp::TokenList(iss,files,filename,&outputList); + } + else { + rawtokens = new simplecpp::TokenList({s.data(),s.size()},files,filename,&outputList); + } } else { f.close(); rawtokens = new simplecpp::TokenList(filename,files,&outputList);