Debian Codegraph @

modules/gcide/gcide.h

raw
1 /* This file is part of GNU Dico.
2 Copyright (C) 2012-2024 Sergey Poznyakoff
3
4 GNU Dico is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Dico is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "dico.h"
18
19 /* GCIDE-specific definitions. */
20 #define GCIDE_IDX_MAGIC "GCIDEIDX"
21 #define GCIDE_IDX_MAGIC_LEN (sizeof(GCIDE_IDX_MAGIC)-1)
22 #define GCIDE_IDX_VERSION 1
23 #define GCIDE_IDX_HEADER_PAGESIZE 10240
24 #define GCIDE_IDX_FILE_NAME "GCIDE.IDX"
25
26 struct gcide_idx_header {
27 char ihdr_magic[GCIDE_IDX_MAGIC_LEN];
28 int ihdr_version;
29 unsigned long ihdr_pagesize;
30 unsigned long ihdr_maxpageref;
31 unsigned long ihdr_num_pages;
32 unsigned long ihdr_num_headwords;
33 unsigned long ihdr_num_defs;
34 };
35
36 struct gcide_ref {
37 unsigned long ref_hwoff;
38 unsigned long ref_hwlen;
39 unsigned long ref_hwbytelen;
40 int ref_letter;
41 unsigned long ref_offset;
42 unsigned long ref_size;
43 char *ref_headword;
44 char ref_primary;
45 };
46
47 struct gcide_page_header {
48 size_t phdr_numentries;
49 size_t phdr_text_offset;
50 };
51
52 struct gcide_idx_page {
53 union {
54 struct gcide_page_header hdr;
55 struct gcide_ref align;
56 } ipg_header;
57 struct gcide_ref ipg_ref[1];
58 };
59
60 typedef struct gcide_idx_file *gcide_idx_file_t;
61 typedef struct gcide_iterator *gcide_iterator_t;
62
63 struct gcide_locus {
64 char const *file;
65 size_t offset;
66 };
67
68 enum {
69 IDXE_OK,
70 IDXE_BADFILE,
71 IDXE_BADVER,
72 IDXE_CORRUPT,
73 IDXE_SYSERR
74 };
75
76 int gcide_idx_file_open(const char *name, size_t cachesize, int rw,
77 struct gcide_idx_file **ret_file);
78 void gcide_idx_file_close(gcide_idx_file_t file);
79
80 int gcide_idx_lock(struct gcide_idx_file *file, int ex);
81 int gcide_idx_unlock(struct gcide_idx_file *file);
82 int gcide_idx_reopen(struct gcide_idx_file *file);
83 int gcide_idx_fileno(struct gcide_idx_file *file);
84
85 size_t gcide_idx_headwords(struct gcide_idx_file *file);
86 size_t gcide_idx_defs(struct gcide_idx_file *file);
87
88 int gcide_idx_enumerate(struct gcide_idx_file *file,
89 int (*fun)(struct gcide_ref *, void *),
90 void *data);
91
92 gcide_iterator_t gcide_idx_locate(struct gcide_idx_file *file, char *headword,
93 size_t len);
94 void gcide_iterator_free(gcide_iterator_t itr);
95 int gcide_iterator_next(gcide_iterator_t itr);
96 int gcide_iterator_rewind(gcide_iterator_t itr);
97 struct gcide_ref *gcide_iterator_ref(gcide_iterator_t itr);
98 size_t gcide_iterator_count(gcide_iterator_t itr);
99 size_t gcide_iterator_compare_count(gcide_iterator_t itr);
100 void gcide_iterator_store_flags(gcide_iterator_t itr, int flags);
101 int gcide_iterator_flags(gcide_iterator_t itr);
102
103 extern char gcide_webchr[256][4];
104 char const *gcide_escape_to_utf8(const char *esc);
105 char const *gcide_entity_to_utf8(const char *str);
106
107 const char *gcide_grk_to_utf8(const char *input, size_t *prd);
108
109
110 enum gcide_content_type
111 {
112 gcide_content_top, /* Top-level element: same as gcide_content_tag, but
113 with tag_parmc == 0 and tag_parmv == NULL. */
114 gcide_content_tag, /* Regular tag. */
115 gcide_content_text, /* Text segment. */
116 gcide_content_nl, /* "Soft" newline. */
117 gcide_content_br /* Line break. */
118 };
119
120 struct gcide_tag {
121 size_t offset;
122 enum gcide_content_type tag_type;
123 struct gcide_tag *tag_next;
124 # define tag_name v.tag.tag_parmv[0]
125 int delete;
126 union {
127 char *text;
128 size_t textpos;
129 struct {
130 size_t tag_parmc;
131 char **tag_parmv;
132 dico_list_t taglist;
133 } tag;
134 } v;
135 };
136
137 struct gcide_parse_tree {
138 char *textspace;
139 size_t textsize;
140 struct gcide_tag *root;
141 };
142
143 struct gcide_parse_tree *gcide_markup_parse(char const *text, size_t len,
144 int dbg, struct gcide_locus *loc);
145 void gcide_parse_tree_free(struct gcide_parse_tree *tp);
146 int gcide_parse_tree_inorder(struct gcide_parse_tree *tp,
147 int (*fun)(int, struct gcide_tag *, void *),
148 void *data);
149
150 int gcide_is_block_tag(struct gcide_tag *tag);
151
152 struct gcide_tag *gcide_tag_alloc(const char *text, size_t len);
153 void gcide_tag_free(struct gcide_tag *tag);
154 int gcide_is_tag(struct gcide_tag *tag, char const *name);
155
156 typedef struct pollfd *WATCHER;
157
158 #if WITH_INOTIFY
159 WATCHER watcher_setup(char const *dbdir);
160 void watcher_close(WATCHER w);
161 int watcher_is_modified(WATCHER w);
162 #else
163 # define watcher_setup(dbdir) NULL
164 # define watcher_close(w)
165 # define watcher_is_modified(w) 1
166 #endif
167
168 #ifndef SIZE_MAX
169 # define SIZE_MAX ((size_t)-1)
170 #endif