[go: up one dir, main page]

File: readhz.c

package info (click to toggle)
sbm 3.7.1-9.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,120 kB
  • ctags: 1,591
  • sloc: asm: 11,940; ansic: 2,871; makefile: 503
file content (171 lines) | stat: -rw-r--r-- 3,983 bytes parent folder | download | duplicates (4)
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
#include<stdio.h>
#include<string.h>

struct hz_info
{
    unsigned int  count;
    unsigned char qu;
    unsigned char wei;
    unsigned char asc1;
    unsigned char asc2;
    unsigned char dat[32];
};

static unsigned char cur_asc=1;
static unsigned int cur_hz=0;

static struct hz_info hz_buf[128];

int read_hz(char *buf,char qu, char wei,FILE *fp);
int insert_hz(unsigned char qu, unsigned char wei, char *dat);
int scan_hz(unsigned char *txt, FILE *fp);
int write_hztable(FILE *fp);
int write_strtable(unsigned char *txt, FILE *fp);

unsigned short get_hzasc(unsigned char qu, unsigned char wei);

int main(int argc, char *argv[])
{
    FILE *fpzk;
    char txt[32767];
    int i=0;
    
    memset(hz_buf, 0, sizeof(struct hz_info) * 128);

    fpzk=fopen("chs16.fon","rb");

    if( fpzk==NULL )
	{
        printf("\nFontLib Error!\n");
		return -1;
    }

    while( (txt[i] = fgetc(stdin)) != EOF ) i++;
    txt[i] = 0;

    printf("%s\n",txt);

    if( scan_hz(txt, fpzk) <0 ) return -1;
    if( write_hztable(stdout) <0 ) return -1;
    if( write_strtable(txt, stdout) <0 ) return -1;

    printf("\nNumber of hz = %d", cur_hz);

    fclose(fpzk);

    return 0;
}

int read_hz(char *buf,char qu, char wei,FILE *fp)
{   char temp[32];
    unsigned long seek=0;
    int i;
    seek= (unsigned long)((qu+95)*94L+wei+95)*32L;
    if(fseek(fp,seek,SEEK_SET)<0) return -1;
    if(fread(temp,32,1,fp)<1) return -1;
    for(i=0;i<16;i++)
    	{
    	buf[i]=temp[i*2];
        buf[i+16]=temp[i*2+1];
        }
    return 0;
}

int insert_hz(unsigned char qu, unsigned char wei, char *dat)
{
    int i;
    for (i=0; i<cur_hz; i++ )
       if(hz_buf[i].qu == qu && hz_buf[i].wei == wei)
       {
           hz_buf[i].count ++;
           return 0;
       }

    if( cur_hz >= 128 || cur_asc >= 256 ) return -1;
   
    hz_buf[cur_hz].qu   = qu;
    hz_buf[cur_hz].wei  = wei;
    hz_buf[cur_hz].asc1 = cur_asc;
    cur_asc ++;
    if( cur_asc == 0x0d ) cur_asc ++;
    if( cur_asc >= 0x1e && cur_asc < 128 ) cur_asc = 128;
    hz_buf[cur_hz].asc2 = cur_asc;
    cur_asc ++;
    if( cur_asc == 0x0d ) cur_asc ++;
    if( cur_asc >= 0x1e && cur_asc < 128 ) cur_asc = 128;

    memcpy(hz_buf[cur_hz].dat, dat, 32);

    hz_buf[cur_hz].count = 1;

    cur_hz ++;
    return 0;    
}

int scan_hz( unsigned char *txt, FILE *fp)
{
    char buf[32];
    while ( *txt != 0 )
    {
        if( *txt > 128 )
        {
            if( read_hz(buf, *txt, *(txt+1), fp) <0 ) return -1;
            if( insert_hz(*txt, *(txt+1), buf) <0 ) return -1;
            txt ++;
        }
        txt ++;
    }
    return 0;
}

int write_hztable(FILE *fp)
{
    int i, j;
    for(i=0; i<cur_hz; i++)
    {
        fprintf(fp,"\n  db %u      ;%c%c-1, count:%d",
	        (unsigned short)hz_buf[i].asc1 ,hz_buf[i].qu, hz_buf[i].wei, hz_buf[i].count);
        fprintf(fp,"\n  db ");
        for( j=0; j<15; j++ )
             fprintf(fp,"0x%02X,",hz_buf[i].dat[j]);
        fprintf(fp,"0x%02X",hz_buf[i].dat[15]);

        fprintf(fp,"\n  db %u      ;%c%c-2",
	        (unsigned short)hz_buf[i].asc2 ,hz_buf[i].qu, hz_buf[i].wei);
        fprintf(fp,"\n  db ");
        for( j = 16; j<31; j++ )
             fprintf(fp,"0x%02X,",hz_buf[i].dat[j]);
        fprintf(fp,"0x%02X",hz_buf[i].dat[31]);
        fprintf(fp,"\n");
    }
    return 0;
}

int write_strtable(unsigned char *txt, FILE *fp)
{ 
    unsigned short asc;
    
    while( *txt != 0 )
    {
        if( *txt < 128 ) fputc( (int)*txt, fp );
	else if( *txt >= 128 )
	{
	    asc = get_hzasc( *txt, *(txt+1) );
            fprintf(fp,"%u,%u", asc & 0xff, asc >> 8);
	    if(*(txt+2) != ',') fputc(',',fp);
	    txt ++;
	}
	txt ++;
    }
    return 0;
}

unsigned short get_hzasc(unsigned char qu, unsigned char wei)
{
    int i;
    for (i=0; i<cur_hz; i++)
        if( hz_buf[i].qu == qu && hz_buf[i].wei == wei )
            return ((unsigned short)hz_buf[i].asc1) | 
                   ((unsigned short)(hz_buf[i].asc2 << 8));
    return 0;
}