#ifndef lint
static char *rcsid = "$Id: filemapper.c,v 1.1 2003/06/04 00:25:53 marka Exp $";
#endif
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <idn/result.h>
#include <idn/assert.h>
#include <idn/log.h>
#include <idn/logmacro.h>
#include <idn/debug.h>
#include <idn/ucs4.h>
#include <idn/ucsmap.h>
#include <idn/filemapper.h>
#define SUPPORT_VERSIONING
#define UCSBUF_LOCAL_SIZE 20
typedef struct ucsbuf {
unsigned long *ucs;
size_t size;
size_t len;
unsigned long local[UCSBUF_LOCAL_SIZE];
} ucsbuf_t;
struct idn__filemapper {
idn_ucsmap_t map;
};
static void ucsbuf_init(ucsbuf_t *b);
static idn_result_t ucsbuf_grow(ucsbuf_t *b);
static idn_result_t ucsbuf_append(ucsbuf_t *b, unsigned long v);
static void ucsbuf_free(ucsbuf_t *b);
static idn_result_t read_file(const char *file, FILE *fp,
idn_ucsmap_t map);
static idn_result_t get_map(char *p, ucsbuf_t *b);
static char *get_ucs(char *p, unsigned long *vp);
idn_result_t
idn__filemapper_create(const char *file, idn__filemapper_t *ctxp) {
FILE *fp;
idn__filemapper_t ctx;
idn_result_t r;
assert(file != NULL && ctxp != NULL);
TRACE(("idn__filemapper_create(file=\"%-.100s\")\n", file));
if ((fp = fopen(file, "r")) == NULL) {
WARNING(("idn__filemapper_create: cannot open %-.100s\n",
file));
return (idn_nofile);
}
if ((ctx = malloc(sizeof(struct idn__filemapper))) == NULL)
return (idn_nomemory);
if ((r = idn_ucsmap_create(&ctx->map)) != idn_success) {
free(ctx);
return (r);
}
r = read_file(file, fp, ctx->map);
fclose(fp);
if (r == idn_success) {
idn_ucsmap_fix(ctx->map);
*ctxp = ctx;
} else {
idn_ucsmap_destroy(ctx->map);
free(ctx);
}
return (r);
}
void
idn__filemapper_destroy(idn__filemapper_t ctx) {
assert(ctx != NULL);
TRACE(("idn__filemapper_destroy()\n"));
idn_ucsmap_destroy(ctx->map);
free(ctx);
}
idn_result_t
idn__filemapper_map(idn__filemapper_t ctx, const unsigned long *from,
unsigned long *to, size_t tolen)
{
idn_result_t r = idn_success;
ucsbuf_t ub;
assert(ctx != NULL && from != NULL && to != NULL);
TRACE(("idn__filemapper_map(from=\"%s\")\n",
idn__debug_ucs4xstring(from, 50)));
ucsbuf_init(&ub);
while (*from != '\0') {
r = idn_ucsmap_map(ctx->map, *from, ub.ucs, ub.size, &ub.len);
switch (r) {
case idn_buffer_overflow:
if ((r = ucsbuf_grow(&ub)) != idn_success)
break;
continue;
case idn_nomapping:
r = idn_success;
case idn_success:
if (tolen < ub.len) {
r = idn_buffer_overflow;
goto ret;
}
memcpy(to, ub.ucs, sizeof(*to) * ub.len);
to += ub.len;
tolen -= ub.len;
break;
default:
goto ret;
}
from++;
}
ret:
ucsbuf_free(&ub);
if (r == idn_success) {
if (tolen == 0)
return (idn_buffer_overflow);
*to = '\0';
}
return (r);
}
static void
ucsbuf_init(ucsbuf_t *b) {
b->ucs = b->local;
b->size = UCSBUF_LOCAL_SIZE;
b->len = 0;
}
static idn_result_t
ucsbuf_grow(ucsbuf_t *b) {
unsigned long *newbuf;
b->size *= 2;
if (b->ucs == b->local) {
b->ucs = malloc(sizeof(unsigned long) * b->size);
if (b->ucs == NULL)
return (idn_nomemory);
memcpy(b->ucs, b->local, sizeof(b->local));
} else {
newbuf = realloc(b->ucs, sizeof(unsigned long) * b->size);
if (newbuf == NULL)
return (idn_nomemory);
b->ucs = newbuf;
}
return (idn_success);
}
static idn_result_t
ucsbuf_append(ucsbuf_t *b, unsigned long v) {
idn_result_t r;
if (b->len + 1 > b->size) {
r = ucsbuf_grow(b);
if (r != idn_success)
return (r);
}
b->ucs[b->len++] = v;
return (idn_success);
}
static void
ucsbuf_free(ucsbuf_t *b) {
if (b->ucs != b->local && b->ucs != NULL)
free(b->ucs);
}
static idn_result_t
read_file(const char *file, FILE *fp, idn_ucsmap_t map) {
char line[1024];
ucsbuf_t ub;
idn_result_t r = idn_success;
int lineno = 0;
ucsbuf_init(&ub);
while (fgets(line, sizeof(line), fp) != NULL) {
char *p = line;
lineno++;
while (isspace((unsigned char)*p))
p++;
if (*p == '\0' || *p == '#')
continue;
#ifdef SUPPORT_VERSIONING
if (lineno == 1 && strncmp("version=", line, 8) == 0)
continue;
#endif
again:
ub.len = 0;
r = get_map(p, &ub);
switch (r) {
case idn_success:
r = idn_ucsmap_add(map, ub.ucs[0],
&ub.ucs[1], ub.len - 1);
break;
case idn_buffer_overflow:
if ((r = ucsbuf_grow(&ub)) != idn_success)
break;
goto again;
case idn_invalid_syntax:
WARNING(("syntax error in file \"%-.100s\" line %d: "
"%-.100s", file, lineno, line));
default:
ucsbuf_free(&ub);
return (r);
}
}
ucsbuf_free(&ub);
return (r);
}
static idn_result_t
get_map(char *p, ucsbuf_t *b) {
unsigned long v;
idn_result_t r = idn_success;
for (;;) {
if ((p = get_ucs(p, &v)) == NULL)
return (idn_invalid_syntax);
if ((r = ucsbuf_append(b, v)) != idn_success)
return (r);
if (b->len == 1) {
if (*p != ';')
return (idn_invalid_syntax);
p++;
while (isspace((unsigned char)*p))
p++;
}
if (*p == ';' || *p == '#' || *p == '\0')
return (r);
}
return (r);
}
static char *
get_ucs(char *p, unsigned long *vp) {
char *end;
while (isspace((unsigned char)*p))
p++;
if (strncmp(p, "U+", 2) == 0)
p += 2;
*vp = strtoul(p, &end, 16);
if (end == p) {
INFO(("idn__filemapper_create: UCS code point expected\n"));
return (NULL);
}
p = end;
while (isspace((unsigned char)*p))
p++;
return p;
}
idn_result_t
idn__filemapper_createproc(const char *parameter, void **ctxp) {
return idn__filemapper_create(parameter, (idn__filemapper_t *)ctxp);
}
void
idn__filemapper_destroyproc(void *ctxp) {
idn__filemapper_destroy((idn__filemapper_t)ctxp);
}
idn_result_t
idn__filemapper_mapproc(void *ctx, const unsigned long *from,
unsigned long *to, size_t tolen) {
return idn__filemapper_map((idn__filemapper_t)ctx, from, to, tolen);
}