1 /**
2  * @file rule.h
3  * @author Hightman Mar
4  * @editor set number ; syntax on ; set autoindent ; set tabstop=4 (vim)
5  * $Id$
6  */
7 
8 #ifndef	_SCWS_RULE_20070525_H_
9 #define	_SCWS_RULE_20070525_H_
10 
11 /* xtree required */
12 #include "xtree.h"
13 
14 #define	SCWS_RULE_MAX			32
15 #define	SCWS_RULE_SPECIAL		0x80000000
16 #define	SCWS_RULE_NOSTATS		0x40000000
17 
18 /* flag: 0x00 ~ 0x4000 */
19 #define	SCWS_ZRULE_NONE			0x00
20 #define	SCWS_ZRULE_PREFIX		0x01
21 #define	SCWS_ZRULE_SUFFIX		0x02
22 #define	SCWS_ZRULE_INCLUDE		0x04	/* with include */
23 #define	SCWS_ZRULE_EXCLUDE		0x08	/* with exclude */
24 #define	SCWS_ZRULE_RANGE		0x10	/* with znum range */
25 
26 /* data structure */
27 typedef struct scws_rule_item
28 {
29 	short flag;
30 	char zmin;
31 	char zmax;
32 	char name[17];
33 	char attr[3];
34 	float tf;
35 	float idf;
36 	unsigned int bit;	/* my bit  */
37 	unsigned int inc;	/* include */
38 	unsigned int exc;	/* exclude */
39 }	*rule_item_t;
40 
41 /* special attrs ratio list(single chain, 12bytes) */
42 typedef struct scws_rule_attr *rule_attr_t;
43 struct scws_rule_attr
44 {
45 	char attr1[2];
46 	char attr2[2];
47 	unsigned char npath[2];
48 	short ratio;
49 	rule_attr_t next;
50 };
51 
52 typedef struct scws_rule
53 {
54 	xtree_t tree;
55 	rule_attr_t attr;
56 	struct scws_rule_item items[SCWS_RULE_MAX];
57 	int ref;	// hightman.20130110: refcount (zero to really free/close)
58 }	rule_st, *rule_t;
59 
60 /* scws ruleset: api */
61 
62 /* create & load ruleset, by fpath & charset */
63 rule_t scws_rule_new(const char *fpath, unsigned char *mblen);
64 
65 /* fork ruleset */
66 rule_t scws_rule_fork(rule_t r);
67 
68 /* free the memory & resource for ruleset */
69 void scws_rule_free(rule_t r);
70 
71 /* get the rule tree record by str */
72 rule_item_t scws_rule_get(rule_t r, const char *str, int len);
73 
74 /* check bit */
75 int scws_rule_checkbit(rule_t r, const char *str, int len, unsigned int bit);
76 
77 /* get rule attr x */
78 int scws_rule_attr_ratio(rule_t r, const char *attr1, const char *attr2, const unsigned char *npath);
79 
80 /* check exclude or include */
81 int scws_rule_check(rule_t r, rule_item_t cr, const char *str, int len);
82 
83 #endif
84