ascii-jtex/eKanji/tools/ekreenc.c

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

FUNCTIONS

This source file includes following functions.
  1. main
  2. usage
  3. x_index
  4. x_memclr

   1 /* ekreenc.c  - reencode an eKanji font.
   2  * by Hirotsugu Kakugawa
   3  * 
   4  *  Copyright (C) 1999  Hirotsugu Kakugawa. All rights reserved. 
   5  *  See "COPYING" for distribution of this software. 
   6  *
   7  *  This program is free software; you can redistribute it and/or modify
   8  *  it under the terms of the GNU General Public License as published by
   9  *  the Free Software Foundation; either version 2 of the License, or
  10  *  (at your option) any later version.
  11  *
  12  *  This program is distributed in the hope that it will be useful,
  13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  *  GNU General Public License for more details.
  16  *
  17  *  You should have received a copy of the GNU General Public License
  18  *  along with this program; if not, write to the Free Software
  19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20  */
  21 
  22 
  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <fcntl.h>
  26 
  27 #define PIXEL_SIZE      24
  28 
  29 int   pixel_size = PIXEL_SIZE;
  30 int   ifd, ofd;
  31 
  32 void   usage(void);
  33 char  *x_index(char *p, char ch);
  34 void   x_memclr(unsigned char *buff, int dsize);
  35 
  36 
  37 
  38 int 
  39 main(int argc, char** argv)
     /* [<][>][^][v][top][bottom][index][help] */
  40 {
  41   FILE  *efp;
  42   unsigned char  buff[24*(24/8)];
  43   char   line[1024];
  44   char  *efile, *ifile, *ofile, *s;
  45   int   dsize, i_need_close, o_need_close;
  46   long  ich, och, och_last;
  47 
  48   efile = ifile = ofile = NULL;
  49 
  50   argc--; argv++;
  51 
  52   while ((argc > 0) && (argv[0][0] == '-')){
  53     if (strcmp(argv[0], "-24") == 0){
  54       pixel_size = 24;
  55     } else if (strcmp(argv[0], "-16") == 0){
  56       pixel_size = 16;
  57     } else if (strcmp(argv[0], "-i") == 0){
  58       argc--; argv++;
  59       ifile = argv[0];
  60     } else if (strcmp(argv[0], "-o") == 0){
  61       argc--; argv++;
  62       ofile = argv[0];
  63     } else if (strcmp(argv[0], "-e") == 0){
  64       argc--; argv++;
  65       efile = argv[0];
  66     } else if ((strcmp(argv[0], "-h") == 0)
  67                || (strcmp(argv[0], "-help") == 0)
  68                || (strcmp(argv[0], "--help") == 0) ){
  69       usage();
  70     }
  71     argc--; argv++;
  72   }
  73   
  74   dsize = pixel_size * ((pixel_size + 7) / 8);
  75 
  76   efp = NULL;
  77   if (efile != NULL){
  78     if ((efp = fopen(efile, "r")) < 0){
  79       fprintf(stderr, "Can't open reeocoding table file: %s\n", ifile);
  80       exit(0);
  81     }
  82   } else {
  83     fprintf(stderr, "ekreenc: No reencoding table file.\n");
  84     usage();
  85   }
  86   
  87   if (ifile != NULL){
  88     i_need_close = 1;
  89     if ((ifd = open(ifile, O_RDONLY)) < 0){
  90       fprintf(stderr, "Can't open input font file: %s\n", ifile);
  91       exit(0);
  92     }
  93   } else {
  94     i_need_close = 0;
  95     ifd = 0;
  96   }
  97   
  98   if (ofile != NULL){
  99     o_need_close = 1;
 100     if ((ofd = creat(ofile, 0644)) < 0){
 101       fprintf(stderr, "Can't open output font file: %s\n", ofile);
 102       exit(0);
 103     }
 104   } else {
 105     o_need_close = 0;
 106     ofd = 1;
 107   }
 108   
 109   och_last = 0;
 110   while (fgets(line, sizeof(line), efp) != NULL){
 111     if ((s = x_index(buff, '#')) != NULL)
 112       *s = '\0';
 113     if (sscanf(line, "%li%li", &och, &ich) != 2)
 114       continue;
 115     if (och_last < (och-1)){
 116       x_memclr(buff, dsize);
 117       while (och_last < (och-1)){
 118         write(ofd, buff, dsize); 
 119         och_last++;
 120       }
 121     }
 122     if (ich > 0){
 123       lseek(ifd, (ich-1) * dsize, SEEK_SET);
 124       if (read(ifd, buff, dsize) > 0){
 125         write(ofd, buff, dsize);
 126       } else {
 127         fprintf(stderr, "No such character: %s\n", argv[0]);
 128       }
 129     } else {
 130       x_memclr(buff, dsize);
 131       write(ofd, buff, dsize);      
 132     }
 133     och_last = och;
 134     och++;
 135   }
 136 
 137   if (i_need_close == 1){
 138     close(ifd);
 139   }
 140   if (o_need_close == 1){
 141     close(ofd);
 142   }
 143   fclose(efp);
 144 
 145   return 0;
 146 }
 147 
 148 
 149 void 
 150 usage(void)
     /* [<][>][^][v][top][bottom][index][help] */
 151 {
 152   fprintf(stderr, "ekreenc  - reencode an eKanji font file.\n");
 153   fprintf(stderr, "Usage: ekreenc -e FILE [option]\n");
 154   fprintf(stderr, " options:\n");
 155   fprintf(stderr, "  -e FILE  Reencoding table file\n");
 156   fprintf(stderr, "  -i FILE  Input font file (stdin if not given)\n");
 157   fprintf(stderr, "  -o FILE  Output font file (stdout if not given)\n");
 158   fprintf(stderr, "  -24      Dot size of the font file 24-dot (default)\n");
 159   fprintf(stderr, "  -16      Dot size of the font file 16-dot\n");
 160   fprintf(stderr, "  -help    Print help.\n");
 161   exit(0);
 162 }
 163 
 164 
 165 char*
 166 x_index(char *p, char ch)
     /* [<][>][^][v][top][bottom][index][help] */
 167 {
 168   while (*p != '\0'){
 169     if (*p == ch)
 170       return p;
 171     p++;
 172   }
 173   return NULL;
 174 }
 175 
 176 void
 177 x_memclr(unsigned char *buff, int dsize)
     /* [<][>][^][v][top][bottom][index][help] */
 178 {
 179   int  i;
 180   
 181   for (i = dsize; i > 0; i--){
 182     *buff = 0;
 183     buff++;
 184   }
 185 }
 186 
 187 
 188 /*EOF*/

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