home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GRECODE / IBMPC.C < prev    next >
C/C++ Source or Header  |  1993-12-22  |  9KB  |  288 lines

  1. /* Conversion of files between different charsets and usages.
  2.    Copyright (C) 1990, 1993 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 void
  165. file_latin1_ibmpc (const STEP *step, FILE *input_file, FILE *output_file)
  166. {
  167.   const unsigned char *table;    /* one to one conversion table */
  168.   const char *const *table2;    /* one to many conversion table */
  169.   int input_char;        /* current character */
  170.  
  171.   if (strict_mapping)
  172.     {
  173.       table2 = step->one_to_many;
  174.       while (input_char = getc (input_file), input_char != EOF)
  175.     if (input_char == '\n')
  176.       {
  177.         putc (DOS_CR, output_file);
  178.         putc (DOS_LF, output_file);
  179.       }
  180.     else if (table2[input_char])
  181.       putc (*table2[input_char], output_file);
  182.     }
  183.   else
  184.     {
  185.       table = step->one_to_one;
  186.       while (input_char = getc (input_file), input_char != EOF)
  187.     if (input_char == '\n')
  188.       {
  189.         putc (DOS_CR, output_file);
  190.         putc (DOS_LF, output_file);
  191.       }
  192.     else
  193.       putc (table[input_char], output_file);
  194.     }
  195. }
  196.  
  197. static void
  198. file_ibmpc_latin1 (const STEP *step, FILE *input_file, FILE *output_file)
  199. {
  200.   const unsigned char *table;    /* one to one conversion table */
  201.   const char *const *table2;    /* one to many conversion table */
  202.   int input_char;        /* current character */
  203.  
  204.   if (strict_mapping)
  205.     {
  206.       table2 = step->one_to_many;
  207.       input_char = getc (input_file);
  208.       while (input_char != EOF && input_char != DOS_EOF)
  209.     if (input_char == DOS_CR)
  210.       {
  211.         input_char = getc (input_file);
  212.         if (input_char == DOS_LF)
  213.           {
  214.         putc ('\n', output_file);
  215.         input_char = getc (input_file);
  216.           }
  217.         else if (table2[DOS_CR])
  218.           putc (*table2[DOS_CR], output_file);
  219.       }
  220.     else
  221.       {
  222.         if (table2[input_char])
  223.           putc (*table2[input_char], output_file);
  224.         input_char = getc (input_file);
  225.       }
  226.     }
  227.   else
  228.     {
  229.       table = step->one_to_one;
  230.       input_char = getc (input_file);
  231.       while (input_char != EOF && input_char != DOS_EOF)
  232.     if (input_char == DOS_CR)
  233.       {
  234.         input_char = getc (input_file);
  235.         if (input_char == DOS_LF)
  236.           {
  237.         putc ('\n', output_file);
  238.         input_char = getc (input_file);
  239.           }
  240.         else
  241.           putc (table[DOS_CR], output_file);
  242.       }
  243.     else
  244.       {
  245.         putc (table[input_char], output_file);
  246.         input_char = getc (input_file);
  247.       }
  248.     }
  249. }
  250.  
  251. static void
  252. init_latin1_ibmpc (STEP *step)
  253. {
  254.   complete_pairs (step, 1, known_pairs, NUMBER_OF_PAIRS, 1);
  255.   step->file_recode = file_latin1_ibmpc;
  256. }
  257.  
  258. static void
  259. init_ibmpc_latin1 (STEP *step)
  260. {
  261.   unsigned char *table;
  262.  
  263.   complete_pairs (step, 1, known_pairs, NUMBER_OF_PAIRS, 0);
  264.   step->file_recode = file_ibmpc_latin1;
  265.  
  266.   if (ascii_graphics)
  267.     {
  268.       table = (unsigned char *) xmalloc (256);
  269.       memcpy (table, step->one_to_one, 256);
  270.       memcpy (table + 176, convert_rulers, 48);
  271.       free ((void *) step->one_to_one);
  272.       step->one_to_one = table;
  273.     }
  274. }
  275.  
  276. void
  277. module_ibmpc (void)
  278. {
  279. #if 0
  280.   declare_alias ("ibmpc", "ibm437");
  281. #endif
  282.  
  283.   declare_step ("latin1", "ibmpc", ONE_TO_MANY,
  284.         init_latin1_ibmpc, NULL);
  285.   declare_step ("ibmpc", "latin1", strict_mapping ? MANY_TO_MANY : MANY_TO_ONE,
  286.         init_ibmpc_latin1, NULL);
  287. }
  288.