home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / GNU / recode-3.4-MIHS / src / ibmpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  9.0 KB  |  324 lines

  1. /* Conversion of files between different charsets and usages.
  2.    Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
  3.    Francois Pinard <pinard@iro.umontreal.ca>, 1988.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful, but
  11.    WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "recode.h"
  21.  
  22. #define DOS_EOF 26        /* old end of file */
  23. #define DOS_CR 13        /* carriage return */
  24. #define DOS_LF 10        /* line feed */
  25.  
  26. /* Correspondance for IBM PC ruler graphics characters into ASCII graphics
  27.    approximations.  The current principles are:
  28.  
  29.    - Single horizontal rulers are made up of dashes.
  30.    - Double horizontal rulers are made up of equal signs.
  31.    - Both single and double vertical are made up with `|'.
  32.    - Both upper corners are rounded down with periods.
  33.    - Lower corners are rounded up with grave/acute accent on left/right.
  34.    - Other crossing rulers are approximated with plus signs, with exceptions
  35.      for double horizontal ruler crossings but not at corners: they are
  36.      equal signs inside a table, and `|' at left or right margin.
  37. */
  38.  
  39. unsigned char convert_rulers[48] =
  40.   {
  41.     '#',            /* 176 */
  42.     '#',            /* 177 */
  43.     '#',            /* 178 */
  44.     '|',            /* 179 */
  45.     '+',            /* 180 */
  46.     '|',            /* 181 */
  47.     '+',            /* 182 */
  48.     '.',            /* 183 */
  49.     '.',            /* 184 */
  50.     '|',            /* 185 */
  51.     '|',            /* 186 */
  52.     '.',            /* 187 */
  53.     '\'',            /* 188 */
  54.     '\'',            /* 189 */
  55.     '\'',            /* 190 */
  56.     '.',            /* 191 */
  57.     '`',            /* 192 */
  58.     '+',            /* 193 */
  59.     '+',            /* 194 */
  60.     '+',            /* 195 */
  61.     '-',            /* 196 */
  62.     '+',            /* 197 */
  63.     '|',            /* 198 */
  64.     '+',            /* 199 */
  65.     '`',            /* 200 */
  66.     '.',            /* 201 */
  67.     '=',            /* 202 */
  68.     '=',            /* 203 */
  69.     '|',            /* 204 */
  70.     '=',            /* 205 */
  71.     '=',            /* 206 */
  72.     '=',            /* 207 */
  73.     '+',            /* 208 */
  74.     '=',            /* 209 */
  75.     '+',            /* 210 */
  76.     '`',            /* 211 */
  77.     '`',            /* 212 */
  78.     '.',            /* 213 */
  79.     '.',            /* 214 */
  80.     '+',            /* 215 */
  81.     '=',            /* 216 */
  82.     '\'',            /* 217 */
  83.     '.',            /* 218 */
  84.     '#',            /* 219 */
  85.     '#',            /* 220 */
  86.     '#',            /* 221 */
  87.     '#',            /* 222 */
  88.     '#',            /* 223 */
  89.   };
  90.  
  91. /* Data for IBM PC to ISO Latin-1 code conversions.  */
  92.  
  93. static KNOWN_PAIR known_pairs[] =
  94.   {
  95.     { 20, 182},            /* pilcrow sign */
  96.     { 21, 167},            /* section sign */
  97.  
  98.     {128, 199},            /* capital letter C with cedilla */
  99.     {129, 252},            /* small letter u with diaeresis */
  100.     {130, 233},            /* small letter e with acute accent */
  101.     {131, 226},            /* small letter a with circumflex accent */
  102.     {132, 228},            /* small letter a with diaeresis */
  103.     {133, 224},            /* small letter a with grave accent */
  104.     {134, 229},            /* small letter a with ring above */
  105.     {135, 231},            /* small letter c with cedilla */
  106.     {136, 234},            /* small letter e with circumflex accent */
  107.     {137, 235},            /* small letter e with diaeresis */
  108.     {138, 232},            /* small letter e with grave accent */
  109.     {139, 239},            /* small letter i with diaeresis */
  110.     {140, 238},            /* small letter i with circumflex accent */
  111.     {141, 236},            /* small letter i with grave accent */
  112.     {142, 196},            /* capital letter A with diaeresis */
  113.     {143, 197},            /* capital letter A with ring above */
  114.     {144, 201},            /* capital letter E with acute accent */
  115.     {145, 230},            /* small ligature a with e */
  116.     {146, 198},            /* capital ligature A with E */
  117.     {147, 244},            /* small letter o with circumblex accent */
  118.     {148, 246},            /* small letter o with diaeresis */
  119.     {149, 242},            /* small letter o with grave accent */
  120.     {150, 251},            /* small letter u with circumflex accent */
  121.     {151, 249},            /* small letter u with grave accent */
  122.     {152, 255},            /* small letter y with diaeresis */
  123.     {153, 214},            /* capital letter O with diaeresis */
  124.     {154, 220},            /* capital letter U with diaeresis */
  125.     {155, 162},            /* cent sign */
  126.     {156, 163},            /* pound sign */
  127.     {157, 165},            /* yen sign */
  128.  
  129.     {160, 225},            /* small letter a with acute accent */
  130.     {161, 237},            /* small letter i with acute accent */
  131.     {162, 243},            /* small letter o with acute accent */
  132.     {163, 250},            /* small letter u with acute accent */
  133.     {164, 241},            /* small letter n with tilde */
  134.     {165, 209},            /* capital letter N with tilde */
  135.     {166, 170},            /* feminine ordinal indicator */
  136.     {167, 186},            /* masculine ordinal indicator */
  137.     {168, 191},            /* inverted question mark */
  138.  
  139.     {170, 172},            /* not sign */
  140.     {171, 189},            /* vulgar fraction one half */
  141.     {172, 188},            /* vulgar fraction one quarter */
  142.     {173, 161},            /* inverted exclamation mark */
  143.     {174, 171},            /* left angle quotation mark */
  144.     {175, 187},            /* right angle quotation mark */
  145.  
  146.     {225, 223},            /* small german letter sharp s */
  147.  
  148.     {230, 181},            /* small Greek letter mu micro sign */
  149.  
  150.     {241, 177},            /* plus-minus sign */
  151.  
  152.     {246, 247},            /* division sign */
  153.  
  154.     {248, 176},            /* degree sign */
  155.  
  156.     {250, 183},            /* middle dot */
  157.  
  158.     {253, 178},            /* superscript two */
  159.  
  160.     {255, 160},            /* no-break space */
  161.   };
  162. #define NUMBER_OF_PAIRS (sizeof (known_pairs) / sizeof (KNOWN_PAIR))
  163.  
  164. static int
  165. file_latin1_ibmpc (const STEP *step, FILE *input_file, FILE *output_file)
  166. {
  167.   int reversible;        /* reversibility of recoding */
  168.   const unsigned char *table;    /* one to one conversion table */
  169.   const char *const *table2;    /* one to many conversion table */
  170.   int input_char;        /* current character */
  171.  
  172.   reversible = 1;
  173.   if (strict_mapping)
  174.     {
  175.       table2 = step->one_to_many;
  176.       while (input_char = getc (input_file), input_char != EOF)
  177.     if (input_char == '\n')
  178.       {
  179.         putc (DOS_CR, output_file);
  180.         putc (DOS_LF, output_file);
  181.       }
  182.     else if (table2[input_char])
  183.       putc (*table2[input_char], output_file);
  184.     else
  185.       reversible = 0;
  186.     }
  187.   else
  188.     {
  189.       table = step->one_to_one;
  190.       while (input_char = getc (input_file), input_char != EOF)
  191.     if (input_char == '\n')
  192.       {
  193.         putc (DOS_CR, output_file);
  194.         putc (DOS_LF, output_file);
  195.       }
  196.     else
  197.       putc (table[input_char], output_file);
  198.     }
  199.   return reversible;
  200. }
  201.  
  202. static int
  203. file_ibmpc_latin1 (const STEP *step, FILE *input_file, FILE *output_file)
  204. {
  205.   int reversible;        /* reversibility of recoding */
  206.   const unsigned char *table;    /* one to one conversion table */
  207.   const char *const *table2;    /* one to many conversion table */
  208.   int input_char;        /* current character */
  209.  
  210.   reversible = 1;
  211.   if (strict_mapping)
  212.     {
  213.       table2 = step->one_to_many;
  214.       input_char = getc (input_file);
  215.       while (1)
  216.     switch (input_char)
  217.       {
  218.       case EOF:
  219.         return reversible;
  220.  
  221.       case DOS_EOF:
  222.         return 0;
  223.  
  224.       case DOS_CR:
  225.         input_char = getc (input_file);
  226.         if (input_char == DOS_LF)
  227.           {
  228.         putc ('\n', output_file);
  229.         input_char = getc (input_file);
  230.           }
  231.         else if (table2[DOS_CR])
  232.           putc (*table2[DOS_CR], output_file);
  233.         else
  234.           reversible = 0;
  235.         break;
  236.  
  237.       case DOS_LF:
  238.         reversible = 0;
  239.         /* Fall through.  */
  240.  
  241.       default:
  242.         if (table2[input_char])
  243.           putc (*table2[input_char], output_file);
  244.         else
  245.           reversible = 0;
  246.         input_char = getc (input_file);
  247.       }
  248.     }
  249.   else
  250.     {
  251.       table = step->one_to_one;
  252.       input_char = getc (input_file);
  253.       while (1)
  254.     switch (input_char)
  255.       {
  256.       case EOF:
  257.         return reversible;
  258.  
  259.       case DOS_EOF:
  260.         return 0;
  261.  
  262.       case DOS_CR:
  263.         input_char = getc (input_file);
  264.         if (input_char == DOS_LF)
  265.           {
  266.         putc ('\n', output_file);
  267.         input_char = getc (input_file);
  268.           }
  269.         else
  270.           putc (table[DOS_CR], output_file);
  271.         break;
  272.  
  273.       case DOS_LF:
  274.         reversible = 0;
  275.         /* Fall through.  */
  276.  
  277.       default:
  278.         putc (table[input_char], output_file);
  279.         input_char = getc (input_file);
  280.       }
  281.     }
  282. }
  283.  
  284. static void
  285. init_latin1_ibmpc (STEP *step)
  286. {
  287.   complete_pairs (step, 1, known_pairs, NUMBER_OF_PAIRS, 1);
  288.   step->file_recode = file_latin1_ibmpc;
  289. }
  290.  
  291. static void
  292. init_ibmpc_latin1 (STEP *step)
  293. {
  294.   unsigned char *table;
  295.  
  296.   complete_pairs (step, 1, known_pairs, NUMBER_OF_PAIRS, 0);
  297.   step->file_recode = file_ibmpc_latin1;
  298.  
  299.   /* FIXME: Allow ascii_graphics even with strict_mapping.  Reported by
  300.      David E. A. Wilson <david@osiris.cs.uow.edu.au>.  */
  301.  
  302.   if (ascii_graphics)
  303.     {
  304.       table = (unsigned char *) xmalloc (256);
  305.       memcpy (table, step->one_to_one, 256);
  306.       memcpy (table + 176, convert_rulers, 48);
  307.       free ((void *) step->one_to_one);
  308.       step->one_to_one = table;
  309.     }
  310. }
  311.  
  312. void
  313. module_ibmpc (void)
  314. {
  315.   declare_step ("Latin-1", "IBM-PC", ONE_TO_MANY, init_latin1_ibmpc, NULL);
  316.   declare_step ("IBM-PC", "Latin-1",
  317.         strict_mapping ? MANY_TO_MANY : MANY_TO_ONE,
  318.         init_ibmpc_latin1, NULL);
  319.  
  320. #if 0
  321.   declare_alias ("IBM-PC", "ibm437");
  322. #endif
  323. }
  324.