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