/rusxmms/librcd

To get this branch, use:
bzr branch http://suren.me/webbzr/rusxmms/librcd

« back to all changes in this revision

Viewing changes to statgen/generate.c

  • Committer: Suren A. Chilingaryan
  • Date: 2005-06-16 23:19:27 UTC
  • Revision ID: Arch-1:ds@dside.dyndns.org--darksoft-2004%librcd--main--0.1--base-0
initial import

(automatically generated log message)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <unistd.h>
 
3
#include <ctype.h>
 
4
#include <locale.h>
 
5
#include <math.h>
 
6
#include <iconv.h>
 
7
#include <langinfo.h>
 
8
#include <sys/types.h>
 
9
#include <sys/stat.h>
 
10
 
 
11
#define first_char 128
 
12
#define last_char 255
 
13
 
 
14
#define original_first_char 192
 
15
#define original_last_char 255
 
16
 
 
17
#define chars_number (last_char-first_char+1)
 
18
#define array_size (chars_number*chars_number)
 
19
 
 
20
struct array_pos {
 
21
    int ll;
 
22
    int uu;
 
23
    int lu;
 
24
    int ul;
 
25
};
 
26
 
 
27
struct pstat {
 
28
    unsigned long p;
 
29
    unsigned long s;
 
30
    unsigned long e;
 
31
};
 
32
 
 
33
iconv_t icnv=(iconv_t)-1;
 
34
 
 
35
int end_symbol(char ch) {
 
36
    if (ch=='\r'||ch=='\n'||ch==0||ch==' '||ch=='\t'||ch==','||ch=='.'||ch=='!'||ch=='?'||ch==';'||ch=='-'||ch==':'||ch=='"'||ch=='\''||ch==')') return 1;
 
37
    return 0;
 
38
}
 
39
 
 
40
int start_symbol(char ch) {
 
41
    if ((ch=='\t')||ch=='\r'||ch=='\n'||(ch==' ')||(ch=='(')||(ch=='"')||(ch=='\'')) return 1;
 
42
    return 0;
 
43
}    
 
44
 
 
45
 
 
46
unsigned char convert_char(unsigned char c) {
 
47
    char r;
 
48
    char *pr, *pc;
 
49
    size_t lr=1,lc=1;
 
50
    pr=&r;pc=&c;
 
51
    
 
52
    if (icnv == (iconv_t)-1) return c;
 
53
    if (iconv(icnv,&pc,&lc,&pr,&lr)<0) {
 
54
        printf("Error converting characters!\n");
 
55
        exit(1);
 
56
    }
 
57
    return r;
 
58
}
 
59
 
 
60
int get_array_pos(struct array_pos *pos, int a, int b) {
 
61
    int la,ua,lb,ub;
 
62
    if ((a<original_first_char)||(a>original_last_char)) return -1;
 
63
    if ((b<original_first_char)||(b>original_last_char)) return -1;
 
64
    
 
65
    la=tolower(a);
 
66
    ua=toupper(a);
 
67
    lb=tolower(b);
 
68
    ub=toupper(b);
 
69
 
 
70
    if ((la<original_first_char)||(la>original_last_char)) la=a;
 
71
    if ((lb<original_first_char)||(lb>original_last_char)) lb=b;
 
72
    if ((ua<original_first_char)||(ua>original_last_char)) ua=a;
 
73
    if ((ub<original_first_char)||(ub>original_last_char)) ub=b;
 
74
    
 
75
    la=convert_char(la);
 
76
    ua=convert_char(ua);
 
77
    lb=convert_char(lb);
 
78
    ub=convert_char(ub);
 
79
    
 
80
//    la=a;lb=b;ua=a;ub=b;
 
81
    
 
82
    pos->ll=(la-first_char)*chars_number+(lb-first_char);
 
83
    if (la!=ua) {
 
84
        pos->ul=(ua-first_char)*chars_number+(lb-first_char);
 
85
    } else {
 
86
        pos->ul=-1;
 
87
    } 
 
88
    if (lb!=ub) {
 
89
        pos->lu=(la-first_char)*chars_number+(ub-first_char);
 
90
    }
 
91
    else {
 
92
        pos->lu=-1;
 
93
    }
 
94
    if ((lb!=ub)&&(la!=ua)) {
 
95
        pos->uu=(ua-first_char)*chars_number+(ub-first_char);
 
96
    } else {
 
97
        pos->uu=-1;
 
98
    }
 
99
    return 0;
 
100
}
 
