/* ---------------------------------------------------------------------
   *** NE PAS SUPPRIMER LES ACCENTS ***
   (c) ED 1995-2005
   Project      : CLIB
   Function     : Constantes ASCII, chaines des codes de controle 0 a 31
   Module       : ASCII
   File         : ASCII.H
   Created      : 15-07-2002
   Modified     : 24-02-2007
   --------------------------------------------------------------------- */

/* ---------------------------------------------------------------------
   Log

   1.9 du 24-02-2007 Mise au point de ASCII_noacc_ansi()
   1.8 du 17-09-2005 Mise au point de ASCII_noacc_ibm()
   1.7 du 13-04-2004 Ajout de ASCII_noacc_ansi()
   1.6 du 13-07-2002 LCLint
   1.5 du 14-12-2000 ASCII_noacc() devient ASCII_noacc_ibm()
   1.4 du 09-08-2000 Ajoute ASCII_noacc()
   1.3 du 09-08-1998 'sASC' devient 'aszAscii' Supprimme les blancs
   1.2 du 10-08-1998 define -> enum
   .                 tableau -> fonction
   1.1 du 02-05-1997 rajoute 'const'
   1.0 du 28-09-1995 Version operationelle

   --------------------------------------------------------------------- */

#ifdef __cplusplus
#error This source file is not C++ but rather C. Please use a C-compiler
#endif

#include <stdio.h>

#include "ed/inc/ascii.h"
#include "ed/inc/sys.h"
#include "ed/inc/bits.h"

/* macros ============================================================== */

#define VER "1.9"
#define ID "ASCII"
#define CPR "(c) ED "__DATE__

/* constants =========================================================== */

static char const *const aszAscii[] =
{
   "NUL"
   ,"SOH"
   ,"STX"
   ,"ETX"
   ,"EOT"
   ,"ENQ"
   ,"ACK"
   ,"BEL"
   ,"BS"
   ,"HT"
   ,"LF"
   ,"VT"
   ,"FF"
   ,"CR"
   ,"SO"
   ,"SI"
   ,"DLE"
   ,"XON"
   ,"DC2"
   ,"XOF"
   ,"DC4"
   ,"NAK"
   ,"SYN"
   ,"ETB"
   ,"CAN"
   ,"EM"
   ,"SUB"
   ,"ESC"
   ,"FS"
   ,"GS"
   ,"RS"
   ,"US"
};

