1 /*
2  *
3  * libnamazu.h - Namazu library api
4  *
5  * $Id: libnamazu.h,v 1.46.4.9 2009-02-17 01:28:39 opengl2772 Exp $
6  *
7  */
8 
9 #ifndef _LIBNAMAZU_H
10 #define _LIBNAMAZU_H
11 
12 #include <stdio.h>   /* for FILE struct */
13 
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif /* __cplusplus */
18 
19 /*
20  * Set the message of error occurred in libnmz.
21  */
22 
23 #ifdef __GNUC__
24 
25 #define nmz_set_dyingmsg(msg) \
26     { \
27         if (nmz_is_debugmode()) { \
28             nmz_set_dyingmsg_sub("%s:%d (%s): %s", \
29                 __FILE__, __LINE__, __PRETTY_FUNCTION__, msg);\
30         } else { \
31             nmz_set_dyingmsg_sub("%s", msg);\
32 	} \
33     }
34 
35 #else /* !__GNUC__ */
36 
37 #define nmz_set_dyingmsg(msg) \
38     { \
39         if (nmz_is_debugmode()) { \
40             nmz_set_dyingmsg_sub("%s:%d: %s", __FILE__, __LINE__, msg);\
41         } else { \
42             nmz_set_dyingmsg_sub("%s", msg);\
43 	} \
44     }
45 
46 #endif /* __GNUC__ */
47 
48 
49 /*
50  *
51  * Critical values.
52  *
53  */
54 
55 enum {
56     /* Size of general buffers. This MUST be larger than QUERY_MAX */
57     BUFSIZE = 1024,
58 
59     QUERY_TOKEN_MAX =  32, /* Max number of tokens in the query. */
60     QUERY_MAX       = 256, /* Max length of the query. */
61 
62     INDEX_MAX       = 64   /* Max number of databases */
63 };
64 
65 
66 /*
67  *
68  * Magic Numbers.
69  *
70  */
71 
72 enum {
73     ESC    = 0x1b,      /* Code of ESC */
74     STDIN  = 0,		/* stdin's  fd */
75     STDOUT = 1,		/* stdout's fd */
76     STDERR = 2		/* stderr's fd */
77 };
78 
79 
80 /*
81  *
82  * Data Structures.
83  *
84  */
85 
86 typedef unsigned char uchar;
87 
88 /*
89  * forward declarations of namazu types.
90  */
91 
92 typedef struct _NmzResult NmzResult;
93 
94 /*
95  * Status code for error handling.
96  */
97 enum nmz_stat {
98     FAILURE = -1,
99     SUCCESS,
100     ERR_FATAL,
101     ERR_TOO_LONG_QUERY,
102     ERR_INVALID_QUERY,
103     ERR_TOO_MANY_TOKENS,
104     ERR_TOO_MUCH_MATCH,
105     ERR_TOO_MUCH_HIT,
106     ERR_REGEX_SEARCH_FAILED,
107     ERR_PHRASE_SEARCH_FAILED,
108     ERR_FIELD_SEARCH_FAILED,
109     ERR_CANNOT_OPEN_INDEX,
110     ERR_NO_PERMISSION,
111     ERR_CANNOT_OPEN_RESULT_FORMAT_FILE,
112     ERR_INDEX_IS_LOCKED,
113     ERR_OLD_INDEX_FORMAT
114 };
115 
116 /*
117  * Modes of searching.
118  */
119 enum nmz_searchmethod {
120     WORD_MODE,
121     PREFIX_MODE,
122     REGEX_MODE,
123     PHRASE_MODE,
124     FIELD_MODE,
125     ERROR_MODE
126 };
127 
128 
129 /*
130  * Methods of sorting.
131  */
132 enum nmz_sortmethod {
133     SORT_BY_SCORE,
134     SORT_BY_DATE,
135     SORT_BY_FIELD
136 };
137 
138 /*
139  * Orders of sorting.
140  */
141 enum nmz_sortorder {
142     ASCENDING,
143     DESCENDING
144 };
145 
146 
147 /*
148  * Data structure for each hit document.
149  */
150 struct nmz_data {
151     int score;
152     int docid;   /* document ID */
153     int idxid;   /* index ID */
154     int date;    /* document's date */
155     int rank;    /* ranking data for stable sorting */
156     char *field; /* field's contents for field-specified search */
157 };
158 
159 /*
160  * Data structure for search result.
161  */
162 struct _NmzResult {
163     int num;           /* number of elements in its data */
164     enum nmz_stat stat; /* status code mainly used for error handling */
165     struct nmz_data *data;  /* dynamic array for storing nmz_data's. */
166 };
167 
168 /*
169  * NMZ.* files' names.
170  */
171 struct nmz_names {
172 #define MAXPATH BUFSIZE
173     char i[MAXPATH];
174     char ii[MAXPATH];
175     char head[MAXPATH]; /* followed by a language code */
176     char foot[MAXPATH]; /* followed by a language code */
177     char body[MAXPATH]; /* followed by a language code */
178     char lock[MAXPATH];
179     char result[MAXPATH];
180     char slog[MAXPATH];
181     char w[MAXPATH];
182     char wi[MAXPATH];
183     char field[MAXPATH];  /* followed by a field name */
184     char t[MAXPATH];
185     char p[MAXPATH];
186     char pi[MAXPATH];
187     char tips[MAXPATH];
188     char access[MAXPATH];
189     char version[MAXPATH];
190     char warnlog[MAXPATH];
191 };
192 
193 /*
194  * NMZ.* files' file pointers
195  */
196 struct nmz_files {
197     FILE *i;
198     FILE *ii;
199     FILE *p;
200     FILE *pi;
201     FILE *w;
202     FILE *wi;
203 };
204 
205 struct nmz_indices {
206     int num;                    /* Number of indices */
207     char *names[INDEX_MAX + 1]; /* Index names */
208     struct nmz_hitnumlist
209     *hitnumlists[INDEX_MAX + 1];       /* hitnum list of each index */
210     int totalhitnums[INDEX_MAX + 1];   /* total hit number of each index */
211 };
212 
213 struct nmz_query {
214     int  tokennum;                   /* Number of tokens in the query */
215     char str[BUFSIZE];               /* Query string */
216     char *tab[QUERY_TOKEN_MAX + 1];  /* Token pointers pointed to str */
217 };
218 
219 
220 /*
221  * List containig hit numbers of each index word by word.
222  */
223 struct nmz_hitnumlist {
224     char *word;
225     int hitnum;
226     enum nmz_stat stat;        /* status code mainly used for error handling */
227     struct nmz_hitnumlist *phrase; /* for a result of a phrase search */
228     struct nmz_hitnumlist *next;
229 };
230 
231 /*
232  * Field cache information
233  */
234 struct field_cache {
235     int idxid;
236     int docid;
237     char field[BUFSIZE];
238     char data[BUFSIZE];
239 };
240 
241 extern void nmz_free_aliases ( void );
242 extern void nmz_free_replaces ( void );
243 extern void nmz_set_sortmethod ( enum nmz_sortmethod method );
244 extern enum nmz_sortmethod nmz_get_sortmethod(void);
245 extern void nmz_set_sortorder ( enum nmz_sortorder order );
246 extern enum nmz_sortorder nmz_get_sortorder(void);
247 extern void nmz_set_maxhit ( int max );
248 extern int nmz_get_maxhit ( void );
249 extern void nmz_set_maxmatch ( int max );
250 extern int nmz_get_maxmatch ( void );
251 extern void nmz_set_debugmode ( int mode );
252 extern int  nmz_is_debugmode ( void );
253 extern void nmz_set_loggingmode ( int mode );
254 extern int  nmz_is_loggingmode ( void );
255 extern void nmz_set_regex_searchmode ( int mode );
256 extern int  nmz_is_regex_searchmode ( void );
257 extern void nmz_set_output_warn_to_file ( int mode );
258 extern int  nmz_is_output_warn_to_file ( void );
259 extern char *nmz_get_dyingmsg ( void );
260 extern char *nmz_set_dyingmsg_sub(const char *fmt, ...);
261 extern char *nmz_msg(const char *fmt, ...);
262 extern void nmz_free_internal(void);
263 
264 /* since v2.0.13 */
265 extern char *nmz_get_version();
266 
267 /* since v2.0.17 */
268 #define nmz_isascii(c) (!(c & 0x80))
269 #define nmz_isprint(c) (nmz_isascii(c) && isprint(c))
270 #define nmz_isdigit(c) (nmz_isascii(c) && isdigit(c))
271 #define nmz_isalnum(c) (nmz_isascii(c) && isalnum(c))
272 #define nmz_isalpha(c) (nmz_isascii(c) && isalpha(c))
273 #define nmz_iscntrl(c) (nmz_isascii(c) && iscntrl(c))
274 #define nmz_islower(c) (nmz_isascii(c) && islower(c))
275 #define nmz_ispunct(c) (nmz_isascii(c) && ispunct(c))
276 #define nmz_isspace(c) (nmz_isascii(c) && isspace(c))
277 #define nmz_isupper(c) (nmz_isascii(c) && isupper(c))
278 #define nmz_isxdigit(c) (nmz_isascii(c) && isxdigit(c))
279 
280 #ifdef __cplusplus
281 }
282 #endif /* __cplusplus */
283 
284 #endif /* _LIBNAMAZU_H */
285