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,418 @@
1
+ #include "global.h"
2
+ #include <stdarg.h>
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+ #include <errno.h>
6
+ #include <assert.h>
7
+ #include <math.h>
8
+ #include <ctype.h>
9
+
10
+ const char *EMPTY_STRING = "";
11
+
12
+ bool x_do_logging = false;
13
+
14
+ INLINE int min3(int a, int b, int c)
15
+ {
16
+ return MIN3(a, b, c);
17
+ }
18
+
19
+ INLINE int min2(int a, int b)
20
+ {
21
+ return MIN(a, b);
22
+ }
23
+
24
+ INLINE int max3(int a, int b, int c)
25
+ {
26
+ return MAX3(a, b, c);
27
+ }
28
+
29
+ INLINE int max2(int a, int b)
30
+ {
31
+ return MAX(a, b);
32
+ }
33
+
34
+ int scmp(const void *p1, const void *p2)
35
+ {
36
+ return strcmp(*(char **) p1, *(char **) p2);
37
+ }
38
+
39
+ int icmp(const void *p1, const void *p2)
40
+ {
41
+ int i1 = *(int *) p1;
42
+ int i2 = *(int *) p2;
43
+
44
+ if (i1 > i2) {
45
+ return 1;
46
+ }
47
+ else if (i1 < i2) {
48
+ return -1;
49
+ }
50
+ return 0;
51
+ }
52
+
53
+ int icmp_risky(const void *p1, const void *p2)
54
+ {
55
+ return (*(int *)p1) - *((int *)p2);
56
+ }
57
+
58
+ unsigned int *imalloc(unsigned int value)
59
+ {
60
+ unsigned int *p = ALLOC(unsigned int);
61
+ *p = value;
62
+ return p;
63
+ }
64
+
65
+ unsigned long *lmalloc(unsigned long value)
66
+ {
67
+ unsigned long *p = ALLOC(unsigned long);
68
+ *p = value;
69
+ return p;
70
+ }
71
+
72
+ f_u32 *u32malloc(f_u32 value)
73
+ {
74
+ f_u32 *p = ALLOC(f_u32);
75
+ *p = value;
76
+ return p;
77
+ }
78
+
79
+ f_u64 *u64malloc(f_u64 value)
80
+ {
81
+ f_u64 *p = ALLOC(f_u64);
82
+ *p = value;
83
+ return p;
84
+ }
85
+
86
+
87
+ #ifndef RUBY_BINDINGS
88
+ /* frt_exit: print error message and exit */
89
+ # ifdef FRT_HAS_VARARGS
90
+ void vfrt_exit(const char *file, int line_num, const char *func,
91
+ const char *err_type, const char *fmt, va_list args)
92
+ # else
93
+ void V_FRT_EXIT(const char *err_type, const char *fmt, va_list args)
94
+ # endif
95
+ {
96
+ fflush(stdout);
97
+ fprintf(stderr, "\n");
98
+ if (progname() != NULL) {
99
+ fprintf(stderr, "%s: ", progname());
100
+ }
101
+
102
+ # ifdef FRT_HAS_VARARGS
103
+ fprintf(stderr, "%s occured at <%s>:%d in %s\n",
104
+ err_type, file, line_num, func);
105
+ # else
106
+ fprintf(stderr, "%s occured:\n", err_type);
107
+ # endif
108
+ vfprintf(stderr, fmt, args);
109
+
110
+ if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':') {
111
+ fprintf(stderr, " %s", strerror(errno));
112
+ }
113
+
114
+ fprintf(stderr, "\n");
115
+ exit(2); /* conventional value for failed execution */
116
+ }
117
+
118
+
119
+ # ifdef FRT_HAS_VARARGS
120
+ void frt_exit(const char *file, int line_num, const char *func,
121
+ const char *err_type, const char *fmt, ...)
122
+ # else
123
+ void FRT_EXIT(const char *err_type, const char *fmt, ...)
124
+ # endif
125
+ {
126
+ va_list args;
127
+ va_start(args, fmt);
128
+ # ifdef FRT_HAS_VARARGS
129
+ vfrt_exit(file, line_num, func, err_type, fmt, args);
130
+ # else
131
+ V_FRT_EXIT(err_type, fmt, args);
132
+ # endif
133
+ va_end(args);
134
+ }
135
+ #endif
136
+
137
+
138
+ /* weprintf: print error message and don't exit */
139
+ void weprintf(const char *fmt, ...)
140
+ {
141
+ va_list args;
142
+
143
+ fflush(stdout);
144
+ if (progname() != NULL)
145
+ fprintf(stderr, "%s: ", progname());
146
+
147
+ va_start(args, fmt);
148
+ vfprintf(stderr, fmt, args);
149
+ va_end(args);
150
+
151
+ if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
152
+ fprintf(stderr, " %s", strerror(errno));
153
+ fprintf(stderr, "\n");
154
+ }
155
+
156
+ #define MAX_PROG_NAME 200
157
+ static char name[MAX_PROG_NAME]; /* program name for error msgs */
158
+
159
+ /* setprogname: set stored name of program */
160
+ void setprogname(const char *str)
161
+ {
162
+ strncpy(name, str, MAX_PROG_NAME - 1);
163
+ }
164
+
165
+ char *progname()
166
+ {
167
+ return name;
168
+ }
169
+
170
+ /* concatenate two strings freeing the second */
171
+ char *estrcat(char *str1, char *str2)
172
+ {
173
+ size_t len1 = strlen(str1);
174
+ size_t len2 = strlen(str2);
175
+ REALLOC_N(str1, char, len1 + len2 + 3); /* leave room for <CR> */
176
+ memcpy(str1 + len1, str2, len2 + 1); /* make sure '\0' copied too */
177
+ free(str2);
178
+ return str1;
179
+ }
180
+
181
+ /* epstrdup: duplicate a string with a format, report if error */
182
+ char *epstrdup(const char *fmt, int len, ...)
183
+ {
184
+ char *string;
185
+ va_list args;
186
+ len += (int) strlen(fmt);
187
+
188
+ string = ALLOC_N(char, len + 1);
189
+ va_start(args, len);
190
+ vsprintf(string, fmt, args);
191
+ va_end(args);
192
+
193
+ return string;
194
+ }
195
+
196
+ /* estrdup: duplicate a string, report if error */
197
+ char *estrdup(const char *s)
198
+ {
199
+ char *t = (char *)malloc(strlen(s) + 1);
200
+
201
+ if (t == NULL) {
202
+ RAISE(MEM_ERROR, "failed to allocate %d bytes", (int)strlen(s) + 1);
203
+ }
204
+
205
+ strcpy(t, s);
206
+ return t;
207
+ }
208
+
209
+ /* emalloc: malloc and report if error */
210
+ void *emalloc(size_t size)
211
+ {
212
+ void *p = malloc(size);
213
+
214
+ if (p == NULL) {
215
+ RAISE(MEM_ERROR, "failed to allocate %d bytes", (int)size);
216
+ }
217
+
218
+ return p;
219
+ }
220
+
221
+ /* ecalloc: malloc, zeroset and report if error */
222
+ void *ecalloc(size_t size)
223
+ {
224
+ void *p = calloc(1, size);
225
+
226
+ if (p == NULL) {
227
+ RAISE(MEM_ERROR, "failed to allocate %d bytes", (int)size);
228
+ }
229
+
230
+ return p;
231
+ }
232
+
233
+ /* erealloc: realloc and report if error */
234
+ void *erealloc(void *ptr, size_t size)
235
+ {
236
+ void *p = realloc(ptr, size);
237
+
238
+ if (p == NULL) {
239
+ RAISE(MEM_ERROR, "failed to reallocate %d bytes", (int)size);
240
+ }
241
+
242
+ return p;
243
+ }
244
+
245
+ /* Pretty print a float to the buffer. The buffer should have at least 32
246
+ * bytes available.
247
+ */
248
+ char *dbl_to_s(char *buf, double num)
249
+ {
250
+ char *p, *e;
251
+
252
+ #ifdef FRT_IS_C99
253
+ if (isinf(num)) {
254
+ return estrdup(num < 0 ? "-Infinity" : "Infinity");
255
+ }
256
+ else if (isnan(num)) {
257
+ return estrdup("NaN");
258
+ }
259
+ #endif
260
+
261
+ sprintf(buf, "%#.7g", num);
262
+ if (!(e = strchr(buf, 'e'))) {
263
+ e = buf + strlen(buf);
264
+ }
265
+ if (!isdigit(e[-1])) {
266
+ /* reformat if ended with decimal point (ex 111111111111111.) */
267
+ sprintf(buf, "%#.6e", num);
268
+ if (!(e = strchr(buf, 'e'))) {
269
+ e = buf + strlen(buf);
270
+ }
271
+ }
272
+ p = e;
273
+ while (p[-1] == '0' && isdigit(p[-2])) {
274
+ p--;
275
+ }
276
+
277
+ memmove(p, e, strlen(e) + 1);
278
+ return buf;
279
+ }
280
+
281
+ /* strfmt: like sprintf except that it allocates memory for the string */
282
+ char *vstrfmt(const char *fmt, va_list args)
283
+ {
284
+ char *string;
285
+ char *p = (char *) fmt, *q;
286
+ int len = (int) strlen(fmt) + 1;
287
+ int slen;
288
+ char *s;
289
+ long i;
290
+ double d;
291
+
292
+ q = string = ALLOC_N(char, len);
293
+
294
+ while (*p) {
295
+ if (*p == '%') {
296
+ p++;
297
+ switch (*p) {
298
+ case 's':
299
+ p++;
300
+ s = va_arg(args, char *);
301
+ if (s) {
302
+ slen = (int) strlen(s);
303
+ len += slen;
304
+ *q = 0;
305
+ REALLOC_N(string, char, len);
306
+ q = string + strlen(string);
307
+ sprintf(q, s);
308
+ q += slen;
309
+ }
310
+ continue;
311
+ case 'f':
312
+ p++;
313
+ len += 32;
314
+ *q = 0;
315
+ REALLOC_N(string, char, len);
316
+ q = string + strlen(string);
317
+ d = va_arg(args, double);
318
+ dbl_to_s(q, d);
319
+ q += strlen(q);
320
+ continue;
321
+ case 'd':
322
+ p++;
323
+ len += 20;
324
+ *q = 0;
325
+ REALLOC_N(string, char, len);
326
+ q = string + strlen(string);
327
+ i = va_arg(args, long);
328
+ sprintf(q, "%ld", i);
329
+ q += strlen(q);
330
+ continue;
331
+ default:
332
+ break;
333
+ }
334
+ }
335
+ *q = *p;
336
+ p++;
337
+ q++;
338
+ }
339
+ *q = 0;
340
+
341
+ return string;
342
+ }
343
+
344
+ char *strfmt(const char *fmt, ...)
345
+ {
346
+ va_list args;
347
+ char *str;
348
+ va_start(args, fmt);
349
+ str = vstrfmt(fmt, args);
350
+ va_end(args);
351
+ return str;
352
+ }
353
+
354
+ void dummy_free(void *p)
355
+ {
356
+ (void)p; /* suppress unused argument warning */
357
+ }
358
+
359
+ #ifdef FRT_IS_C99
360
+ extern void usleep(unsigned long usec);
361
+ #else
362
+ # ifdef RUBY_BINDINGS
363
+ struct timeval rb_time_interval _((VALUE));
364
+ # else
365
+ # include <unistd.h>
366
+ # endif
367
+ #endif
368
+
369
+ extern void micro_sleep(const int micro_seconds)
370
+ {
371
+ #ifdef RUBY_BINDINGS
372
+ rb_thread_wait_for(rb_time_interval(rb_float_new((double)micro_seconds/1000000.0)));
373
+ #else
374
+ # ifdef POSH_OS_WIN32
375
+ Sleep(micro_seconds / 1000);
376
+ # else
377
+ usleep(micro_seconds);
378
+ # endif
379
+ #endif
380
+ }
381
+
382
+ typedef struct FreeMe
383
+ {
384
+ void *p;
385
+ free_ft free_func;
386
+ } FreeMe;
387
+
388
+ static FreeMe *free_mes = NULL;
389
+ static int free_mes_size = 0;
390
+ static int free_mes_capa = 0;
391
+
392
+ void register_for_cleanup(void *p, free_ft free_func)
393
+ {
394
+ FreeMe *free_me;
395
+ if (free_mes_capa == 0) {
396
+ free_mes_capa = 16;
397
+ free_mes = ALLOC_N(FreeMe, free_mes_capa);
398
+ }
399
+ else if (free_mes_capa <= free_mes_size) {
400
+ free_mes_capa *= 2;
401
+ REALLOC_N(free_mes, FreeMe, free_mes_capa);
402
+ }
403
+ free_me = free_mes + free_mes_size++;
404
+ free_me->p = p;
405
+ free_me->free_func = free_func;
406
+ }
407
+
408
+ void do_clean_up()
409
+ {
410
+ int i;
411
+ for (i = 0; i < free_mes_size; i++) {
412
+ FreeMe *free_me = free_mes + i;
413
+ free_me->free_func(free_me->p);
414
+ }
415
+ free(free_mes);
416
+ free_mes = NULL;
417
+ free_mes_size = free_mes_capa = 0;
418
+ }
@@ -0,0 +1,117 @@
1
+ #ifndef FRT_GLOBAL_H
2
+ #define FRT_GLOBAL_H
3
+
4
+ #include "config.h"
5
+ #include "except.h"
6
+ #include "lang.h"
7
+ #include <stdlib.h>
8
+ #include <stdio.h>
9
+ #include <stdarg.h>
10
+
11
+ #define MAX_WORD_SIZE 255
12
+ #define MAX_FILE_PATH 1024
13
+
14
+ #if defined(__GNUC__)
15
+ # define INLINE __inline__
16
+ #else
17
+ # define INLINE
18
+ #endif
19
+
20
+ typedef void (*free_ft)(void *key);
21
+
22
+ #define NELEMS(array) ((int)(sizeof(array)/sizeof(array[0])))
23
+
24
+
25
+ #define ZEROSET(ptr, type) memset(ptr, 0, sizeof(type))
26
+ #define ZEROSET_N(ptr, type, n) memset(ptr, 0, sizeof(type)*(n))
27
+
28
+ /*
29
+ #define ALLOC_AND_ZERO(type) (type*)memset(emalloc(sizeof(type)), 0, sizeof(type))
30
+ #define ALLOC_AND_ZERO_N(type,n) (type*)memset(emalloc(sizeof(type)*(n)), 0, sizeof(type)*(n))
31
+ */
32
+ #define ALLOC_AND_ZERO(type) (type*)frt_calloc(sizeof(type))
33
+ #define ALLOC_AND_ZERO_N(type,n) (type*)frt_calloc(sizeof(type)*(n))
34
+
35
+ #define REF(a) (a)->ref_cnt++
36
+ #define DEREF(a) (a)->ref_cnt--
37
+
38
+ #define NEXT_NUM(index, size) (((index) + 1) % (size))
39
+ #define PREV_NUM(index, size) (((index) + (size) - 1) % (size))
40
+
41
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
42
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
43
+
44
+ #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
45
+ #define MAX3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
46
+
47
+ #define RECAPA(self, len, capa, ptr, type) \
48
+ do {\
49
+ if (self->len >= self->capa) {\
50
+ if (self->capa > 0) {\
51
+ self->capa <<= 1;\
52
+ } else {\
53
+ self->capa = 4;\
54
+ }\
55
+ REALLOC_N(self->ptr, type, self->capa);\
56
+ }\
57
+ } while (0)
58
+
59
+ #ifdef POSH_OS_WIN32
60
+ # define Jx fprintf(stderr,"%s, %d\n", __FILE__, __LINE__);
61
+ # define Xj fprintf(stdout,"%s, %d\n", __FILE__, __LINE__);
62
+ #else
63
+ # define Jx fprintf(stderr,"%s, %d: %s\n", __FILE__, __LINE__, __func__);
64
+ # define Xj fprintf(stdout,"%s, %d: %s\n", __FILE__, __LINE__, __func__);
65
+ #endif
66
+
67
+ extern char *progname();
68
+ extern void setprogname(const char *str);
69
+
70
+ extern unsigned int *imalloc(unsigned int value);
71
+ extern unsigned long *lmalloc(unsigned long value);
72
+ extern f_u32 *u32malloc(f_u32 value);
73
+ extern f_u64 *u64malloc(f_u64 value);
74
+
75
+ extern void *emalloc(size_t n);
76
+ extern void *ecalloc(size_t n);
77
+ extern void *erealloc(void *ptr, size_t n);
78
+ extern char *estrdup(const char *s);
79
+ extern char *estrcat(char *str, char *str_cat);
80
+
81
+ extern const char *EMPTY_STRING;
82
+
83
+ extern int scmp(const void *p1, const void *p2);
84
+ extern int icmp(const void *p1, const void *p2);
85
+ extern int icmp_risky(const void *p1, const void *p2);
86
+
87
+ extern int min2(int a, int b);
88
+ extern int min3(int a, int b, int c);
89
+ extern int max2(int a, int b);
90
+ extern int max3(int a, int b, int c);
91
+
92
+ extern char *dbl_to_s(char *buf, double num);
93
+ extern char *strfmt(const char *fmt, ...);
94
+ extern char *vstrfmt(const char *fmt, va_list args);
95
+
96
+ extern void micro_sleep(const int micro_seconds);
97
+
98
+ extern void register_for_cleanup(void *p, free_ft free_func);
99
+ extern void do_clean_up();
100
+
101
+ /**
102
+ * A dummy function which can be passed to functions which expect a free
103
+ * function such as h_new() if you don't want the free functions to do anything.
104
+ * This function will do nothing.
105
+ *
106
+ * @param p the object which this function will be called on.
107
+ */
108
+ extern void dummy_free(void *p);
109
+
110
+ #ifdef DEBUG
111
+ extern bool x_do_logging;
112
+ #define xlog if (x_do_logging) printf
113
+ #else
114
+ #define xlog()
115
+ #endif
116
+
117
+ #endif