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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
/*
* Sary::Searcher - a searcher class for Sary
*
* $Id: searcher.c,v 1.4 2005/02/04 04:33:45 knok Exp $
*
* Copyright (C) 2000 TAKAOKA Kazuma <kazuma-t@is.aist-nara.ac.jp>
* Copyright (C) 2002 Hiroyuki Komatsu <komatsu@taiyaki.org>
* All right reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
#include <stddef.h>
#include <errno.h>
#include "sary.h"
#include "ruby.h"
#include "version.h"
#define GET_Searcher(obj, dat) Data_Get_Struct(obj, SarySearcher, dat)
static VALUE SearcherClass;
/* For Searcher class. */
static VALUE rsearcher_s_new(int argc, VALUE *argv, VALUE klass);
static void rsearcher_destroy(SarySearcher *searcher);
static VALUE rsearcher_search(VALUE klass, VALUE pattern);
static VALUE rsearcher_multi_search(VALUE klass, VALUE pattern_array);
static VALUE rsearcher_isearch(VALUE klass, VALUE pattern, VALUE len);
static VALUE rsearcher_isearch_reset(VALUE klass);
static VALUE rsearcher_icase_search(VALUE klass, VALUE pattern);
static VALUE rsearcher_get_next_context_lines(int argc, VALUE *argv,
VALUE klass);
static VALUE rsearcher_get_next_tagged_region(VALUE klass,
VALUE start_tag, VALUE end_tag);
static VALUE rsearcher_each_context_lines(int argc, VALUE *argv, VALUE klass);
static VALUE rsearcher_each_tagged_region(VALUE klass,
VALUE start_tag, VALUE end_tag);
static VALUE rsearcher_count_occurrences(VALUE klass);
static VALUE rsearcher_get_offsets(VALUE klass);
static VALUE rsearcher_get_line_by_offset(VALUE klass, VALUE offset);
static VALUE rsearcher_get_ranges(VALUE klass);
static VALUE rsearcher_get_line_by_range(VALUE klass, VALUE range);
/* static VALUE rsearcher_sort_occurence(VALUE klass); */
static VALUE rsearcher_enable_cache(VALUE klass);
/*
* Initilize Searcher class instance.
*/
static VALUE
rsearcher_s_new(int argc, VALUE *argv, VALUE klass)
{
NEWOBJ(data, struct RData);
OBJSETUP(data, klass, T_DATA);
rb_obj_call_init((VALUE)data, argc, argv);
return (VALUE)data;
}
static VALUE
rsearcher_initialize(int argc, VALUE *argv, VALUE klass)
{
SarySearcher *searcher;
char *file_name_ptr;
char *array_name_ptr;
VALUE file_name, array_name;
rb_scan_args(argc, argv, "11", &file_name, &array_name);
Check_SafeStr(file_name);
#if RUBY_VERSION_CODE >= 180
file_name_ptr = StringValuePtr(file_name);
#else
file_name_ptr = STR2CSTR(file_name);
#endif
if (array_name == Qnil)
searcher = sary_searcher_new(file_name_ptr);
else {
Check_SafeStr(array_name);
#if RUBY_VERSION_CODE >= 180
array_name_ptr = StringValuePtr(array_name);
#else
array_name_ptr = STR2CSTR(array_name);
#endif
searcher = sary_searcher_new2(file_name_ptr, array_name_ptr);
}
if (searcher == NULL)
rb_raise(rb_eIOError, g_strerror(errno));
Check_Type(klass, T_DATA);
RDATA(klass)->dfree = (RUBY_DATA_FUNC)rsearcher_destroy;
RDATA(klass)->dmark = (RUBY_DATA_FUNC)0;
DATA_PTR(klass) = searcher;
return klass;
}
/*
* Destroy instance.
*/
static void
rsearcher_destroy(SarySearcher *searcher)
{
sary_searcher_destroy(searcher);
}
/*
* Instance methods.
*/
static VALUE
rsearcher_search(VALUE klass, VALUE pattern)
{
SarySearcher *searcher;
char *pat;
/* int count, len; */
int len;
GET_Searcher(klass, searcher);
Check_SafeStr(pattern);
#if RUBY_VERSION_CODE >= 180
pat = StringValuePtr(pattern);
len = RSTRING(pattern)->len;
#else
pat = str2cstr(pattern, &len);
#endif
if (sary_searcher_search2(searcher, pat, len))
return Qtrue;
else
return Qfalse;
}
static VALUE
rsearcher_multi_search(VALUE klass, VALUE pattern_array)
{
SarySearcher *searcher;
char **pat;
/* int count, len, i; */
int len, i;
VALUE pattern;
GET_Searcher(klass, searcher);
len = RARRAY(pattern_array)->len;
if (len == 0) {
return Qfalse;
}
pat = ALLOCA_N(char*, len);
for (i = 0; i < len; i++) {
pattern = rb_ary_entry(pattern_array, (long)i);
Check_SafeStr(pattern);
#if RUBY_VERSION_CODE >= 180
pat[i] = StringValuePtr(pattern);
#else
pat[i] = STR2CSTR(pattern);
#endif
}
if (sary_searcher_multi_search(searcher, pat, len))
return Qtrue;
else
return Qfalse;
}
static VALUE
rsearcher_isearch(VALUE klass, VALUE pattern, VALUE len)
{
SarySearcher *searcher;
char *pat;
/* int count; */
GET_Searcher(klass, searcher);
Check_SafeStr(pattern);
#if RUBY_VERSION_CODE >= 180
pat = StringValuePtr(pattern);
#else
pat = STR2CSTR(pattern);
#endif
if (sary_searcher_isearch(searcher, pat, NUM2INT(len)))
return Qtrue;
else
return Qfalse;
}
static VALUE
rsearcher_isearch_reset(VALUE klass)
{
SarySearcher *searcher;
GET_Searcher(klass, searcher);
sary_searcher_isearch_reset(searcher);
return klass;
}
static VALUE
rsearcher_icase_search(VALUE klass, VALUE pattern)
{
SarySearcher *searcher;
char *pat;
/* int count, len; */
int len;
GET_Searcher(klass, searcher);
Check_SafeStr(pattern);
#if RUBY_VERSION_CODE >= 180
pat = StringValuePtr(pattern);
len = RSTRING(pattern)->len;
#else
pat = str2cstr(pattern, &len);
#endif
if (sary_searcher_icase_search2(searcher, pat, len))
return Qtrue;
else
return Qfalse;
}
static VALUE
rsearcher_get_next_context_lines(int argc, VALUE *argv, VALUE klass)
{
SarySearcher *searcher;
VALUE bkwrd, frwrd;
int bk, fr, len;
char *region;
GET_Searcher(klass, searcher);
if (sary_searcher_count_occurrences(searcher) == 0)
return Qnil;
rb_scan_args(argc, argv, "02", &bkwrd, &frwrd);
bk = (bkwrd == Qnil) ? 0 : NUM2INT(bkwrd);
fr = (frwrd == Qnil) ? 0 : NUM2INT(frwrd);
region = sary_searcher_get_next_context_lines2(searcher, bk, fr, &len);
if (region == NULL)
return Qnil;
else
return rb_str_new(region, len);
}
static VALUE
rsearcher_get_next_tagged_region(VALUE klass,
VALUE start_tag, VALUE end_tag)
{
SarySearcher *searcher;
char* stag, *etag, *region;
int slen, elen, rlen;
GET_Searcher(klass, searcher);
if (sary_searcher_count_occurrences(searcher) == 0)
return Qnil;
Check_SafeStr(start_tag);
Check_SafeStr(end_tag);
#if RUBY_VERSION_CODE >= 180
stag = StringValuePtr(start_tag);
etag = StringValuePtr(end_tag);
slen = RSTRING(stag)->len;
elen = RSTRING(etag)->len;
#else
stag = rb_str2cstr(start_tag, &slen);
etag = rb_str2cstr(end_tag, &elen);
#endif
region = sary_searcher_get_next_tagged_region2(searcher, stag, slen,
etag, elen, &rlen);
if (region == NULL)
return Qnil;
else
return rb_str_new(region, rlen);
}
static VALUE
rsearcher_each_context_lines(int argc, VALUE *argv, VALUE klass)
{
VALUE str;
while (!NIL_P(str = rsearcher_get_next_context_lines(argc, argv, klass)))
rb_yield(str);
return klass;
}
static VALUE
rsearcher_each_tagged_region(VALUE klass, VALUE start_tag, VALUE end_tag)
{
VALUE str;
while (!NIL_P(str = rsearcher_get_next_tagged_region(klass,
start_tag, end_tag)))
rb_yield(str);
return klass;
}
static VALUE
rsearcher_count_occurrences(VALUE klass)
{
SarySearcher *searcher;
GET_Searcher(klass, searcher);
return INT2NUM(sary_searcher_count_occurrences(searcher));
}
static VALUE
rsearcher_get_offsets(VALUE klass)
{
SarySearcher *searcher;
SaryText *text;
long len, l;
int tmp;
gchar *bof, *cur;
VALUE ary;
GET_Searcher(klass, searcher);
len = sary_searcher_count_occurrences(searcher);
if (len == 0)
return Qnil;
ary = rb_ary_new2(len);
text = sary_searcher_get_text(searcher);
bof = text->bof;
for (l = 0; l < len; l ++) {
cur = sary_searcher_get_next_context_lines2(searcher, 0, 0, &tmp);
rb_ary_store(ary, l, INT2NUM(cur - bof));
}
return ary;
}
static VALUE
rsearcher_get_line_by_offset(VALUE klass, VALUE offset)
{
SarySearcher *searcher;
SaryText *text;
int o, l;
gchar *c;
o = NUM2INT(offset);
GET_Searcher(klass, searcher);
text = sary_searcher_get_text(searcher);
c = text->bof + o;
sary_text_set_cursor(text, c);
l = sary_text_get_linelen(text);
return rb_str_new(c, l);
}
static VALUE
rsearcher_get_ranges(VALUE klass)
{
SarySearcher *searcher;
SaryText *text;
long len, l;
int tmp;
gchar *bof, *cur;
VALUE ary;
GET_Searcher(klass, searcher);
len = sary_searcher_count_occurrences(searcher);
if (len == 0)
return Qnil;
ary = rb_ary_new2(len);
text = sary_searcher_get_text(searcher);
bof = text->bof;
for (l = 0; l < len; l ++) {
VALUE range;
cur = sary_searcher_get_next_context_lines2(searcher, 0, 0, &tmp);
range = rb_range_new(INT2NUM(cur - bof), INT2NUM(cur - bof + tmp + 1), 1);
rb_ary_store(ary, l, range);
}
return ary;
}
static VALUE
rsearcher_get_line_by_range(VALUE klass, VALUE range)
{
SarySearcher *searcher;
SaryText *text;
int o, l;
gchar *c;
ID first, last;
first = rb_intern("first");
last = rb_intern("last");
o = NUM2INT(rb_funcall(range, first, 0, 0));
l = NUM2INT(rb_funcall(range, last, 0, 0)) - NUM2INT(rb_funcall(range, first, 0, 0));
if (rb_funcall(range, rb_intern("exclude_end?"), 0, 0))
l--;
GET_Searcher(klass, searcher);
text = sary_searcher_get_text(searcher);
c = text->bof + o;
sary_text_set_cursor(text, c);
return rb_str_new(c, l);
}
static VALUE
rsearcher_enable_cache(VALUE klass)
{
SarySearcher *searcher;
GET_Searcher(klass, searcher);
sary_searcher_enable_cache(searcher);
return klass;
}
static VALUE
rsearcher_sort_occurrences(VALUE klass)
{
SarySearcher *searcher;
GET_Searcher(klass, searcher);
if (sary_searcher_count_occurrences(searcher) != 0)
sary_searcher_sort_occurrences(searcher);
return klass;
}
/*
* Initialize class
*/
void
Init_sarysearcher(VALUE module)
{
SearcherClass = rb_define_class_under(module, "Searcher", rb_cObject);
rb_define_singleton_method(SearcherClass, "new", rsearcher_s_new, -1);
rb_define_method(SearcherClass, "initialize", rsearcher_initialize, -1);
rb_define_method(SearcherClass, "search", rsearcher_search, 1);
rb_define_method(SearcherClass, "multi_search", rsearcher_multi_search, 1);
rb_define_method(SearcherClass, "isearch", rsearcher_isearch, 2);
rb_define_method(SearcherClass, "isearch_reset",
rsearcher_isearch_reset, 0);
rb_define_method(SearcherClass, "icase_search",
rsearcher_icase_search, 1);
rb_define_method(SearcherClass, "get_next_context_lines",
rsearcher_get_next_context_lines, -1);
rb_define_method(SearcherClass, "get_next_context_line",
rsearcher_get_next_context_lines, -1);
rb_define_method(SearcherClass, "get_next_tagged_region",
rsearcher_get_next_tagged_region, 2);
rb_define_method(SearcherClass, "each_context_lines",
rsearcher_each_context_lines, -1);
rb_define_method(SearcherClass, "each_context_line",
rsearcher_each_context_lines, -1);
rb_define_method(SearcherClass, "each_tagged_region",
rsearcher_each_tagged_region, 2);
rb_define_method(SearcherClass, "count_occurrences",
rsearcher_count_occurrences, 0);
rb_define_method(SearcherClass, "sort_occurrences",
rsearcher_sort_occurrences, 0);
rb_define_method(SearcherClass, "get_offsets",
rsearcher_get_offsets, 0);
rb_define_method(SearcherClass, "get_line_by_offset",
rsearcher_get_line_by_offset, 1);
rb_define_method(SearcherClass, "get_ranges",
rsearcher_get_ranges, 0);
rb_define_method(SearcherClass, "get_line_by_range",
rsearcher_get_line_by_range, 1);
rb_define_method(SearcherClass, "enable_cache",
rsearcher_enable_cache, 0);
}
|