/***************************************

               Conversione
           last update 10/07/87
      AMIGA-Version by Frank Kremser
PC-Original-Version by Dr. Edgar Huckert
       (C) 1987  by Markt & Technik

****************************************

Raccolta di routine di conversione

***************************************/

#include <stdio.h>

#define MAX10  10000
#define MAX16  0x1000

/* Conversione decimale di numero intero in una Stringa */
/* Procedimento di divisione con resto                  */
/* Gli zeri iniziali vengono soppressi                  */
char *intdec(zahl)
int zahl;
{
  static char ergebnis[10];
  int maxpot,flag;
  char c,*dest;

  flag   = 0;        /* Memorizz. Flag del numero   */
  maxpot = MAX10;    /* Valore 10 alla n            */
  dest   = ergebnis; /* Puntatore stringa risultato */
  if (zahl < 0)
    {
     zahl *= (-1);
     *dest++ = '-';
    }
  while (maxpot >= 1)
    {
     c = zahl / maxpot + '0';
     if (( c != '0') || ( maxpot == 1) || flag)
       {
        flag    = 1;
        *dest++ = c;
       }
     zahl   %= maxpot;
     maxpot /= 10;
    }
  *dest = 0;
  return(ergebnis);
}   /* end intdec */

/* Conversione esadecimale di numero intero in una Stringa */
/* Procedimento di divisione con resto                     */
/* Diversamente da intdec, non viene mai prodotto il segno */
/* Il procedimento e' sostituibile con uno shift verso     */
/* sinistra in gruppi di quattro                           */
/* Gli zeri iniziali vengono soppressi                     */
char *inthex(zahl)
unsigned zahl;
{
  static char ergebnis[10];
  unsigned maxpot,flag;
  char c,*dest;

  flag   = 0;        /* Memorizz. Flag del numero   */
  maxpot = MAX16;    /* Valore 16 alla n            */
  dest   = ergebnis; /* Puntatore stringa risultato */
  while (maxpot >= 1)
    {
     c = zahl / maxpot + '0';
     if (( c != '0') || ( maxpot == 1) || flag)
       {
        flag    = 1;
        if (c > '9') c += 7;
        *dest++ = c;
       }
     zahl   %= maxpot;
     maxpot /= 16;
    }
  *dest = 0;
  return(ergebnis);
}   /* end inthex */

/* Conversione binaria di numero intero in una stringa      */
/* Procedimento: shift verso sinistra                       */
/* Diversamente da intdec, non viene mai prodotto un segno  */
/* Gli zeri iniziali vengono soppressi                      */
char *intbin(zahl)
unsigned zahl;
{
  static char ergebnis[33];
  int flag,n,muster,nbits;
  char *dest;

  flag = 0;
  dest = ergebnis;
  nbits = sizeof(unsigned) * 8;
  if (nbits == 16) muster = 0x8000;
  else             muster = 0x80000000;

  for (n = 0; n < nbits; n++)
    {
     if (zahl & muster)
       {
        flag    = 1;
        *dest++ = '1';
       }
     else
       {
        if (flag) *dest++ = '0';
       }
     zahl = zahl << 1;
    }
  if (! flag) *dest++ = '0';
  *dest = 0;
  return(ergebnis);
}   /* end intbin */

/* Conversione stringa dec. ASCII in numero intero         */
/* Fornisce -1 in caso di errore                           */
/* Diversamente fornisce il n.ro delle posiz. convert.     */
int myatoi(str,iadr)
char *str;
int *iadr;
{
  int nospace,c;
  int zahl,vorz,stellen;

  nospace = 0;   /* flag caratt. <> white space */
  zahl    = 0;
  vorz    = 1;
  stellen = 0;
  while (*str != 0)
    {
     c = *str++ & 0xff;
     stellen++;
     if ((c == ' ') || (c == 0x0a) ||
         (c == 0x09) || (c == 0x0d))
       {
        if (nospace) break;
        continue;
       }
     if ((c == '-') && (! nospace))
       {
        vorz = -1;
        continue;
       }
     if ((c >= '0') && (c <= '9'))
       {
        zahl = (zahl * 10) + (c - '0');
        nospace = 1;
       }
     else return(-1);
    }
  *iadr = zahl * vorz;
  return(stellen);
}   /* end myatoi */

/* Valutazione stringhe                        */
/* Riconosce \n,\r,\t,\b e \777 (n.ri ottali)  */
/* fornisce il n.ro delle posizioni convertite */
/* fornisce -1 in caso di errore               */
int streval(inpstr,outstr)
char inpstr[],*outstr;
{
  int n,nhk,zahl,esc,linp;
  char c;

  nhk     = 0;  /* conta gli apostrofi       */
  esc     = 0;  /* Flag in rappr. sostitut.  */
  linp    = strlen(inpstr);
  *outstr = 0;

  for (n=0; n < linp; n++)
    {
     c = inpstr[n];
     if (c == 0x0a) break;
     /* Ignorare tutto prima di '"' */
     if ((c != '"') && (nhk == 0)) continue;
     if (esc)
       {
        if ((c >= '0') && (c <= '7'))
          {
           /* Convertire in numero ottale       */
           /* Tener conto di max. tre posiz.    */
           zahl = (zahl * 8) + (c - '0');
           /* analisi carattere successivo      */
           c = inpstr[n+1];
           if (! ((c >= '0') && (c <= '7')))
             {
              /* raggiunta fine del n.ro ottale */
              *outstr++ = zahl;
              esc = 0;
              continue;
             }
           if (esc++ >= 3)
             {
              /* elabora tre posizioni */
              *outstr++ = zahl;
              esc = 0;
              continue;
             }
           continue;
          }   /* if ((c >= '0') && (c <= '7')) */
        else
          {
           /* Sequenza successiva                   */
           /* il carattere successivo viene preso   */
           /* direttamente o interpretato           */
           if (c == 'n') c = 0x0a;   /* \n LF  */
           if (c == 'b') c = 0x08;   /* \b Bsp */
           if (c == 't') c = 0x09;   /* \t Tab */
           if (c == 'r') c = 0x0d;   /* \r CR  */
           *outstr++ = c;
           esc = 0;
           continue;
          }
       }
     if (c == '"')
       {
        nhk++;
        if (nhk >= 2)
          {
           /* Fine di una Stringa */
           *outstr = 0;
           return(n+1);
          }
        continue;
       }
     if (c == '\\')
       {
        /* Inizio di una sequenza di Escape */
        esc  = 1;
        zahl = 0;
        continue;
       }
     /* Carattere normale: viene preso */
     *outstr++ = c;
    }
  return(-1);
}   /* end streval */

void main()
{
  char cfeld[50],ofeld[50];
  int ivar,n;

  printf("\n%s",intdec(127));
  printf("\n%s",intdec(-127));
  printf("\n%s",inthex(127));
  printf("\n%s",inthex(-127));
  printf("\n%s",intbin(127));
  printf("\n%s",intbin(-127));
  printf("\nImmetti n.ro decim.: ");
  gets(cfeld);
  if (myatoi(cfeld,&ivar) < 0) printf("\nErrore di convers.");
  else printf("\nConvertito: %d",ivar);
  printf("\nImmetti Stringa fra Apostrofi:");
  gets(cfeld);
  ivar = streval(cfeld,ofeld);
  if (ivar < 0) printf("\nErrore di convers.");
  else
    {
     printf("\n");
     ivar = strlen(ofeld);
     for (n=0; n <= ivar; n++)
       printf("%02x ",ofeld[n]);
    }
}   /* end main */
