Command Line Version, with GCC Makefile. One day it will run on linux.

This commit is contained in:
Quantum 2013-08-30 21:10:07 -04:00
parent 3f25dabf5b
commit 850cab8be8
8 changed files with 1150 additions and 7 deletions

View file

@ -3,7 +3,7 @@ INCDIR=include
CXX=cl /nologo
LD=link /nologo
CXXFLAGS=/c /O1 /I$(INCDIR) /W4 /Zi /DWIN32_LEAN_AND_MEAN /DWINVER=0x0501 /D_WIN32_WINNT=0x0501 /wd4100 /DUNICODE /D_UNICODE
CXXFLAGS=/c /O1 /I$(INCDIR) /W4 /Zi /DWIN32_LEAN_AND_MEAN /DWINVER=0x0501 /D_WIN32_WINNT=0x0501 /wd4100 /DUNICODE /D_UNICODE /EHsc
LDFLAGS=/subsystem:windows /debug /manifest /incremental:no /opt:REF
LDFLAGS=$(LDFLAGS) "/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"
RC=rc /nologo
@ -31,16 +31,24 @@ FILES=$(OUTDIR)\Zalgo.obj \
$(OUTDIR)\NLSWrap.obj \
$(OUTDIR)\MyDropTarget.obj \
$(OUTDIR)\Zalgo.res
DISTS=$(DISTDIR)\Zalgo.exe \
$(DISTDIR)\hecomes.exe \
$(DISTDIR)\hegoes.exe
all: initdir $(DISTDIR)\Zalgo.exe
CMDFILE=$(OUTDIR)\ZalgoLib.obj \
$(OUTDIR)\OptionParser.obj
all: initdir $(DISTS)
hecomes: $(DISTDIR)\hecomes.exe
hecomes: $(DISTDIR)\hegoes.exe
initdir:
if not exist build md build
if not exist $(OUTDIR) md $(OUTDIR)
if not exist build md dist
if not exist $(DISTDIR) md $(DISTDIR)
@if not exist build md build
@if not exist $(OUTDIR) md $(OUTDIR)
@if not exist build md dist
@if not exist $(DISTDIR) md $(DISTDIR)
compress: $(DISTDIR)\Zalgo.exe
compress: $(DISTS)
upx --best $**
$(INCDIR)\MainWindow.hpp: $(INCDIR)\Window.hpp $(INCDIR)\MyDropTarget.hpp $(INCDIR)\PreviewWindow.hpp
@ -57,6 +65,11 @@ $(SRCDIR)\Window.cpp: $(INCDIR)\Window.hpp
$(SRCDIR)\NLSWrap.cpp: $(INCDIR)\NLSWrap.hpp
$(SRCDIR)\MainLogic.cpp: $(INCDIR)\MainWindow.hpp $(INCDIR)\NLSWrap.hpp $(INCDIR)\ConversionData.inc
$(SRCDIR)\ZalgoLib.cpp: $(INCDIR)\ZalgoLib.h $(INCDIR)\ConversionData.inc
$(SRCDIR)\OptionParser.cpp: $(INCDIR)\OptionParser.h
$(SRCDIR)\hecomes.cpp: $(INCDIR)\OptionParser.h $(INCDIR)\ZalgoLib.h
$(SRCDIR)\hegoes.cpp: $(INCDIR)\OptionParser.h $(INCDIR)\ZalgoLib.h
$(OUTDIR)\Zalgo.res: Zalgo.rc res\x-sampa.txt res\init.txt
$(RC) $(RCFLAGS) /fo$@ Zalgo.rc
@ -66,3 +79,9 @@ $(OUTDIR)\Zalgo.res: Zalgo.rc res\x-sampa.txt res\init.txt
$(DISTDIR)\Zalgo.exe: $(FILES)
$(LD) /out:$@ $(LDFLAGS) $** $(LIBS)
mt.exe -nologo -manifest $@.manifest -outputresource:$@;1 && del $@.manifest || set ERRORLEVEL=0
$(DISTDIR)\hecomes.exe: $(CMDFILE) $(OUTDIR)\hecomes.obj
$(LD) /out:$@ $**
$(DISTDIR)\hegoes.exe: $(CMDFILE) $(OUTDIR)\hegoes.obj
$(LD) /out:$@ $**

62
Makefile.gcc Normal file
View file

