Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#define SIMPLECPP_TOKENLIST_ALLOW_PTR 0
#include "simplecpp.h"

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sys/stat.h>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <utility>
#include <vector>

Expand All @@ -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;

Expand Down Expand Up @@ -86,9 +93,6 @@ int main(int argc, char **argv)
break;
}
dui.includes.emplace_back(std::move(value));
} else if (std::strcmp(arg, "-is")==0) {
found = true;
use_istream = true;
}
break;
case 's':
Expand All @@ -102,6 +106,10 @@ int main(int argc, char **argv)
}
dui.std = std::move(value);
}
else if (std::strcmp(arg, "-ss")==0) {
toklist_inf = Sstream;
found = true;
}
break;
case 'q':
if (std::strcmp(arg, "-q")==0) {
Expand All @@ -116,9 +124,17 @@ int main(int argc, char **argv)
}
break;
case 'f':
if (std::strcmp(arg, "-f")==0) {
if (std::strcmp(arg, "-file")==0) {
toklist_inf = File;
found = true;
}
else if (std::strcmp(arg, "-fs")==0) {
toklist_inf = Fstream;
found = true;
}
else if (std::strcmp(arg, "-f")==0) {
fail_on_error = true;
found = true;
}
break;
case 'l':
Expand All @@ -127,6 +143,12 @@ int main(int argc, char **argv)
found = true;
}
break;
case 'b':
if (std::strcmp(arg, "-buf")==0) {
toklist_inf = CharBuffer;
found = true;
}
break;
}
if (!found) {
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
Expand Down Expand Up @@ -157,7 +179,10 @@ 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 << " -file Consume input as file pointer (default)." << std::endl;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong opinions but an alternative would be: -input=file|fs|ss|buf

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. I based it on the existing single option and did not consider using a key-value approach.

std::cout << " -fs Consume input as file stream." << std::endl;
std::cout << " -ss Consume input as string stream." << std::endl;
std::cout << " -buf Consume input as char 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;
Expand Down Expand Up @@ -197,8 +222,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);
Expand Down
Loading