root/libswish3/trunk/src/libswish3/swish.c

Revision 2171, 4.0 kB (checked in by karpet, 2 months ago)

get rid of the circular reference in TokenList/Token?; make swish_init() a separate call from swish_init_swish3() so it can be called as class method rather than once-per-object; clean up some ref_cnt logic and mem debugging

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #include <stdlib.h>
21 #include "libswish3.h"
22
23 int SWISH_DEBUG = 0;            /* global var */
24 int SWISH_WARNINGS = 1;         /* global var */
25
26 swish_3 *
27 swish_init_swish3(
28     void (*handler) (swish_ParserData *),
29     void *stash
30 )
31 {
32     swish_3 *s3;
33     s3 = swish_xmalloc(sizeof(swish_3));
34     s3->ref_cnt = 0;
35     s3->config = swish_init_config();
36     s3->config->ref_cnt++;
37     swish_config_set_default(s3->config);
38     s3->analyzer = swish_init_analyzer(s3->config);
39     s3->analyzer->ref_cnt++;
40     s3->parser = swish_init_parser(handler);
41     s3->parser->ref_cnt++;
42     s3->stash = stash;
43    
44     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) {
45         SWISH_DEBUG_MSG("s3 ptr 0x%x", (int)s3);
46     }
47    
48     return s3;
49 }
50
51 void
52 swish_free_swish3(
53     swish_3 *s3
54 )
55 {   
56     s3->parser->ref_cnt--;
57     if (s3->parser->ref_cnt < 1) {
58         swish_free_parser(s3->parser);
59     }
60
61     s3->analyzer->ref_cnt--;
62     if (s3->analyzer->ref_cnt < 1) {
63         swish_free_analyzer(s3->analyzer);
64     }
65
66     s3->config->ref_cnt--;
67     if (s3->config->ref_cnt < 1) {
68         swish_free_config(s3->config);
69     }
70
71     if (s3->ref_cnt != 0) {
72         SWISH_WARN("s3 ref_cnt != 0: %d\n", s3->ref_cnt);
73     }
74     swish_xfree(s3);
75 }
76
77 /* MUST call this before instantiating any swish_3 objects */
78 void
79 swish_init(
80 )
81 {
82
83 /* global var that scripts can check to determine what version of Swish they are
84      * using. the second 0 indicates that it will not override it if already set */
85     setenv("SWISH3", "1", 0);
86
87 /* global debug flag */
88     setenv("SWISH_DEBUG", "0", 0);
89     setenv("SWISH_DEBUG_MEMORY", "0", 0);
90     setenv("SWISH_DEBUG_CONFIG", "0", 0);
91     setenv("SWISH_DEBUG_DOCINFO", "0", 0);
92     setenv("SWISH_DEBUG_TOKENLIST", "0", 0);
93     setenv("SWISH_DEBUG_TOKENIZER", "0", 0);
94     setenv("SWISH_DEBUG_PARSER", "0", 0);
95     setenv("SWISH_DEBUG_NAMEDBUFFER", "0", 0);
96     setenv("SWISH_WARNINGS", "1", 0);
97     if (!SWISH_DEBUG) {
98
99         SWISH_DEBUG += swish_string_to_int(getenv("SWISH_DEBUG"));
100
101 /* additional env vars just increase the global var value */
102
103         if (swish_string_to_int(getenv("SWISH_DEBUG_MEMORY"))) {
104             SWISH_DEBUG += SWISH_DEBUG_MEMORY;
105         }
106         if (swish_string_to_int(getenv("SWISH_DEBUG_CONFIG"))) {
107             SWISH_DEBUG += SWISH_DEBUG_CONFIG;
108         }
109         if (swish_string_to_int(getenv("SWISH_DEBUG_DOCINFO"))) {
110             SWISH_DEBUG += SWISH_DEBUG_DOCINFO;
111         }
112         if (swish_string_to_int(getenv("SWISH_DEBUG_TOKENLIST"))) {
113             SWISH_DEBUG += SWISH_DEBUG_TOKENLIST;
114         }
115         if (swish_string_to_int(getenv("SWISH_DEBUG_TOKENIZER"))) {
116             SWISH_DEBUG += SWISH_DEBUG_TOKENIZER;
117         }
118         if (swish_string_to_int(getenv("SWISH_DEBUG_PARSER"))) {
119             SWISH_DEBUG += SWISH_DEBUG_PARSER;
120         }
121         if (swish_string_to_int(getenv("SWISH_DEBUG_NAMEDBUFFER"))) {
122             SWISH_DEBUG += SWISH_DEBUG_NAMEDBUFFER;
123         }
124     }
125    
126     SWISH_WARNINGS = swish_string_to_int(getenv("SWISH_WARNINGS"));
127
128 /*
129      * initialize the library and check potential API mismatches
130      * between the version it was compiled for and the actual shared
131      * library used.
132 */
133     LIBXML_TEST_VERSION swish_init_memory();
134     swish_verify_utf8_locale();
135
136 }
Note: See TracBrowser for help on using the browser.