Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / ruby / ohcount.i
1
2 %module ohcount
3 %{
4 #include "../src/detector.h"
5 #include "../src/languages.h"
6 #include "../src/sourcefile.h"
7 %}
8
9 %include typemaps.i
10
11 #if defined(SWIGRUBY)
12
13 %typemap(in) (register const char *str, register unsigned int len) {
14   Check_Type($input, T_STRING);
15   $1 = StringValuePtr($input);
16   $2 = RSTRING_LEN($input);
17 };
18
19 %typemap(out) char ** {
20   VALUE arr = rb_ary_new();
21   int i;
22   for (i = 0; $1[i] != NULL; i++)
23     rb_ary_push(arr, rb_str_new2($1[i]));
24   $result = arr;
25 };
26
27 #elif defined(SWIGPYTHON)
28
29 %typemap(in) (register const char *str, register unsigned int len) {
30   if (!PyString_Check($input)) {
31     PyErr_SetString(PyExc_SyntaxError, "Invalid parameter");
32     return NULL;
33   }
34   $1 = PyString_AsString($input);
35   $2 = PyString_Size($input);
36 };
37
38 %typemap(out) char ** {
39   int i;
40   PyObject *arr = PyList_New(0);
41   for (i = 0; $1[i] != NULL; i++)
42     PyList_Append(arr, PyString_FromString($1[i]));
43   $result = arr;
44 };
45
46
47 #else
48 #error "You should define specific translation rules for this language."
49 #endif
50
51 %nodefaultctor SourceFile;
52 %immutable;
53 %include "../src/languages.h"
54 %include "../src/structs.h"
55 %mutable;
56
57 %extend Loc {
58   int total() {
59     return ohcount_loc_total(self);
60   }
61 }
62
63 %extend LocListItem {
64   int code() {
65     return ohcount_loc_list_code(self);
66   }
67   int comments() {
68     return ohcount_loc_list_comments(self);
69   }
70   int blanks() {
71     return ohcount_loc_list_blanks(self);
72   }
73   int total() {
74     return ohcount_loc_list_total(self);
75   }
76   int filecount() {
77     return ohcount_loc_list_filecount(self);
78   }
79 }
80
81 %extend SourceFile {
82   void set_diskpath(const char *diskpath) {
83     ohcount_sourcefile_set_diskpath(self, diskpath);
84   }
85   void set_contents(const char *contents) {
86     ohcount_sourcefile_set_contents(self, contents);
87   }
88   char *get_contents() {
89     return ohcount_sourcefile_get_contents(self);
90   }
91   int contents_size() {
92     return ohcount_sourcefile_get_contents_size(self);
93   }
94   const char *get_language() {
95     return ohcount_sourcefile_get_language(self);
96   }
97   void parse() {
98     ohcount_sourcefile_parse(self);
99   }
100   ParsedLanguageList *get_parsed_language_list() {
101     return ohcount_sourcefile_get_parsed_language_list(self);
102   }
103   LicenseList *get_license_list() {
104     return ohcount_sourcefile_get_license_list(self);
105   }
106   LocList *get_loc_list() {
107     return ohcount_sourcefile_get_loc_list(self);
108   }
109   LocDeltaList *_diff(SourceFile *to) {
110     return ohcount_sourcefile_diff(self, to);
111   }
112 #if defined(SWIGRUBY)
113   void set_filenames(VALUE filenames) {
114     int i, length = RARRAY_LEN(filenames);
115     char **fnames = calloc(length + 1, sizeof(char *));
116     VALUE *iter = RARRAY_PTR(filenames);
117     for (i = 0; i < length; i++, iter++)
118       fnames[i] = StringValuePtr(*iter);
119     self->filenames = fnames;
120   }
121   SourceFile(const char *filepath, VALUE opt_hash=NULL) {
122     SourceFile *sourcefile = ohcount_sourcefile_new(filepath);
123     if (opt_hash) {
124       VALUE val;
125       val = rb_hash_aref(opt_hash, ID2SYM(rb_intern("contents")));
126       if (val && rb_type(val) == T_STRING)
127         ohcount_sourcefile_set_contents(sourcefile, StringValuePtr(val));
128       val = rb_hash_aref(opt_hash, ID2SYM(rb_intern("file_location")));
129       if (val && rb_type(val) == T_STRING)
130         ohcount_sourcefile_set_diskpath(sourcefile, StringValuePtr(val));
131       val = rb_hash_aref(opt_hash, ID2SYM(rb_intern("filenames")));
132       if (val && rb_type(val) == T_ARRAY)
133         SourceFile_set_filenames(sourcefile, val);
134     }
135     return sourcefile;
136   }
137 #elif defined(SWIGPYTHON)
138   void set_filenames(PyObject *filenames) {
139     int i, length;
140     char **fnames;
141     if (!PyList_Check(filenames)) {
142       PyErr_SetString(PyExc_SyntaxError, "Invalid parameter, expected a list of strings");
143       return;
144     }
145     length = PyList_Size(filenames);
146     fnames = calloc(length + 1, sizeof(char *));
147     for (i = 0; i < length; i++) {
148       PyObject *s = PyList_GetItem(filenames, i);
149       if (!PyString_Check(s)) {
150         PyErr_SetString(PyExc_SyntaxError, "Invalid parameter, expected a list of strings");
151         return;
152       }
153       fnames[i] = PyString_AsString(s);
154     }
155     ohcount_sourcefile_set_filenames(self, fnames);
156     free(fnames);
157   }
158   static void py_annotate_callback(const char *language, const char *entity,
159                   int start, int end, void *userdata) {
160     PyObject *list = (PyObject *) userdata;
161     PyObject *dict = PyDict_New();
162     PyDict_SetItem(dict, PyString_FromString("language"),
163         PyString_FromString(language));
164     PyDict_SetItem(dict, PyString_FromString("entity"),
165         PyString_FromString(entity));
166     PyDict_SetItem(dict, PyString_FromString("start"),
167         PyInt_FromLong(start));
168     PyDict_SetItem(dict, PyString_FromString("end"),
169         PyInt_FromLong(end));
170     PyList_Append(list, dict);
171   }
172   PyObject *annotate() {
173     PyObject *list = PyList_New(0);
174     ohcount_sourcefile_parse_with_callback(self, SourceFile_py_annotate_callback, list);
175     return list;
176   }
177   PyObject *raw_entities() {
178     PyObject *list = PyList_New(0);
179     ohcount_sourcefile_parse_entities_with_callback(self, SourceFile_py_annotate_callback, list);
180     return list;
181   }
182   SourceFile(const char *filepath, PyObject *args) {
183     SourceFile *sourcefile = ohcount_sourcefile_new(filepath);
184     if (args) {
185       PyObject *val;
186       if (!PyDict_Check(args)) {
187         PyErr_SetString(PyExc_SyntaxError, "Invalid argument");
188         ohcount_sourcefile_free(sourcefile);
189         return NULL;
190       }
191       val = PyDict_GetItem(args, PyString_FromString("contents"));
192       if (val && PyString_Check(val))
193         ohcount_sourcefile_set_contents(sourcefile, PyString_AsString(val));
194       val = PyDict_GetItem(args, PyString_FromString("file_location"));
195       if (val && PyString_Check(val))
196         ohcount_sourcefile_set_diskpath(sourcefile, PyString_AsString(val));
197       val = PyDict_GetItem(args, PyString_FromString("filenames"));
198       if (val && PyString_Check(val))
199         SourceFile_set_filenames(sourcefile, val);
200     }
201     return sourcefile;
202   }
203
204 #endif
205   ~SourceFile() {
206     if (self->filenames)
207       free(self->filenames);
208     ohcount_sourcefile_free(self);
209   }
210 };
211
212 %extend SourceFileListItem {
213 #if defined(SWIGRUBY)
214
215   static VALUE rb_add_directory(VALUE directory, SourceFileList *list) {
216     if (directory && rb_type(directory) == T_STRING)
217       ohcount_sourcefile_list_add_directory(list, StringValuePtr(directory));
218     return Qnil;
219   }
220   static VALUE rb_add_file(VALUE file, SourceFileList *list) {
221     if (file && rb_type(file) == T_STRING)
222       ohcount_sourcefile_list_add_file(list, StringValuePtr(file));
223     return Qnil;
224   }
225   SourceFileListItem(VALUE opt_hash=NULL) {
226     SourceFileList *list = ohcount_sourcefile_list_new();
227     if (opt_hash) {
228       VALUE val;
229       val = rb_hash_aref(opt_hash, ID2SYM(rb_intern("paths")));
230       if (val && rb_type(val) == T_ARRAY)
231         rb_iterate(rb_each, val, SourceFileListItem_rb_add_directory, (VALUE)list);
232       val = rb_hash_aref(opt_hash, ID2SYM(rb_intern("files")));
233       if (val && rb_type(val) == T_ARRAY)
234         rb_iterate(rb_each, val, SourceFileListItem_rb_add_file, (VALUE)list);
235     }
236     return list;
237   }
238
239 #elif defined(SWIGPYTHON)
240
241   SourceFileListItem(PyObject *args) {
242     SourceFileList *list = ohcount_sourcefile_list_new();
243     if (args) {
244       PyObject *val;
245       if (!PyDict_Check(args)) {
246         PyErr_SetString(PyExc_SyntaxError, "Invalid argument");
247         ohcount_sourcefile_list_free(list);
248         return NULL;
249       }
250       val = PyDict_GetItem(args, PyString_FromString("paths"));
251       if (val && PyList_Check(val)) {
252         int i, length = PyList_Size(val);
253         for (i = 0; i < length; i++) {
254           PyObject *s = PyList_GetItem(val, i);
255           if (!PyString_Check(s)) {
256             PyErr_SetString(PyExc_SyntaxError, "Invalid 'paths', expected a list of strings");
257             ohcount_sourcefile_list_free(list);
258             return NULL;
259           }
260           ohcount_sourcefile_list_add_directory(list, PyString_AsString(s));
261         }
262       }
263       val = PyDict_GetItem(args, PyString_FromString("files"));
264       if (val && PyList_Check(val)) {
265         int i, length = PyList_Size(val);
266         for (i = 0; i < length; i++) {
267           PyObject *s = PyList_GetItem(val, i);
268           if (!PyString_Check(s)) {
269             PyErr_SetString(PyExc_SyntaxError, "Invalid 'files', expected a list of strings");
270             ohcount_sourcefile_list_free(list);
271             return NULL;
272           }
273           ohcount_sourcefile_list_add_file(list, PyString_AsString(s));
274         }
275       }
276     }
277     return list;
278   }
279
280 #endif
281
282   ~SourceFileListItem() {
283     ohcount_sourcefile_list_free(self);
284   }
285   void add_file(const char *filepath) {
286     ohcount_sourcefile_list_add_file(self, filepath);
287   }
288   void add_directory(const char *directory) {
289     ohcount_sourcefile_list_add_directory(self, directory);
290   }
291   LocList *analyze_languages() {
292     return ohcount_sourcefile_list_analyze_languages(self);
293   }
294 }
295
296 int ohcount_is_binary_filename(const char *filename);
297
298 struct LanguageMap *ohcount_hash_language_from_name(register const char *str, register unsigned int len);