@ -0,0 +1,62 @@
SRCDIR=src
INCDIR=include
ifdef CROSS
KROSS=$(CROSS)-
endif
CXX=$(KROSS)g++ -std=c++11
STRIP=$(KROSS)strip
LD=$(CXX)
CXXFLAGS=-c -O3 -I$(INCDIR) -Wall -DWIN32_LEAN_AND_MEAN -DWINVER=0x0501 -D_WIN32_WINNT=0x0501 -DUNICODE -D_UNICODE
LDFLAGS=-static-libstdc++
LIBS=
ifdef DEBUG
BUILD=Debug
CXXFLAGS=$(CXXFLAGS) -DDEBUG
LDFLAGS=$(LDFLAGS) -g
else
BUILD=Release
endif
OUTDIR=build/$(BUILD)
DISTDIR=dist/$(BUILD)
DISTS=$(DISTDIR)/hecomes \
$(DISTDIR)/hegoes
FILES=$(OUTDIR)/ZalgoLib.o \
$(OUTDIR)/OptionParser.o
all: initdir $(DISTS)
hecomes: $(DISTDIR)/hecomes
hecomes: $(DISTDIR)/hegoes
.PHONY: clean
clean:
rm $(OUTDIR)/*.o
rm $(DISTS)
strip:
$(STRIP) $(DISTS)
initdir:
-test -d build || mkdir build
-test -d $(OUTDIR) || mkdir $(OUTDIR)
-test -d build || mkdir dist
-test -d $(DISTDIR) || mkdir $(DISTDIR)
$(SRCDIR)/ZalgoLib.cpp: $(INCDIR)/ZalgoLib.h $(INCDIR)/ConversionData.inc
$(SRCDIR)/OptionParser.cpp: $(INCDIR)/OptionParser.h
$(SRCDIR)/hecomes.cpp: $(INCDIR)/OptionParser.h $(INCDIR)/ZalgoLib.h
$(SRCDIR)/hegoes.cpp: $(INCDIR)/OptionParser.h $(INCDIR)/ZalgoLib.h
$(OUTDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -o $@ -c $<
$(DISTDIR)/hecomes: $(FILES) $(OUTDIR)/hecomes.o
$(LD) $(LDFLAGS) -o $@ $^
$(DISTDIR)/hegoes: $(FILES) $(OUTDIR)/hegoes.o
$(LD) $(LDFLAGS) -o $@ $^

307
include/OptionParser.h Normal file
View file

@ -0,0 +1,307 @@
/**
* Copyright (C) 2010 Johannes Weißl <jargon@molb.org>
* License: your favourite BSD-style license
*
* git clone http://github.com/weisslj/cpp-optparse.git
*
* This is yet another option parser for C++. It is modelled after the
* excellent Python optparse API. Although incomplete, anyone familiar to
* optparse should feel at home:
* http://docs.python.org/library/optparse.html
*
* Design decisions:
* - elegant and easy usage more important than speed / flexibility
* - shortness more important than feature completeness
* * no unicode
* * no checking for user programming errors
*
* Why not use getopt/getopt_long?
* - not C++ / not completely POSIX
* - too cumbersome to use, would need lot of additional code
*
* Why not use Boost.Program_options?
* - boost not installed on all target platforms (esp. cluster, HPC, ...)
* - too big to include just for option handling:
* 322 *.h (44750 lines) + 7 *.cpp (2078 lines)
*
* Why not use tclap/Opag/Options/CmdLine/Anyoption/Argument_helper/...?
* - no reason, writing one is faster than code inspection :-)
* - similarity to Python desired for faster learning curve
*
* Future work:
* - nargs > 1?
* - comments?
*
* Python only features:
* - conflict handlers
* - adding new actions
*
*
* Example:
*
* using optparse::OptionParser;
*
* OptionParser parser = OptionParser() .description("just an example");
*
* parser.add_option("-f", "--file") .dest("filename")
* .help("write report to FILE") .metavar("FILE");
* parser.add_option("-q", "--quiet")
* .action("store_false") .dest("verbose") .set_default("1")
* .help("don't print status messages to stdout");
*
* optparse::Values options = parser.parse_args(argc, argv);
* vector<string> args = parser.args();
*
* if (options.get("verbose"))
* cout << options["filename"] << endl;
*
*/
#ifndef OPTIONPARSER_H_
#define OPTIONPARSER_H_
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4512)
#endif
namespace optparse {
class OptionParser;
class OptionGroup;
class Option;
class Values;
class Value;
class Callback;
typedef std::map<std::string,std::string> strMap;
typedef std::map<std::string,std::list<std::string> > lstMap;
typedef std::map<std::string,Option const*> optMap;
const char* const SUPPRESS_HELP = "SUPPRESS" "HELP";
const char* const SUPPRESS_USAGE = "SUPPRESS" "USAGE";
//! Class for automatic conversion from string -> anytype
class Value {
public:
Value() : str(), valid(false) {}
Value(const std::string& v) : str(v), valid(true) {}
operator const char*() { return str.c_str(); }
operator bool() { bool t; return (valid && (std::istringstream(str) >> t)) ? t : false; }
operator short() { short t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator unsigned short() { unsigned short t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator int() { int t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator unsigned int() { unsigned int t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator long() { long t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator unsigned long() { unsigned long t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator float() { float t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator double() { double t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
operator long double() { long double t; return (valid && (std::istringstream(str) >> t)) ? t : 0; }
private:
const std::string str;
bool valid;
};
class Values {
public:
Values() : _map() {}
const std::string& operator[] (const std::string& d) const;
std::string& operator[] (const std::string& d) { return _map[d]; }
bool is_set(const std::string& d) const { return _map.find(d) != _map.end(); }
bool is_set_by_user(const std::string& d) const { return _userSet.find(d) != _userSet.end(); }
void is_set_by_user(const std::string& d, bool yes);
Value get(const std::string& d) const { return (is_set(d)) ? Value((*this)[d]) : Value(); }
typedef std::list<std::string>::iterator iterator;
typedef std::list<std::string>::const_iterator const_iterator;
std::list<std::string>& all(const std::string& d) { return _appendMap[d]; }
const std::list<std::string>& all(const std::string& d) const { return _appendMap.find(d)->second; }
private:
strMap _map;
lstMap _appendMap;
std::set<std::string> _userSet;
};
class OptionParser {
public:
OptionParser();
virtual ~OptionParser() {}
OptionParser& usage(const std::string& u) { set_usage(u); return *this; }
OptionParser& version(const std::string& v) { _version = v; return *this; }
OptionParser& description(const std::string& d) { _description = d; return *this; }
OptionParser& add_help_option(bool h) { _add_help_option = h; return *this; }
OptionParser& add_version_option(bool v) { _add_version_option = v; return *this; }
OptionParser& prog(const std::string& p) { _prog = p; return *this; }
OptionParser& epilog(const std::string& e) { _epilog = e; return *this; }
OptionParser& set_defaults(const std::string& dest, const std::string& val) {
_defaults[dest] = val; return *this;
}
OptionParser& enable_interspersed_args() { _interspersed_args = true; return *this; }
OptionParser& disable_interspersed_args() { _interspersed_args = false; return *this; }
OptionParser& add_option_group(const OptionGroup& group);
const std::string& usage() const { return _usage; }
const std::string& version() const { return _version; }
const std::string& description() const { return _description; }
bool add_help_option() const { return _add_help_option; }
bool add_version_option() const { return _add_version_option; }
const std::string& prog() const { return _prog; }
const std::string& epilog() const { return _epilog; }
bool interspersed_args() const { return _interspersed_args; }
Option& add_option(const std::string& opt);
Option& add_option(const std::string& opt1, const std::string& opt2);
Option& add_option(const std::string& opt1, const std::string& opt2, const std::string& opt3);
Option& add_option(const std::vector<std::string>& opt);
Values& parse_args(int argc, char const* const* argv);
Values& parse_args(const std::vector<std::string>& args);
template<typename InputIterator>
Values& parse_args(InputIterator begin, InputIterator end) {
return parse_args(std::vector<std::string>(begin, end));
}
const std::list<std::string>& args() const { return _leftover; }
std::vector<std::string> args() {
return std::vector<std::string>(_leftover.begin(), _leftover.end());
}
std::string format_help() const;
std::string format_option_help(unsigned int indent = 2) const;
void print_help() const;
void set_usage(const std::string& u);
std::string get_usage() const;
void print_usage(std::ostream& out) const;
void print_usage() const;
std::string get_version() const;
void print_version(std::ostream& out) const;
void print_version() const;
void error(const std::string& msg) const;
void exit() const;
private:
const Option& lookup_short_opt(const std::string& opt) const;
const Option& lookup_long_opt(const std::string& opt) const;
void handle_short_opt(const std::string& opt, const std::string& arg);
void handle_long_opt(const std::string& optstr);
void process_opt(const Option& option, const std::string& opt, const std::string& value);
std::string format_usage(const std::string& u) const;
std::string _usage;
std::string _version;
std::string _description;
bool _add_help_option;
bool _add_version_option;
std::string _prog;
std::string _epilog;
bool _interspersed_args;
Values _values;
std::list<Option> _opts;
optMap _optmap_s;
optMap _optmap_l;
strMap _defaults;
std::list<OptionGroup const*> _groups;
std::list<std::string> _remaining;
std::list<std::string> _leftover;
};
class OptionGroup : public OptionParser {
public:
OptionGroup(const OptionParser& p, const std::string& t, const std::string& d = "") :
_parser(p), _title(t), _group_description(d) {}
virtual ~OptionGroup() {}
OptionGroup& title(const std::string& t) { _title = t; return *this; }
OptionGroup& group_description(const std::string& d) { _group_description = d; return *this; }
const std::string& title() const { return _title; }
const std::string& group_description() const { return _group_description; }
private:
const OptionParser& _parser;
std::string _title;
std::string _group_description;
};
class Option {
public:
Option() : _action("store"), _type("string"), _nargs(1), _callback(0) {}
virtual ~Option() {}
Option& action(const std::string& a);
Option& type(const std::string& t) { _type = t; return *this; }
Option& dest(const std::string& d) { _dest = d; return *this; }
Option& set_default(const std::string& d) { _default = d; return *this; }
template<typename T>
Option& set_default(T t) { std::ostringstream ss; ss << t; _default = ss.str(); return *this; }
Option& nargs(size_t n) { _nargs = n; return *this; }
Option& set_const(const std::string& c) { _const = c; return *this; }
template<typename InputIterator>
Option& choices(InputIterator begin, InputIterator end) {
_choices.assign(begin, end); type("choice"); return *this;
}
Option& help(const std::string& h) { _help = h; return *this; }
Option& metavar(const std::string& m) { _metavar = m; return *this; }
Option& callback(Callback& c) { _callback = &c; return *this; }
const std::string& action() const { return _action; }
const std::string& type() const { return _type; }
const std::string& dest() const { return _dest; }
const std::string& get_default() const { return _default; }
size_t nargs() const { return _nargs; }
const std::string& get_const() const { return _const; }
const std::list<std::string>& choices() const { return _choices; }
const std::string& help() const { return _help; }
const std::string& metavar() const { return _metavar; }
Callback* callback() const { return _callback; }
private:
std::string check_type(const std::string& opt, const std::string& val) const;
std::string format_option_help(unsigned int indent = 2) const;
std::string format_help(unsigned int indent = 2) const;
std::set<std::string> _short_opts;
std::set<std::string> _long_opts;
std::string _action;
std::string _type;
std::string _dest;
std::string _default;
size_t _nargs;
std::string _const;
std::list<std::string> _choices;
std::string _help;
std::string _metavar;
Callback* _callback;
friend class OptionParser;
};
class Callback {
public:
virtual void operator() (const Option& option, const std::string& opt, const std::string& val, const OptionParser& parser) = 0;
virtual ~Callback() {}
};
}
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#endif

26
include/ZalgoLib.h Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#ifndef id890CAB5C_C6A7_4753_9237A1F15D010A89
#define id890CAB5C_C6A7_4753_9237A1F15D010A89
#include <iostream>
#include <cstdio>
#include <vector>
class ZalgoProcess {
public:
ZalgoProcess() : input("-"), output("-") {}
ZalgoProcess(const std::string& input, const std::string& output) :
input(input), output(output) {}
ZalgoProcess(const std::vector<std::string> &args);
ZalgoProcess &open();
ZalgoProcess &close();
ZalgoProcess &zalgo(int level = 10, bool up = true, bool middle = true, bool down = true);
ZalgoProcess &unzalgo();
protected:
std::string input, output;
std::FILE *infile, *outfile;
};
#endif

556
src/OptionParser.cpp Normal file
View file

@ -0,0 +1,556 @@
/**
* Copyright (C) 2010 Johannes Weißl <jargon@molb.org>
* License: your favourite BSD-style license
*
* See OptionParser.h for help.
*/
#include "OptionParser.h"
#include <cstdlib>
#include <algorithm>
#include <complex>
#include <ciso646>
#if defined(ENABLE_NLS) && ENABLE_NLS
# include <libintl.h>
# define _(s) gettext(s)
#else
# define _(s) ((const char *) (s))
#endif
using namespace std;
namespace optparse {
////////// auxiliary (string) functions { //////////
class str_wrap {
public:
str_wrap(const string& l, const string& r) : lwrap(l), rwrap(r) {}
str_wrap(const string& w) : lwrap(w), rwrap(w) {}
string operator() (const string& s) { return lwrap + s + rwrap; }
const string lwrap, rwrap;
};
template<typename InputIterator, typename UnaryOperator>
static string str_join_trans(const string& sep, InputIterator begin, InputIterator end, UnaryOperator op) {
string buf;
for (InputIterator it = begin; it != end; ++it) {
if (it != begin)
buf += sep;
buf += op(*it);
}
return buf;
}
template<class InputIterator>
static string str_join(const string& sep, InputIterator begin, InputIterator end) {
return str_join_trans(sep, begin, end, str_wrap(""));
}
static string& str_replace(string& s, const string& patt, const string& repl) {
size_t pos = 0, n = patt.length();
while (true) {
pos = s.find(patt, pos);
if (pos == string::npos)
break;
s.replace(pos, n, repl);
pos += repl.size();
}
return s;
}
static string str_replace(const string& s, const string& patt, const string& repl) {
string tmp = s;
str_replace(tmp, patt, repl);
return tmp;
}
static string str_format(const string& s, size_t pre, size_t len, bool indent_first = true) {
stringstream ss;
string p;
if (indent_first)
p = string(pre, ' ');
size_t pos = 0, linestart = 0;
size_t line = 0;
while (true) {
bool wrap = false;
size_t new_pos = s.find_first_of(" \n\t", pos);
if (new_pos == string::npos)
break;
if (s[new_pos] == '\n') {
pos = new_pos + 1;
wrap = true;
}
if (line == 1)
p = string(pre, ' ');
if (wrap || new_pos + pre > linestart + len) {
ss << p << s.substr(linestart, pos - linestart - 1) << endl;
linestart = pos;
line++;
}
pos = new_pos + 1;
}
ss << p << s.substr(linestart) << endl;
return ss.str();
}
static string str_inc(const string& s) {
stringstream ss;
string v = (s != "") ? s : "0";
long i;
istringstream(v) >> i;
ss << i+1;
return ss.str();
}
static unsigned int cols() {
unsigned int n = 80;
#ifndef _WIN32
const char *s = getenv("COLUMNS");
if (s)
istringstream(s) >> n;
#endif
return n;
}
static string basename(const string& s) {
string b = s;
size_t i = b.find_last_not_of('/');
if (i == string::npos) {
if (b[0] == '/')
b.erase(1);
return b;
}
b.erase(i+1, b.length()-i-1);
i = b.find_last_of("/");
if (i != string::npos)
b.erase(0, i+1);
return b;
}
////////// } auxiliary (string) functions //////////
////////// class OptionParser { //////////
OptionParser::OptionParser() :
_usage(_("%prog [options]")),
_add_help_option(true),
_add_version_option(true),
_interspersed_args(true) {}
Option& OptionParser::add_option(const string& opt) {
const string tmp[1] = { opt };
return add_option(vector<string>(&tmp[0], &tmp[1]));
}
Option& OptionParser::add_option(const string& opt1, const string& opt2) {
const string tmp[2] = { opt1, opt2 };
return add_option(vector<string>(&tmp[0], &tmp[2]));
}
Option& OptionParser::add_option(const string& opt1, const string& opt2, const string& opt3) {
const string tmp[3] = { opt1, opt2, opt3 };
return add_option(vector<string>(&tmp[0], &tmp[3]));
}
Option& OptionParser::add_option(const vector<string>& v) {
_opts.resize(_opts.size()+1);
Option& option = _opts.back();
string dest_fallback;
for (vector<string>::const_iterator it = v.begin(); it != v.end(); ++it) {
if (it->substr(0,2) == "--") {
const string s = it->substr(2);
if (option.dest() == "")
option.dest(str_replace(s, "-", "_"));
option._long_opts.insert(s);
_optmap_l[s] = &option;
} else {
const string s = it->substr(1,1);
if (dest_fallback == "")
dest_fallback = s;
option._short_opts.insert(s);
_optmap_s[s] = &option;
}
}
if (option.dest() == "")
option.dest(dest_fallback);
return option;
}
OptionParser& OptionParser::add_option_group(const OptionGroup& group) {
for (list<Option>::const_iterator oit = group._opts.begin(); oit != group._opts.end(); ++oit) {
const Option& option = *oit;
for (set<string>::const_iterator it = option._short_opts.begin(); it != option._short_opts.end(); ++it)
_optmap_s[*it] = &option;
for (set<string>::const_iterator it = option._long_opts.begin(); it != option._long_opts.end(); ++it)
_optmap_l[*it] = &option;
}
_groups.push_back(&group);
return *this;
}
const Option& OptionParser::lookup_short_opt(const string& opt) const {
optMap::const_iterator it = _optmap_s.find(opt);
if (it == _optmap_s.end())
error(_("no such option") + string(": -") + opt);
return *it->second;
}
void OptionParser::handle_short_opt(const string& opt, const string& arg) {
_remaining.pop_front();
string value;
const Option& option = lookup_short_opt(opt);
if (option._nargs == 1) {
value = arg.substr(2);
if (value == "") {
if (_remaining.empty())
error("-" + opt + " " + _("option requires an argument"));
value = _remaining.front();
_remaining.pop_front();
}
} else {
if (arg.length() > 2)
_remaining.push_front(string("-") + arg.substr(2));
}
process_opt(option, string("-") + opt, value);
}
const Option& OptionParser::lookup_long_opt(const string& opt) const {
list<string> matching;
for (optMap::const_iterator it = _optmap_l.begin(); it != _optmap_l.end(); ++it) {
if (it->first.compare(0, opt.length(), opt) == 0)
matching.push_back(it->first);
}
if (matching.size() > 1) {
string x = str_join(", ", matching.begin(), matching.end());
error(_("ambiguous option") + string(": --") + opt + " (" + x + "?)");
}
if (matching.size() == 0)
error(_("no such option") + string(": --") + opt);
return *_optmap_l.find(matching.front())->second;
}
void OptionParser::handle_long_opt(const string& optstr) {
_remaining.pop_front();
string opt, value;
size_t delim = optstr.find("=");
if (delim != string::npos) {
opt = optstr.substr(0, delim);
value = optstr.substr(delim+1);
} else
opt = optstr;
const Option& option = lookup_long_opt(opt);
if (option._nargs == 1 and delim == string::npos) {
if (not _remaining.empty()) {
value = _remaining.front();
_remaining.pop_front();
}
}
if (option._nargs == 1 and value == "")
error("--" + opt + " " + _("option requires an argument"));
process_opt(option, string("--") + opt, value);
}
Values& OptionParser::parse_args(const int argc, char const* const* const argv) {
if (prog() == "")
prog(basename(argv[0]));
return parse_args(&argv[1], &argv[argc]);
}
Values& OptionParser::parse_args(const vector<string>& v) {
_remaining.assign(v.begin(), v.end());
if (add_version_option() and version() != "") {
add_option("--version") .action("version") .help(_("show program's version number and exit"));
_opts.splice(_opts.begin(), _opts, --(_opts.end()));
}
if (add_help_option()) {
add_option("-h", "--help") .action("help") .help(_("show this help message and exit"));
_opts.splice(_opts.begin(), _opts, --(_opts.end()));
}
while (not _remaining.empty()) {
const string arg = _remaining.front();
if (arg == "--") {
_remaining.pop_front();
break;
}
if (arg.substr(0,2) == "--") {
handle_long_opt(arg.substr(2));
} else if (arg.substr(0,1) == "-" and arg.length() > 1) {
handle_short_opt(arg.substr(1,1), arg);
} else {
_remaining.pop_front();
_leftover.push_back(arg);
if (not interspersed_args())
break;
}
}
while (not _remaining.empty()) {
const string arg = _remaining.front();
_remaining.pop_front();
_leftover.push_back(arg);
}
for (strMap::const_iterator it = _defaults.begin(); it != _defaults.end(); ++it) {
if (not _values.is_set(it->first))
_values[it->first] = it->second;
}
for (list<Option>::const_iterator it = _opts.begin(); it != _opts.end(); ++it) {
if (it->get_default() != "" and not _values.is_set(it->dest()))
_values[it->dest()] = it->get_default();
}
return _values;
}
void OptionParser::process_opt(const Option& o, const string& opt, const string& value) {
if (o.action() == "store") {
string err = o.check_type(opt, value);
if (err != "")
error(err);
_values[o.dest()] = value;
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "store_const") {
_values[o.dest()] = o.get_const();
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "store_true") {
_values[o.dest()] = "1";
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "store_false") {
_values[o.dest()] = "0";
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "append") {
string err = o.check_type(opt, value);
if (err != "")
error(err);
_values[o.dest()] = value;
_values.all(o.dest()).push_back(value);
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "append_const") {
_values[o.dest()] = o.get_const();
_values.all(o.dest()).push_back(o.get_const());
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "count") {
_values[o.dest()] = str_inc(_values[o.dest()]);
_values.is_set_by_user(o.dest(), true);
}
else if (o.action() == "help") {
print_help();
std::exit(0);
}
else if (o.action() == "version") {
print_version();
std::exit(0);
}
else if (o.action() == "callback" && o.callback()) {
(*o.callback())(o, opt, value, *this);
}
}
string OptionParser::format_option_help(unsigned int indent /* = 2 */) const {
stringstream ss;
if (_opts.empty())
return ss.str();
for (list<Option>::const_iterator it = _opts.begin(); it != _opts.end(); ++it) {
if (it->help() != SUPPRESS_HELP)
ss << it->format_help(indent);
}
return ss.str();
}
string OptionParser::format_help() const {
stringstream ss;
if (usage() != SUPPRESS_USAGE)
ss << get_usage() << endl;
if (description() != "")
ss << str_format(description(), 0, cols()) << endl;
ss << _("Options") << ":" << endl;
ss << format_option_help();
for (list<OptionGroup const*>::const_iterator it = _groups.begin(); it != _groups.end(); ++it) {
const OptionGroup& group = **it;
ss << endl << " " << group.title() << ":" << endl;
if (group.group_description() != "")
ss << str_format(group.group_description(), 4, cols()) << endl;
ss << group.format_option_help(4);
}
if (epilog() != "")
ss << endl << str_format(epilog(), 0, cols());
return ss.str();
}
void OptionParser::print_help() const {
cout << format_help();
}
void OptionParser::set_usage(const string& u) {
string lower = u;
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
if (lower.compare(0, 7, "usage: ") == 0)
_usage = u.substr(7);
else
_usage = u;
}
string OptionParser::format_usage(const string& u) const {
stringstream ss;
ss << _("Usage") << ": " << u << endl;
return ss.str();
}
string OptionParser::get_usage() const {
if (usage() == SUPPRESS_USAGE)
return string("");
return format_usage(str_replace(usage(), "%prog", prog()));
}
void OptionParser::print_usage(ostream& out) const {
string u = get_usage();
if (u != "")
out << u << endl;
}
void OptionParser::print_usage() const {
print_usage(cout);
}
string OptionParser::get_version() const {
return str_replace(_version, "%prog", prog());
}
void OptionParser::print_version(ostream& out) const {
out << get_version() << endl;
}
void OptionParser::print_version() const {
print_version(cout);
}
void OptionParser::exit() const {
std::exit(2);
}
void OptionParser::error(const string& msg) const {
print_usage(cerr);
cerr << prog() << ": " << _("error") << ": " << msg << endl;
exit();
}
////////// } class OptionParser //////////
////////// class Values { //////////
const string& Values::operator[] (const string& d) const {
strMap::const_iterator it = _map.find(d);
static const string empty = "";
return (it != _map.end()) ? it->second : empty;
}
void Values::is_set_by_user(const string& d, bool yes) {
if (yes)
_userSet.insert(d);
else
_userSet.erase(d);
}
////////// } class Values //////////
////////// class Option { //////////
string Option::check_type(const string& opt, const string& val) const {
istringstream ss(val);
stringstream err;
if (type() == "int" || type() == "long") {
long t;
if (not (ss >> t))
err << _("option") << " " << opt << ": " << _("invalid integer value") << ": '" << val << "'";
}
else if (type() == "float" || type() == "double") {
double t;
if (not (ss >> t))
err << _("option") << " " << opt << ": " << _("invalid floating-point value") << ": '" << val << "'";
}
else if (type() == "choice") {
if (find(choices().begin(), choices().end(), val) == choices().end()) {
list<string> tmp = choices();
transform(tmp.begin(), tmp.end(), tmp.begin(), str_wrap("'"));
err << _("option") << " " << opt << ": " << _("invalid choice") << ": '" << val << "'"
<< " (" << _("choose from") << " " << str_join(", ", tmp.begin(), tmp.end()) << ")";
}
}
else if (type() == "complex") {
complex<double> t;
if (not (ss >> t))
err << _("option") << " " << opt << ": " << _("invalid complex value") << ": '" << val << "'";
}
return err.str();
}
string Option::format_option_help(unsigned int indent /* = 2 */) const {
string mvar_short, mvar_long;
if (nargs() == 1) {
string mvar = metavar();
if (mvar == "") {
mvar = type();
transform(mvar.begin(), mvar.end(), mvar.begin(), ::toupper);
}
mvar_short = " " + mvar;
mvar_long = "=" + mvar;
}
stringstream ss;
ss << string(indent, ' ');
if (not _short_opts.empty()) {
ss << str_join_trans(", ", _short_opts.begin(), _short_opts.end(), str_wrap("-", mvar_short));
if (not _long_opts.empty())
ss << ", ";
}
if (not _long_opts.empty())
ss << str_join_trans(", ", _long_opts.begin(), _long_opts.end(), str_wrap("--", mvar_long));
return ss.str();
}
string Option::format_help(unsigned int indent /* = 2 */) const {
stringstream ss;
string h = format_option_help(indent);
unsigned int width = cols();
unsigned int opt_width = min(width*3/10, 36u);
bool indent_first = false;
ss << h;
// if the option list is too long, start a new paragraph
if (h.length() >= (opt_width-1)) {
ss << endl;
indent_first = true;
} else {
ss << string(opt_width - h.length(), ' ');
if (help() == "")
ss << endl;
}
if (help() != "") {
string help_str = (get_default() != "") ? str_replace(help(), "%default", get_default()) : help();
ss << str_format(help_str, opt_width, width, indent_first);
}
return ss.str();
}
Option& Option::action(const string& a) {
_action = a;
if (a == "store_const" || a == "store_true" || a == "store_false" ||
a == "append_const" || a == "count" || a == "help" || a == "version")
nargs(0);
return *this;
}
////////// } class Option //////////
}

119
src/ZalgoLib.cpp Normal file
View file

@ -0,0 +1,119 @@
#define _CRT_SECURE_NO_WARNINGS
#include <ZalgoLib.h>
#include <random>
#include <string>
namespace Data {
typedef std::basic_string<wchar_t> wstring;
# include <ConversionData.inc>
const wstring zalgo_up = Data::zalgo_up_;
const wstring zalgo_down = Data::zalgo_down_;
const wstring zalgo_mid = Data::zalgo_mid_;
}
namespace Zalgo {
std::uniform_int_distribution<> char_up (0, Data::zalgo_up.length() - 1);
std::uniform_int_distribution<> char_mid (0, Data::zalgo_mid.length() - 1);
std::uniform_int_distribution<> char_down(0, Data::zalgo_down.length() - 1);
}
ZalgoProcess::ZalgoProcess(const std::vector<std::string> &args) :
input("-"), output("-")
{
switch (args.size()) {
case 0:
break;
case 2:
output = args[1];
case 1:
input = args[0];
break;
default:
throw std::string("Error: You may only specify one input file and another output file\n");
}
}
ZalgoProcess &ZalgoProcess::open() {
if (input == "-")
infile = stdin;
else
infile = std::fopen(input.c_str(), "r,ccs=UTF-8");
if (infile == NULL)
throw std::string("Can't open input file: ") + input;
if (output == "-")
outfile = stdout;
else
outfile = std::fopen(output.c_str(), "w,ccs=UTF-8");
if (outfile == NULL)
throw std::string("Can't open output file: ") + output;
return *this;
}
ZalgoProcess &ZalgoProcess::zalgo(int level, bool up, bool mid, bool down) {
static std::mt19937 rng;
std::uniform_int_distribution<> dist_up(level / 3, level);
std::uniform_int_distribution<> dist_mid(level / 6, level / 2);
std::uniform_int_distribution<> dist_down(level / 3, level);
bool initial = true;
while (true) {
wint_t ch = fgetwc(infile);
if (ch == WEOF)
return *this;
if (ch == 0xFEFF)
continue;
if (ch == L'?' && initial) {
initial = false;
continue;
}
fputwc(ch, outfile);
switch (ch) {
case L' ':
case L'\t':
case L'\r':
case L'\n':
case L'\f':
continue;
}
#define IMPLEMENT_ZALGO(type) \
if (type) { \
int times = dist_##type(rng); \
for (int i = 0; i < times; ++i) \
fputwc(Data::zalgo_##type[Zalgo::char_##type(rng)], outfile); \
}
IMPLEMENT_ZALGO(up);
IMPLEMENT_ZALGO(mid);
IMPLEMENT_ZALGO(down);
}
}
ZalgoProcess &ZalgoProcess::unzalgo() {
bool initial = true;
while (true) {
wint_t ch = fgetwc(infile);
if (ch == WEOF)
return *this;
if (ch == 0xFEFF)
continue;
if (ch == L'?' && initial) {
initial = false;
continue;
}
if (ch >= 0x0300 && ch < 0x0370 || ch == 0x489)
continue;
fputwc(ch, outfile);
}
}
ZalgoProcess &ZalgoProcess::close() {
if (input != "-")
fclose(infile);
if (output != "-")
fclose(outfile);
return *this;
}

39
src/hecomes.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <OptionParser.h>
#include <ZalgoLib.h>
int main(int argc, char *argv[]) {
optparse::OptionParser parser = optparse::OptionParser();
parser.description("Encodes a file with Zalgo!");
parser.set_defaults("up", "1");
parser.set_defaults("middle", "1");
parser.set_defaults("down", "1");
parser.add_option("-l", "--level").type("int").set_default("10").metavar("LEVEL")
.help("Zalgo Level (mini=6, normal=10, kazi=14)");
parser.add_option("-u", "--no-up").action("store_false").dest("up")
.help("When he comes, he shall not go up");
parser.add_option("--up").action("store_true").dest("up")
.help("When he comes, he goes up (default)");
parser.add_option("-m", "--no-middle").action("store_false").dest("middle")
.help("When he comes, he shall not mess up the middle");
parser.add_option("--middle").action("store_true").dest("middle")
.help("When he comes, he messes up the middle (default)");
parser.add_option("-d", "--no-down").action("store_false").dest("down")
.help("When he comes, he shall not go down");
parser.add_option("--down").action("store_true").dest("down")
.help("When he comes, he goes down (default)");
optparse::Values& options = parser.parse_args(argc, argv);
bool up = options.get("up");
bool middle = options.get("middle");
bool down = options.get("down");
int level = options.get("level");
try {
ZalgoProcess process(parser.args());
process.open().zalgo(level, up, middle, down).close();
} catch (std::string e) {
std::cerr << "Error: " << e << std::endl;
}
}

15
src/hegoes.cpp Normal file
View file

@ -0,0 +1,15 @@
#include <OptionParser.h>
#include <ZalgoLib.h>
int main(int argc, char *argv[]) {
optparse::OptionParser parser = optparse::OptionParser();
parser.description("Decodes a file encoded with Zalgo!");
parser.parse_args(argc, argv);
try {
ZalgoProcess process(parser.args());
process.open().unzalgo().close();
} catch (std::string e) {
std::cerr << "Error: " << e << std::endl;
}
}