1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* Ȥ줿ƥȤ鸡Ԥ
*/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <anthy/filemap.h>
#include <anthy/textdict.h>
#include "dic_main.h"
struct textdict {
char *fn;
char *ptr;
struct filemapping *mapping;
};
struct textdict *
anthy_textdict_open(const char *fn, int create)
{
struct textdict *td = malloc(sizeof(struct textdict));
if (!td) {
return NULL;
}
td->fn = strdup(fn);
if (!td->fn) {
free(td);
return NULL;
}
td->mapping = NULL;
return td;
}
static void
unmap(struct textdict *td)
{
if (td->mapping) {
anthy_munmap(td->mapping);
td->mapping = NULL;
}
}
void
anthy_textdict_close(struct textdict *td)
{
if (!td) {
return ;
}
unmap(td);
free(td->fn);
free(td);
}
static int
update_mapping(struct textdict *td)
{
if (td->mapping) {
anthy_munmap(td->mapping);
}
td->mapping = anthy_mmap(td->fn, 1);
if (!td->mapping) {
td->ptr = NULL;
return 1;
}
td->ptr = anthy_mmap_address(td->mapping);
return 0;
}
static int
expand_file(struct textdict *td, int len)
{
FILE *fp;
char buf[256];
int c;
fp = fopen(td->fn, "a+");
if (!fp) {
return -1;
}
memset(buf, '\n', 256);
c = 1;
if (len > 256) {
c *= fwrite(buf, 256, len / 256, fp);
}
if (len % 256) {
c *= fwrite(buf, len % 256, 1, fp);
}
fclose(fp);
if (c == 0) {
return -1;
}
return 0;
}
void
anthy_textdict_scan(struct textdict *td, int offset, void *ptr,
int (*fun)(void *, int, const char *, const char *))
{
FILE *fp;
char buf[1024];
if (!td) {
return ;
}
fp = fopen(td->fn, "r");
if (!fp) {
return ;
}
if (fseek(fp, offset, SEEK_SET)) {
fclose(fp);
return ;
}
while (fgets(buf, 1024, fp)) {
char *p = strchr(buf, ' ');
int len, r;
len = strlen(buf);
offset += len;
buf[len - 1] = 0;
if (!p) {
continue;
}
*p = 0;
p++;
while (*p == ' ') {
p++;
}
/* call it */
r = fun(ptr, offset, buf, p);
if (r) {
break;
}
}
fclose(fp);
}
int
anthy_textdict_delete_line(struct textdict *td, int offset)
{
FILE *fp;
char buf[1024];
int len, size;
fp = fopen(td->fn, "r");
if (!fp) {
return -1;
}
if (fseek(fp, offset, SEEK_SET)) {
fclose(fp);
return -1;
}
if (!fgets(buf, 1024, fp)) {
fclose(fp);
return -1;
}
len = strlen(buf);
fclose(fp);
update_mapping(td);
if (!td->mapping) {
return -1;
}
size = anthy_mmap_size(td->mapping);
memmove(&td->ptr[offset], &td->ptr[offset+len], size - offset - len);
unmap(td);
if (size - len == 0) {
unlink(td->fn);
return 0;
}
truncate(td->fn, size - len);
return 0;
}
int
anthy_textdict_insert_line(struct textdict *td, int offset,
const char *line)
{
int len = strlen(line);
int size;
if (!td) {
return -1;
}
if (expand_file(td, len)) {
return -1;
}
update_mapping(td);
size = anthy_mmap_size(td->mapping);
memmove(&td->ptr[offset+len], &td->ptr[offset], size - offset - len);
memcpy(&td->ptr[offset], line, len);
return 0;
}
|