1 #if defined(HAVE_CONFIG_H) && !defined(GEANYPY_WINDOWS)
2 # include "config.h"
3 #endif
4
5 #include "geanypy.h"
6
7
8 static PyObject *
Encodings_convert_to_utf8(PyObject * module,PyObject * args,PyObject * kwargs)9 Encodings_convert_to_utf8(PyObject *module, PyObject *args, PyObject *kwargs)
10 {
11 gchar *buffer = NULL, *used_encoding = NULL, *new_buffer = NULL;
12 gssize size = -1; /* bug alert: this is gsize in Geany for some reason */
13 PyObject *result;
14 static gchar *kwlist[] = { "buffer", "size", NULL };
15
16 if (PyArg_ParseTupleAndKeywords(args, kwargs, "s|l", kwlist, &buffer, &size))
17 {
18 new_buffer = encodings_convert_to_utf8(buffer, size, &used_encoding);
19 if (new_buffer != NULL)
20 {
21 result = Py_BuildValue("ss", new_buffer, used_encoding);
22 g_free(new_buffer);
23 g_free(used_encoding);
24 return result;
25 }
26 }
27
28 Py_RETURN_NONE;
29 }
30
31
32 static PyObject *
Encodings_convert_to_utf8_from_charset(PyObject * module,PyObject * args,PyObject * kwargs)33 Encodings_convert_to_utf8_from_charset(PyObject *module, PyObject *args, PyObject *kwargs)
34 {
35 gchar *buffer = NULL, *charset = NULL, *new_buffer = NULL;
36 gssize size = -1;
37 gint fast = 0;
38 PyObject *result;
39 static gchar *kwlist[] = { "buffer", "size", "charset", "fast", NULL };
40
41 if (PyArg_ParseTupleAndKeywords(args, kwargs, "ss|li", kwlist, &buffer,
42 &charset, &size, &fast))
43 {
44 new_buffer = encodings_convert_to_utf8_from_charset(buffer, size, charset, fast);
45 if (new_buffer != NULL)
46 {
47 result = Py_BuildValue("s", new_buffer);
48 g_free(new_buffer);
49 return result;
50 }
51 }
52
53 Py_RETURN_NONE;
54 }
55
56
57 static PyObject *
Encodings_get_charset_from_index(PyObject * module,PyObject * args,PyObject * kwargs)58 Encodings_get_charset_from_index(PyObject *module, PyObject *args, PyObject *kwargs)
59 {
60 gint idx = 0;
61 const gchar *charset = NULL;
62 static gchar *kwlist[] = { "index", NULL };
63
64 if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &idx))
65 {
66 charset = encodings_get_charset_from_index(idx);
67 if (charset != NULL)
68 return Py_BuildValue("s", charset);
69 }
70
71 Py_RETURN_NONE;
72 }
73
74
75 static const gchar *encoding_names[] = {
76 "ISO_8859_1",
77 "ISO_8859_2",
78 "ISO_8859_3",
79 "ISO_8859_4",
80 "ISO_8859_5",
81 "ISO_8859_6",
82 "ISO_8859_7",
83 "ISO_8859_8",
84 "ISO_8859_8_I",
85 "ISO_8859_9",
86 "ISO_8859_10",
87 "ISO_8859_13",
88 "ISO_8859_14",
89 "ISO_8859_15",
90 "ISO_8859_16",
91
92 "UTF_7",
93 "UTF_8",
94 "UTF_16LE",
95 "UTF_16BE",
96 "UCS_2LE",
97 "UCS_2BE",
98 "UTF_32LE",
99 "UTF_32BE",
100
101 "ARMSCII_8",
102 "BIG5",
103 "BIG5_HKSCS",
104 "CP_866",
105
106 "EUC_JP",
107 "EUC_KR",
108 "EUC_TW",
109
110 "GB18030",
111 "GB2312",
112 "GBK",
113 "GEOSTD8",
114 "HZ",
115
116 "IBM_850",
117 "IBM_852",
118 "IBM_855",
119 "IBM_857",
120 "IBM_862",
121 "IBM_864",
122
123 "ISO_2022_JP",
124 "ISO_2022_KR",
125 "ISO_IR_111",
126 "JOHAB",
127 "KOI8_R",
128 "KOI8_U",
129
130 "SHIFT_JIS",
131 "TCVN",
132 "TIS_620",
133 "UHC",
134 "VISCII",
135
136 "WINDOWS_1250",
137 "WINDOWS_1251",
138 "WINDOWS_1252",
139 "WINDOWS_1253",
140 "WINDOWS_1254",
141 "WINDOWS_1255",
142 "WINDOWS_1256",
143 "WINDOWS_1257",
144 "WINDOWS_1258",
145
146 "NONE",
147 "CP_932",
148
149 "MAX"
150 };
151
152
153 static PyObject *
Encodings_get_list(PyObject * module)154 Encodings_get_list(PyObject *module)
155 {
156 int i;
157 PyObject *list;
158 list = PyList_New(0);
159 for (i = 0; i < GEANY_ENCODINGS_MAX; i++)
160 PyList_Append(list, PyString_FromString(encoding_names[i]));
161 return list;
162 }
163
164
165 static
166 PyMethodDef EncodingsModule_methods[] = {
167 {
168 "convert_to_utf8",
169 (PyCFunction)Encodings_convert_to_utf8, METH_KEYWORDS,
170 "Tries to convert the supplied buffer to UTF-8 encoding. Returns "
171 "the converted buffer and the encoding that was used."
172 },
173 {
174 "convert_to_utf8_from_charset",
175 (PyCFunction)Encodings_convert_to_utf8_from_charset, METH_KEYWORDS,
176 "Tries to convert the supplied buffer to UTF-8 from the supplied "
177 "charset. If the fast parameter is not False (default), additional "
178 "checks to validate the converted string are performed."
179 },
180 {
181 "get_charset_from_index",
182 (PyCFunction)Encodings_get_charset_from_index, METH_KEYWORDS,
183 "Gets the character set name of the specified index."
184 },
185 {
186 "get_list",
187 (PyCFunction) Encodings_get_list, METH_NOARGS,
188 "Gets a list of all supported encodings."
189 },
190 { NULL }
191 };
192
193
194 PyMODINIT_FUNC
initencoding(void)195 initencoding(void)
196 {
197 int i;
198 PyObject *m;
199
200 m = Py_InitModule3("encoding", EncodingsModule_methods,
201 "Encoding conversion functions.");
202
203 for (i = 0; i < GEANY_ENCODINGS_MAX; i++)
204 PyModule_AddIntConstant(m, encoding_names[i], (glong) i);
205 }
206