101
 
 
102
 
 
103
struct pstat *analyze(const unsigned char *text, unsigned long length) {
 
104
    struct pstat *a;
 
105
    unsigned long i;
 
106
    struct array_pos pos;
 
107
    
 
108
    a=(struct pstat*)malloc(array_size*sizeof(struct pstat));
 
109
    if (!a) return NULL;
 
110
 
 
111
    for (i=0;i<array_size;i++) {
 
112
        a[i].p=0;
 
113
        a[i].s=0;
 
114
        a[i].e=0;
 
115
    }
 
116
        
 
117
    for (i=1;i<length;i++) {
 
118
        if (get_array_pos(&pos,text[i-1],text[i])>=0) {
 
119
            if (pos.ll>=0) {
 
120
                if ((i==1)||(start_symbol(text[i-2]))) a[pos.ll].s++;
 
121
                else if ((i+2==length)||(end_symbol(text[i+1]))) a[pos.ll].e++;
 
122
                else a[pos.ll].p++;
 
123
            }
 
124
            if (pos.ul>=0) {
 
125
                if ((i==1)||(start_symbol(text[i-2]))) a[pos.ul].s++;
 
126
                else if ((i+2==length)||(end_symbol(text[i+1]))) a[pos.ul].e++;
 
127
                else a[pos.ul].p++;
 
128
            }
 
129
//          if (pos.lu>=0) {
 
130
//              if ((i==1)||(start_symbol(text[i-2]))) a[pos.lu].s++;
 
131
//              else if ((i+2==length)||(end_symbol(text[i+1]))) a[pos.lu].e++;
 
132
//              else a[pos.lu].p++;
 
133
//          }
 
134
            if (pos.uu>=0) {
 
135
                if ((i==1)||(start_symbol(text[i-2]))) a[pos.uu].s++;
 
136
                else if ((i+2==length)||(end_symbol(text[i+1]))) a[pos.uu].e++;
 
137
                else a[pos.uu].p++;
 
138
            }
 
139
        }
 
140
    }
 
141
    return a;
 
142
}
 
143
 
 
144
 
 
145
int print(struct pstat *a) {
 
146
    int i,j,k,n;
 
147
    
 
148
    for (i=first_char,k=0,n=0;i<=last_char;i++)
 
149
        for (j=first_char;j<=last_char;j++,k++) {
 
150
            if ((a[k].p)||(a[k].s)||(a[k].e)) {
 
151
                if ((n)&&(n%8==0)) printf(",\n");
 
152
                else if (n) printf(", ");
 
153
                printf("{'%c','%c',%lf,%lf,%lf}",i,j,a[k].p?log10(a[k].p):-2,a[k].s?log10(a[k].s):-2,a[k].e?log10(a[k].e):-2);
 
154
                n++;
 
155
            }
 
156
        }
 
157
    if ((n%8)!=1) printf("\n");
 
158
    return n;
 
159
}
 
160
 
 
161
 
 
162
unsigned long npow(unsigned long n) {
 
163
    unsigned long res=2;
 
164
    while (res<=n) res*=2;
 
165
    return res;
 
166
}
 
