URIReader fixes / improvements

This commit is contained in:
Thulinma 2020-02-20 14:03:28 +01:00
parent 6cea9f2092
commit 029c96879d

View file

@ -3,11 +3,10 @@
class URITest : public Util::DataCallback{ class URITest : public Util::DataCallback{
public: public:
int goStdin(bool useCallback = true);
int goHTTP(char *, bool useCallback = true);
void dump(const char *ptr, size_t size); void dump(const char *ptr, size_t size);
size_t wanted = 100000000; size_t wanted = 100000000;
void dataCallback(const char *ptr, size_t size); void dataCallback(const char *ptr, size_t size);
int main(int argc, char ** argv);
}; };
void URITest::dataCallback(const char *ptr, size_t size){ void URITest::dataCallback(const char *ptr, size_t size){
@ -18,82 +17,38 @@ void URITest::dump(const char *ptr, size_t size){
if (fwrite(ptr, sizeof(char), size, stdout) != size){INFO_MSG("error: %s", strerror(errno));} if (fwrite(ptr, sizeof(char), size, stdout) != size){INFO_MSG("error: %s", strerror(errno));}
} }
int URITest::goStdin(bool useCallback){ int URITest::main(int argc, char **argv){
HTTP::URIReader U; Util::redirectLogsIfNeeded();
HTTP::URL uri; Util::Config cfg(argv[0]);
uri.path = "-"; JSON::Value option;
U.open(uri); option["arg_num"] = 1;
option["arg"] = "string";
option["help"] = "Name of the input URI or - for stdin";
option["value"].append("-");
cfg.addOption("input", option);
option.null();
option["short"] = "r";
option["long"] = "readall";
option["help"] = "Read all data all at once in blocking mode";
option["value"].append(0);
cfg.addOption("readall", option);
if (!cfg.parseArgs(argc, argv)){return 1;}
if (useCallback){ cfg.activate();
MEDIUM_MSG("read from STDIN with callbacks"); HTTP::URIReader R(cfg.getString("input"));
U.readAll(*this);
}else{ if (cfg.getBool("readall")){
MEDIUM_MSG("read from STDIN without callbacks");
char *dPtr = 0; char *dPtr = 0;
size_t dLen = 0; size_t dLen = 0;
U.readAll(dPtr, dLen); R.readAll(dPtr, dLen);
dump(dPtr, dLen); dump(dPtr, dLen);
// INFO_MSG("length: %d", dLen);
}
return 0;
}
int URITest::goHTTP(char *uri, bool useCallback){
HTTP::URIReader d;
d.open(uri);
if (useCallback){
MEDIUM_MSG("read file or url with callbacks");
// d.readAll(*this);
while (!d.isEOF()){d.readSome(10486, *this);}
}else{ }else{
MEDIUM_MSG("read file or url without callbacks"); while (!R.isEOF() && cfg.is_active){R.readSome(10486, *this);}
char *dPtr = 0;
size_t dLen = 0;
d.readAll(dPtr, dLen);
dump(dPtr, dLen);
} }
return 0; return 0;
} }
int main(int argc, char **argv){ int main(int argc, char **argv){
// Util::Config::printDebugLevel = 10;
Util::Config cfg;
cfg.activate();
URITest t; URITest t;
t.main(argc, argv);
if (!isatty(fileno(stdin))){
if (argv[1]){
t.goStdin(false);
}else{
t.goStdin();
}
}else{
if (argc == 1){
std::cout << "no arguments applied!" << std::endl;
std::cout << "usage: " << std::endl;
std::cout << "STDIN:\t urireader < filename " << std::endl;
std::cout << "URL:\t urireader http://url " << std::endl;
std::cout << "FILE:\t urireader path_to_file " << std::endl << std::endl;
std::cout << "Outputs content to stdout, use ' > outputfile' after the command to write "
"contents to disk"
<< std::endl
<< std::endl;
return 0;
}else{
if (argv[2]){
t.goHTTP(argv[1], false);
}else{
t.goHTTP(argv[1]);
}
}
}
return 0;
} }