/*--------------------------------------------------------------------
 * FICHERO:  ExtraeDo.c
 * OBJETIVO: Definir la funcin ExtraeDocumentacion()
 * AUTOR:    Pedro Reina
 * FECHA:    V.21.4.1995
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Ficheros de cabecera
 *------------------------------------------------------------------*/

#include "Zeus.h"

/*--------------------------------------------------------------------
 * Definicin de macros constantes
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Variables globales
 *------------------------------------------------------------------*/

FILE *SalidaDoc;

/*--------------------------------------------------------------------
 * Declaracin de funciones
 *------------------------------------------------------------------*/

short Estudia();
short ExtraeDoc();

/*--------------------------------------------------------------------
 * Definicin de macros funciones
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Definicin de funciones
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * FUNCION:  ExtraeDocumentacion()
 * OBJETIVO: Escribir Olimpo.doc con las cabeceras de todas las funciones
 *           de Olimpo
 * ENTRADAS: Variables globales
 * SALIDAS:  Ninguna. El programa puede abortar
 *------------------------------------------------------------------*/
void ExtraeDocumentacion()
  {
  short i, TotalFuncion=0;
  char  Nombre [LONG_FICH_LARGO];

  PresentaOpcion ("Extraccin de documentacin");

  strcpy (Nombre,"Olimpo.doc");
  ArreglaSeparador (Nombre);
  SalidaDoc = AbreFicheroGrabar(Nombre); 

  for ( i=0 ; i<TotalOlimpo ; i++ ) 
    { TotalFuncion += Estudia ( Olimpo[i] ); }

  fclose (SalidaDoc);
  printf ( "\nHay un total de %d funciones", TotalFuncion );
  printf ("\nTerminado.\n");
  }
  
/*--------------------------------------------------------------------
 * FUNCION:  Estudia()
 * OBJETIVO: Dirigir la extraccin de documentacin de un objeto
 * ENTRADAS: La abreviatura del objeto
 * SALIDAS:  El nmero de funciones encontradas
 *------------------------------------------------------------------*/
short Estudia (Abrev)
char *Abrev;
  {
  char  Sigue;
  char  Aux[LONG_PALABRA], NombreFichero[LONG_FICH_LARGO];
  short i, TotalFuncion;
  FILE *Fichero;
  
  strcpy (NombreFichero, Abrev);
  strcat (NombreFichero,".des");
  ArreglaSeparador (NombreFichero);
  Fichero = AbreFicheroLeer (NombreFichero);

  TotalFuente = 0;

  LeePalabra (Fichero, Aux);
  printf ( "\nEstudiando objeto %s...", Aux);

  Sigue = 1;
  while ( Sigue )
    {
    LeePalabra (Fichero,Aux);
    Sigue = strcmp (Aux,"Fuente");
    }
        
  Sigue = 1;
  while ( Sigue )
    {
    LeePalabra (Fichero,Aux);
    if ( !strcmp (Aux,"Cabecera") )
      { Sigue = 0; }
    else
      { AlmacenaFuente (Aux); }
    }
  fclose (Fichero);
  
  strcpy (NombreFichero, Abrev);
  strcat (NombreFichero,".h");
  ArreglaSeparador (NombreFichero);
  TotalFuncion = ExtraeDoc (NombreFichero);

  for ( i=0 ; i<TotalFuente ; i++ ) 
    { 
    strcpy (NombreFichero, Fuente[i]);
    strcat (NombreFichero,".c");
    ArreglaSeparador (NombreFichero);
    TotalFuncion += ExtraeDoc ( NombreFichero ); 
    }
    
  printf ( " %d funciones", TotalFuncion );
  fprintf ( SalidaDoc, "En %s hay %d funciones\n\n\n", 
            Abrev, TotalFuncion );
  return ( TotalFuncion );  
  }  
  
/*--------------------------------------------------------------------
 * FUNCION:  ExtraeDoc()
 * OBJETIVO: Escribir en un fichero las cabeceras de las funciones
 *           de Olimpo que haya en un fichero
 * ENTRADAS: El nombre del fichero
 * SALIDAS:  El nmero de funciones encontradas
 *------------------------------------------------------------------*/
short ExtraeDoc (Nombre)
char *Nombre;
  {
  FILE *Entrada;
  short TotalFuncion=0;
  char  Dentro = 0;
  char  Linea [MAXLIN];
  
  Entrada = AbreFicheroLeer (Nombre);
  while ( !feof(Entrada) )
    {         
    fgets (Linea, MAXLIN, Entrada);  
    if ( strstr (Linea,"----------Olimpo") )
      {
      Dentro = 1;
      TotalFuncion++;
      }
    else if ( strstr (Linea,"----------*/") ||
              strstr (Linea,"* ALGORITMO:") )
      {
      if ( Dentro )
        {
        Dentro = 0;
        fputs ("\n",SalidaDoc);
        }
      }
    else if ( Dentro )
      { fputs (Linea+3, SalidaDoc); }
    }               
  
  fclose (Entrada);
  return ( TotalFuncion );
  }
  