sdsykes-ferret 0.11.6.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +338 -0
  5. data/TODO +17 -0
  6. data/TUTORIAL +231 -0
  7. data/bin/ferret-browser +79 -0
  8. data/ext/analysis.c +1555 -0
  9. data/ext/analysis.h +219 -0
  10. data/ext/api.c +69 -0
  11. data/ext/api.h +27 -0
  12. data/ext/array.c +123 -0
  13. data/ext/array.h +53 -0
  14. data/ext/bitvector.c +540 -0
  15. data/ext/bitvector.h +272 -0
  16. data/ext/compound_io.c +383 -0
  17. data/ext/config.h +42 -0
  18. data/ext/document.c +156 -0
  19. data/ext/document.h +53 -0
  20. data/ext/except.c +120 -0
  21. data/ext/except.h +168 -0
  22. data/ext/extconf.rb +14 -0
  23. data/ext/ferret.c +402 -0
  24. data/ext/ferret.h +91 -0
  25. data/ext/filter.c +156 -0
  26. data/ext/fs_store.c +483 -0
  27. data/ext/global.c +418 -0
  28. data/ext/global.h +117 -0
  29. data/ext/hash.c +567 -0
  30. data/ext/hash.h +473 -0
  31. data/ext/hashset.c +170 -0
  32. data/ext/hashset.h +187 -0
  33. data/ext/header.h +58 -0
  34. data/ext/helper.c +62 -0
  35. data/ext/helper.h +13 -0
  36. data/ext/inc/lang.h +48 -0
  37. data/ext/inc/threading.h +31 -0
  38. data/ext/index.c +6425 -0
  39. data/ext/index.h +961 -0
  40. data/ext/lang.h +66 -0
  41. data/ext/libstemmer.c +92 -0
  42. data/ext/libstemmer.h +79 -0
  43. data/ext/mempool.c +87 -0
  44. data/ext/mempool.h +35 -0
  45. data/ext/modules.h +162 -0
  46. data/ext/multimapper.c +310 -0
  47. data/ext/multimapper.h +51 -0
  48. data/ext/posh.c +1006 -0
  49. data/ext/posh.h +1007 -0
  50. data/ext/priorityqueue.c +151 -0
  51. data/ext/priorityqueue.h +143 -0
  52. data/ext/q_boolean.c +1608 -0
  53. data/ext/q_const_score.c +161 -0
  54. data/ext/q_filtered_query.c +209 -0
  55. data/ext/q_fuzzy.c +268 -0
  56. data/ext/q_match_all.c +148 -0
  57. data/ext/q_multi_term.c +677 -0
  58. data/ext/q_parser.c +2825 -0
  59. data/ext/q_phrase.c +1126 -0
  60. data/ext/q_prefix.c +100 -0
  61. data/ext/q_range.c +350 -0
  62. data/ext/q_span.c +2402 -0
  63. data/ext/q_term.c +337 -0
  64. data/ext/q_wildcard.c +171 -0
  65. data/ext/r_analysis.c +2575 -0
  66. data/ext/r_index.c +3472 -0
  67. data/ext/r_qparser.c +585 -0
  68. data/ext/r_search.c +4105 -0
  69. data/ext/r_store.c +513 -0
  70. data/ext/r_utils.c +963 -0
  71. data/ext/ram_store.c +471 -0
  72. data/ext/search.c +1741 -0
  73. data/ext/search.h +885 -0
  74. data/ext/similarity.c +150 -0
  75. data/ext/similarity.h +82 -0
  76. data/ext/sort.c +983 -0
  77. data/ext/stem_ISO_8859_1_danish.c +338 -0
  78. data/ext/stem_ISO_8859_1_danish.h +16 -0
  79. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  80. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  81. data/ext/stem_ISO_8859_1_english.c +1156 -0
  82. data/ext/stem_ISO_8859_1_english.h +16 -0
  83. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  84. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  85. data/ext/stem_ISO_8859_1_french.c +1276 -0
  86. data/ext/stem_ISO_8859_1_french.h +16 -0
  87. data/ext/stem_ISO_8859_1_german.c +512 -0
  88. data/ext/stem_ISO_8859_1_german.h +16 -0
  89. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  90. data/ext/stem_ISO_8859_1_italian.h +16 -0
  91. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  92. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  93. data/ext/stem_ISO_8859_1_porter.c +776 -0
  94. data/ext/stem_ISO_8859_1_porter.h +16 -0
  95. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  96. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  97. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  98. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  99. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  100. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  101. data/ext/stem_KOI8_R_russian.c +701 -0
  102. data/ext/stem_KOI8_R_russian.h +16 -0
  103. data/ext/stem_UTF_8_danish.c +344 -0
  104. data/ext/stem_UTF_8_danish.h +16 -0
  105. data/ext/stem_UTF_8_dutch.c +653 -0
  106. data/ext/stem_UTF_8_dutch.h +16 -0
  107. data/ext/stem_UTF_8_english.c +1176 -0
  108. data/ext/stem_UTF_8_english.h +16 -0
  109. data/ext/stem_UTF_8_finnish.c +808 -0
  110. data/ext/stem_UTF_8_finnish.h +16 -0
  111. data/ext/stem_UTF_8_french.c +1296 -0
  112. data/ext/stem_UTF_8_french.h +16 -0
  113. data/ext/stem_UTF_8_german.c +526 -0
  114. data/ext/stem_UTF_8_german.h +16 -0
  115. data/ext/stem_UTF_8_italian.c +1113 -0
  116. data/ext/stem_UTF_8_italian.h +16 -0
  117. data/ext/stem_UTF_8_norwegian.c +302 -0
  118. data/ext/stem_UTF_8_norwegian.h +16 -0
  119. data/ext/stem_UTF_8_porter.c +794 -0
  120. data/ext/stem_UTF_8_porter.h +16 -0
  121. data/ext/stem_UTF_8_portuguese.c +1055 -0
  122. data/ext/stem_UTF_8_portuguese.h +16 -0
  123. data/ext/stem_UTF_8_russian.c +709 -0
  124. data/ext/stem_UTF_8_russian.h +16 -0
  125. data/ext/stem_UTF_8_spanish.c +1137 -0
  126. data/ext/stem_UTF_8_spanish.h +16 -0
  127. data/ext/stem_UTF_8_swedish.c +313 -0
  128. data/ext/stem_UTF_8_swedish.h +16 -0
  129. data/ext/stopwords.c +401 -0
  130. data/ext/store.c +692 -0
  131. data/ext/store.h +777 -0
  132. data/ext/term_vectors.c +352 -0
  133. data/ext/threading.h +31 -0
  134. data/ext/utilities.c +446 -0
  135. data/ext/win32.h +54 -0
  136. data/lib/ferret.rb +29 -0
  137. data/lib/ferret/browser.rb +246 -0
  138. data/lib/ferret/browser/s/global.js +192 -0
  139. data/lib/ferret/browser/s/style.css +148 -0
  140. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  141. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  142. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  143. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  144. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  145. data/lib/ferret/browser/views/layout.rhtml +22 -0
  146. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  147. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  148. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  149. data/lib/ferret/browser/webrick.rb +14 -0
  150. data/lib/ferret/document.rb +130 -0
  151. data/lib/ferret/field_infos.rb +44 -0
  152. data/lib/ferret/index.rb +786 -0
  153. data/lib/ferret/number_tools.rb +157 -0
  154. data/lib/ferret_version.rb +3 -0
  155. data/setup.rb +1555 -0
  156. data/test/test_all.rb +5 -0
  157. data/test/test_helper.rb +24 -0
  158. data/test/threading/number_to_spoken.rb +132 -0
  159. data/test/threading/thread_safety_index_test.rb +79 -0
  160. data/test/threading/thread_safety_read_write_test.rb +76 -0
  161. data/test/threading/thread_safety_test.rb +133 -0
  162. data/test/unit/analysis/tc_analyzer.rb +548 -0
  163. data/test/unit/analysis/tc_token_stream.rb +646 -0
  164. data/test/unit/index/tc_index.rb +762 -0
  165. data/test/unit/index/tc_index_reader.rb +699 -0
  166. data/test/unit/index/tc_index_writer.rb +437 -0
  167. data/test/unit/index/th_doc.rb +315 -0
  168. data/test/unit/largefile/tc_largefile.rb +46 -0
  169. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  170. data/test/unit/search/tc_filter.rb +135 -0
  171. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  172. data/test/unit/search/tc_index_searcher.rb +61 -0
  173. data/test/unit/search/tc_multi_searcher.rb +128 -0
  174. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  175. data/test/unit/search/tc_search_and_sort.rb +179 -0
  176. data/test/unit/search/tc_sort.rb +49 -0
  177. data/test/unit/search/tc_sort_field.rb +27 -0
  178. data/test/unit/search/tc_spans.rb +190 -0
  179. data/test/unit/search/tm_searcher.rb +384 -0
  180. data/test/unit/store/tc_fs_store.rb +77 -0
  181. data/test/unit/store/tc_ram_store.rb +35 -0
  182. data/test/unit/store/tm_store.rb +34 -0
  183. data/test/unit/store/tm_store_lock.rb +68 -0
  184. data/test/unit/tc_document.rb +81 -0
  185. data/test/unit/ts_analysis.rb +2 -0
  186. data/test/unit/ts_index.rb +2 -0
  187. data/test/unit/ts_largefile.rb +4 -0
  188. data/test/unit/ts_query_parser.rb +2 -0
  189. data/test/unit/ts_search.rb +2 -0
  190. data/test/unit/ts_store.rb +2 -0
  191. data/test/unit/ts_utils.rb +2 -0
  192. data/test/unit/utils/tc_bit_vector.rb +295 -0
  193. data/test/unit/utils/tc_number_tools.rb +117 -0
  194. data/test/unit/utils/tc_priority_queue.rb +106 -0
  195. metadata +285 -0
