enc/mktable.c

Go to the documentation of this file.
00001 /**********************************************************************
00002   mktable.c
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2007  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <locale.h>
00033 
00034 #define __USE_ISOC99
00035 #include <ctype.h>
00036 
00037 #include "regenc.h"
00038 
00039 #define ASCII                0
00040 #define UNICODE_ISO_8859_1   1
00041 #define ISO_8859_1           2
00042 #define ISO_8859_2           3
00043 #define ISO_8859_3           4
00044 #define ISO_8859_4           5
00045 #define ISO_8859_5           6
00046 #define ISO_8859_6           7
00047 #define ISO_8859_7           8
00048 #define ISO_8859_8           9
00049 #define ISO_8859_9          10
00050 #define ISO_8859_10         11
00051 #define ISO_8859_11         12
00052 #define ISO_8859_13         13
00053 #define ISO_8859_14         14
00054 #define ISO_8859_15         15
00055 #define ISO_8859_16         16
00056 #define KOI8                17
00057 #define KOI8_R              18
00058 
00059 typedef struct {
00060   int   num;
00061   const char* name;
00062 } ENC_INFO;
00063 
00064 static ENC_INFO Info[] = {
00065   { ASCII,               "ASCII" },
00066   { UNICODE_ISO_8859_1,  "UNICODE_ISO_8859_1"  },
00067   { ISO_8859_1,  "ISO_8859_1"  },
00068   { ISO_8859_2,  "ISO_8859_2"  },
00069   { ISO_8859_3,  "ISO_8859_3"  },
00070   { ISO_8859_4,  "ISO_8859_4"  },
00071   { ISO_8859_5,  "ISO_8859_5"  },
00072   { ISO_8859_6,  "ISO_8859_6"  },
00073   { ISO_8859_7,  "ISO_8859_7"  },
00074   { ISO_8859_8,  "ISO_8859_8"  },
00075   { ISO_8859_9,  "ISO_8859_9"  },
00076   { ISO_8859_10, "ISO_8859_10" },
00077   { ISO_8859_11, "ISO_8859_11" },
00078   { ISO_8859_13, "ISO_8859_13" },
00079   { ISO_8859_14, "ISO_8859_14" },
00080   { ISO_8859_15, "ISO_8859_15" },
00081   { ISO_8859_16, "ISO_8859_16" },
00082   { KOI8,        "KOI8" },
00083   { KOI8_R,      "KOI8_R" }
00084 };
00085 
00086 
00087 static int IsAlpha(int enc, int c)
00088 {
00089   if (enc == ASCII)
00090     return isalpha(c);
00091 
00092   if (c >= 0x41 && c <= 0x5a) return 1;
00093   if (c >= 0x61 && c <= 0x7a) return 1;
00094 
00095   switch (enc) {
00096   case UNICODE_ISO_8859_1:
00097   case ISO_8859_1:
00098   case ISO_8859_9:
00099     if (c == 0xaa) return 1;
00100     if (c == 0xb5) return 1;
00101     if (c == 0xba) return 1;
00102     if (c >= 0xc0 && c <= 0xd6) return 1;
00103     if (c >= 0xd8 && c <= 0xf6) return 1;
00104     if (c >= 0xf8 && c <= 0xff) return 1;
00105     break;
00106 
00107   case ISO_8859_2:
00108     if (c == 0xa1 || c == 0xa3) return 1;
00109     if (c == 0xa5 || c == 0xa6) return 1;
00110     if (c >= 0xa9 && c <= 0xac) return 1;
00111     if (c >= 0xae && c <= 0xaf) return 1;
00112     if (c == 0xb1 || c == 0xb3) return 1;
00113     if (c == 0xb5 || c == 0xb6) return 1;
00114     if (c >= 0xb9 && c <= 0xbc) return 1;
00115     if (c >= 0xbe && c <= 0xbf) return 1;
00116     if (c >= 0xc0 && c <= 0xd6) return 1;
00117     if (c >= 0xd8 && c <= 0xf6) return 1;
00118     if (c >= 0xf8 && c <= 0xfe) return 1;
00119     break;
00120 
00121   case ISO_8859_3:
00122     if (c == 0xa1) return 1;
00123     if (c == 0xa6) return 1;
00124     if (c >= 0xa9 && c <= 0xac) return 1;
00125     if (c == 0xaf) return 1;
00126     if (c == 0xb1) return 1;
00127     if (c == 0xb5 || c == 0xb6) return 1;
00128     if (c >= 0xb9 && c <= 0xbc) return 1;
00129     if (c == 0xbf) return 1;
00130     if (c >= 0xc0 && c <= 0xc2) return 1;
00131     if (c >= 0xc4 && c <= 0xcf) return 1;
00132     if (c >= 0xd1 && c <= 0xd6) return 1;
00133     if (c >= 0xd8 && c <= 0xe2) return 1;
00134     if (c >= 0xe4 && c <= 0xef) return 1;
00135     if (c >= 0xf1 && c <= 0xf6) return 1;
00136     if (c >= 0xf8 && c <= 0xfe) return 1;
00137     break;
00138 
00139   case ISO_8859_4:
00140     if (c >= 0xa1 && c <= 0xa3) return 1;
00141     if (c == 0xa5 || c == 0xa6) return 1;
00142     if (c >= 0xa9 && c <= 0xac) return 1;
00143     if (c == 0xae) return 1;
00144     if (c == 0xb1 || c == 0xb3) return 1;
00145     if (c == 0xb5 || c == 0xb6) return 1;
00146     if (c >= 0xb9 && c <= 0xbf) return 1;
00147     if (c >= 0xc0 && c <= 0xd6) return 1;
00148     if (c >= 0xd8 && c <= 0xf6) return 1;
00149     if (c >= 0xf8 && c <= 0xfe) return 1;
00150     break;
00151 
00152   case ISO_8859_5:
00153     if (c >= 0xa1 && c <= 0xcf && c != 0xad) return 1;
00154     if (c >= 0xd0 && c <= 0xff && c != 0xf0 && c != 0xfd) return 1;
00155     break;
00156 
00157   case ISO_8859_6:
00158     if (c >= 0xc1 && c <= 0xda) return 1;
00159     if (c >= 0xe0 && c <= 0xf2) return 1;
00160     break;
00161 
00162   case ISO_8859_7:
00163     if (c == 0xb6) return 1;
00164     if (c >= 0xb8 && c <= 0xba) return 1;
00165     if (c == 0xbc) return 1;
00166     if (c >= 0xbe && c <= 0xbf) return 1;
00167     if (c == 0xc0) return 1;
00168     if (c >= 0xc1 && c <= 0xdb && c != 0xd2) return 1;
00169     if (c >= 0xdc && c <= 0xfe) return 1;
00170     break;
00171 
00172   case ISO_8859_8:
00173     if (c == 0xb5) return 1;
00174     if (c >= 0xe0 && c <= 0xfa) return 1;
00175     break;
00176 
00177   case ISO_8859_10:
00178     if (c >= 0xa1 && c <= 0xa6) return 1;
00179     if (c >= 0xa8 && c <= 0xac) return 1;
00180     if (c == 0xae || c == 0xaf) return 1;
00181     if (c >= 0xb1 && c <= 0xb6) return 1;
00182     if (c >= 0xb8 && c <= 0xbc) return 1;
00183     if (c >= 0xbe && c <= 0xff) return 1;
00184     break;
00185 
00186   case ISO_8859_11:
00187     if (c >= 0xa1 && c <= 0xda) return 1;
00188     if (c >= 0xdf && c <= 0xfb) return 1;
00189     break;
00190 
00191   case ISO_8859_13:
00192     if (c == 0xa8) return 1;
00193     if (c == 0xaa) return 1;
00194     if (c == 0xaf) return 1;
00195     if (c == 0xb5) return 1;
00196     if (c == 0xb8) return 1;
00197     if (c == 0xba) return 1;
00198     if (c >= 0xbf && c <= 0xd6) return 1;
00199     if (c >= 0xd8 && c <= 0xf6) return 1;
00200     if (c >= 0xf8 && c <= 0xfe) return 1;
00201     break;
00202 
00203   case ISO_8859_14:
00204     if (c == 0xa1 || c == 0xa2) return 1;
00205     if (c == 0xa4 || c == 0xa5) return 1;
00206     if (c == 0xa6 || c == 0xa8) return 1;
00207     if (c >= 0xaa && c <= 0xac) return 1;
00208     if (c >= 0xaf && c <= 0xb5) return 1;
00209     if (c >= 0xb7 && c <= 0xff) return 1;
00210     break;
00211 
00212   case ISO_8859_15:
00213     if (c == 0xaa) return 1;
00214     if (c == 0xb5) return 1;
00215     if (c == 0xba) return 1;
00216     if (c >= 0xc0 && c <= 0xd6) return 1;
00217     if (c >= 0xd8 && c <= 0xf6) return 1;
00218     if (c >= 0xf8 && c <= 0xff) return 1;
00219     if (c == 0xa6) return 1;
00220     if (c == 0xa8) return 1;
00221     if (c == 0xb4) return 1;
00222     if (c == 0xb8) return 1;
00223     if (c == 0xbc) return 1;
00224     if (c == 0xbd) return 1;
00225     if (c == 0xbe) return 1;
00226     break;
00227 
00228   case ISO_8859_16:
00229     if (c == 0xa1) return 1;
00230     if (c == 0xa2) return 1;
00231     if (c == 0xa3) return 1;
00232     if (c == 0xa6) return 1;
00233     if (c == 0xa8) return 1;
00234     if (c == 0xaa) return 1;
00235     if (c == 0xac) return 1;
00236     if (c == 0xae) return 1;
00237     if (c == 0xaf) return 1;
00238     if (c == 0xb2) return 1;
00239     if (c == 0xb3) return 1;
00240     if (c == 0xb4) return 1;
00241     if (c >= 0xb8 && c <= 0xba) return 1;
00242     if (c == 0xbc) return 1;
00243     if (c == 0xbd) return 1;
00244     if (c == 0xbe) return 1;
00245     if (c == 0xbf) return 1;
00246     if (c >= 0xc0 && c <= 0xde) return 1;
00247     if (c >= 0xdf && c <= 0xff) return 1;
00248     break;
00249 
00250   case KOI8_R:
00251     if (c == 0xa3 || c == 0xb3) return 1;
00252     /* fall */
00253   case KOI8:
00254     if (c >= 0xc0 && c <= 0xff) return 1;
00255     break;
00256 
00257   default:
00258     exit(-1);
00259   }
00260 
00261   return 0;
00262 }
00263 
00264 static int IsBlank(int enc, int c)
00265 {
00266   if (enc == ASCII)
00267     return isblank(c);
00268 
00269   if (c == 0x09 || c == 0x20) return 1;
00270 
00271   switch (enc) {
00272   case UNICODE_ISO_8859_1:
00273   case ISO_8859_1:
00274   case ISO_8859_2:
00275   case ISO_8859_3:
00276   case ISO_8859_4:
00277   case ISO_8859_5:
00278   case ISO_8859_6:
00279   case ISO_8859_7:
00280   case ISO_8859_8:
00281   case ISO_8859_9:
00282   case ISO_8859_10:
00283   case ISO_8859_11:
00284   case ISO_8859_13:
00285   case ISO_8859_14:
00286   case ISO_8859_15:
00287   case ISO_8859_16:
00288   case KOI8:
00289     if (c == 0xa0) return 1;
00290     break;
00291 
00292   case KOI8_R:
00293     if (c == 0x9a) return 1;
00294     break;
00295 
00296   default:
00297     exit(-1);
00298   }
00299 
00300   return 0;
00301 }
00302 
00303 static int IsCntrl(int enc, int c)
00304 {
00305   if (enc == ASCII)
00306     return iscntrl(c);
00307 
00308   if (c >= 0x00 && c <= 0x1F) return 1;
00309 
00310   switch (enc) {
00311   case UNICODE_ISO_8859_1:
00312     if (c == 0xad) return 1;
00313     /* fall */
00314   case ISO_8859_1:
00315   case ISO_8859_2:
00316   case ISO_8859_3:
00317   case ISO_8859_4:
00318   case ISO_8859_5:
00319   case ISO_8859_6:
00320   case ISO_8859_7:
00321   case ISO_8859_8:
00322   case ISO_8859_9:
00323   case ISO_8859_10:
00324   case ISO_8859_11:
00325   case ISO_8859_13:
00326   case ISO_8859_14:
00327   case ISO_8859_15:
00328   case ISO_8859_16:
00329   case KOI8:
00330     if (c >= 0x7f && c <= 0x9F) return 1;
00331     break;
00332 
00333 
00334   case KOI8_R:
00335     if (c == 0x7f) return 1;
00336     break;
00337 
00338   default:
00339     exit(-1);
00340   }
00341 
00342   return 0;
00343 }
00344 
00345 static int IsDigit(int enc ARG_UNUSED, int c)
00346 {
00347   if (c >= 0x30 && c <= 0x39) return 1;
00348   return 0;
00349 }
00350 
00351 static int IsGraph(int enc, int c)
00352 {
00353   if (enc == ASCII)
00354     return isgraph(c);
00355 
00356   if (c >= 0x21 && c <= 0x7e) return 1;
00357 
00358   switch (enc) {
00359   case UNICODE_ISO_8859_1:
00360   case ISO_8859_1:
00361   case ISO_8859_2:
00362   case ISO_8859_4:
00363   case ISO_8859_5:
00364   case ISO_8859_9:
00365   case ISO_8859_10:
00366   case ISO_8859_13:
00367   case ISO_8859_14:
00368   case ISO_8859_15:
00369   case ISO_8859_16:
00370     if (c >= 0xa1 && c <= 0xff) return 1;
00371     break;
00372 
00373   case ISO_8859_3:
00374     if (c >= 0xa1) {
00375       if (c == 0xa5 || c == 0xae || c == 0xbe || c == 0xc3 || c == 0xd0 ||
00376           c == 0xe3 || c == 0xf0)
00377         return 0;
00378       else
00379         return 1;
00380     }
00381     break;
00382 
00383   case ISO_8859_6:
00384     if (c == 0xa4 || c == 0xac || c == 0xad || c == 0xbb || c == 0xbf)
00385       return 1;
00386     if (c >= 0xc1 && c <= 0xda) return 1;
00387     if (c >= 0xe0 && c <= 0xf2) return 1;
00388     break;
00389 
00390   case ISO_8859_7:
00391     if (c >= 0xa1 && c <= 0xfe &&
00392         c != 0xa4 && c != 0xa5 && c != 0xaa &&
00393         c != 0xae && c != 0xd2) return 1;
00394     break;
00395 
00396   case ISO_8859_8:
00397     if (c >= 0xa2 && c <= 0xfa) {
00398       if (c >= 0xbf && c <= 0xde) return 0;
00399       return 1;
00400     }
00401     break;
00402 
00403   case ISO_8859_11:
00404     if (c >= 0xa1 && c <= 0xda) return 1;
00405     if (c >= 0xdf && c <= 0xfb) return 1;
00406     break;
00407 
00408   case KOI8:
00409     if (c >= 0xc0 && c <= 0xff) return 1;
00410     break;
00411 
00412   case KOI8_R:
00413     if (c >= 0x80 && c <= 0xff && c != 0x9a) return 1;
00414     break;
00415 
00416   default:
00417     exit(-1);
00418   }
00419 
00420   return 0;
00421 }
00422 
00423 static int IsLower(int enc, int c)
00424 {
00425   if (enc == ASCII)
00426     return islower(c);
00427 
00428   if (c >= 0x61 && c <= 0x7a) return 1;
00429 
00430   switch (enc) {
00431   case UNICODE_ISO_8859_1:
00432   case ISO_8859_1:
00433   case ISO_8859_9:
00434     if (c == 0xaa) return 1;
00435     if (c == 0xb5) return 1;
00436     if (c == 0xba) return 1;
00437     if (c >= 0xdf && c <= 0xf6) return 1;
00438     if (c >= 0xf8 && c <= 0xff) return 1;
00439     break;
00440 
00441   case ISO_8859_2:
00442     if (c == 0xb1 || c == 0xb3) return 1;
00443     if (c == 0xb5 || c == 0xb6) return 1;
00444     if (c >= 0xb9 && c <= 0xbc) return 1;
00445     if (c >= 0xbe && c <= 0xbf) return 1;
00446     if (c >= 0xdf && c <= 0xf6) return 1;
00447     if (c >= 0xf8 && c <= 0xfe) return 1;
00448     break;
00449 
00450   case ISO_8859_3:
00451     if (c == 0xb1) return 1;
00452     if (c == 0xb5 || c == 0xb6) return 1;
00453     if (c >= 0xb9 && c <= 0xbc) return 1;
00454     if (c == 0xbf) return 1;
00455     if (c == 0xdf) return 1;
00456     if (c >= 0xe0 && c <= 0xe2) return 1;
00457     if (c >= 0xe4 && c <= 0xef) return 1;
00458     if (c >= 0xf1 && c <= 0xf6) return 1;
00459     if (c >= 0xf8 && c <= 0xfe) return 1;
00460     break;
00461 
00462   case ISO_8859_4:
00463     if (c == 0xa2) return 1;
00464     if (c == 0xb1 || c == 0xb3) return 1;
00465     if (c == 0xb5 || c == 0xb6) return 1;
00466     if (c >= 0xb9 && c <= 0xbc) return 1;
00467     if (c >= 0xbe && c <= 0xbf) return 1;
00468     if (c == 0xdf) return 1;
00469     if (c >= 0xe0 && c <= 0xf6) return 1;
00470     if (c >= 0xf8 && c <= 0xfe) return 1;
00471     break;
00472 
00473   case ISO_8859_5:
00474     if (c >= 0xd0 && c <= 0xff && c != 0xf0 && c != 0xfd) return 1;
00475     break;
00476 
00477   case ISO_8859_6:
00478     break;
00479 
00480   case ISO_8859_7:
00481     if (c == 0xc0) return 1;
00482     if (c >= 0xdc && c <= 0xfe) return 1;
00483     break;
00484 
00485   case ISO_8859_8:
00486     if (c == 0xb5) return 1;
00487     break;
00488 
00489   case ISO_8859_10:
00490     if (c >= 0xb1 && c <= 0xb6) return 1;
00491     if (c >= 0xb8 && c <= 0xbc) return 1;
00492     if (c == 0xbe || c == 0xbf) return 1;
00493     if (c >= 0xdf && c <= 0xff) return 1;
00494     break;
00495 
00496   case ISO_8859_11:
00497     break;
00498 
00499   case ISO_8859_13:
00500     if (c == 0xb5) return 1;
00501     if (c == 0xb8) return 1;
00502     if (c == 0xba) return 1;
00503     if (c == 0xbf) return 1;
00504     if (c >= 0xdf && c <= 0xf6) return 1;
00505     if (c >= 0xf8 && c <= 0xfe) return 1;
00506     break;
00507 
00508   case ISO_8859_14:
00509     if (c == 0xa2) return 1;
00510     if (c == 0xa5) return 1;
00511     if (c == 0xab) return 1;
00512     if (c == 0xb1 || c == 0xb3 || c == 0xb5) return 1;
00513     if (c >= 0xb8 && c <= 0xba) return 1;
00514     if (c == 0xbc) return 1;
00515     if (c == 0xbe || c == 0xbf) return 1;
00516     if (c >= 0xdf && c <= 0xff) return 1;
00517     break;
00518 
00519   case ISO_8859_15:
00520     if (c == 0xaa) return 1;
00521     if (c == 0xb5) return 1;
00522     if (c == 0xba) return 1;
00523     if (c >= 0xdf && c <= 0xf6) return 1;
00524     if (c >= 0xf8 && c <= 0xff) return 1;
00525     if (c == 0xa8) return 1;
00526     if (c == 0xb8) return 1;
00527     if (c == 0xbd) return 1;
00528     break;
00529 
00530   case ISO_8859_16:
00531     if (c == 0xa2) return 1;
00532     if (c == 0xa8) return 1;
00533     if (c == 0xae) return 1;
00534     if (c == 0xb3) return 1;
00535     if (c >= 0xb8 && c <= 0xba) return 1;
00536     if (c == 0xbd) return 1;
00537     if (c == 0xbf) return 1;
00538     if (c >= 0xdf && c <= 0xff) return 1;
00539     break;
00540 
00541   case KOI8_R:
00542     if (c == 0xa3) return 1;
00543     /* fall */
00544   case KOI8:
00545     if (c >= 0xc0 && c <= 0xdf) return 1;
00546     break;
00547 
00548   default:
00549     exit(-1);
00550   }
00551 
00552   return 0;
00553 }
00554 
00555 static int IsPrint(int enc, int c)
00556 {
00557   if (enc == ASCII)
00558     return isprint(c);
00559 
00560   if (c >= 0x20 && c <= 0x7e) return 1;
00561 
00562   switch (enc) {
00563   case UNICODE_ISO_8859_1:
00564     /* if (c >= 0x09 && c <= 0x0d) return 1; */
00565     if (c == 0x85) return 1;
00566     /* fall */
00567   case ISO_8859_1:
00568   case ISO_8859_2:
00569   case ISO_8859_4:
00570   case ISO_8859_5:
00571   case ISO_8859_9:
00572   case ISO_8859_10:
00573   case ISO_8859_13:
00574   case ISO_8859_14:
00575   case ISO_8859_15:
00576   case ISO_8859_16:
00577     if (c >= 0xa0 && c <= 0xff) return 1;
00578     break;
00579 
00580   case ISO_8859_3:
00581     if (c >= 0xa0) {
00582       if (c == 0xa5 || c == 0xae || c == 0xbe || c == 0xc3 || c == 0xd0 ||
00583           c == 0xe3 || c == 0xf0)
00584         return 0;
00585       else
00586         return 1;
00587     }
00588     break;
00589 
00590   case ISO_8859_6:
00591     if (c == 0xa0) return 1;
00592     if (c == 0xa4 || c == 0xac || c == 0xad || c == 0xbb || c == 0xbf)
00593       return 1;
00594     if (c >= 0xc1 && c <= 0xda) return 1;
00595     if (c >= 0xe0 && c <= 0xf2) return 1;
00596     break;
00597 
00598   case ISO_8859_7:
00599     if (c >= 0xa0 && c <= 0xfe &&
00600         c != 0xa4 && c != 0xa5 && c != 0xaa &&
00601         c != 0xae && c != 0xd2) return 1;
00602     break;
00603 
00604   case ISO_8859_8:
00605     if (c >= 0xa0 && c <= 0xfa) {
00606       if (c >= 0xbf && c <= 0xde) return 0;
00607       if (c == 0xa1) return 0;
00608       return 1;
00609     }
00610     break;
00611 
00612   case ISO_8859_11:
00613     if (c >= 0xa0 && c <= 0xda) return 1;
00614     if (c >= 0xdf && c <= 0xfb) return 1;
00615     break;
00616 
00617   case KOI8:
00618     if (c == 0xa0) return 1;
00619     if (c >= 0xc0 && c <= 0xff) return 1;
00620     break;
00621 
00622   case KOI8_R:
00623     if (c >= 0x80 && c <= 0xff) return 1;
00624     break;
00625 
00626   default:
00627     exit(-1);
00628   }
00629 
00630   return 0;
00631 }
00632 
00633 static int IsPunct(int enc, int c)
00634 {
00635   if (enc == ASCII)
00636     return ispunct(c);
00637 
00638   if (enc == UNICODE_ISO_8859_1) {
00639     if (c == 0x24 || c == 0x2b || c == 0x5e || c == 0x60 ||
00640         c == 0x7c || c == 0x7e) return 1;
00641     if (c >= 0x3c && c <= 0x3e) return 1;
00642   }
00643 
00644   if (c >= 0x21 && c <= 0x2f) return 1;
00645   if (c >= 0x3a && c <= 0x40) return 1;
00646   if (c >= 0x5b && c <= 0x60) return 1;
00647   if (c >= 0x7b && c <= 0x7e) return 1;
00648 
00649   switch (enc) {
00650   case ISO_8859_1:
00651   case ISO_8859_9:
00652   case ISO_8859_15:
00653     if (c == 0xad) return 1;
00654     /* fall */
00655   case UNICODE_ISO_8859_1:
00656     if (c == 0xa1) return 1;
00657     if (c == 0xab) return 1;
00658     if (c == 0xb7) return 1;
00659     if (c == 0xbb) return 1;
00660     if (c == 0xbf) return 1;
00661     break;
00662 
00663   case ISO_8859_2:
00664   case ISO_8859_4:
00665   case ISO_8859_5:
00666   case ISO_8859_14:
00667     if (c == 0xad) return 1;
00668     break;
00669 
00670   case ISO_8859_3:
00671   case ISO_8859_10:
00672     if (c == 0xad) return 1;
00673     if (c == 0xb7) return 1;
00674     if (c == 0xbd) return 1;
00675     break;
00676 
00677   case ISO_8859_6:
00678     if (c == 0xac) return 1;
00679     if (c == 0xad) return 1;
00680     if (c == 0xbb) return 1;
00681     if (c == 0xbf) return 1;
00682     break;
00683 
00684   case ISO_8859_7:
00685     if (c == 0xa1 || c == 0xa2) return 1;
00686     if (c == 0xab) return 1;
00687     if (c == 0xaf) return 1;
00688     if (c == 0xad) return 1;
00689     if (c == 0xb7 || c == 0xbb) return 1;
00690     break;
00691 
00692   case ISO_8859_8:
00693     if (c == 0xab) return 1;
00694     if (c == 0xad) return 1;
00695     if (c == 0xb7) return 1;
00696     if (c == 0xbb) return 1;
00697     if (c == 0xdf) return 1;
00698     break;
00699 
00700   case ISO_8859_13:
00701     if (c == 0xa1 || c == 0xa5) return 1;
00702     if (c == 0xab || c == 0xad) return 1;
00703     if (c == 0xb4 || c == 0xb7) return 1;
00704     if (c == 0xbb) return 1;
00705     if (c == 0xff) return 1;
00706     break;
00707 
00708   case ISO_8859_16:
00709     if (c == 0xa5) return 1;
00710     if (c == 0xab) return 1;
00711     if (c == 0xad) return 1;
00712     if (c == 0xb5) return 1;
00713     if (c == 0xb7) return 1;
00714     if (c == 0xbb) return 1;
00715     break;
00716 
00717   case KOI8_R:
00718     if (c == 0x9e) return 1;
00719     break;
00720 
00721   case ISO_8859_11:
00722   case KOI8:
00723     break;
00724 
00725   default:
00726     exit(-1);
00727   }
00728 
00729   return 0;
00730 }
00731 
00732 static int IsSpace(int enc, int c)
00733 {
00734   if (enc == ASCII)
00735     return isspace(c);
00736 
00737   if (c >= 0x09 && c <= 0x0d) return 1;
00738   if (c == 0x20) return 1;
00739 
00740   switch (enc) {
00741   case UNICODE_ISO_8859_1:
00742     if (c == 0x85) return 1;
00743     /* fall */
00744   case ISO_8859_1:
00745   case ISO_8859_2:
00746   case ISO_8859_3:
00747   case ISO_8859_4:
00748   case ISO_8859_5:
00749   case ISO_8859_6:
00750   case ISO_8859_7:
00751   case ISO_8859_8:
00752   case ISO_8859_9:
00753   case ISO_8859_10:
00754   case ISO_8859_11:
00755   case ISO_8859_13:
00756   case ISO_8859_14:
00757   case ISO_8859_15:
00758   case ISO_8859_16:
00759   case KOI8:
00760     if (c == 0xa0) return 1;
00761     break;
00762 
00763   case KOI8_R:
00764     if (c == 0x9a) return 1;
00765     break;
00766 
00767   default:
00768     exit(-1);
00769   }
00770 
00771   return 0;
00772 }
00773 
00774 static int IsUpper(int enc, int c)
00775 {
00776   if (enc == ASCII)
00777     return isupper(c);
00778 
00779   if (c >= 0x41 && c <= 0x5a) return 1;
00780 
00781   switch (enc) {
00782   case UNICODE_ISO_8859_1:
00783   case ISO_8859_1:
00784   case ISO_8859_9:
00785     if (c >= 0xc0 && c <= 0xd6) return 1;
00786     if (c >= 0xd8 && c <= 0xde) return 1;
00787     break;
00788 
00789   case ISO_8859_2:
00790     if (c == 0xa1 || c == 0xa3) return 1;
00791     if (c == 0xa5 || c == 0xa6) return 1;
00792     if (c >= 0xa9 && c <= 0xac) return 1;
00793     if (c >= 0xae && c <= 0xaf) return 1;
00794     if (c >= 0xc0 && c <= 0xd6) return 1;
00795     if (c >= 0xd8 && c <= 0xde) return 1;
00796     break;
00797 
00798   case ISO_8859_3:
00799     if (c == 0xa1) return 1;
00800     if (c == 0xa6) return 1;
00801     if (c >= 0xa9 && c <= 0xac) return 1;
00802     if (c == 0xaf) return 1;
00803     if (c >= 0xc0 && c <= 0xc2) return 1;
00804     if (c >= 0xc4 && c <= 0xcf) return 1;
00805     if (c >= 0xd1 && c <= 0xd6) return 1;
00806     if (c >= 0xd8 && c <= 0xde) return 1;
00807     break;
00808 
00809   case ISO_8859_4:
00810     if (c == 0xa1 || c == 0xa3) return 1;
00811     if (c == 0xa5 || c == 0xa6) return 1;
00812     if (c >= 0xa9 && c <= 0xac) return 1;
00813     if (c == 0xae) return 1;
00814     if (c == 0xbd) return 1;
00815     if (c >= 0xc0 && c <= 0xd6) return 1;
00816     if (c >= 0xd8 && c <= 0xde) return 1;
00817     break;
00818 
00819   case ISO_8859_5:
00820     if (c >= 0xa1 && c <= 0xcf && c != 0xad) return 1;
00821     break;
00822 
00823   case ISO_8859_6:
00824     break;
00825 
00826   case ISO_8859_7:
00827     if (c == 0xb6) return 1;
00828     if (c >= 0xb8 && c <= 0xba) return 1;
00829     if (c == 0xbc) return 1;
00830     if (c >= 0xbe && c <= 0xbf) return 1;
00831     if (c >= 0xc1 && c <= 0xdb && c != 0xd2) return 1;
00832     break;
00833 
00834   case ISO_8859_8:
00835   case ISO_8859_11:
00836     break;
00837 
00838   case ISO_8859_10:
00839     if (c >= 0xa1 && c <= 0xa6) return 1;
00840     if (c >= 0xa8 && c <= 0xac) return 1;
00841     if (c == 0xae || c == 0xaf) return 1;
00842     if (c >= 0xc0 && c <= 0xde) return 1;
00843     break;
00844 
00845   case ISO_8859_13:
00846     if (c == 0xa8) return 1;
00847     if (c == 0xaa) return 1;
00848     if (c == 0xaf) return 1;
00849     if (c >= 0xc0 && c <= 0xd6) return 1;
00850     if (c >= 0xd8 && c <= 0xde) return 1;
00851     break;
00852 
00853   case ISO_8859_14:
00854     if (c == 0xa1) return 1;
00855     if (c == 0xa4 || c == 0xa6) return 1;
00856     if (c == 0xa8) return 1;
00857     if (c == 0xaa || c == 0xac) return 1;
00858     if (c == 0xaf || c == 0xb0) return 1;
00859     if (c == 0xb2 || c == 0xb4 || c == 0xb7) return 1;
00860     if (c == 0xbb || c == 0xbd) return 1;
00861     if (c >= 0xc0 && c <= 0xde) return 1;
00862     break;
00863 
00864   case ISO_8859_15:
00865     if (c >= 0xc0 && c <= 0xd6) return 1;
00866     if (c >= 0xd8 && c <= 0xde) return 1;
00867     if (c == 0xa6) return 1;
00868     if (c == 0xb4) return 1;
00869     if (c == 0xbc) return 1;
00870     if (c == 0xbe) return 1;
00871     break;
00872 
00873   case ISO_8859_16:
00874     if (c == 0xa1) return 1;
00875     if (c == 0xa3) return 1;
00876     if (c == 0xa6) return 1;
00877     if (c == 0xaa) return 1;
00878     if (c == 0xac) return 1;
00879     if (c == 0xaf) return 1;
00880     if (c == 0xb2) return 1;
00881     if (c == 0xb4) return 1;
00882     if (c == 0xbc) return 1;
00883     if (c == 0xbe) return 1;
00884     if (c >= 0xc0 && c <= 0xde) return 1;
00885     break;
00886 
00887   case KOI8_R:
00888     if (c == 0xb3) return 1;
00889     /* fall */
00890   case KOI8:
00891     if (c >= 0xe0 && c <= 0xff) return 1;
00892     break;
00893 
00894   default:
00895     exit(-1);
00896   }
00897 
00898   return 0;
00899 }
00900 
00901 static int IsXDigit(int enc, int c)
00902 {
00903   if (enc == ASCII)
00904     return isxdigit(c);
00905 
00906   if (c >= 0x30 && c <= 0x39) return 1;
00907   if (c >= 0x41 && c <= 0x46) return 1;
00908   if (c >= 0x61 && c <= 0x66) return 1;
00909   return 0;
00910 }
00911 
00912 static int IsWord(int enc, int c)
00913 {
00914   if (enc == ASCII) {
00915     return (isalpha(c) || isdigit(c) || c == 0x5f);
00916   }
00917 
00918   if (c >= 0x30 && c <= 0x39) return 1;
00919   if (c >= 0x41 && c <= 0x5a) return 1;
00920   if (c == 0x5f) return 1;
00921   if (c >= 0x61 && c <= 0x7a) return 1;
00922 
00923   switch (enc) {
00924   case UNICODE_ISO_8859_1:
00925   case ISO_8859_1:
00926   case ISO_8859_9:
00927     if (c == 0xaa) return 1;
00928     if (c >= 0xb2 && c <= 0xb3) return 1;
00929     if (c == 0xb5) return 1;
00930     if (c >= 0xb9 && c <= 0xba) return 1;
00931     if (c >= 0xbc && c <= 0xbe) return 1;
00932     if (c >= 0xc0 && c <= 0xd6) return 1;
00933     if (c >= 0xd8 && c <= 0xf6) return 1;
00934     if (c >= 0xf8 && c <= 0xff) return 1;
00935     break;
00936 
00937   case ISO_8859_2:
00938     if (c == 0xa1 || c == 0xa3) return 1;
00939     if (c == 0xa5 || c == 0xa6) return 1;
00940     if (c >= 0xa9 && c <= 0xac) return 1;
00941     if (c >= 0xae && c <= 0xaf) return 1;
00942     if (c == 0xb1 || c == 0xb3) return 1;
00943     if (c == 0xb5 || c == 0xb6) return 1;
00944     if (c >= 0xb9 && c <= 0xbc) return 1;
00945     if (c >= 0xbe && c <= 0xbf) return 1;
00946     if (c >= 0xc0 && c <= 0xd6) return 1;
00947     if (c >= 0xd8 && c <= 0xf6) return 1;
00948     if (c >= 0xf8 && c <= 0xfe) return 1;
00949     break;
00950 
00951   case ISO_8859_3:
00952     if (c == 0xa1) return 1;
00953     if (c == 0xa6) return 1;
00954     if (c >= 0xa9 && c <= 0xac) return 1;
00955     if (c == 0xaf) return 1;
00956     if (c >= 0xb1 && c <= 0xb3) return 1;
00957     if (c == 0xb5 || c == 0xb6) return 1;
00958     if (c >= 0xb9 && c <= 0xbd) return 1;
00959     if (c == 0xbf) return 1;
00960     if (c >= 0xc0 && c <= 0xc2) return 1;
00961     if (c >= 0xc4 && c <= 0xcf) return 1;
00962     if (c >= 0xd1 && c <= 0xd6) return 1;
00963     if (c >= 0xd8 && c <= 0xe2) return 1;
00964     if (c >= 0xe4 && c <= 0xef) return 1;
00965     if (c >= 0xf1 && c <= 0xf6) return 1;
00966     if (c >= 0xf8 && c <= 0xfe) return 1;
00967     break;
00968 
00969   case ISO_8859_4:
00970     if (c >= 0xa1 && c <= 0xa3) return 1;
00971     if (c == 0xa5 || c == 0xa6) return 1;
00972     if (c >= 0xa9 && c <= 0xac) return 1;
00973     if (c == 0xae) return 1;
00974     if (c == 0xb1 || c == 0xb3) return 1;
00975     if (c == 0xb5 || c == 0xb6) return 1;
00976     if (c >= 0xb9 && c <= 0xbf) return 1;
00977     if (c >= 0xc0 && c <= 0xd6) return 1;
00978     if (c >= 0xd8 && c <= 0xf6) return 1;
00979     if (c >= 0xf8 && c <= 0xfe) return 1;
00980     break;
00981 
00982   case ISO_8859_5:
00983     if (c >= 0xa1 && c <= 0xcf && c != 0xad) return 1;
00984     if (c >= 0xd0 && c <= 0xff && c != 0xf0 && c != 0xfd) return 1;
00985     break;
00986 
00987   case ISO_8859_6:
00988     if (c >= 0xc1 && c <= 0xda) return 1;
00989     if (c >= 0xe0 && c <= 0xea) return 1;
00990     if (c >= 0xeb && c <= 0xf2) return 1;
00991     break;
00992 
00993   case ISO_8859_7:
00994     if (c == 0xb2 || c == 0xb3) return 1;
00995     if (c == 0xb6) return 1;
00996     if (c >= 0xb8 && c <= 0xba) return 1;
00997     if (c >= 0xbc && c <= 0xbf) return 1;
00998     if (c == 0xc0) return 1;
00999     if (c >= 0xc1 && c <= 0xdb && c != 0xd2) return 1;
01000     if (c >= 0xdc && c <= 0xfe) return 1;
01001     break;
01002 
01003   case ISO_8859_8:
01004     if (c == 0xb2 || c == 0xb3 || c == 0xb5 || c == 0xb9) return 1;
01005     if (c >= 0xbc && c <= 0xbe) return 1;
01006     if (c >= 0xe0 && c <= 0xfa) return 1;
01007     break;
01008 
01009   case ISO_8859_10:
01010     if (c >= 0xa1 && c <= 0xff) {
01011       if (c != 0xa7 && c != 0xad && c != 0xb0 && c != 0xb7 && c != 0xbd)
01012         return 1;
01013     }
01014     break;
01015 
01016   case ISO_8859_11:
01017     if (c >= 0xa1 && c <= 0xda) return 1;
01018     if (c >= 0xdf && c <= 0xfb) return 1;
01019     break;
01020 
01021   case ISO_8859_13:
01022     if (c == 0xa8) return 1;
01023     if (c == 0xaa) return 1;
01024     if (c == 0xaf) return 1;
01025     if (c == 0xb2 || c == 0xb3 || c == 0xb5 || c == 0xb9) return 1;
01026     if (c >= 0xbc && c <= 0xbe) return 1;
01027     if (c == 0xb8) return 1;
01028     if (c == 0xba) return 1;
01029     if (c >= 0xbf && c <= 0xd6) return 1;
01030     if (c >= 0xd8 && c <= 0xf6) return 1;
01031     if (c >= 0xf8 && c <= 0xfe) return 1;
01032     break;
01033 
01034   case ISO_8859_14:
01035     if (c >= 0xa1 && c <= 0xff) {
01036       if (c == 0xa3 || c == 0xa7 || c == 0xa9 || c == 0xad || c == 0xae ||
01037           c == 0xb6) return 0;
01038       return 1;
01039     }
01040     break;
01041 
01042   case ISO_8859_15:
01043     if (c == 0xaa) return 1;
01044     if (c >= 0xb2 && c <= 0xb3) return 1;
01045     if (c == 0xb5) return 1;
01046     if (c >= 0xb9 && c <= 0xba) return 1;
01047     if (c >= 0xbc && c <= 0xbe) return 1;
01048     if (c >= 0xc0 && c <= 0xd6) return 1;
01049     if (c >= 0xd8 && c <= 0xf6) return 1;
01050     if (c >= 0xf8 && c <= 0xff) return 1;
01051     if (c == 0xa6) return 1;
01052     if (c == 0xa8) return 1;
01053     if (c == 0xb4) return 1;
01054     if (c == 0xb8) return 1;
01055     break;
01056 
01057   case ISO_8859_16:
01058     if (c == 0xa1) return 1;
01059     if (c == 0xa2) return 1;
01060     if (c == 0xa3) return 1;
01061     if (c == 0xa6) return 1;
01062     if (c == 0xa8) return 1;
01063     if (c == 0xaa) return 1;
01064     if (c == 0xac) return 1;
01065     if (c == 0xae) return 1;
01066     if (c == 0xaf) return 1;
01067     if (c == 0xb2) return 1;
01068     if (c == 0xb3) return 1;
01069     if (c == 0xb4) return 1;
01070     if (c >= 0xb8 && c <= 0xba) return 1;
01071     if (c == 0xbc) return 1;
01072     if (c == 0xbd) return 1;
01073     if (c == 0xbe) return 1;
01074     if (c == 0xbf) return 1;
01075     if (c >= 0xc0 && c <= 0xde) return 1;
01076     if (c >= 0xdf && c <= 0xff) return 1;
01077     break;
01078 
01079   case KOI8_R:
01080     if (c == 0x9d) return 1;
01081     if (c == 0xa3 || c == 0xb3) return 1;
01082     /* fall */
01083   case KOI8:
01084     if (c >= 0xc0 && c <= 0xff) return 1;
01085     break;
01086 
01087   default:
01088     exit(-1);
01089   }
01090 
01091   return 0;
01092 }
01093 
01094 static int IsAscii(int enc ARG_UNUSED, int c)
01095 {
01096   if (c >= 0x00 && c <= 0x7f) return 1;
01097   return 0;
01098 }
01099 
01100 static int IsNewline(int enc ARG_UNUSED, int c)
01101 {
01102   if (c == 0x0a) return 1;
01103   return 0;
01104 }
01105 
01106 static int exec(FILE* fp, ENC_INFO* einfo)
01107 {
01108 #define NCOL  8
01109 
01110   int c, val, enc;
01111 
01112   enc = einfo->num;
01113 
01114   fprintf(fp, "static const unsigned short Enc%s_CtypeTable[256] = {\n",
01115           einfo->name);
01116 
01117   for (c = 0; c < 256; c++) {
01118     val = 0;
01119     if (IsNewline(enc, c))  val |= BIT_CTYPE_NEWLINE;
01120     if (IsAlpha (enc, c))   val |= (BIT_CTYPE_ALPHA | BIT_CTYPE_ALNUM);
01121     if (IsBlank (enc, c))   val |= BIT_CTYPE_BLANK;
01122     if (IsCntrl (enc, c))   val |= BIT_CTYPE_CNTRL;
01123     if (IsDigit (enc, c))   val |= (BIT_CTYPE_DIGIT | BIT_CTYPE_ALNUM);
01124     if (IsGraph (enc, c))   val |= BIT_CTYPE_GRAPH;
01125     if (IsLower (enc, c))   val |= BIT_CTYPE_LOWER;
01126     if (IsPrint (enc, c))   val |= BIT_CTYPE_PRINT;
01127     if (IsPunct (enc, c))   val |= BIT_CTYPE_PUNCT;
01128     if (IsSpace (enc, c))   val |= BIT_CTYPE_SPACE;
01129     if (IsUpper (enc, c))   val |= BIT_CTYPE_UPPER;
01130     if (IsXDigit(enc, c))   val |= BIT_CTYPE_XDIGIT;
01131     if (IsWord  (enc, c))   val |= BIT_CTYPE_WORD;
01132     if (IsAscii (enc, c))   val |= BIT_CTYPE_ASCII;
01133 
01134     if (c % NCOL == 0) fputs("  ", fp);
01135     fprintf(fp, "0x%04x", val);
01136     if (c != 255) fputs(",", fp);
01137     if (c != 0 && c % NCOL == (NCOL-1))
01138       fputs("\n", fp);
01139     else
01140       fputs(" ", fp);
01141   }
01142   fprintf(fp, "};\n");
01143   return 0;
01144 }
01145 
01146 extern int main(int argc ARG_UNUSED, char* argv[] ARG_UNUSED)
01147 {
01148   int i;
01149   FILE* fp = stdout;
01150 
01151   setlocale(LC_ALL, "C");
01152   /* setlocale(LC_ALL, "POSIX"); */
01153   /* setlocale(LC_ALL, "en_GB.iso88591"); */
01154   /* setlocale(LC_ALL, "de_BE.iso88591"); */
01155   /* setlocale(LC_ALL, "fr_FR.iso88591"); */
01156 
01157   for (i = 0; i < (int )(sizeof(Info)/sizeof(ENC_INFO)); i++) {
01158     exec(fp, &Info[i]);
01159   }
01160 
01161   return 0;
01162 }
01163 

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7