Added directly the source code of ParameterRead into the project. Shorts parameters are now working!

This commit is contained in:
tobast 2011-11-28 22:09:25 +01:00
parent f9b52a5ba3
commit e4f65d1b25
7 changed files with 387 additions and 7 deletions

View File

@ -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)

219
ParameterRead.cpp Normal file
View File

@ -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<argc;++i)
{
bool incrementI=false;
std::string argstr=argv[i];
std::string rawarg="", value="";
switch(getParamType(argstr))
{
case LONG_PARAM:
value=getParamValue(argstr);
if(hasValue(rawparam(argstr)))
{
if(value.empty())
value=PARAMETER_MISSING;
paramMap[rawparam(argstr)]=value;
}
else
{
paramMap[rawparam(argstr)]=(value.empty()) ? PARAMETER_NOVALUE : value;
}
break;
case SHORT_PARAM:
rawarg=rawparam(argstr);
for(unsigned j=0;j<rawarg.size();j++)
{
if(hasValue(charToString(rawarg[j])))
{
value=PARAMETER_MISSING;
if((unsigned)j==rawarg.size()-1)
{
if(i+1<argc && getParamType(std::string(argv[i+1]))==OTHER_PARAM)
{
value=argv[i+1];
incrementI=true;
}
}
paramMap[charToString(rawarg[j])]=value;
}
else
paramMap[charToString(rawarg[j])]=PARAMETER_NOVALUE;
}
break;
case OTHER_PARAM:
paramMap[rawdatastr()]=std::string(argv[i]);
rawcount++;
break;
default:
// How the hell did we reach there?!
break;
}
if(incrementI)
i++;
}
}
bool ParameterRead::isSet(std::string name)
{
/// checks if a given parameter is set
if(paramMap.find(name) != paramMap.end())
return true;
return false;
}
std::string ParameterRead::getValue(std::string name)
{
/// returns the value of a parameter, if any is associated.
ParameterMap::iterator it=paramMap.find(name);
if(it==paramMap.end())
throw -1;
return (*it).second;
}
bool ParameterRead::isValueSet(std::string name, bool die)
{
ParameterMap::iterator it=paramMap.find(name);
if(it==paramMap.end() || (*it).second==PARAMETER_MISSING || (*it).second==PARAMETER_NOVALUE)
{
if(die)
throw -1;
return false;
}
return true;
}
bool ParameterRead::hasValue(std::string name)
{
/// checks if a parameter requires a value (like -i inputfile, or --input=inputfile)
for(unsigned i=0;i<valueparam.size();i++)
if(valueparam[i]==name)
return true;
return false;
}
std::string ParameterRead::getRawParam(int count)
{
/// returns the value of the "count" raw data parameter
if(count>=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;
}

89
ParameterRead.h Normal file
View File

@ -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 <map>
#include <vector>
#include <string>
#include <sstream>
#define PARAMETER_MISSING "__MISSING__"
#define PARAMETER_NOVALUE "__NOVALUE__"
#define PARAMETER_RAWBEGIN "__RAWDATA"
#define PARAMETER_RAWEND "__"
typedef std::map<std::string, std::string> ParameterMap;
class ParameterRead
{
public:
enum ParameterType {
SHORT_PARAM, LONG_PARAM, OTHER_PARAM
};
ParameterRead(int i_argc, char** i_argv);
void setValueParam(std::vector<std::string> 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<std::string> valueparam;
int argc;
char** argv;
int rawcount;
};
#endif//DEF_PARAMETERREAD

View File

@ -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

2
func.h
View File

@ -34,7 +34,7 @@
#ifndef DEF_FUNCTION
#define DEF_FUNCTION
#include <parameterread/ParameterRead.h>
#include "ParameterRead.h"
#include <iostream>
#include "CurlPost.h"
#include "data.h"

View File

@ -34,7 +34,7 @@
#include "CurlPost.h"
#include "data.h"
#include "func.h"
#include <parameterread/ParameterRead.h>
#include "ParameterRead.h"
#include <iostream>
#include <string>

72
manpage Normal file
View File

@ -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) <contact@tobast.fr>.
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 <error-report@tobast.fr>.