From e4f65d1b2532a9fe386b79fd9759909a32e331fa Mon Sep 17 00:00:00 2001 From: tobast Date: Mon, 28 Nov 2011 22:09:25 +0100 Subject: [PATCH] Added directly the source code of ParameterRead into the project. Shorts parameters are now working! --- Makefile | 4 +- ParameterRead.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++++++ ParameterRead.h | 89 +++++++++++++++++++ func.cpp | 6 +- func.h | 2 +- main.cpp | 2 +- manpage | 72 +++++++++++++++ 7 files changed, 387 insertions(+), 7 deletions(-) create mode 100644 ParameterRead.cpp create mode 100644 ParameterRead.h create mode 100644 manpage diff --git a/Makefile b/Makefile index 7e7cba5..8e7fd50 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ CXX=g++ CXXFLAGS=-Wall -Werror -O2 -CXXLIBS=-lparameterread +CXXLIBS=-lcurl TARGET=pastebincl -OBJS=CurlPost.o func.o main.o +OBJS=CurlPost.o func.o main.o ParameterRead.o all: $(TARGET) diff --git a/ParameterRead.cpp b/ParameterRead.cpp new file mode 100644 index 0000000..f8e76c2 --- /dev/null +++ b/ParameterRead.cpp @@ -0,0 +1,219 @@ + +/* + * PROGRAM: + * Command-line pastebin + * + * AUTHOR: + * Théophile BASTIAN (a.k.a. Tobast) + * + * CONTACT & WEBSITE: + * http://tobast.fr/ (contact feature included) + * error-report@tobast.fr (error reporting only) + * + * SHORT DESCRIPTION: + * See first license line. + * + * LICENSE: + * "Command-line pastebin" is a software designed to submit a "paste" on http://pastebin.com/ using a command-line tool + * Copyright (C) 2011 Théophile BASTIAN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl.txt. +*/ + +#include "ParameterRead.h" + +ParameterRead::ParameterRead(int i_argc, char** i_argv) : argc(i_argc), argv(i_argv) {} + +void ParameterRead::addValueParam(std::string i_valueparam) +{ + /// adds a parameter name that requires a following value. + valueparam.push_back(i_valueparam); +} + + +void ParameterRead::execute() +{ + /// Processes the parameters + + rawcount=0; + for(int i=1;i=rawcount) + throw -1; + std::ostringstream oss; + oss << count; + std::string label=PARAMETER_RAWBEGIN; + label+=oss.str(); + label+=PARAMETER_RAWEND; + return paramMap[label]; +} + +std::string ParameterRead::rawparam(std::string in) +{ + /// returns the "raw" parameter, eg. "input" instead of "--input=file/path.txt" or "la" instead of "-la" + size_t found=0; + switch(getParamType(in)) + { + case SHORT_PARAM: + in.erase(0,1); // erases the - + break; + case LONG_PARAM: + in.erase(0,2); // erases the -- + found=in.find('='); + if(found!=std::string::npos) + in.erase(in.begin()+found, in.end()); + break; + default: + break; + } + return in; +} + +ParameterRead::ParameterType ParameterRead::getParamType(std::string in) +{ + /// Checks the parameter type (long parameter, eg "--param", short, eg. "-i", or other) + if(in.size()>0 && in[0]=='-') + { + if(in.size()>1 && in[1]=='-') + return LONG_PARAM; + return SHORT_PARAM; + } + return OTHER_PARAM; +} + +std::string ParameterRead::getParamValue(std::string in) +{ + /// returns the value for a parameter of type LONG_PARAM, eg. "value" for "--parameter=value". + if(getParamType(in)==LONG_PARAM) + { + size_t found=in.find("="); + if(found!=std::string::npos) + { + in.erase(in.begin(), in.begin()+found+1); + return in; + } + } + return ""; +} + +std::string ParameterRead::rawdatastr() +{ + /// returns the "raw data string", which means the string line __RAWDATAx__ where x stands for "rawcount" + std::ostringstream oss; + oss << rawcount; + return std::string(PARAMETER_RAWBEGIN)+oss.str()+PARAMETER_RAWEND; +} + +std::string ParameterRead::charToString(char c) +{ + /// string::string() does not accept "char". So, let's convert a char to a std::string ! + std::string o=""; + o+=c; + return o; +} + diff --git a/ParameterRead.h b/ParameterRead.h new file mode 100644 index 0000000..a5a98e3 --- /dev/null +++ b/ParameterRead.h @@ -0,0 +1,89 @@ + +/* + * PROGRAM: + * Command-line pastebin + * + * AUTHOR: + * Théophile BASTIAN (a.k.a. Tobast) + * + * CONTACT & WEBSITE: + * http://tobast.fr/ (contact feature included) + * error-report@tobast.fr (error reporting only) + * + * SHORT DESCRIPTION: + * See first license line. + * + * LICENSE: + * "Command-line pastebin" is a software designed to submit a "paste" on http://pastebin.com/ using a command-line tool + * Copyright (C) 2011 Théophile BASTIAN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl.txt. +*/ + +#ifndef DEF_PARAMETERREAD +#define DEF_PARAMETERREAD + +#include +#include +#include +#include + +#define PARAMETER_MISSING "__MISSING__" +#define PARAMETER_NOVALUE "__NOVALUE__" + +#define PARAMETER_RAWBEGIN "__RAWDATA" +#define PARAMETER_RAWEND "__" + +typedef std::map ParameterMap; + +class ParameterRead +{ + public: + enum ParameterType { + SHORT_PARAM, LONG_PARAM, OTHER_PARAM + }; + + ParameterRead(int i_argc, char** i_argv); + void setValueParam(std::vector i_valueparam) { valueparam=i_valueparam; }; + void addValueParam(std::string i_valueparam); + + void execute(); + + ParameterMap getMap() const { return paramMap; } + bool isSet(std::string name); + std::string getValue(std::string name); + bool isValueSet(std::string name, bool die=false); + bool hasValue(std::string name); + + int rawParamCount() const { return rawcount; }; + std::string getRawParam(int count); + + protected: //meth + std::string rawparam(std::string in); + ParameterType getParamType(std::string in); + std::string getParamValue(std::string in); + std::string rawdatastr(); + + std::string charToString(char c); + + protected: + ParameterMap paramMap; + std::vector valueparam; + int argc; + char** argv; + int rawcount; +}; + +#endif//DEF_PARAMETERREAD + diff --git a/func.cpp b/func.cpp index 24735f1..3b96e14 100644 --- a/func.cpp +++ b/func.cpp @@ -122,7 +122,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri { if(pr.isSet("n")) request->setPostData("api_paste_name", pr.getValue("n")); - else if(pr.isSet("name")); + else if(pr.isSet("name")) request->setPostData("api_paste_name", pr.getValue("name")); } else @@ -132,7 +132,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri { if(pr.isSet("s")) request->setPostData("api_paste_format", pr.getValue("s")); - else if(pr.isSet("syntax")); + else if(pr.isSet("syntax")) request->setPostData("api_paste_format", pr.getValue("syntax")); } @@ -145,7 +145,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri { if(pr.isSet("e")) request->setPostData("api_paste_expire_date", pr.getValue("e")); - else if(pr.isSet("expire")); + else if(pr.isSet("expire")) request->setPostData("api_paste_expire_date", pr.getValue("expire")); } else diff --git a/func.h b/func.h index 56f7326..86ab3be 100644 --- a/func.h +++ b/func.h @@ -34,7 +34,7 @@ #ifndef DEF_FUNCTION #define DEF_FUNCTION -#include +#include "ParameterRead.h" #include #include "CurlPost.h" #include "data.h" diff --git a/main.cpp b/main.cpp index 8503de1..19f6bb5 100644 --- a/main.cpp +++ b/main.cpp @@ -34,7 +34,7 @@ #include "CurlPost.h" #include "data.h" #include "func.h" -#include +#include "ParameterRead.h" #include #include diff --git a/manpage b/manpage new file mode 100644 index 0000000..5e6947b --- /dev/null +++ b/manpage @@ -0,0 +1,72 @@ +NAME + PastebinCL - Pastebin Commnd-Line, posts a "paste" on http://pastebin.com/ + +SYNOPSIS + pastebincl [options] + +DESCRIPTION + PastebinCL is a program which sends a "paste" at http://pastebin.com/ using a command-line interface. + + ARGUMENTS + + The possible arguments are: + + -v, --version + Prints the program version and basic informatons, and then return. + + -h, -?, --help + Prints a short help, and then return. + + --licence + Prints the program licence, and then return. + + --usergen + Generates an "users" file for the software with the informations given after. + + -n [name], --name=[name] + Defines [name] as the paste name. + + -s [syntax], --syntax=[syntax] + Defines [syntax] as the syntax highlighting pattern of your text, eg. "c" or "lolcode" (see the full list on http://pastebin.com/). + + -e [expire_time], --expire [expire_time] + Defines [expire_time] as the time before the paste expires. Possible values : + - TODO + + -p, --private + Posts a private past instead of a public one. + + -g, --guest + Posts the paste as a guest. It's the default option if no configuration file was set. + + USAGE + + When you run pastebincl, it will wait for user entry until you pressed ctrl+D. + Then, it will send the text on pastebin with the options given. + + POST AS A REGISTERED USER + + To post as a registered user, you must generate a file containing your username and password. + To do so, use the software named "pastebincl --usergen". + +EXAMPLES + To post a paste without options: + $ pastebincl + (type your text, then ctrl+D) + + To post without options a file named "foo.txt" + $ cat foo.txt | pastebincl + + To post a paste named "foo", in C language, privately, which expires in 10 minutes + $ pastebincl -n foo -s c -e 10M -p + (type your text, then ctrl+D) + +AUTHOR + Both pastebincl and this manpage were written by Théophile BASTIAN (aka. Tobast) . + +LICENCE + Pastebincl is under GNU GPL licence, as published by the Free Software Fundation on version 3. + +REPORTING BUGS + Please report every bug you encounter to . +