Browse Source

Shuffle stuff around, and add some comments

Jakob Truelsen 16 years ago
parent
commit
062909f67f
5 changed files with 363 additions and 2 deletions
  1. 328 1
      src/arguments.cc
  2. 11 0
      src/commandlineparser.cc
  3. 9 0
      src/multipageloader.cc
  4. 9 0
      src/pageconverter.cc
  5. 6 1
      src/settings.cc

+ 328 - 1
src/arguments.cc

@@ -14,7 +14,334 @@
 // You should have received a copy of the GNU General Public License
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <arghandlers.inc>
+#include "commandlineparser_p.hh"
+
+/*!
+  \class ArgHandler
+  \brief Class responcible for handling an argument
+*/
+
+/*!
+  \var ArgHandler::longName
+  \brief The long name of the argument, e.g. "help" for "--help"
+*/
+
+/*!
+  \var ArgHandler::desc
+  \brief A descriptive text of the argument
+*/
+
+/*!
+  \var ArgHandler::shortSwitch
+  \brief Sort name, e.g. 'h' for '-h', if 0 there is no short name
+*/
+
+/*!
+  \var ArgHandler::argn
+  \brief The names of the arguments to the switch
+*/
+
+/*!											  
+  \var ArgHandler::display
+  \brief Indicate that the argument is not hidden
+*/
+
+/*!
+  \var ArgHandler::extended
+  \brief Indicate if the argument is an extended argument
+*/
+
+/*!											  
+  \var ArgHandler::qthack
+  \brief Indicate that the argument is only available with hacked qt
+*/
+
+/*!
+  \fn ArgHandler::operator()(const char ** args, CommandLineParserPrivate & parser)
+  Callend when the switch was specified
+  \param args The arguments to the switch, garantied to have size of argn
+  \param settings The settings to store the information in
+*/
+
+/*!
+  \fn ArgHandler::useDefault(CommandLineParserPrivate & parser)
+  Set give settings its default value
+
+  This is a NOOP for ArgHandler
+  \param parser The parser giving the request
+*/
+void ArgHandler::useDefault(CommandLineParserPrivate & parser) {
+	Q_UNUSED(parser);
+} 
+
+/*!
+  \fn ArgHandler::getDesc() const
+  Get the description of this switch
+*/  
+QString ArgHandler::getDesc() const {
+	return desc;
+}
+
+/*!
+  \fn ArgHandler::~ArgHandler()
+  Dummy virtual destructor
+*/  
+ArgHandler::~ArgHandler() {}
+
+/*!
+  \class CommandLineParserPrivate
+  Implementation details for CommandLineParser
+*/
+
+/*!
+  Sets a variable to some constant
+*/
+template <typename T> class ConstSetter: public ArgHandler {
+public:
+	T & dst;
+	const T src;
+	const T def;
+	ConstSetter(T & arg, const T s, const T d): dst(arg), src(s), def(d) {};
+	bool operator() (const char **, CommandLineParserPrivate &) {
+		dst=src;
+		return true;
+	}
+	virtual void useDefault(CommandLineParserPrivate &) {
+		dst=def;
+	}
+};
+
+/*!
+  SomeSetter template method base
+*/
+template <typename TT> 
+struct SomeSetterTM {
+	typedef TT T;
+	//T strToT(const char * val, bool & ok);
+	static QString TToStr(const T &, bool & ok) {ok=false; return "";}
+};
+
+/*!
+  TemplateMethod class used to set a single variable of some type TT::T
+*/
+template <typename TT>
+struct SomeSetter: public ArgHandler {
+	typedef typename TT::T T;
+	T & val;
+	T def;
+	bool hasDef;
+
+	SomeSetter(T & a, QString an, T d): val(a), def(d), hasDef(true) {
+		argn.push_back(an);
+	}
+
+	SomeSetter(T & a, QString an): val(a), hasDef(false) {
+		argn.push_back(an);
+	}
+
+	virtual void useDefault(CommandLineParserPrivate &) {
+		if (hasDef)
+			val=def;
+	}
+
+	bool operator() (const char ** vals, CommandLineParserPrivate &) {
+		bool ok;
+		val = TT::strToT(vals[0], ok);
+		return ok;
+	}
+
+	virtual QString getDesc() const {
+		if (!hasDef) return desc;
+		bool ok;
+		QString x = TT::TToStr(def,ok);
+		if (!ok) return desc;
+		return desc + " (default " + x + ")";
+	}
+};
+
+struct IntTM: public SomeSetterTM<int> {
+	static int strToT(const char * val, bool & ok) {
+		return QString(val).toInt(&ok);
+	}
+	static QString TToStr(const int & t, bool & ok) {
+		ok=(t!=-1);
+		return QString::number(t);
+	}
+};
+/*!
+  Argument handler setting an int variable
+*/
+typedef SomeSetter<IntTM> IntSetter;
+
+struct FloatTM: public SomeSetterTM<float> {
+	static float strToT(const char * val, bool & ok) {
+		return QString(val).toFloat(&ok);
+	}
+	static QString TToStr(const float & t, bool & ok) {
+		ok=(t!=-1);
+		return QString::number(t);
+	}
+};
+/*!
+  Argument handler setting an float variable
+*/
+typedef SomeSetter<FloatTM> FloatSetter;
+
+struct StrTM: public SomeSetterTM<const char *> {
+	static const char * strToT(const char * val, bool & ok) {
+		ok=true;
+		return val;
+	}
+	static QString TToStr(const char * t, bool & ok) {
+		ok = (t[0] != '\0');
+		return QString(t);
+	}
+};
+/*!
+  Argument handler setting a string variable
+*/
+typedef SomeSetter<StrTM> StrSetter;
+
+struct QStrTM: public SomeSetterTM<QString> {
+	static QString strToT(const char * val, bool & ok) {
+		ok=true;
+		return QString(val);
+	}
+	static QString TToStr(QString t, bool & ok) {
+		ok=!t.isEmpty();
+		return t;
+	}
+};
+/*!
+  Argument handler setting a string variable
+*/
+typedef SomeSetter<QStrTM> QStrSetter;
+
+struct UnitRealTM: public SomeSetterTM<QPair<qreal, QPrinter::Unit> > {
+	static QPair<qreal, QPrinter::Unit> strToT(const char * val, bool &ok) {
+		return Settings::strToUnitReal(val, &ok);
+	}
+};
+/*!
+  Argument handler setting a real-number/unit combo variable  
+ */
+typedef SomeSetter<UnitRealTM> UnitRealSetter;
+
+struct PageSizeTM: public SomeSetterTM<QPrinter::PageSize> {
+	static QPrinter::PageSize strToT(const char * val, bool &ok) {
+		return Settings::strToPageSize(val, &ok);
+	}
+};
+/*!
+  Argument handler setting a page size variable  
+ */
+typedef SomeSetter<PageSizeTM> PageSizeSetter;
+
+struct ProxyTM: public SomeSetterTM<Settings::ProxySettings> {
+	static Settings::ProxySettings strToT(const char * val, bool &ok) {
+		return Settings::strToProxy(val, &ok);
+	}
+};
+/*!
+  Argument handler setting a proxy variable  
+ */
+typedef SomeSetter<ProxyTM> ProxySetter;
+
+struct OrientationTM: public SomeSetterTM<QPrinter::Orientation> {
+	static QPrinter::Orientation strToT(const char * val, bool &ok) {
+		return Settings::strToOrientation(val, &ok);
+	}
+};
+/*!
+  Argument handler setting a orientation variable  
+ */
+typedef SomeSetter<OrientationTM> OrientationSetter;
+
+
+/*!
+  Argument handler responsible for calling a function
+*/
+template <typename T> struct Caller: public ArgHandler {
+	Caller() {}
+	Caller(QString a1) {
+		argn.push_back(a1);
+	}
+	bool operator() (const char **vals, CommandLineParserPrivate & s) {
+		return T()(vals,s);
+	}
+};
+
+//All these function would have been lambda function, had C++ supported them, now we are forced to write them here
+
+/*!
+  Lamba: Call the usage method
+*/
+template <bool v>
+struct HelpFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p) {
+		p.usage(stdout,v);
+		exit(0);
+	}
+};
+
+/*!
+  Lambda: Call the man method
+*/
+struct ManPageFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p) {
+		p.manpage(stdout);
+		exit(0);
+	}
+};
+
+/*!
+  Lambda: Call the man method
+*/
+template <bool T>
+struct ReadmeFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p) {
+		p.readme(stdout, T);
+		exit(0);
+	}
+};
+
+/*!
+  Lambda: Call the version method
+*/
+struct VersionFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p) {
+		p.version(stdout);
+		exit(0);
+	}
+};
+
+/*!
+  Set the default header
+*/
+struct DefaultHeaderFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p) {
+		p.settings.header.left="[webpage]";
+		p.settings.header.right="[page]/[toPage]";
+		p.settings.header.line=true;
+		p.settings.margin.top = Settings::strToUnitReal("2cm");
+		return true;
+	}
+};
+
+/*!
+  Setup default book mode
+*/
+struct BookFunc {
+	bool operator()(const char **, CommandLineParserPrivate & p) {
+		p.settings.header.left="[section]";
+		p.settings.header.right="[page]/[toPage]";
+		p.settings.header.line=true;
+		p.settings.outline = true;
+		p.settings.printToc = true;
+		p.settings.margin.top = Settings::strToUnitReal("2cm");
+		return true;
+	}
+};
 
 /*!
   The next arguments we add will belong to this section

+ 11 - 0
src/commandlineparser.cc

@@ -16,6 +16,17 @@
 #include "commandlineparser_p.hh"
 #include <qwebframe.h>
 
+/*!
+  \file commandlineparser.hh
+  \brief Defines the CommandLineParser class
+*/
+
+/*!
+  \file commandlineparser_p.hh
+  \brief Defines the CommandLineParserPrivate, ArgHandler and Outputter class
+*/
+
+
 /*!
   Output description of switches to an outputter
   \param o The outputter to output to

+ 9 - 0
src/multipageloader.cc

@@ -17,6 +17,15 @@
 #include <QFile>
 #include <QFileInfo>
 
+/*!
+  \file multipageloader.hh
+  \brief Defines the MultiPageLoader class
+*/
+
+/*!
+  \file multipageloader_p.hh
+  \brief Defines the MultiPageLoaderPrivate class
+
 /*!
  * Track and handle network errors
  * \param reply The networkreply that has finished

+ 9 - 0
src/pageconverter.cc

@@ -24,6 +24,15 @@
 #include <qapplication.h>
 #include <QPair>
 
+/*!
+  \file pageconverter.hh
+  \brief Defines the PageConverter class
+*/
+
+/*!
+  \file pageconverter_p.hh
+  \brief Defines the PageConverterPrivate class
+*/
 
 PageConverterPrivate::PageConverterPrivate(Settings & s, PageConverter & o) :
 	settings(s), pageLoader(s), hfLoader(s), outer(o) {

+ 6 - 1
src/settings.cc

@@ -15,11 +15,16 @@
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 #include <settings.hh>
 
+/*!
+  \file settings.hh
+  \brief Defines the Settings class
+*/
+
 /*!
   Convert a string to a paper size, basicly all thinkable values are allowed.
   if a unknown value is given A4 is returned
   \param s The string to convert
-  \parem ok If supplied indicates if the conversion was successfull
+  \param ok If supplied indicates if the conversion was successfull
 */
 QPrinter::PageSize Settings::strToPageSize(const char * s, bool * ok) {
 	if (ok) *ok=true;