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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*--------------------------------------------------------------*
* *
* SUFARY --- Suffix Array ΤΥ饤֥ *
* *
* chfile.c - եγĽ *
* *
*--------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
#include <process.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include "sufary.h"
/* %%%%% ؿƱΤδط %%%%%
sa_openfiles()
+- sa_opentextfile()
+- sa_openarrayfile()
sa_cloasefiles()
+- sa_closetextfile()
+- sa_closearrayfile()
%%%%%%%%%%%%%%%%%%%%%%%% */
/**********************************************
* SUFARY *sa_openfiles(char *s, char *t);
*
* purpose
* ꤵ줿ƥȥե(s)arrayե(t)
* arrayե̾NULL˻ꤹСƥȥե̾
* '.ary' ղäΤarrayե̾ˤʤ롣
*
* parameters
* t : arrayե̾
* s : ƥȥե̾
*
* return value
* 줿SUFARYѿ
**********************************************/
SUFARY *sa_openfiles(s, t)
char *s, *t;
{
SUFARY *newary;
int tfd, afd;
char aryname[8192];
newary = (SUFARY *)calloc(sizeof(SUFARY), 1);
if (!newary){
/* ݼ */
fprintf(stderr,"new array allocation failed.\n");
exit (1);
/* return NULL; */
}
/* ե̾γǼ 980126 */
strcpy(newary->filename,s);
if (t == NULL){
/* 1 : array file ꤵƤʤ */
if (sa_opentextfile(newary, s) == ERROR){
/* ץ顼 */
free(newary);
return NULL;
}
#if 0
/* ary Υե̾ */
/* t = malloc(strlen(s) + 4); */
/* allocaϤδؿǤΤͭmallocư˴롣 */
aryname = (char *)alloca(strlen(s) + 4);
if ( aryname == NULL ){
fprintf(stderr,"array filename allocation failed.\n");
exit(1);
}
#endif
sprintf(aryname, "%s.ary", s); /* (Rel1.4: .pat -> .ary) */
if (sa_openarrayfile(newary, aryname) == ERROR){
sa_closetextfile(newary);
free(newary);
return NULL;
}
/* free(t); */
} else {
if (sa_opentextfile(newary, s) == ERROR){
free(newary);
return NULL;
}
if (sa_openarrayfile(newary, t) == ERROR){
sa_closetextfile(newary); /* ? */
free(newary);
return NULL;
}
}
return newary;
}
/**********************************************
* eresult sa_opentextfile(SUFARY *ary,char *s);
*
* purpose
* ƥȥե
*
* parameters
* ary : ե(ʤ)ǼSUFARYѿ
* s : ƥȥե̾
*
* return value
* / (eresult)
**********************************************/
eresult sa_opentextfile(ary, s)
SUFARY *ary;
char *s;
{
off_t sz; /* ե륵 */
struct stat st;
int fd;
caddr_t map;
/* ˥ץƤΤХ */
if (ary->txtfd > 0 || ary->txtmap){
sa_closetextfile(ary);
}
/* եΥץ */
if ((fd = open(s, O_RDONLY)) < 0){
printf("cannot open text file '%s'.\n",s);
return ERROR;
}
fstat(fd, &st);
sz = st.st_size;
#ifdef NO_MMAP
map = (caddr_t)malloc(sz);
read(fd, map, sz);
#else
/* ޥå use mmap */
if ((map = mmap((caddr_t)0, sz, PROT_READ, MAP_SHARED, fd, 0))
== (caddr_t)-1 ){
printf ("file mapping error. '%s'\n",s);
return ERROR;
}
#endif
/* ΤǥǡǼreturn */
ary->txtfd = fd;
ary->txtsz = sz;
ary->txtmap = map;
return CONT;
}
/**********************************************
* eresult sa_openarrayfile(SUFARY *ary, char *s);
*
* purpose
* Array ե
*
* parameters
* ary : ե(ʤ)ǼSUFARYѿ
* s : Array ե̾
*
* return value
* / (eresult)
**********************************************/
eresult sa_openarrayfile(ary, s)
SUFARY *ary;
char *s;
{
off_t sz; /* ե륵 */
FILE *patfile;
struct stat st;
int fd;
caddr_t map;
/* ˥ץƤΤХ */
if (ary->aryfd > 0 || ary->arymap){
sa_closearrayfile(ary);
}
if ((fd = open(s, O_RDONLY)) < 0){
printf("cannot open array file '%s'.\n",s);
return ERROR;
}
fstat(fd, &st);
sz = st.st_size;
#ifdef NO_MMAP
map = (caddr_t)malloc(sz);
read(fd, map, sz);
#else
/* ޥå */
if ((map = mmap((caddr_t)0, sz, PROT_READ, MAP_SHARED, fd, 0))
== (caddr_t)-1 ){
printf ("file mapping error. '%s'\n",s);
return ERROR;
}
#endif
/* arrayեΥΤ뤿ν */
/* Ĥäfdopen()ˤǤʤΤʡ tomoak-i */
if ((patfile = fopen(s, "r")) == NULL){
printf("cannot open array file '%s'.\n",s);
return ERROR;
}
fseek(patfile, 0L, 2);
ary->arraysize = ftell(patfile) / sizeof(long); /* 970715 */
/* Ĥ */
fclose(patfile);
ary->aryfd = fd;
ary->arysz = sz;
ary->arymap = ary->aryorig = map;
ary->left = 0;
ary->right = ary->arraysize - 1;
/* left, right ϸϰϤ¦ؤ 980319 */
return CONT;
}
/**********************************************
* void sa_closefiles(SUFARY *ary);
*
* purpose
* ꤵ줿եĤ
*
* parameters
* ary : Ĥե˴ؤSUFARYѿ
*
* return value
* ʤ
*
* description
* ƥȥեarrayեƱĤ
**********************************************/
void sa_closefiles(ary)
SUFARY *ary;
{
sa_closetextfile(ary);
sa_closearrayfile(ary);
free(ary);
ary = NULL;
return;
}
/**********************************************
* void sa_closetextfile(SUFARY *ary);
*
* purpose
* ꤵ줿ƥȥեĤ
*
* parameters
* ary : Ĥե˴ؤSUFARYѿ
*
* return value
* ʤ
**********************************************/
void sa_closetextfile(ary)
SUFARY *ary;
{
if (ary->txtfd != -1){
/* mmap β */
#ifdef NO_MMAP
free(ary->txtmap);
#else
munmap(ary->txtmap, ary->txtsz);
#endif
close(ary->txtfd);
ary->txtmap = NULL;
ary->txtfd = -1;
ary->txtsz = 0;
}
return;
}
/**********************************************
* void sa_closearrayfile(void);
*
* purpose
* ꤵ줿arrayեĤ륯
*
* parameters
* ary : Ĥե˴ؤSUFARYѿ
*
* return value
* ʤ
**********************************************/
void sa_closearrayfile(ary)
SUFARY *ary;
{
/* ⡧mmap Ĥɬפ뤫 */
if (ary->aryfd != -1){
/* mmap β */
#ifdef NO_MMAP
free(ary->txtmap);
#else
munmap(ary->txtmap, ary->txtsz);
#endif
close(ary->aryfd);
ary->arymap = NULL;
ary->aryorig = NULL;
ary->aryfd = -1;
ary->arysz = 0;
}
ary->arraysize = 0;
ary->left = 0;
ary->right = 0;
return;
}
|