@@ -0,0 +1,42 @@
1
+ #ifndef FRT_DEFINES_H
2
+ #define FRT_DEFINES_H
3
+
4
+ #include <sys/types.h>
5
+ #include <limits.h>
6
+ #include "posh.h"
7
+
8
+ #ifndef false
9
+ #define false 0
10
+ #endif
11
+ #ifndef true
12
+ #define true 1
13
+ #endif
14
+
15
+ typedef unsigned int bool;
16
+ typedef unsigned char uchar;
17
+
18
+ typedef posh_u16_t f_u16;
19
+ typedef posh_i16_t f_i16;
20
+ typedef posh_u32_t f_u32;
21
+ typedef posh_i32_t f_i32;
22
+ typedef posh_u64_t f_u64;
23
+ typedef posh_i64_t f_i64;
24
+
25
+ #if ( LONG_MAX == 2147483647 ) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)
26
+ #define F_OFF_T_PFX "ll"
27
+ #else
28
+ #define F_OFF_T_PFX "l"
29
+ #endif
30
+
31
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
32
+ #define FRT_IS_C99
33
+ #define FRT_HAS_ISO_VARARGS
34
+ #define FRT_HAS_VARARGS
35
+ #endif
36
+
37
+ #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
38
+ #define FRT_HAS_GNUC_VARARGS
39
+ #define FRT_HAS_VARARGS
40
+ #endif
41
+
42
+ #endif
@@ -0,0 +1,156 @@
1
+ #include "document.h"
2
+ #include <string.h>
3
+
4
+ /****************************************************************************
5
+ *
6
+ * DocField
7
+ *
8
+ ****************************************************************************/
9
+
10
+ DocField *df_new(const char *name)
11
+ {
12
+ DocField *df = ALLOC(DocField);
13
+ df->name = estrdup(name);
14
+ df->size = 0;
15
+ df->capa = DF_INIT_CAPA;
16
+ df->data = ALLOC_N(char *, df->capa);
17
+ df->lengths = ALLOC_N(int, df->capa);
18
+ df->destroy_data = false;
19
+ df->boost = 1.0;
20
+ return df;
21
+ }
22
+
23
+ DocField *df_add_data_len(DocField *df, char *data, int len)
24
+ {
25
+ if (df->size >= df->capa) {
26
+ df->capa <<= 2;
27
+ REALLOC_N(df->data, char *, df->capa);
28
+ REALLOC_N(df->lengths, int, df->capa);
29
+ }
30
+ df->data[df->size] = data;
31
+ df->lengths[df->size] = len;
32
+ df->size++;
33
+ return df;
34
+ }
35
+
36
+ DocField *df_add_data(DocField *df, char *data)
37
+ {
38
+ return df_add_data_len(df, data, strlen(data));
39
+ }
40
+
41
+ void df_destroy(DocField *df)
42
+ {
43
+ if (df->destroy_data) {
44
+ int i;
45
+ for (i = 0; i < df->size; i++) {
46
+ free(df->data[i]);
47
+ }
48
+ }
49
+ free(df->data);
50
+ free(df->lengths);
51
+ free(df->name);
52
+ free(df);
53
+ }
54
+
55
+ char *df_to_s(DocField *df)
56
+ {
57
+ int i;
58
+ int len = strlen(df->name) + 10;
59
+ char *str, *s;
60
+ for (i = 0; i < df->size; i++) {
61
+ len += df->lengths[i] + 5;
62
+ }
63
+ s = str = ALLOC_N(char, len);
64
+ sprintf(str, "%s: ", df->name);
65
+ s += strlen(str);
66
+ if (df->size == 1) {
67
+ *(s++) = '"';
68
+ strncpy(s, df->data[0], df->lengths[0]);
69
+ s += df->lengths[0];
70
+ *(s++) = '"';
71
+ *(s++) = '\0';
72
+ }
73
+ else {
74
+ *(s++) = '[';
75
+ *(s++) = '"';
76
+ strncpy(s, df->data[0], df->lengths[0]);
77
+ s += df->lengths[0];
78
+ *(s++) = '"';
79
+ for (i = 1; i < df->size; i++) {
80
+ *(s++) = ',';
81
+ *(s++) = ' ';
82
+ *(s++) = '"';
83
+ strncpy(s, df->data[i], df->lengths[i]);
84
+ s += df->lengths[i];
85
+ *(s++) = '"';
86
+ }
87
+ sprintf(s, "]");
88
+ }
89
+ return str;
90
+ }
91
+
92
+ /****************************************************************************
93
+ *
94
+ * Document
95
+ *
96
+ ****************************************************************************/
97
+
98
+ Document *doc_new()
99
+ {
100
+ Document *doc = ALLOC(Document);
101
+ doc->field_dict = h_new_str(NULL, (free_ft)&df_destroy);
102
+ doc->size = 0;
103
+ doc->capa = DOC_INIT_CAPA;
104
+ doc->fields = ALLOC_N(DocField *, doc->capa);
105
+ doc->boost = 1.0;
106
+ return doc;
107
+ }
108
+
109
+ DocField *doc_add_field(Document *doc, DocField *df)
110
+ {
111
+ if (!h_set_safe(doc->field_dict, df->name, df)) {
112
+ RAISE(EXCEPTION, "tried to add %s field which alread existed\n",
113
+ df->name);
114
+ }
115
+ if (doc->size >= doc->capa) {
116
+ doc->capa <<= 1;
117
+ REALLOC_N(doc->fields, DocField *, doc->capa);
118
+ }
119
+ doc->fields[doc->size] = df;
120
+ doc->size++;
121
+ return df;
122
+ }
123
+
124
+ DocField *doc_get_field(Document *doc, const char *name)
125
+ {
126
+ return h_get(doc->field_dict, name);
127
+ }
128
+
129
+ char *doc_to_s(Document *doc)
130
+ {
131
+ int i;
132
+ int len = 100;
133
+ char **fields = ALLOC_N(char *, doc->size);
134
+ char *buf, *s;
135
+ for (i = 0; i < doc->size; i++) {
136
+ fields[i] = df_to_s(doc->fields[i]);
137
+ len += strlen(fields[i]) + 10;
138
+ }
139
+ s = buf = ALLOC_N(char, len);
140
+ sprintf(buf, "Document [\n");
141
+ s += strlen(buf);
142
+ for (i = 0; i < doc->size; i++) {
143
+ sprintf(s, " =>%s\n", fields[i]);
144
+ free(fields[i]);
145
+ s += strlen(s);
146
+ }
147
+ return buf;
148
+ }
149
+
150
+ void doc_destroy(Document *doc)
151
+ {
152
+ h_destroy(doc->field_dict);
153
+ free(doc->fields);
154
+ free(doc);
155
+ }
156
+
@@ -0,0 +1,53 @@
1
+ #ifndef FRT_DOCUMENT_H
2
+ #define FRT_DOCUMENT_H
3
+
4
+ #include "global.h"
5
+ #include "hash.h"
6
+
7
+ /****************************************************************************
8
+ *
9
+ * DocField
10
+ *
11
+ ****************************************************************************/
12
+
13
+ #define DF_INIT_CAPA 1
14
+ typedef struct DocField
15
+ {
16
+ char *name;
17
+ int size;
18
+ int capa;
19
+ int *lengths;
20
+ char **data;
21
+ float boost;
22
+ bool destroy_data : 1;
23
+ } DocField;
24
+
25
+ extern DocField *df_new(const char *name);
26
+ extern DocField *df_add_data(DocField *df, char *data);
27
+ extern DocField *df_add_data_len(DocField *df, char *data, int len);
28
+ extern void df_destroy(DocField *df);
29
+ extern char *df_to_s(DocField *df);
30
+
31
+ /****************************************************************************
32
+ *
33
+ * Document
34
+ *
35
+ ****************************************************************************/
36
+
37
+ #define DOC_INIT_CAPA 8
38
+ typedef struct Document
39
+ {
40
+ HashTable *field_dict;
41
+ int size;
42
+ int capa;
43
+ DocField **fields;
44
+ float boost;
45
+ } Document;
46
+
47
+ extern Document *doc_new();
48
+ extern DocField *doc_add_field(Document *doc, DocField *df);
49
+ extern DocField *doc_get_field(Document *doc, const char *fname);
50
+ extern char *doc_to_s(Document *doc);
51
+ extern void doc_destroy(Document *doc);
52
+
53
+ #endif
@@ -0,0 +1,120 @@
1
+ #include <stdarg.h>
2
+ #include "global.h"
3
+ #include "except.h"
4
+ #include "threading.h"
5
+
6
+ /**
7
+ * Set this to false if you don't want to print the error location when an
8
+ * exception occurs.
9
+ */
10
+ bool except_show_pos = true;
11
+
12
+ const char *const FRT_ERROR_TYPES[] = {
13
+ "Body",
14
+ "Finally",
15
+ "Exception",
16
+ "IO Error",
17
+ "File Not Found Error",
18
+ "Argument Error",
19
+ "End-of-File Error",
20
+ "Unsupported Function Error",
21
+ "State Error",
22
+ "Parse Error",
23
+ "Memory Error",
24
+ "Index Error",
25
+ "Lock Error"
26
+ };
27
+
28
+ char *const UNSUPPORTED_ERROR_MSG = "Unsupported operation";
29
+ char *const EOF_ERROR_MSG = "Read past end of file";
30
+ char xmsg_buffer[XMSG_BUFFER_SIZE];
31
+ char xmsg_buffer_final[XMSG_BUFFER_SIZE];
32
+
33
+ static thread_key_t exception_stack_key;
34
+ static thread_once_t exception_stack_key_once = THREAD_ONCE_INIT;
35
+
36
+ static void exception_stack_alloc(void)
37
+ {
38
+ thread_key_create(&exception_stack_key, NULL);
39
+ }
40
+
41
+ /*
42
+ static void put_stack(char *inst, xcontext_t *top)
43
+ {
44
+ int x = 10;
45
+ fprintf(stderr, "( %s )", inst);
46
+ while (top && x--) {
47
+ fprintf(stderr, "=> %x ", top);
48
+ top = top->next;
49
+ }
50
+ fprintf(stderr, "\n");
51
+ }
52
+ */
53
+
54
+ void xpush_context(xcontext_t *context)
55
+ {
56
+ xcontext_t *top_context;
57
+ thread_once(&exception_stack_key_once, *exception_stack_alloc);
58
+ top_context = thread_getspecific(exception_stack_key);
59
+ context->next = top_context;
60
+ thread_setspecific(exception_stack_key, context);
61
+ context->handled = true;
62
+ context->in_finally = false;
63
+ }
64
+
65
+ static INLINE void xraise_context(xcontext_t *context,
66
+ volatile int excode,
67
+ const char *const msg)
68
+ {
69
+ context->msg = msg;
70
+ context->excode = excode;
71
+ context->handled = false;
72
+ longjmp(context->jbuf, excode);
73
+ }
74
+
75
+ #ifndef FRT_HAS_VARARGS
76
+ void RAISE(int excode, const char *fmt, ...)
77
+ {
78
+ va_list args;
79
+ va_start(args, fmt);
80
+ vsnprintf(xmsg_buffer, XMSG_BUFFER_SIZE, fmt, args);
81
+ xraise(excode, xmsg_buffer);
82
+ va_end(args);
83
+ }
84
+ #endif
85
+
86
+ void xraise(int excode, const char *const msg)
87
+ {
88
+ xcontext_t *top_context;
89
+ thread_once(&exception_stack_key_once, *exception_stack_alloc);
90
+ top_context = thread_getspecific(exception_stack_key);
91
+
92
+ if (!top_context) {
93
+ FRT_EXIT(FRT_ERROR_TYPES[excode], msg);
94
+ }
95
+ else if (!top_context->in_finally) {
96
+ xraise_context(top_context, excode, msg);
97
+ }
98
+ else if (top_context->handled) {
99
+ top_context->msg = msg;
100
+ top_context->excode = excode;
101
+ top_context->handled = false;
102
+ }
103
+ }
104
+
105
+ void xpop_context()
106
+ {
107
+ xcontext_t *top_cxt, *context;
108
+ thread_once(&exception_stack_key_once, *exception_stack_alloc);
109
+ top_cxt = thread_getspecific(exception_stack_key);
110
+ context = top_cxt->next;
111
+ thread_setspecific(exception_stack_key, context);
112
+ if (!top_cxt->handled) {
113
+ if (context) {
114
+ xraise_context(context, top_cxt->excode, top_cxt->msg);
115
+ }
116
+ else {
117
+ FRT_EXIT(FRT_ERROR_TYPES[top_cxt->excode], top_cxt->msg);
118
+ }
119
+ }
120
+ }
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Exception Handling Framework
3
+ *
4
+ * Exception Handling looks something like this;
5
+ *
6
+ * <pre>
7
+ * TRY
8
+ * RAISE(EXCEPTION, msg1);
9
+ * break;
10
+ * case EXCEPTION:
11
+ * // This should be called
12
+ * exception_handled = true;
13
+ * HANDLED();
14
+ * break;
15
+ * default:
16
+ * // shouldn't enter here
17
+ * break;
18
+ * XFINALLY
19
+ * // this code will always be run
20
+ * if (close_widget_one(arg) == 0) {
21
+ * RAISE(EXCEPTION_CODE, msg);
22
+ * }
23
+ * // this code will also always run, even if the above exception is
24
+ * // raised
25
+ * if (close_widget_two(arg) == 0) {
26
+ * RAISE(EXCEPTION_CODE, msg);
27
+ * }
28
+ * XENDTRY
29
+ * </pre>
30
+ *
31
+ * Basically exception handling uses the following macros;
32
+ *
33
+ * TRY
34
+ * Sets up the exception handler and need be placed before any expected
35
+ * exceptions would be raised.
36
+ *
37
+ * case <EXCEPTION_CODE>:
38
+ * Internally the exception handling uses a switch statement so use the case
39
+ * statement with the appropriate error code to catch Exceptions. Hence, if
40
+ * you want to catch all exceptions, use the default keyword.
41
+ *
42
+ * HANDLED
43
+ * If you catch and handle an exception you need to explicitely call
44
+ * HANDLED(); or the exeption will be re-raised once the current exception
45
+ * handling context is left.
46
+ *
47
+ * case FINALLY:
48
+ * Code in this block is always called. Use this block to close any
49
+ * resources opened in the Exception handling body.
50
+ *
51
+ * ENDTRY
52
+ * Must be placed at the end of all exception handling code.
53
+ *
54
+ * XFINALLY
55
+ * Similar to case FINALLY: except that it uses a fall through (ie, you must
56
+ * not use a break before it) instead of a jump to get to it. This saves a
57
+ * jump. It must be used in combination with XENDTRY and must not have any
58
+ * other catches. This is an optimization so should probably be not be used
59
+ * in most cases.
60
+ *
61
+ * XCATCHALL
62
+ * Like XFINALLY but the block is only called when an exception is raised.
63
+ * Must use in combination with XENDTRY and do not have any other FINALLY or
64
+ * catch block.
65
+ *
66
+ * XENDTRY
67
+ * Must use in combination with XFINALLY or XCATCHALL. Simply, it doesn't
68
+ * jump to FINALLY, making it more efficient.
69
+ */
70
+ #ifndef FRT_EXCEPT_H
71
+ #define FRT_EXCEPT_H
72
+
73
+ #include <setjmp.h>
74
+ #include "config.h"
75
+
76
+ #define BODY 0
77
+ #define FINALLY 1
78
+ #define EXCEPTION 2
79
+ #define FERRET_ERROR 2
80
+ #define IO_ERROR 3
81
+ #define FILE_NOT_FOUND_ERROR 4
82
+ #define ARG_ERROR 5
83
+ #define EOF_ERROR 6
84
+ #define UNSUPPORTED_ERROR 7
85
+ #define STATE_ERROR 8
86
+ #define PARSE_ERROR 9
87
+ #define MEM_ERROR 10
88
+ #define INDEX_ERROR 11
89
+ #define LOCK_ERROR 12
90
+
91
+ extern char *const UNSUPPORTED_ERROR_MSG;
92
+ extern char *const EOF_ERROR_MSG;
93
+ extern bool except_show_pos;
94
+
95
+ typedef struct xcontext_t
96
+ {
97
+ jmp_buf jbuf;
98
+ struct xcontext_t *next;
99
+ const char *msg;
100
+ volatile int excode;
101
+ unsigned int handled : 1;
102
+ unsigned int in_finally : 1;
103
+ } xcontext_t;
104
+
105
+ #define TRY\
106
+ do {\
107
+ xcontext_t xcontext;\
108
+ xpush_context(&xcontext);\
109
+ switch (setjmp(xcontext.jbuf)) {\
110
+ case BODY:
111
+
112
+
113
+ #define XENDTRY\
114
+ }\
115
+ xpop_context();\
116
+ } while (0);
117
+
118
+ #define ENDTRY\
119
+ }\
120
+ if (!xcontext.in_finally) {\
121
+ xpop_context();\
122
+ xcontext.in_finally = 1;\
123
+ longjmp(xcontext.jbuf, FINALLY);\
124
+ }\
125
+ } while (0);
126
+
127
+ #define RETURN_EARLY() xpop_context()
128
+
129
+
130
+ #define XFINALLY default: xcontext.in_finally = 1;
131
+
132
+ #define XCATCHALL break; default: xcontext.in_finally = 1;
133
+
134
+ #define HANDLED() xcontext.handled = 1; /* true */
135
+
136
+ #define XMSG_BUFFER_SIZE 2048
137
+
138
+ #ifdef FRT_HAS_ISO_VARARGS
139
+ # define RAISE(excode, ...) do {\
140
+ snprintf(xmsg_buffer, XMSG_BUFFER_SIZE, __VA_ARGS__);\
141
+ snprintf(xmsg_buffer_final, XMSG_BUFFER_SIZE,\
142
+ "Error occured in %s:%d - %s\n\t%s\n",\
143
+ __FILE__, __LINE__, __func__, xmsg_buffer);\
144
+ xraise(excode, xmsg_buffer_final);\
145
+ } while (0)
146
+ #elif defined(FRT_HAS_GNUC_VARARGS)
147
+ # define RAISE(excode, args...) do {\
148
+ snprintf(xmsg_buffer, XMSG_BUFFER_SIZE, ##args);\
149
+ snprintf(xmsg_buffer_final, XMSG_BUFFER_SIZE,\
150
+ "Error occured in %s:%d - %s\n\t%s\n",\
151
+ __FILE__, __LINE__, __func__, xmsg_buffer);\
152
+ xraise(excode, xmsg_buffer_final);\
153
+ } while (0)
154
+
155
+ #else
156
+ extern void RAISE(int excode, const char *fmt, ...);
157
+ #endif
158
+ #define RAISE_HELL() RAISE(FERRET_ERROR, "Hell")
159
+
160
+
161
+ extern void xraise(int excode, const char *const msg);
162
+ extern void xpush_context(xcontext_t *context);
163
+ extern void xpop_context();
164
+
165
+ extern char xmsg_buffer[XMSG_BUFFER_SIZE];
166
+ extern char xmsg_buffer_final[XMSG_BUFFER_SIZE];
167
+
168
+ #endif