File: xjdrad.c

package info (click to toggle)
xjdic 24-13
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,080 kB
  • sloc: ansic: 23,275; makefile: 191; sh: 55
file content (239 lines) | stat: -rw-r--r-- 6,093 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**************************************************************************
*                          X J D R A D                                    *
*          Displays Radical Table for Lookup Usage                        *
*         Japanese-English Dictionary program (X11 version)               *
*                                                   Author: Jim Breen     *
***************************************************************************/
/*  This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>

#include "xjdic.h"

#define RADPERLINE 20

struct stat *xbuf;

unsigned char RKname[50] = {"radkfile"};
unsigned char IRKname[50] = {"radkfile"};
unsigned char DicDir[100];
unsigned char sver[] = {SVER};
unsigned char ENVname[50];
unsigned char testline[200];
unsigned char RVon[] = {0x1b,'[','7','m',0};
unsigned char RVoff[] = {0x1b,'[','m',0};

FILE  *xfopen(char *file_name, char *file_mode, int *xfilelen);
void xjdicrc();
void RadDisp();

/*====stringcomp==stricmp & strcasecmp pulled together=========*/
/*    (my own routine, because different systems need one or the other    */
int stringcomp(unsigned char *s1, unsigned char *s2)
{	
	int i;	 unsigned char c1,c2;
	
	for(i = 0; i < strlen(s1);i++)
	{
		c1 = s1[i];
		if (c1 < 0x60) c1 = (c1|0x20);
		c2 = s2[i];
		if (c2 < 0x60) c2 = (c2|0x20);
		if (c1 != c2) return(1);
	}
	return (0);
}
/*=====RadDisp===Display Radical Data==============================*/
void RadDisp()
{
	int i,j,k,l,flen;
	FILE *fk;
	unsigned char *ptr;

	fk = xfopen(RKname,"r",&flen);
	k=99;
	j = 0;
	printf("RADICAL TABLE FOR USE WITH XJDIC RADICAL LOOKUP FUNCTION\n\n");
	while(!feof(fk))
	{
		fgets(testline,199,fk);
		if(feof(fk)) break;
		testline[strlen(testline)-1] = 0;
		if (testline[0] != '$') continue;
		ptr = strtok(testline+4," ");
		l = atoi(ptr);
		if (l != k)
		{
			k = l;
			printf(" %s%s%s ",RVon,ptr,RVoff);
			if (k < 10) printf(" ");
			if(++j % RADPERLINE == 0) printf("\n");
		}
		printf(" %c%c ",testline[2],testline[3]);
		if(++j % RADPERLINE == 0) printf("\n");
	}
	fclose(fk);
	printf("\n");
}
/*+++++++++ xfopen +++ does a fopen, but tries the dicdir first +++*/
/*
	xfopen generalizes the dictionary and other file opening, by:

		trying the "dicdir", and then current directories
		returning the file length as well as the pointer to FILE
								*/
FILE  *xfopen(char *file_name, char *file_mode, int *xfilelen)
{
	FILE *fx;

	char *fnbuff;

/* printf ("XFOPEN: fn=%s mode=%s stream_p=%p\n",file_name,file_mode,fx); */
	fnbuff = (char *)malloc(strlen(DicDir) + strlen(file_name)+10);
	if (fnbuff == NULL)
	{
		printf("malloc failure opening: %s\n",file_name);
		exit(1);
	}
	strcpy(fnbuff,DicDir);
	if (fnbuff[strlen(fnbuff)-1] != '/') strcat (fnbuff,"/");
	strcat(fnbuff,file_name);
	fx = fopen(fnbuff,file_mode);
	if (fx != NULL)
	{
		if(stat(fnbuff, xbuf) != 0)
		{
			printf ("Stat() error (l)for %s [%s]\n",fnbuff,strerror(errno));
			exit(1);
		}
		*xfilelen = (xbuf->st_size);
		free(fnbuff);
/* printf ("XFOPEN: stream_p=%p addr = %p\n",fx,&fx); */
		return(fx);
	}
	fx = fopen(file_name,file_mode);
	if (fx != NULL) 
	{
		if(stat(file_name, xbuf) != 0)
		{
			printf ("Stat() error (s) for %s [%s]\n",file_name,strerror(errno));
			exit(1);
		}
		*xfilelen = xbuf->st_size;
/* printf ("XFOPEN: stream_p=%p addr = %p\n",fx,&fx); */
		return(fx);
	}
	printf("Unable to open: %s\n",file_name);
	exit(1);
}
/*=====xjdicrc - access and analyze "xjdicrc" file (if any)==============*/
void xjdicrc()
{
	unsigned char xjdicdir[256],rcstr[80],*rcwd;
	int ft,fn;
	FILE *fm;

	xjdicdir[0] = '\0';
	if (strlen(ENVname) > 2)
	{
		if (getenv("HOME") != NULL) {
			strcpy(xjdicdir,ENVname);
			strcat(xjdicdir,"/");
		}
	}
	else    
	{
		strcpy(xjdicdir,getenv("HOME"));
		strcat(xjdicdir,"/");
	}

	strcat(xjdicdir,".xjdicrc");
	fm = fopen(xjdicdir,"r");
	if (fm == NULL)
	{
		strcpy(xjdicdir,".xjdicrc");
		fm = fopen(xjdicdir,"r");
	}
	if (fm == NULL && getenv("HOME") != NULL)
	{
		strcpy(xjdicdir,getenv("HOME"));
		strcat(xjdicdir,"/");
		strcat(xjdicdir,".xjdicrc");
		fm = fopen(xjdicdir,"r");
	}
	if (fm != NULL)
	{
		while(fgets(rcstr,79,fm) != NULL)
		{
			rcwd = (unsigned char *)strtok(rcstr," \t");
			if( stringcomp((unsigned char *)"dicdir",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(DicDir,rcwd);
				continue;
			}
			if( stringcomp((unsigned char *)"radkfile",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(RKname,rcwd);
				continue;
			}
		}
	}
	else
	{
		printf("No .xjdicrc file detected\n");
		return;
	}
	fclose(fm);
}

/*                  M A I N                                      */

int main()
{
	int i,j;
  	unsigned char *dicenv,strtmp[50];
	
  	printf("XJDRAD version %s (Japanese Dictionary) Copyright J.W.Breen 2003.\n",sver);

	xbuf = (void *)malloc(sizeof(struct stat));
	dicenv = (unsigned char *)getenv("XJDIC");
	if (!dicenv) dicenv = (unsigned char *)DEFAULT_DICDIR;
	if (strlen(dicenv) <= 2)
	{
		dicenv = (unsigned char *)getcwd(ENVname,sizeof(ENVname));
		if (dicenv == NULL) 
		{
			printf("Cannot extract working directory!\n");
			exit(1);
		}
	}
	else
	{
		strcpy (ENVname,dicenv);
        }
        sprintf(RKname, "%s/%s", dicenv, IRKname);
	xjdicrc();
	RadDisp();
}