File: skk.c

package info (click to toggle)
sumika 0.11-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,780 kB
  • ctags: 1,977
  • sloc: ansic: 14,857; sh: 9,050; makefile: 550; yacc: 316; sed: 16
file content (182 lines) | stat: -rw-r--r-- 3,601 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "skk.h"

struct _skkdic_info {
    int fd;
    char *data;
    size_t len;
    int first;
    int boarder;
//    struct skk_line head;
} *skk_dic;

static char *get_skkdicfullpath(void) {
    char *homedirectory, *skkdicfullpath;
    char skkdicname[] = ".skk-jisyo";

    homedirectory = getenv("HOME");
    if(!homedirectory) {
	struct passwd *pw;
	pw = getpwuid(getuid());
	homedirectory = strdup(pw->pw_dir);
	free(pw);
    }

    skkdicfullpath = malloc(strlen(homedirectory) + strlen(skkdicname) + 2);
    if(skkdicfullpath != NULL) {
	snprintf(skkdicfullpath,
		 strlen(homedirectory) + strlen(skkdicname) + 2,
		 "%s/%s", homedirectory, skkdicname);
    }
    return skkdicfullpath;
}

int read_skk_dic(word **head) {
    FILE *fp;
    int fd;
    struct stat st;
    char *ptr;
    void *addr;
    int len, buflen;
    char *skkdicfullpath;
    char buf[256], desc[256], phon[256];
    int okuri = 0;

    skkdicfullpath = get_skkdicfullpath();
    if(skkdicfullpath == NULL) {
	*head = NULL;
	return -1;
    }

    if(lstat(skkdicfullpath, &st) == -1) {
	free(skkdicfullpath);
	return -1;
    }
/*
    fd = open(skkdicfullpath, O_RDONLY);
    if(fd == -1) {
	free(skkdicfullpath);
	return -1;
    }

    addr = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
    if(addr == MAP_FAILED) {
	free(skkdicfullpath);
	return -1;
    }
    close(fd);

    *ptr = addr;

    for(i = 0; i < len; i++) {
       buflen = read_line(ptr, buf, sizeof(buf));
       if(buf != NULL)
       ptr += buflen;
    }

    munmap(addr, st.st_size);
*/
    fp = fopen(skkdicfullpath, "r");

    free(skkdicfullpath);

    if(!fp) {
	*head = NULL;
	return -1;
    }

    *head = NULL;
    while(fgets(buf, sizeof(buf), fp)) {
	if(buf[0] != '#' && buf[0] != ';') {
	    sscanf(buf, "%s %s", phon, desc); /* XXX */

	    word_append(head, WORD_TYPE_SKK,
			phon, desc, NULL, 0, okuri, NULL); /* XXX */
	} else {
	    if(strstr(buf, "okuri-ari")) {
		okuri = 0;
	    }
	    if(strstr(buf, "okuri-nasi")) {
		okuri = 1;
	    }
	}
    }
    fclose(fp);
    return 0;

}

int write_skk_dic(word *head) {
    char *skkdicfullpath;
    word *pos;
    FILE *fp;
    int fd;
    struct flock lock;

    skkdicfullpath = get_skkdicfullpath();
    if(skkdicfullpath == NULL) {
	return -1;
    }

    fp = fopen(skkdicfullpath, "w");
    free(skkdicfullpath);
    if(!fp) {
	return -1;
    }

    fd = fileno(fp);
    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;

    if(fcntl(fd, F_SETLK, &lock) < 0) {
	fprintf(stderr, "Lock failed: %s\n", strerror(errno));
	fclose(fp);
	return -1;
    }

    fprintf(fp, ";; okuri-ari entries.\n");
    for(pos = head; pos != NULL; pos = pos->next) {
	if(pos->okuri == 0)
	    fprintf(fp, "%s %s\n", pos->phon, pos->desc);
    }
    fprintf(fp, ";; okuri-nasi entries.\n");
    for(pos = head; pos != NULL; pos = pos->next) {
	if(pos->okuri == 1)
	    fprintf(fp, "%s %s\n", pos->phon, pos->desc);
    }

    lock.l_type = F_UNLCK;
    if(fcntl(fd, F_SETLK, &lock) < 0) {
	fprintf(stderr, "Unlock failed: %s\n", strerror(errno));
	fclose(fp);
	return -1;
    }

    fclose(fp);
    return 0;
}
/*
int read_line(char *ptr, ptrlen, char *buf, int len) {
    int i = 0;
    for(i = 0; i < len; i++, ptr++) {
	if(i >= ptrlen) break;
	if(*ptr == '\0' || *ptr == '\n') {
	    buf[i] = '\0';
	    i++;
	    break;
	}
	buf[i] = *ptr;
    }
    return i;
}
*/