4 #include "../src/detector.h"
5 #include "../src/languages.h"
6 #include "../src/sourcefile.h"
13 %typemap(in) (register const char *str, register unsigned int len) {
14 Check_Type($input, T_STRING);
15 $1 = STR2CSTR($input);
16 $2 = RSTRING($input)->len;
19 %typemap(out) char ** {
20 VALUE arr = rb_ary_new();
22 for (i = 0; $1[i] != NULL; i++)
23 rb_ary_push(arr, rb_str_new2($1[i]));
27 #elif defined(SWIGPYTHON)
29 %typemap(in) (register const char *str, register unsigned int len) {
30 if (!PyString_Check($input)) {
31 PyErr_SetString(PyExc_SyntaxError, "Invalid parameter");
34 $1 = PyString_AsString($input);
35 $2 = PyString_Size($input);
38 %typemap(out) char ** {
40 PyObject *arr = PyList_New(0);
41 for (i = 0; $1[i] != NULL; i++)
42 PyList_Append(arr, PyString_FromString($1[i]));
48 #error "You should define specific translation rules for this language."
51 %nodefaultctor SourceFile;
53 %include "../src/languages.h"
54 %include "../src/structs.h"
59 return ohcount_loc_total(self);
65 return ohcount_loc_list_code(self);
68 return ohcount_loc_list_comments(self);
71 return ohcount_loc_list_blanks(self);
74 return ohcount_loc_list_total(self);
77 return ohcount_loc_list_filecount(self);
82 void set_diskpath(const char *diskpath) {
83 ohcount_sourcefile_set_diskpath(self, diskpath);
85 void set_contents(const char *contents) {
86 ohcount_sourcefile_set_contents(self, contents);
88 char *get_contents() {
89 return ohcount_sourcefile_get_contents(self);
92 return ohcount_sourcefile_get_contents_size(self);
94 const char *get_language() {
95 return ohcount_sourcefile_get_language(self);
98 ohcount_sourcefile_parse(self);
100 ParsedLanguageList *get_parsed_language_list() {
101 return ohcount_sourcefile_get_parsed_language_list(self);
103 LicenseList *get_license_list() {
104 return ohcount_sourcefile_get_license_list(self);
106 LocList *get_loc_list() {
107 return ohcount_sourcefile_get_loc_list(self);
109 LocDeltaList *_diff(SourceFile *to) {
110 return ohcount_sourcefile_diff(self, to);
112 #if defined(SWIGRUBY)
113 void set_filenames(VALUE filenames) {
114 int i, length = RARRAY(filenames)->len;
115 char **fnames = calloc(length + 1, sizeof(char *));
116 VALUE *iter = RARRAY(filenames)->ptr;
117 for (i = 0; i < length; i++, iter++)
118 fnames[i] = StringValuePtr(*iter);
119 self->filenames = fnames;
121 SourceFile(const char *filepath, VALUE opt_hash=NULL) {
122 SourceFile *sourcefile = ohcount_sourcefile_new(filepath);
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, STR2CSTR(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, STR2CSTR(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);
137 #elif defined(SWIGPYTHON)
138 void set_filenames(PyObject *filenames) {
141 if (!PyList_Check(filenames)) {
142 PyErr_SetString(PyExc_SyntaxError, "Invalid parameter, expected a list of strings");
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");
153 fnames[i] = PyString_AsString(s);
155 ohcount_sourcefile_set_filenames(self, fnames);
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);
172 PyObject *annotate() {
173 PyObject *list = PyList_New(0);
174 ohcount_sourcefile_parse_with_callback(self, SourceFile_py_annotate_callback, list);
177 PyObject *raw_entities() {
178 PyObject *list = PyList_New(0);
179 ohcount_sourcefile_parse_entities_with_callback(self, SourceFile_py_annotate_callback, list);
182 SourceFile(const char *filepath, PyObject *args) {
183 SourceFile *sourcefile = ohcount_sourcefile_new(filepath);
186 if (!PyDict_Check(args)) {
187 PyErr_SetString(PyExc_SyntaxError, "Invalid argument");
188 ohcount_sourcefile_free(sourcefile);
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);
207 free(self->filenames);
208 ohcount_sourcefile_free(self);
212 %extend SourceFileList {
213 #if defined(SWIGRUBY)
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, STR2CSTR(directory));
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, STR2CSTR(file));
225 SourceFileList(VALUE opt_hash=NULL) {
226 SourceFileList *list = ohcount_sourcefile_list_new();
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, SourceFileList_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, SourceFileList_rb_add_file, (VALUE)list);
239 #elif defined(SWIGPYTHON)
241 SourceFileList(PyObject *args) {
242 SourceFileList *list = ohcount_sourcefile_list_new();
245 if (!PyDict_Check(args)) {
246 PyErr_SetString(PyExc_SyntaxError, "Invalid argument");
247 ohcount_sourcefile_list_free(list);
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);
260 ohcount_sourcefile_list_add_directory(list, PyString_AsString(s));
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);
273 ohcount_sourcefile_list_add_file(list, PyString_AsString(s));
283 ohcount_sourcefile_list_free(self);
285 void add_file(const char *filepath) {
286 ohcount_sourcefile_list_add_file(self, filepath);
288 void add_directory(const char *directory) {
289 ohcount_sourcefile_list_add_directory(self, directory);
291 LocList *analyze_languages() {
292 return ohcount_sourcefile_list_analyze_languages(self);
296 int ohcount_is_binary_filename(const char *filename);
298 struct LanguageMap *ohcount_hash_language_from_name(register const char *str, register unsigned int len);