Skip to content
This repository was archived by the owner on May 21, 2021. It is now read-only.

Commit 3a8b262

Browse files
committed
move cli.h to include/mecab
1 parent 2ef5e72 commit 3a8b262

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

extensions/python/mecab/_C/cli.h

Lines changed: 0 additions & 41 deletions
This file was deleted.

extensions/python/mecab/_C/mecab.cc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
#include <iostream>
22

33
#include "PythonCommon.h"
4-
#include "cli.h"
4+
#include "mecab/cli.h"
55
#include "tagger.h"
66

7+
#define ADD_MECAB_CLI(function_name, function_to_run) \
8+
static PyObject* function_name(PyObject* self, PyObject* args) { \
9+
PyObject* list = NULL; \
10+
if (!PyArg_UnpackTuple(args, "args", 1, 1, &list)) { \
11+
PyErr_SetString(PyExc_ValueError, "#function_name takes only 1 argument"); \
12+
return NULL; \
13+
} \
14+
if (!PyList_Check(list)) { \
15+
PyErr_SetString(PyExc_TypeError, "argument must be list of str"); \
16+
return NULL; \
17+
} \
18+
size_t argc = PyList_Size(list); \
19+
char** argv = new char*[argc]; \
20+
for (size_t i = 0; i < argc; ++i) { \
21+
PyObject* item = PyList_GetItem(list, i); \
22+
if (!PyUnicode_Check(item)) { \
23+
PyErr_SetString(PyExc_ValueError, "argument must be list of str"); \
24+
return NULL; \
25+
} \
26+
item = PyUnicode_AsUTF8String(item); \
27+
argv[i] = PyBytes_AsString(item); \
28+
} \
29+
function_to_run(argc, argv); \
30+
delete[] argv; \
31+
Py_INCREF(Py_None); \
32+
return Py_None; \
33+
}
34+
735
ADD_MECAB_CLI(mecab_main_python, mecab_main);
836
ADD_MECAB_CLI(mecab_dict_index_python, mecab_dict_index);
937
ADD_MECAB_CLI(mecab_dict_gen_python, mecab_dict_gen);

extensions/python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_coverage_args_for_ld():
2626

2727
mecab = Extension(
2828
"mecab._C",
29-
sources=["mecab/_C/mecab.cc", "mecab/_C/tagger.cc", "mecab/_C/cli.cc",],
29+
sources=["mecab/_C/mecab.cc", "mecab/_C/tagger.cc"],
3030
libraries=get_libraries(),
3131
include_dirs=["../../include"],
3232
extra_compile_args=["-std=c++11"] + get_coverage_args_for_cc(),

extensions/python/mecab/_C/cli.cc renamed to include/mecab/cli.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cli.h"
1+
#ifndef __MECAB_CLI_H__
2+
#define __MECAB_CLI_H__
23

34
#include "mecab/cost_trainer.h"
45
#include "mecab/dictionary_compiler.h"
@@ -176,3 +177,5 @@ int mecab_dict_index(int argc, char** argv) {
176177
int mecab_cost_train(int argc, char** argv) {
177178
return MeCab::Learner::run(argc, argv);
178179
}
180+
181+
#endif // __MECAB_CLI_H__

0 commit comments

Comments
 (0)