1 /* -*- c-basic-offset: 2 -*- */
2 /*
3   Copyright(C) 2015 Brazil
4 
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License version 2.1 as published by the Free Software Foundation.
8 
9   This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
17 */
18 
19 #include "grn_scanner.h"
20 
21 grn_scanner *
grn_scanner_open(grn_ctx * ctx,grn_obj * expr,grn_operator op,grn_bool record_exist)22 grn_scanner_open(grn_ctx *ctx,
23                  grn_obj *expr,
24                  grn_operator op,
25                  grn_bool record_exist)
26 {
27   grn_scanner *scanner;
28 
29   scanner = GRN_MALLOC(sizeof(grn_scanner));
30   if (!scanner) {
31     return NULL;
32   }
33 
34   scanner->source_expr = expr;
35   scanner->expr = grn_expr_rewrite(ctx, expr);
36   if (!scanner->expr) {
37     scanner->expr = expr;
38   }
39 
40   scanner->sis = grn_scan_info_build(ctx,
41                                      scanner->expr,
42                                      &(scanner->n_sis),
43                                      op,
44                                      record_exist);
45   if (!scanner->sis) {
46     grn_scanner_close(ctx, scanner);
47     return NULL;
48   }
49 
50   return scanner;
51 }
52 
53 void
grn_scanner_close(grn_ctx * ctx,grn_scanner * scanner)54 grn_scanner_close(grn_ctx *ctx, grn_scanner *scanner)
55 {
56   if (!scanner) {
57     return;
58   }
59 
60   if (scanner->sis) {
61     int i;
62     for (i = 0; i < scanner->n_sis; i++) {
63       grn_scan_info_close(ctx, scanner->sis[i]);
64     }
65     GRN_FREE(scanner->sis);
66   }
67 
68   if (scanner->expr != scanner->source_expr) {
69     grn_obj_close(ctx, scanner->expr);
70   }
71 
72   GRN_FREE(scanner);
73 }
74