summaryrefslogtreecommitdiffstats
path: root/mbbsd/convert.c
blob: 1dd90308e853cb507b652fb2fe10ff9e9d37f0fd (plain) (blame)
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
/* $Id: convert.c 1374 2003-11-27 14:11:40Z victor $ */
#include "bbs.h"

#ifdef CONVERT

extern read_write_type write_type;
extern read_write_type read_type;

static int gb_read(int fd, void *buf, size_t count)
{
    int len = read(fd, buf, count);
    if (len > 0)
    gb2big((char *)buf, &len, 0);
    return len;
}

static int gb_write(int fd, void *buf, size_t count)
{
    big2gb((char *)buf, &count, 0);
    return write(fd, buf, count);
}

static int utf8_read(int fd, void *buf, size_t count)
{
    count = read(fd, buf, count);
    if (count > 0) {
    strcpy(buf, utf8_uni(buf, &count, 0));
    uni2big(buf, &count, 0);
    ((char *)buf)[count] = 0;
    }
    return count;
}

static int utf8_write(int fd, void *buf, size_t count)
{
    strcpy(buf, big2uni(buf, &count, 0));
    uni_utf8(buf, &count, 0);
    ((char *)buf)[count] = 0;
    return write(fd, buf, count);
}

void set_converting_type(int which)
{
    if (which == CONV_NORMAL) {
    read_type = read;
    write_type = (read_write_type)write;
    }
    else if (which == CONV_GB) {
    read_type = gb_read;
    write_type = gb_write;
    }
    else if (which == CONV_UTF8) {
    read_type = utf8_read;
    write_type = utf8_write;
    }
}

#endif