2 * ¥½¡¼¥È¤µ¤ì¤¿¥Æ¥¥¹¥È¤«¤é¸¡º÷¤ò¹Ô¤¦
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <anthy/filemap.h>
24 #include <anthy/textdict.h>
30 struct filemapping *mapping;
34 anthy_textdict_open(const char *fn, int create)
36 struct textdict *td = malloc(sizeof(struct textdict));
51 unmap(struct textdict *td)
54 anthy_munmap(td->mapping);
60 anthy_textdict_close(struct textdict *td)
71 update_mapping(struct textdict *td)
74 anthy_munmap(td->mapping);
76 td->mapping = anthy_mmap(td->fn, 1);
81 td->ptr = anthy_mmap_address(td->mapping);
86 expand_file(struct textdict *td, int len)
91 fp = fopen(td->fn, "a+");
95 memset(buf, '\n', 256);
98 c *= fwrite(buf, 256, len / 256, fp);
101 c *= fwrite(buf, len % 256, 1, fp);
111 anthy_textdict_scan(struct textdict *td, int offset, void *ptr,
112 int (*fun)(void *, int, const char *, const char *))
119 fp = fopen(td->fn, "r");
123 if (fseek(fp, offset, SEEK_SET)) {
127 while (fgets(buf, 1024, fp)) {
128 char *p = strchr(buf, ' ');
142 r = fun(ptr, offset, buf, p);
151 anthy_textdict_delete_line(struct textdict *td, int offset)
156 fp = fopen(td->fn, "r");
160 if (fseek(fp, offset, SEEK_SET)) {
164 if (!fgets(buf, 1024, fp)) {
174 size = anthy_mmap_size(td->mapping);
175 memmove(&td->ptr[offset], &td->ptr[offset+len], size - offset - len);
177 if (size - len == 0) {
181 truncate(td->fn, size - len);
186 anthy_textdict_insert_line(struct textdict *td, int offset,
189 int len = strlen(line);
194 if (expand_file(td, len)) {
198 size = anthy_mmap_size(td->mapping);
199 memmove(&td->ptr[offset+len], &td->ptr[offset], size - offset - len);
200 memcpy(&td->ptr[offset], line, len);