167
 
 
168
main(int argc, char *argv[]) {
 
169
    FILE *f;
 
170
    struct stat st;
 
171
    unsigned char *text;
 
172
    unsigned long len;
 
173
    struct pstat *a;
 
174
    int num;
 
175
    long i,sum;
 
176
    char locale[32];
 
177
 
 
178
 
 
179
    if (argc!=3) {
 
180
        printf("Usage: %s <file name> <encoding>\n",argv[0]);
 
181
        exit(0);
 
182
    }
 
183
 
 
184
    if (strlen(argv[2])>12) {
 
185
        printf("Invalid encoding(%s) specified!\n",argv[2]);
 
186
        exit(1);
 
187
    }
 
188
    
 
189
    if ((!strcasecmp(argv[2],"koi"))||(!strcasecmp(argv[2],"koi8"))||(!strcasecmp(argv[2],"koi-8"))||(!strcasecmp(argv[2],"koi8-r")))
 
190
        sprintf(locale,"%s","KOI8-R");
 
191
    else if ((!strcasecmp(argv[2],"win"))||(!strcasecmp(argv[2],"cp1251"))||(!strcasecmp(argv[2],"cp-1251"))||(!strcasecmp(argv[2],"win1251"))||(!strcasecmp(argv[2],"win-1251")))
 
192
        sprintf(locale,"%s","CP1251");
 
193
    else if ((!strcasecmp(argv[2],"alt"))||(!strcasecmp(argv[2],"cp866"))||(!strcasecmp(argv[2],"cp-866"))||(!strcasecmp(argv[2],"ibm866"))||(!strcasecmp(argv[2],"ibm-866")))
 
194
        sprintf(locale,"%s","IBM866");
 
195
    else
 
196
        sprintf(locale,"%s",argv[2]);
 
197
    
 
198
    if (!setlocale(LC_CTYPE,"")) {
 
199
        printf("Can't set locale!\n");
 
200
        exit(1);
 
201
    }
 
202
 
 
203
    if (strcmp(locale,nl_langinfo(CODESET))) {
 
204
        if ((icnv=iconv_open(locale,nl_langinfo(CODESET)))<0) {
 
205
            printf("Can't initialize iconv!\n");
 
206
            exit(1);
 
207
        }
 
208
    }
 
209
    
 
210
    
 
211
    if (stat(argv[1],&st)) {
 
212
        printf("Specified file can't be stated!\n");
 
213
        iconv_close(icnv);
 
214
        exit(1);
 
215
    }
 
216
    
 
217
    if (!S_ISREG(st.st_mode)) {
 
218
        printf("Specified file isn't regular file!\n");
 
219
        iconv_close(icnv);
 
220
        exit(1);
 
221
    }
 
222
    
 
223
    text=(unsigned char*)malloc(st.st_size);
 
224
    if (!text) {
 
225
        printf("Can't allocate %lu bytes of memory!\n",st.st_size);
 
226
        iconv_close(icnv);
 
227
        exit(1);
 
228
    }
 
229
    
 
230
    f=fopen(argv[1],"r");
 
231
    if (!f) {
 
232
        printf("Failed to open specified file. Check permissions!\n");
 
233
        free(text);
 
234
        iconv_close(icnv);
 
235
        exit(1);
 
236
    }
 
237
    if (fread(text,1,st.st_size,f)!=st.st_size) {
 
238
        printf("Problem reading specified file!\n");
 
239
        free(text);
 
240
        fclose(f);
 
241
        iconv_close(icnv);
 
242
        exit(1);
 
243
    }
 
244
    fclose(f);
 
245
    
 
246
    a=analyze(text,st.st_size);
 
247
    if (a) {
 
248
        printf("static const lng_stat2 enc_%s[]={\n",argv[2]);
 
249
        num=print(a);
 
250
        printf("};\n\n");
 
251
        free(a);
 
252
        fprintf(stderr,"static unsigned int indexes2=%lu;\n",num);
 
253
        fprintf(stderr,"static unsigned int npow2=%lu;\n",npow(num));
 
254
    } else printf("Failed to allocate %lu bytes of memory!\n",array_size*sizeof(struct pstat));
 
255
 
 
256
    free(text);
 
257
    iconv_close(icnv);
 
258
}