commit e9c70ad3db60879a39754c9da5ec400098586c72 Author: Lokendra Singh Date: Mon Aug 26 11:59:04 2019 +0530 added: getopts and more flags (#69) * added: getopts and more flags * multi-argument typemap for int argc and char **argv * separate methods for init calls * typemap: switch list -> tuple list being mutable can't be used as dictionary keys diff --git a/python/lttoolbox.i b/python/lttoolbox.i index 3b767d7..7fbdaf2 100644 --- a/python/lttoolbox.i +++ b/python/lttoolbox.i @@ -1,12 +1,45 @@ %module lttoolbox -%{ +%include +%include + + +%typemap(in) (int argc, char **argv) { + if (PyTuple_Check($input)) { + int i = 0; + $1 = PyTuple_Size($input); + $2 = (char **) malloc(($1 + 1)*sizeof(char *)); + for (i = 0; i < $1; i++) { + PyObject *py_obj = PyTuple_GetItem($input, i); + if (PyUnicode_Check(py_obj)) { + $2[i] = strdup(PyUnicode_AsUTF8(py_obj)); + } + else { + PyErr_SetString(PyExc_TypeError, "tuple must contain strings"); + free($2); + return NULL; + } + } + $2[i] = 0; + } else { + PyErr_SetString(PyExc_TypeError, "not a tuple"); + return NULL; + } +} + +%typemap(freearg) (int argc, char **argv) { + free((char *) $2); +} + +%inline%{ #define SWIG_FILE_WITH_INIT #include #include #include #include +#include + class FST: public FSTProcessor { public: @@ -19,31 +52,57 @@ public: load(dictionary); fclose(dictionary); } - void lt_proc(char arg, char *input_path, char *output_path) + + void lt_proc(int argc, char **argv, char *input_path, char *output_path) { - FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); - switch(arg) + FILE* input = fopen(input_path, "r"); + FILE* output = fopen(output_path, "w"); + int cmd = 0; + int c = 0; + optind = 1; + while (true) { - case 'g': - initGeneration(); - generation(input, output); + c = getopt(argc, argv, "bgpw"); + if (c == -1) + { break; + } + switch(c) + { + case 'b': + case 'g': + case 'p': + if(cmd == 0) + { + cmd = c; + } + break; + case 'w': + setDictionaryCaseMode(true); + break; + default: + break; + } + } + + switch(cmd) + { case 'b': - initBiltrans(); bilingual(input, output); break; + + case 'g': + generation(input, output); + break; + case 'p': - initPostgeneration(); - intergeneration(input, output); + postgeneration(input, output); break; - case 'w': - setDictionaryCaseMode(true); - case 'a': + default: - initAnalysis(); analysis(input, output); break; - } + } fclose(input); fclose(output); @@ -51,16 +110,3 @@ public: }; %} - - -%include -%include -%include -%include - -class FST: public FSTProcessor -{ -public: - FST(char *dictionary_path); - void lt_proc(char arg, char *input_path, char *output_path); -};