/***************************************
*                                      *
*   Copyright (c) 1998 Jean-Eric Pin   *
*   All rights reserved.               *
*                                      *
*   TAB = 2 spaces                     *
*                                      *
***************************************/

/*-------------------------------------------------------------------
 * Utilitaires.c    Jean-Eric Pin 14/08/99
 *-------------------------------------------------------------------
 */     

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "Main.h"
#include "Utilitaires.h"

extern unsigned short NbLettres;
char s[TAILLE_CHAINE_MAX];
static char Tampon[TAILLE_TAMPON];
static short Position = 0;    /* Prochaine position libre dans Tampon */

/****************************************************
*
* Nombre de Chiffre. OK
*
****************************************************/

short NombreDeChiffres(long n)
{
  short NbChiffres = 1;
  
  while (n >= 10)
  {
    n /= 10;
    NbChiffres++;
  }
  return(NbChiffres);
}

/************************************************************************
*                     
* LireCaractere. D'apres Kernighan-Ritchie (en francais), page 78 
*                    
************************************************************************/

char LireCaractere(void)    /* Lit un caractere */
{
  return (Position > 0) ? Tampon[--Position] : getchar();
}

/************************************************************************
*                     
* RemettreCaractere. D'apres Kernighan-Ritchie (en francais), page 78 
*                    
************************************************************************/

void RemettreCaractere(char c)
{
  if (Position >= TAILLE_TAMPON)
    printf("RemettreCaractere : trop de caracteres\n");
  else
    Tampon[Position++] = c;
}

/************************************************************************
*                     
* LireChaines. D'apres Kernighan-Ritchie (en francais), page 77-78 
* Retourne NOMBRE ('0') si un nombre a ete lu (stocke dans s)
* et retourne c si un caractere c a ete lu.
*                    
************************************************************************/

char LireChaines(char s[])
{
  short i;
  char c;
  
  while ((s[0] = c = LireCaractere()) == ' ' || c == '\t')
    ;
  s[1] = '\0';
  if (!isdigit(c))    /* Pas un nombre */
    return(c);
  else
  {
    i = 0;
    while (isdigit(s[++i] = c = LireCaractere()))
      ;
    s[i] = '\0';
    if (c != EOF)
      RemettreCaractere(c);
    return(NOMBRE);
  }
}

/************************************************************************
*                     
* LireLigne. D'apres Kernighan-Ritchie (en francais), page 163 
*  Lit une ligne en retirant le \n, retourne sa longueur.
*                  
************************************************************************/

short LireLigne(char *ligne, short max, FILE *fichier)
{
  short longueur;
  
  if (fgets(ligne, max, fichier) == NULL)
    return(0);
  else
  {
    longueur = strlen(ligne);
    ligne[longueur] = '\0';
    return(longueur);
  }
}

/************************************************************************
*                     
* FiltreAlphabet. Teste si un mot est dans A*, ou A est un alphabet.
* Retourne 1 si oui, et 0 sinon.
*                    
************************************************************************/

short FiltreAlphabet(lettre *Mot)
{
  short i;
  unsigned short LongueurMot;
  
  LongueurMot = strlen((char *)Mot);    /* A changer si lettre = short !! */
  if ((Mot != NULL) && (Mot[0] != '1'))
  {
    for (i = 0; i < LongueurMot; i++)
      if (((Mot[i] - 'a') >= NbLettres) || ((Mot[i] - 'a') < 0))
        break;   /* Pas tres stable, a revoir */
    return (i == LongueurMot);
  }
  return (1);
}