commit 37cded7f01ed88a6f4cd839f27be4e3745085900 Author: vaydheesh Date: Sat Aug 17 20:37:04 2019 +0530 added: getopts and more flags diff --git a/python/lex_tools.i b/python/lex_tools.i index dd9eda4..88897be 100644 --- a/python/lex_tools.i +++ b/python/lex_tools.i @@ -1,9 +1,40 @@ %module lextools -%{ +%include +%include + +%typemap(in) char ** { + if (PyList_Check($input)) { + int size = PyList_Size($input); + int i = 0; + $1 = (char **) malloc((size+1)*sizeof(char *)); + for (i = 0; i < size; i++) { + PyObject *py_obj = PyList_GetItem($input, i); + if (PyUnicode_Check(py_obj)) { + $1[i] = strdup(PyUnicode_AsUTF8(py_obj)); + } + else { + PyErr_SetString(PyExc_TypeError, "list must contain strings"); + free($1); + return NULL; + } + } + $1[i] = 0; + } else { + PyErr_SetString(PyExc_TypeError, "not a list"); + return NULL; + } +} + +%typemap(freearg) char ** { + free((char *) $1); +} + +%inline%{ #define SWIG_FILE_WITH_INIT #include +#include class LRXProc: public LRXProcessor { @@ -18,17 +49,40 @@ public: fclose(dictionary); } - void lrx_proc(char arg, char *input_path, char *output_path) + void lrx_proc(char argc, char **argv, char *input_path, char *output_path) { bool useMaxEnt = false; - 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"); + optind = 1; + while(true) { - case 'm': - useMaxEnt = true; + int c = getopt(argc, argv, "mztd"); + if(c == -1) + { break; - default: - useMaxEnt = false; + } + + switch(c) + { + case 'm': + useMaxEnt = true; + break; + + case 'z': + setNullFlush(true); + break; + + case 't': + setTraceMode(true); + break; + + case 'd': + setDebugMode(true); + break; + default: + break; + } } init(); if(useMaxEnt) @@ -45,15 +99,3 @@ public: }; %} - - -%include -%include - - -class LRXProc: public LRXProcessor -{ -public: - LRXProc(char *dictionary_path); - void lrx_proc(char arg, char *input_path, char *output_path); -};