static charset_s const charset_ansi[] =
{
#define ITEM(a, b, c, d, e, f, g, h, i, j) \
   {d, #b},
#include "ed/inc/ansi.itm"
#undef ITEM
};

static charset_s const charset_oem[] =
{
#define ITEM(a, b, c, d, e, f, g, h, i, j) \
   {d, #c},
#include "ed/inc/ansi.itm"
#undef ITEM
};

/* types =============================================================== */
/* structures ========================================================== */
/* private variables =================================================== */
/* private functions =================================================== */
/* internal public functions =========================================== */
/* internal public data ================================================ */
/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   ASCII_sid ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *ASCII_sid (void)
{
   return ID;
}

/* ---------------------------------------------------------------------
   ASCII_sver ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *ASCII_sver (void)
{
   return VER;
}

/* ---------------------------------------------------------------------
   ASCII_str()
   ---------------------------------------------------------------------
   Retourne une chaine traduisant les caracteres ASCII non imprimables
   ---------------------------------------------------------------------
   E : caractere
   S : caractere filtre
   --------------------------------------------------------------------- */
const char *ASCII_str (eASCII const c)
{
   const char *sz = NULL;

   ENUM_CHECK ();

   ASSERT (ASCII_NB == NELEM (aszAscii));

   if (c < ASCII_NB)
   {
      sz = aszAscii[c];
   }

   return sz;
}

/* ---------------------------------------------------------------------
   ASCII_oem_info()
   ---------------------------------------------------------------------
   Retourne les informations d'un caractere OEM
   ---------------------------------------------------------------------
   E : caractere OEM
   S : informations
   --------------------------------------------------------------------- */
charset_s const *ASCII_oem_info (charset_oem_e const c)
{
   charset_s const *p = NULL;

   ENUM_CHECK ();

   ASSERT (ASCII_NB == NELEM (aszAscii));

   if (c > 31 && c < 256)
   {
      p = charset_oem + c - 32;
   }

   return p;
}

/* ---------------------------------------------------------------------
   ASCII_noacc_ibm()
   ---------------------------------------------------------------------
   Retourne un caractere non accentue a partir d'un caractere
   quelquonque (ASCII etendu IBM uniquement, aka OEM)
   ---------------------------------------------------------------------
   E : caractere
   S : caractere filtre
   --------------------------------------------------------------------- */
int ASCII_noacc_ibm (int c, int *const pErr)
{
   if (c & bit7)
   {
      switch (c & 0xFF)
      {
      case OEM_agrave:
      case OEM_acirc:
      case OEM_aring:
      case OEM_aacute:
      case OEM_auml:
      case OEM_atilde:
         c = 'a';
         break;

      case OEM_ccedil:
         c = 'c';
         break;

      case OEM_egrave:
      case OEM_ecirc:
      case OEM_euml:
      case OEM_eacute:
         c = 'e';
         break;

      case OEM_igrave:
      case OEM_icirc:
      case OEM_iuml:
      case OEM_iacute:
         c = 'i';
         break;

      case OEM_ntilde:
         c = 'n';
         break;

      case OEM_ograve:
      case OEM_ocirc:
      case OEM_ouml:
      case OEM_oacute:
      case OEM_oslash:
      case OEM_otilde:
         c = 'o';
         break;

      case OEM_ugrave:
      case OEM_ucirc:
      case OEM_uuml:
      case OEM_uacute:
         c = 'u';
         break;

      case OEM_yuml:
      case OEM_yacute:
         c = 'y';
         break;

      case OEM_Agrave:
      case OEM_Acirc:
      case OEM_Aacute:
      case OEM_Auml:
      case OEM_Auml2:
      case OEM_Atilde:
         c = 'A';
         break;

      case OEM_Ccedil:
         c = 'C';
         break;

      case OEM_Igrave:
      case OEM_Icirc:
      case OEM_Iuml:
      case OEM_Iacute:
         c = 'I';
         break;

      case OEM_Ntilde:
         c = 'N';
         break;

      case OEM_Ograve:
      case OEM_Ocirc:
      case OEM_Ouml:
      case OEM_Oacute:
      case OEM_Oslash:
      case OEM_Otilde:
         c = 'O';
         break;

      case OEM_Ugrave:
      case OEM_Ucirc:
      case OEM_Uuml:
      case OEM_Uacute:
         c = 'U';
         break;

      case OEM_Egrave:
      case OEM_Ecirc:
      case OEM_Euml:
      case OEM_Eacute:
         c = 'E';
         break;

      case OEM_Yacute:
         c = 'Y';
         break;

      case OEM_iquest:
         c = '?';
         break;

      case '­':
         c = '!';
         break;

      case OEM_laquo:
      case OEM_raquo:
         c = '"';
         break;

      case 'ï':
         c = '\'';
         break;

      case OEM_times:
         c = 'x';
         break;

      case 'ð':
         c = '-';
         break;

      case 'ö':
         c = '/';
         break;

      case '÷':
         c = ',';
         break;

      case 'ú':
         c = '.';
         break;

      case 'ø':
         c = 'o';
         break;

      case 'û':
         c = '1';
         break;

      case 'ü':
         c = '3';
         break;

      case 'ý':
         c = '2';
         break;

      case 'þ':
         c = '*';
         break;

      case ' ':
         c = ' ';
         break;

      case OEM_not:
      case 'ª':
      case OEM_gra_up2_right2:
      case OEM_gra_dn2_right2:
      case OEM_gra_up_right:
      case OEM_gra_dn_left:
      case 'È':
      case 'É':
      case 'Ù':
      case 'Ú':
         c = '.';
         break;

      case OEM_vertical_dot_hyphen:
      case OEM_gra_ver:
      case OEM_fra_ver2:
      case 'Ý':
         c = '|';
         break;

      case OEM_gra_ver_left:
      case OEM_gra_ver2_left2:
      case OEM_gra_hor_up:
      case OEM_gra_hor_dn:
      case OEM_gra_ver_right:
      case OEM_gra_hor_ver:
      case 'Ê':
      case 'Ë':
      case 'Ì':
      case 'Î':
         c = '+';
         break;

      case OEM_gra_hor:
      case 'Í':
      case 'Ü':
      case 'ß':
      case 'î':
      case 'ò':
      case 'ù':
         c = '-';
         break;

      case OEM_fill_10_pcent:
         c = 'o';
         break;

      case OEM_fill_50_pcent:
         c = 'O';
         break;

      case OEM_fill_90_pcent:
         c = 'x';
         break;

      case 'Û':
         c = '#';
         break;

      case OEM_aelig:
      case OEM_AElig:
      case OEM_Pound:
      case OEM_fnof:
      case OEM_supa:
      case OEM_deg:
      case OEM_dot_in_circle:
      case OEM_frac12:
      case OEM_frac14:
      case OEM_copy:
      case OEM_Cvbar:
      case OEM_yen:
      case 'Ï':
      case 'Ð':
      case 'Ñ':
      case 'Õ':
      case 'á':
      case 'æ':
      case 'ç':
      case 'è':
      case 'ñ':
      case 'ó':
      case 'ô':
      case 'õ':
      case OEM_blank:
      case OEM_uml:
         c = '.';
         break;

      default:
#ifndef NDEBUG
         printf ("%c (%d)\n", c, c);
#endif
         ASSERT (0);
         if (pErr)
         {
            *pErr = 1;
         }

      }
   }
   return c;
}

/* ---------------------------------------------------------------------
   ASCII_noacc_ansi()
   ---------------------------------------------------------------------
   Retourne un caractere non accentue a partir d'un caractere
   quelquonque (ASCII etendu ANSI uniquement)
   ---------------------------------------------------------------------
   E : caractere
   S : caractere filtre
   --------------------------------------------------------------------- */
int ASCII_noacc_ansi (int const c)
{
   switch ((char)c)
   {
   case 'à':
   case 'á':
   case 'â':
   case 'ã':
   case 'ä':
   case 'å':
      return 'a';
   case 'À':
   case 'Á':
   case 'Â':
   case 'Ã':
   case 'Ä':
   case 'Å':
      return 'A';
   case 'ç':
      return 'c';
   case 'Ç':
      return 'C';
   case 'é':
   case 'è':
   case 'ê':
   case 'ë':
      return 'e';
   case 'É':
   case 'È':
   case 'Ê':
   case 'Ë':
      return 'E';
   case 'ì':
   case 'í':
   case 'î':
   case 'ï':
      return 'i';
   case 'Ì':
   case 'Í':
   case 'Î':
   case 'Ï':
      return 'I';
   case 'ñ':
      return 'n';
   case 'Ñ':
      return 'N';
   case 'ò':
   case 'ó':
   case 'ô':
   case 'õ':
   case 'ö':
      return 'o';
   case 'Ò':
   case 'Ó':
   case 'Ô':
   case 'Õ':
   case 'Ö':
      return 'O';
   case 'ù':
   case 'ú':
   case 'û':
   case 'ü':
      return 'u';
   case 'Ù':
   case 'Ú':
   case 'Û':
   case 'Ü':
      return 'U';
   case 'ý':
   case 'ÿ':
      return 'y';
   case 'Ý':
      return 'Y';
   default:
      return c;
   }
}

/* public variables ==================================================== */
