ccv/mkcscv.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. main
  2. conv

   1 /*
   2  * mkcscv2x.c - make code conversion table
   3  * by Hirotsugu Kakugawa
   4  *
   5  *  28 Jul 1997
   6  */
   7 /*
   8  * Copyright (C) 1997 Hirotsugu Kakugawa. 
   9  * All rights reserved.
  10  *
  11  * This program is free software; you can redistribute it and/or modify
  12  * it under the terms of the GNU General Public License as published by
  13  * the Free Software Foundation; either version 2, or (at your option)
  14  * any later version.
  15  * 
  16  * This program is distributed in the hope that it will be useful,
  17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19  * GNU General Public License for more details.
  20  * 
  21  * You should have received a copy of the GNU General Public License
  22  * along with this program; if not, write to the Free Software
  23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  24  */
  25 
  26 
  27 #include  <stdio.h>
  28 #include  <stdlib.h>
  29 
  30 #define  UNDEF  -1L
  31 #define  BASE    128
  32 
  33 #define TABLE_SIZE      20000
  34 struct conv_table {
  35   long  code1;
  36   long  code2;
  37 };
  38 int    block_size = 256;
  39 int    compact = 0;
  40 
  41 void conv(struct conv_table*, FILE*, char **);
  42 
  43 int
  44 main(int argc, char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  45 {
  46   struct conv_table *cvtbl;
  47 
  48   argv++;
  49   argc--;
  50 
  51   compact = 0;
  52   if ((argc >= 1) && (strcmp(argv[0], "-c") == 0)){
  53     compact = 1;
  54     argc--;
  55     argv++;
  56   }
  57 
  58   if (argc < 4){
  59     fprintf(stderr, 
  60             "Usage: mkcscv [-c] %s [block size]\n", 
  61             "CS1-NAME CS1-ENC CS2-NAME CS2-ENC");
  62     exit(1);
  63   }
  64 
  65   fprintf(stderr, "*** Making CCV file: (%s, %s) => (%s, %s)\n", 
  66           argv[0], argv[1], argv[2], argv[3]); 
  67 
  68   if (argc >= 5)
  69     block_size = atoi(argv[5]);
  70 
  71   cvtbl = (struct conv_table*)calloc(TABLE_SIZE+1, sizeof(struct conv_table));
  72   if (cvtbl == NULL){
  73     fprintf(stderr, "No memory\n");
  74     exit(1);
  75   }
  76   
  77   conv(cvtbl, stdin, argv);
  78 
  79   return 0;
  80 }
  81 
  82 void conv(struct conv_table* cvtbl, FILE* fp, char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  83 {
  84   int   c1min, c1max, c2min, c2max, c1, c2, blocks;
  85   int   index, print_block, i, t;
  86   char  line[BUFSIZ];
  87   
  88   for (i = 0; i < TABLE_SIZE; i++){ 
  89     cvtbl[i].code1 = UNDEF;
  90     cvtbl[i].code2 = UNDEF;
  91   }
  92 
  93   /* Input must be in code order. */
  94   c1min = 0xffff;        c1max = 0x00;
  95   c2min = block_size-1;  c2max = 0x00;
  96   for (i = 0; ; i++){
  97     if (fgets(line, sizeof(line), fp) == NULL)
  98       break;
  99     sscanf(line, "%li%li", &cvtbl[i].code1, &cvtbl[i].code2);
 100     c1 = cvtbl[i].code1 / block_size;
 101     c2 = cvtbl[i].code1 % block_size;
 102     if (c1min > c1)
 103       c1min = c1;
 104     if (c1max < c1)
 105       c1max = c1;
 106     if (c2min > c2)
 107       c2min = c2;
 108     if (c2max < c2)
 109       c2max = c2;
 110   }
 111 
 112   printf("; Conversion table: %s ==> %s\n", argv[0], argv[2]);
 113   printf("(charset-external-name %s)\n", argv[0]);
 114   printf("(charset-external-encoding %s)\n", argv[1]);
 115   printf("(charset-internal-name %s)\n", argv[2]);
 116   printf("(charset-internal-encoding %s)\n", argv[3]);
 117   if (compact == 0)
 118     printf("(table-type array)\n");
 119   else
 120     printf("(table-type random-arrays)\n");
 121   printf("; Code point C is converted to C' by the following formula:\n");
 122   printf(";   C' = Table[(c1 - c1min)*M + (c2 - c2min)],\n");
 123   printf(";   where c1 = C/B and c2 = C%%B, and M = c2max - c2min + 1.\n");
 124   printf(";   B is a block size given by the 'block-size:' parameter.\n");
 125   printf("(c1-min 0x%x)\n", c1min);
 126   printf("(c1-max 0x%x)\n", c1max);
 127   printf("(c2-min 0x%x)\n", c2min);
 128   printf("(c2-max 0x%x)\n", c2max);
 129   printf("(block-size %d)\n", block_size);
 130 
 131   if (compact == 0){
 132     printf("(nblocks %d)\n", c1max-c1min+1);
 133   } else {
 134     blocks = 0;
 135     index = 0;
 136     for (c1 = c1min; c1 <= c1max; c1++){
 137       print_block = 0;
 138       for (c2 = c2min; c2 <= c2max; c2++){
 139         if (cvtbl[index].code1 == c1*block_size+c2){
 140           if (print_block == 0)
 141             blocks++;
 142           print_block = 1;
 143           index++;
 144         }
 145       }
 146     }
 147     printf("(nblocks %d)\n", blocks);
 148   }
 149 
 150   index = 0;
 151   for (c1 = c1min; c1 <= c1max; c1++){
 152     if (compact == 0){
 153       print_block = 1;
 154     } else {
 155       print_block = 0;
 156       for (c2 = c2min; c2 <= c2max; c2++){
 157         if (cvtbl[index].code1 == c1*block_size+c2){
 158           print_block = 1;
 159           break;
 160         }
 161       }
 162     }
 163     if (print_block == 1){
 164       printf("; 0x%04x ... 0x%04x\n", 
 165              c1*block_size+c2min, c1*block_size+c2max);
 166       printf("(block %d", c1-c1min);
 167       t = 0;
 168       for (c2 = c2min; c2 <= c2max; c2++){
 169         if ((t % 8) == 0)
 170           printf("\n    ");
 171         if (cvtbl[index].code1 == c1*block_size+c2)
 172           printf("0x%04lx ", cvtbl[index++].code2);
 173         else 
 174           printf("-1     ");
 175         t++;
 176       }
 177       printf(")\n");
 178     }
 179   }
 180 }
 181 
 182 /*EOF*/

/* [<][>][^][v][top][bottom][index][help] */