/* 2ndIndex zu 1stGuide, dem Hypertextsystem von Guido Vollbeding
 * 2ndIndex von Dirk Haun, 30.04./01.05.1993 (Pure C 1.1)
 *            & Guido Vollbeding, 28.09.1993 (Pure C 1.1 vom Mar 20 1993)
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TRUE    1
#define FALSE   0
#define forever for (;;)

typedef struct node
{
  struct node *next;
  char s[];
}
NODE;

/* Prototypes */
int make_index( void );
int insert_node( NODE *base, char *entry );

/* globale Variablen */
NODE stwbase = { 0 }, datbase = { 0 };
char refpath[256];
long refpos;
FILE *fp;

int main( int argc, char *argv[] )
{
  char *xp;
  NODE *actfile;

  if (--argc <= 0)
  {
    fprintf( stderr, "\n Aufruf: 2ndindex [pfadname] [>output]\n\n" );
    return 0;
  }
  strcpy( refpath, argv[1] );
  if ((xp = strrchr( refpath, '\\' )) == 0)
  {
    fprintf( stderr, "\n 2ndindex: Bitte kompletten Pfadnamen angeben.\n\n" );
    return 1;
  }
  refpos = xp + 1 - refpath;
  if ((fp = fopen( refpath, "rb" )) == 0)
  {
    fprintf( stderr, "\n 2ndindex: Fehler beim ™ffnen!\n\n" );
    return 1;
  }
  fprintf( stderr, "\n Erstelle Index von %s ...\n", refpath );
  actfile = &datbase;
  forever
  {
    make_index();
    forever
    {
      if ((actfile = actfile->next) == 0) /* fertig! */
      { 
        fprintf( stderr, "\n 2ndindex: Operation erfolgreich abgeschlossen.\n\n" );
        fclose( fp ); return 0;
      }
      if ((xp = strrchr( actfile->s, '.' )) != 0)
      {
        if (stricmp( xp, ".IMG" ) == 0) continue;
        if (stricmp( xp, ".IFF" ) == 0) continue;
        if (stricmp( xp, ".JPG" ) == 0) continue;
        if (stricmp( xp, ".GEM" ) == 0) continue;
        if (stricmp( xp, ".RSC" ) == 0) continue;
        if (stricmp( xp, ".SAM" ) == 0) continue;
        if (stricmp( xp, ".SND" ) == 0) continue;
        if (stricmp( xp, ".AVR" ) == 0) continue;
      }
      if (freopen( actfile->s, "rb", fp ) == 0) continue;
      break;
    }
    strcpy( refpath, actfile->s );
} }

int make_index( void )
{
  int c;
  char *cp, *dp, wort[256], datei[256], pfad[256];

  forever
  {
    c = fgetc( fp );
    if (c == EOF) return TRUE;
    if (c == (unsigned char)'Ý')
    {
      cp = wort;
      forever
      {
        c = fgetc( fp );
        if (c == EOF) return FALSE;
        if (c == (unsigned char)'Ý') break;
        *cp++ = c;
      }
      *cp = 0;
      cp = datei;
      forever
      {
        c = fgetc( fp );
        if (c == EOF) return FALSE;
        if (c == (unsigned char)'Ý') break;
        *cp++ = c;
      }
      *cp = 0;
      if (insert_node( &stwbase, wort ))
      {
        strcpy( pfad, refpath ); dp = datei;
        forever
        {
          cp = strrchr( pfad, '\\' );
          if (strncmp( dp, "..\\", 3 )) break;
          *cp = 0; dp += 3;
        }
        strcpy( cp + 1, dp );
        printf( "\tÝ%sÝ%sÝ\n", wort, pfad + refpos );
        insert_node( &datbase, pfad );
} } } }

int insert_node( NODE *np, char *entry )
{
  while (np->next)
  {
    np = np->next;
    if (stricmp( np->s, entry ) == 0) return FALSE;
  }                       /* L„nge des Strings + Nullbyte + Zeiger */
  if ((np->next = malloc( strlen( entry ) + (1 + sizeof(NODE)) )) == 0)
    return FALSE;
  np = np->next;
  np->next = 0;
  strcpy( np->s, entry );
  return TRUE;
}
