"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "ocre-0.029/id3/arbid.c" of archive ocre_v0_029.tgz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 
    2 #include <fcntl.h>
    3 #include <stdio.h>
    4 #include <stdlib.h>
    5 #include <unistd.h>
    6 
    7 #include "arbid.h"
    8 
    9 /* como el nodo en memoria, pero con enteros en lugar de punteros */
   10 struct tnodof{
   11   int tiponodo;
   12   int variable;
   13   int umbral;
   14   int menor, mayorIgual;} ;
   15 
   16 int nodosEscritos;
   17 
   18 tpnodo nodoalloc ()
   19 {
   20   return ( (tpnodo) malloc(sizeof(struct tnodo)) );
   21 }
   22 
   23 tpnodo creaNodo (int clNodo, int variable, int umbral, tpnodo menor, tpnodo mayorIgual)
   24 {
   25   tpnodo nnodo ;
   26   nnodo = nodoalloc ();
   27   nnodo->tiponodo   = clNodo;
   28   nnodo->variable   = variable;
   29   nnodo->umbral     = umbral;
   30   nnodo->menor      = menor;
   31   nnodo->mayorIgual = mayorIgual;
   32   return nnodo ;
   33 }
   34 
   35 int escribeRama (int fd, tpnodo rama)
   36 {
   37   // int pmn, pmy;
   38   struct tnodof nodof;
   39   nodof.tiponodo = rama->tiponodo ;
   40   nodof.variable = rama->variable ;
   41   nodof.umbral   = rama->umbral ;
   42   if (rama->tiponodo == nd_segun) {
   43     nodof.menor      = escribeRama (fd, rama->menor);
   44     nodof.mayorIgual = escribeRama (fd, rama->mayorIgual);
   45     }
   46   else {
   47     nodof.menor      = 0 ;
   48     nodof.mayorIgual = 0 ;
   49     }
   50   write (fd, & nodof, sizeof(nodof));
   51   nodosEscritos ++ ;
   52   return  nodosEscritos - 1 ;
   53 }
   54 
   55 void escribeArbol (char * nomFich, tpnodo arbol)
   56 {
   57   int fd;
   58   fd = open (nomFich, O_WRONLY | O_CREAT | O_TRUNC, 0666);
   59   if (fd < 0) {
   60     printf ("error en arbid.escribeArbol al intentar abrir %s\n", nomFich);
   61     exit (1);
   62     }
   63   nodosEscritos = 0;
   64   escribeRama (fd, arbol);
   65   close (fd);
   66 }
   67 
   68 int escribeSeRamaTexto (tpnodo rama)
   69 {
   70   int pmn, pmy;
   71   if (rama->tiponodo == nd_segun) {
   72     pmn    = escribeSeRamaTexto (rama->menor);
   73     pmy    = escribeSeRamaTexto (rama->mayorIgual);
   74     printf ("%4d segun  %5d %5d %5d %5d\n",
   75              nodosEscritos,
   76              rama->variable, rama->umbral, pmn, pmy);
   77     }
   78   else {
   79     switch (rama->tiponodo) {
   80       case nd_falso :
   81         printf ("%4d falso     --    --    --    --\n", nodosEscritos);
   82         break ;;
   83       case nd_cierto:
   84         printf ("%4d cierto    --    --    --    --\n", nodosEscritos);
   85         break ;;
   86       case nd_nose  :
   87         printf ("%4d nose      --    --    --    --\n", nodosEscritos);
   88         break ;;
   89       }
   90     }
   91   nodosEscritos ++ ;
   92   return  nodosEscritos - 1 ;
   93 }
   94 
   95 void escribeSeArbolTexto (tpnodo arbol)
   96 {
   97   nodosEscritos = 0;
   98   escribeSeRamaTexto (arbol);
   99 }
  100 
  101 tpnodo leeRama (int fd, int posicion)
  102 {
  103   struct tnodof nodof;
  104   tpnodo nnodo ;
  105 /* situarse en posicion */
  106   lseek (fd, posicion * sizeof(nodof), SEEK_SET);
  107   read (fd, & nodof, sizeof(nodof));
  108   nnodo = nodoalloc ();
  109   nnodo->tiponodo   = nodof.tiponodo ;
  110   nnodo->variable   = nodof.variable ;
  111   nnodo->umbral     = nodof.umbral ;
  112   if (nodof.tiponodo == nd_segun) {
  113     nnodo->menor      = leeRama(fd, nodof.menor);
  114     nnodo->mayorIgual = leeRama(fd, nodof.mayorIgual);
  115     }
  116   else {
  117     nnodo->menor      = NULL ;
  118     nnodo->mayorIgual = NULL ;
  119     }
  120   return nnodo;
  121 }
  122 
  123 tpnodo leeArbol (char * nomFich)
  124 {
  125   int fd;
  126   int posicion, tam;
  127   struct tnodof nodof;
  128   tpnodo arbol;
  129   fd = open (nomFich, 0);
  130   if (fd < 0) {
  131     printf ("error en arbid.leeArbol al intentar abrir %s\n", nomFich);
  132     exit (1);
  133     }
  134   tam = lseek (fd, 0L, SEEK_END);
  135   posicion = (tam / sizeof(nodof)) - 1 ;
  136   arbol = leeRama (fd, posicion);
  137   close (fd);
  138   return arbol;
  139 }
  140 
  141 void anadeNodoHMenor (tpnodo np, tpnodo nh)
  142 {
  143   np->menor = nh;
  144 }
  145 
  146 void anadeNodoHMayorI (tpnodo np, tpnodo nh)
  147 {
  148   np->mayorIgual = nh;
  149 }
  150 
  151 int evalArbolVector (tpnodo arb, int v[])
  152 {
  153   switch (arb->tiponodo) {
  154     case nd_falso   :
  155       return nd_falso ;
  156     case nd_cierto  :
  157       return nd_cierto ;
  158     case nd_nose    :
  159       return nd_nose ;
  160     case nd_segun   :
  161       if (v[arb->variable] < arb->umbral)
  162         return evalArbolVector (arb->menor, v);
  163       else    /* mayor o igual */
  164         return evalArbolVector (arb->mayorIgual, v);
  165     }
  166   return nd_nose ;  // no llega aqui. para evitar warning
  167 }
  168 
  169 void destruyeArbol (tpnodo nodo)
  170 {
  171   if (nodo->tiponodo == nd_segun) {
  172     destruyeArbol (nodo->menor);
  173     destruyeArbol (nodo->mayorIgual);
  174     }
  175   free (nodo);
  176 }
  177