Make SJ4 more portable
This commit is contained in:
parent
df18e66b18
commit
201d9dba0a
7 changed files with 50 additions and 84 deletions
|
|
@ -5,7 +5,15 @@
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <io.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
int sj4_snprintf(char*, size_t, const char*, ...);
|
int sj4_snprintf(char*, size_t, const char*, ...);
|
||||||
size_t sj4_strlcpy(char*, const char*, size_t);
|
size_t sj4_strlcpy(char*, const char*, size_t);
|
||||||
|
int sj4_ftruncate(FILE*, size_t);
|
||||||
|
int sj4_unlink(const char*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,6 @@ typedef struct dictfile {
|
||||||
#endif
|
#endif
|
||||||
FILE* fp;
|
FILE* fp;
|
||||||
VFile vf;
|
VFile vf;
|
||||||
int fd;
|
|
||||||
|
|
||||||
u_char* buffer;
|
u_char* buffer;
|
||||||
long bufsiz;
|
long bufsiz;
|
||||||
|
|
@ -144,7 +143,6 @@ typedef struct stdyfile {
|
||||||
int refcnt;
|
int refcnt;
|
||||||
ino_t inode;
|
ino_t inode;
|
||||||
FILE* fp;
|
FILE* fp;
|
||||||
int fd;
|
|
||||||
|
|
||||||
u_char* header;
|
u_char* header;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,6 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#ifdef _WIN32
|
|
||||||
#include <io.h>
|
|
||||||
#else
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "sj_kcnv.h"
|
#include "sj_kcnv.h"
|
||||||
#include "sj_struct.h"
|
#include "sj_struct.h"
|
||||||
|
|
@ -235,9 +230,6 @@ extern u_char kigou[];
|
||||||
/* sdepend.c */
|
/* sdepend.c */
|
||||||
DictFile* opendict(SJ4_CONTEXT char*, char*);
|
DictFile* opendict(SJ4_CONTEXT char*, char*);
|
||||||
int closedict(SJ4_CONTEXT DictFile*);
|
int closedict(SJ4_CONTEXT DictFile*);
|
||||||
void lock_dict(DictFile*, int);
|
|
||||||
void unlock_dict(DictFile*, int);
|
|
||||||
int is_dict_locked(DictFile*);
|
|
||||||
StdyFile* openstdy(SJ4_CONTEXT char*, char*);
|
StdyFile* openstdy(SJ4_CONTEXT char*, char*);
|
||||||
int closestdy(SJ4_CONTEXT StdyFile*);
|
int closestdy(SJ4_CONTEXT StdyFile*);
|
||||||
int putstydic(SJ4_CONTEXT2);
|
int putstydic(SJ4_CONTEXT2);
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@
|
||||||
/* increase this as needed... */
|
/* increase this as needed... */
|
||||||
#define SJ4BUFSZ (4 * 1024)
|
#define SJ4BUFSZ (4 * 1024)
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef union sj4kouhobuffer {
|
typedef union sj4kouhobuffer {
|
||||||
char sjis[SJ4BUFSZ];
|
char sjis[SJ4BUFSZ];
|
||||||
char eucjp[SJ4BUFSZ];
|
char eucjp[SJ4BUFSZ];
|
||||||
|
|
@ -34,4 +38,8 @@ int sj4_getkan(Sj4Lib*, const void*, int len, Sj4Kouho*);
|
||||||
int sj4_nextkan(Sj4Lib*);
|
int sj4_nextkan(Sj4Lib*);
|
||||||
void sj4_close(Sj4Lib*);
|
void sj4_close(Sj4Lib*);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
9
lib/sj4common/ftruncate.c
Normal file
9
lib/sj4common/ftruncate.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "sj4compat.h"
|
||||||
|
|
||||||
|
int sj4_ftruncate(FILE* fp, size_t sz) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
return chsize(fileno(fp), sz);
|
||||||
|
#else
|
||||||
|
return ftruncate(fileno(fp), sz);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
5
lib/sj4common/unlink.c
Normal file
5
lib/sj4common/unlink.c
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#include "sj4compat.h"
|
||||||
|
|
||||||
|
int sj4_unlink(const char* path) {
|
||||||
|
return unlink(path);
|
||||||
|
}
|
||||||
|
|
@ -56,10 +56,6 @@
|
||||||
#define L_END SEEK_END
|
#define L_END SEEK_END
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#define ftruncate chsize
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SJ4_GLOBAL
|
#ifdef SJ4_GLOBAL
|
||||||
DictFile* dictlink = NULL;
|
DictFile* dictlink = NULL;
|
||||||
StdyFile* stdylink = NULL;
|
StdyFile* stdylink = NULL;
|
||||||
|
|
@ -129,30 +125,6 @@ fputfile(FILE* fp, long pos, int len, u_char* p) {
|
||||||
}
|
}
|
||||||
return SJ4_NormalEnd;
|
return SJ4_NormalEnd;
|
||||||
}
|
}
|
||||||
static int
|
|
||||||
getfile(int fd, int pos, int len, u_char* p) {
|
|
||||||
if(lseek(fd, pos, L_SET) == ERROR) {
|
|
||||||
serv_errno = SJ4_FileSeekError;
|
|
||||||
return ERROR;
|
|
||||||
}
|
|
||||||
if(read(fd, p, len) != len) {
|
|
||||||
serv_errno = SJ4_FileReadError;
|
|
||||||
return ERROR;
|
|
||||||
}
|
|
||||||
return SJ4_NormalEnd;
|
|
||||||
}
|
|
||||||
static int
|
|
||||||
putfile(int fd, int pos, int len, u_char* p) {
|
|
||||||
if(lseek(fd, pos, L_SET) == ERROR) {
|
|
||||||
serv_errno = SJ4_FileSeekError;
|
|
||||||
return ERROR;
|
|
||||||
}
|
|
||||||
if(write(fd, p, len) != len) {
|
|
||||||
serv_errno = SJ4_FileWriteError;
|
|
||||||
return ERROR;
|
|
||||||
}
|
|
||||||
return SJ4_NormalEnd;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
check_passwd(u_char* buf, char* passwd) {
|
check_passwd(u_char* buf, char* passwd) {
|
||||||
|
|
@ -197,7 +169,7 @@ getdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
||||||
}
|
}
|
||||||
static int
|
static int
|
||||||
putidx(SJ4_CONTEXT DictFile* dp, short dummy) {
|
putidx(SJ4_CONTEXT DictFile* dp, short dummy) {
|
||||||
return putfile(dp->fd, dp->idxstrt, dp->dict.idxlen, idxbuf);
|
return fputfile(dp->fp, dp->idxstrt, dp->dict.idxlen, idxbuf);
|
||||||
}
|
}
|
||||||
static int
|
static int
|
||||||
putdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
putdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
||||||
|
|
@ -224,7 +196,7 @@ putdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
||||||
i = dp->segstrt + dp->dict.seglen * seg;
|
i = dp->segstrt + dp->dict.seglen * seg;
|
||||||
if((p = dp->buffer + i) != dicbuf) memcpy(p, dicbuf, dp->dict.seglen);
|
if((p = dp->buffer + i) != dicbuf) memcpy(p, dicbuf, dp->dict.seglen);
|
||||||
|
|
||||||
return putfile(dp->fd, i, dp->dict.seglen, p);
|
return fputfile(dp->fp, i, dp->dict.seglen, p);
|
||||||
}
|
}
|
||||||
static int
|
static int
|
||||||
rszdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
rszdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
||||||
|
|
@ -245,10 +217,10 @@ rszdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
|
||||||
free((char*)(dp->buffer));
|
free((char*)(dp->buffer));
|
||||||
dp->buffer = p;
|
dp->buffer = p;
|
||||||
dp->bufsiz = i;
|
dp->bufsiz = i;
|
||||||
if(ftruncate(dp->fd, i) == -1) return -1;
|
if(sj4_ftruncate(dp->fp, i) == -1) return -1;
|
||||||
}
|
}
|
||||||
put4byte(dp->buffer + DICTSEGNUM, seg);
|
put4byte(dp->buffer + DICTSEGNUM, seg);
|
||||||
return putfile(dp->fd, 0, HEADERLENGTH, dp->buffer);
|
return fputfile(dp->fp, 0, HEADERLENGTH, dp->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
DictFile*
|
DictFile*
|
||||||
|
|
@ -335,7 +307,6 @@ opendict(SJ4_CONTEXT char* name, char* passwd) {
|
||||||
dfp->refcnt = 1;
|
dfp->refcnt = 1;
|
||||||
dfp->fp = fp;
|
dfp->fp = fp;
|
||||||
dfp->vf = vf;
|
dfp->vf = vf;
|
||||||
dfp->fd = vf.buffer == NULL ? fileno(fp) : -1;
|
|
||||||
dfp->buffer = dp;
|
dfp->buffer = dp;
|
||||||
dfp->bufsiz = sbuf.st_size;
|
dfp->bufsiz = sbuf.st_size;
|
||||||
dfp->idxstrt = get4byte(dp + DICTIDXPOS);
|
dfp->idxstrt = get4byte(dp + DICTIDXPOS);
|
||||||
|
|
@ -389,30 +360,6 @@ int closedict(SJ4_CONTEXT DictFile* dfp) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SJ4_LOCK
|
|
||||||
static fd_set zero_fd_set; /* XXX */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void lock_dict(DictFile* p, int fd) {
|
|
||||||
#ifdef SJ4_LOCK
|
|
||||||
FD_SET(fd, &(p->lock));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlock_dict(DictFile* p, int fd) {
|
|
||||||
#ifdef SJ4_LOCK
|
|
||||||
FD_CLR(fd, &(p->lock));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int is_dict_locked(DictFile* p) {
|
|
||||||
#ifdef SJ4_LOCK
|
|
||||||
return memcmp(&(p->lock), &zero_fd_set, sizeof(zero_fd_set));
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
check_stdyfile(u_char* buf) {
|
check_stdyfile(u_char* buf) {
|
||||||
return (STDYVERSION != get4byte(buf + VERSIONPOS)) ? FALSE : TRUE;
|
return (STDYVERSION != get4byte(buf + VERSIONPOS)) ? FALSE : TRUE;
|
||||||
|
|
@ -521,7 +468,6 @@ openstdy(SJ4_CONTEXT char* name, char* passwd) {
|
||||||
sfp->refcnt = 1;
|
sfp->refcnt = 1;
|
||||||
sfp->inode = sbuf.st_ino;
|
sfp->inode = sbuf.st_ino;
|
||||||
sfp->fp = fp;
|
sfp->fp = fp;
|
||||||
sfp->fd = fileno(fp);
|
|
||||||
sfp->header = hd;
|
sfp->header = hd;
|
||||||
|
|
||||||
sfp->link = stdylink;
|
sfp->link = stdylink;
|
||||||
|
|
@ -572,13 +518,13 @@ int closestdy(SJ4_CONTEXT StdyFile* sfp) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int putstydic(SJ4_CONTEXT2) {
|
int putstydic(SJ4_CONTEXT2) {
|
||||||
int fd;
|
FILE* fp;
|
||||||
u_char* hd;
|
u_char* hd;
|
||||||
StdyFile* sf;
|
StdyFile* sf;
|
||||||
long len;
|
long len;
|
||||||
|
|
||||||
sf = (StdyFile*)stdy_base;
|
sf = (StdyFile*)stdy_base;
|
||||||
fd = sf->fd;
|
fp = sf->fp;
|
||||||
hd = sf->header;
|
hd = sf->header;
|
||||||
|
|
||||||
put4byte(hd + STDYNORMCNT, sf->stdy.stdycnt);
|
put4byte(hd + STDYNORMCNT, sf->stdy.stdycnt);
|
||||||
|
|
@ -586,25 +532,25 @@ int putstydic(SJ4_CONTEXT2) {
|
||||||
len = sizeof(STDYIN) * sf->stdy.stdymax;
|
len = sizeof(STDYIN) * sf->stdy.stdymax;
|
||||||
put4byte(hd + STDYNORMLEN, len);
|
put4byte(hd + STDYNORMLEN, len);
|
||||||
|
|
||||||
if(putfile(fd, 0, HEADERLENGTH + COMMENTLENGTH, hd)) return ERROR;
|
if(fputfile(fp, 0, HEADERLENGTH + COMMENTLENGTH, hd)) return ERROR;
|
||||||
|
|
||||||
return putfile(fd, get4byte(hd + STDYNORMPOS), len, (u_char*)sf->stdy.stdydic);
|
return fputfile(fp, get4byte(hd + STDYNORMPOS), len, (u_char*)sf->stdy.stdydic);
|
||||||
}
|
}
|
||||||
|
|
||||||
int putcldic(SJ4_CONTEXT2) {
|
int putcldic(SJ4_CONTEXT2) {
|
||||||
int fd;
|
FILE* fp;
|
||||||
u_char* hd;
|
u_char* hd;
|
||||||
StdyFile* sf;
|
StdyFile* sf;
|
||||||
|
|
||||||
sf = (StdyFile*)stdy_base;
|
sf = (StdyFile*)stdy_base;
|
||||||
fd = sf->fd;
|
fp = sf->fp;
|
||||||
hd = sf->header;
|
hd = sf->header;
|
||||||
|
|
||||||
if(putfile(fd, get4byte(hd + STDYCLIDXPOS),
|
if(fputfile(fp, get4byte(hd + STDYCLIDXPOS),
|
||||||
get4byte(hd + STDYCLIDXLEN), (u_char*)sf->stdy.clstdyidx)) /* XXX */
|
get4byte(hd + STDYCLIDXLEN), (u_char*)sf->stdy.clstdyidx)) /* XXX */
|
||||||
return ERROR;
|
return ERROR;
|
||||||
return putfile(fd, get4byte(hd + STDYCLSEGPOS),
|
return fputfile(fp, get4byte(hd + STDYCLSEGPOS),
|
||||||
get4byte(hd + STDYCLSEGLEN), sf->stdy.clstdydic);
|
get4byte(hd + STDYCLSEGLEN), sf->stdy.clstdydic);
|
||||||
}
|
}
|
||||||
|
|
||||||
int makedict(char* path, int idxlen, int seglen, int segnum) {
|
int makedict(char* path, int idxlen, int seglen, int segnum) {
|
||||||
|
|
@ -661,7 +607,7 @@ int makedict(char* path, int idxlen, int seglen, int segnum) {
|
||||||
ret = SJ4_NormalEnd;
|
ret = SJ4_NormalEnd;
|
||||||
error:
|
error:
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if(ret != SJ4_NormalEnd) unlink(path);
|
if(ret != SJ4_NormalEnd) sj4_unlink(path);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -741,7 +687,7 @@ int makestdy(char* path, int stynum, int clstep, int cllen) {
|
||||||
ret = SJ4_NormalEnd;
|
ret = SJ4_NormalEnd;
|
||||||
error:
|
error:
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if(ret != SJ4_NormalEnd) unlink(path);
|
if(ret != SJ4_NormalEnd) sj4_unlink(path);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -752,7 +698,7 @@ void sj_closeall(SJ4_CONTEXT2) {
|
||||||
|
|
||||||
int set_dictpass(DictFile* dp, char* pass) {
|
int set_dictpass(DictFile* dp, char* pass) {
|
||||||
set_passwd(dp->buffer, pass);
|
set_passwd(dp->buffer, pass);
|
||||||
return putfile(dp->fd, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer);
|
return fputfile(dp->fp, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_stdypass(SJ4_CONTEXT char* pass) {
|
int set_stdypass(SJ4_CONTEXT char* pass) {
|
||||||
|
|
@ -760,7 +706,7 @@ int set_stdypass(SJ4_CONTEXT char* pass) {
|
||||||
|
|
||||||
sp = (StdyFile*)stdy_base;
|
sp = (StdyFile*)stdy_base;
|
||||||
set_passwd(sp->header, pass);
|
set_passwd(sp->header, pass);
|
||||||
return putfile(sp->fd, 0, HEADERLENGTH + COMMENTLENGTH, sp->header);
|
return fputfile(sp->fp, 0, HEADERLENGTH + COMMENTLENGTH, sp->header);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
@ -770,7 +716,7 @@ set_comment(u_char* buf, char* comment) {
|
||||||
|
|
||||||
int set_dictcmnt(DictFile* dp, char* cmnt) {
|
int set_dictcmnt(DictFile* dp, char* cmnt) {
|
||||||
set_comment(dp->buffer, cmnt);
|
set_comment(dp->buffer, cmnt);
|
||||||
return putfile(dp->fd, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer);
|
return fputfile(dp->fp, 0, HEADERLENGTH + COMMENTLENGTH, dp->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_stdycmnt(SJ4_CONTEXT char* cmnt) {
|
int set_stdycmnt(SJ4_CONTEXT char* cmnt) {
|
||||||
|
|
@ -778,7 +724,7 @@ int set_stdycmnt(SJ4_CONTEXT char* cmnt) {
|
||||||
|
|
||||||
sp = (StdyFile*)stdy_base;
|
sp = (StdyFile*)stdy_base;
|
||||||
set_comment(sp->header, cmnt);
|
set_comment(sp->header, cmnt);
|
||||||
return putfile(sp->fd, 0, HEADERLENGTH + COMMENTLENGTH, sp->header);
|
return fputfile(sp->fp, 0, HEADERLENGTH + COMMENTLENGTH, sp->header);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_stdysize(SJ4_CONTEXT int* stynum, int* clstep, int* cllen) {
|
int get_stdysize(SJ4_CONTEXT int* stynum, int* clstep, int* cllen) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue