/********************************************************************/
/*    Random Fantasy Name Construction Set (RFNCS.c) Version 1.0    */
/*                                                                  */
/*                           For the Amiga                          */
/*                                                                  */
/*                                                                  */
/* by Lee E. Wulff                     Created on December 15, 1997 */
/********************************************************************/
/* Compiled using Lattice C Version 5.04                 circa 1989 */
/********************************************************************/
/* Syntax for use:                                                  */
/*                                                                  */
/*        RFNCS <Name source> <Number to generate>                  */
/********************************************************************/
/*                    !!!!Shell Use Only!!!!                        */
/*                                                                  */
/*         See documentation for format of Name Source File         */
/********************************************************************/
/*    This code is based on the program NAMN.C by Johan Danforth    */
/*                                                                  */
/*      This code is in the public domain, feel free to use it      */
/********************************************************************/

/* Include required header files */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Define global variables */

FILE * NmFlPt;
char TempString[100];

/* Start of program */

void main(argc,argv)
int argc;
char *argv[];
{
   /* Define some local variables for the main function */
   
   char Part1[100][10], Part2[100][10], Part3[100][10];
   int Point1 = 0, Point2 = 0, Point3 = 0;
   
   char TheName[30];
   int LCount;

   printf("\nRandom Fantasy Name Construction Set Version 1.0\n");
   printf("Written by L E Wulff, 15 DEC 1997\n");

   /* Check for the required number of arguments */
   
   if(argc != 3)
   {
      printf("\nWrong number of arguments!\n");
      printf("format is RFNCS <data file> <number of names>\n");
   }
   
   /* Attempt to open the given name file and verify it is open */
      
   NmFlPt = fopen(argv[1],"r");
   
   if(NmFlPt == NULL)
   {
      printf("\nCan't seem to find the file. Is your path correct?\n");
      exit (0);
   }
   
   /* Find the start of data in the file */
   
   do
   {
      ReadLine();
   }while(strcmpi(TempString, "[start]") != 0);
   
   /* Read in the first data part and find the start of second data part */
   
   ReadLine(); 
   do
   {
      strcpy(Part1[Point1++], TempString);      
      ReadLine();
   }while(strcmpi(TempString, "[middle]") !=0); 

   /* Read in the second data part and find the start of third data part */
   
   ReadLine(); 
   do
   {
      strcpy(Part2[Point2++], TempString);      
      ReadLine();
   }while(strcmpi(TempString, "[end]") !=0); 

   /* Read in the third data part and find the end of data */
   
   ReadLine(); 
   do
   {
      strcpy(Part3[Point3++], TempString);      
      ReadLine();
   }while(strcmpi(TempString, "[stop]") !=0);
   
   /* Close the file */ 
   
   fclose(NmFlPt);
   
   /* Fire up the random number generator */
   
   srand(time(NULL));
   
   /* Build random name and print it the number of times given */
   
   printf("\n");
   
   for(LCount = 0; LCount < atoi(argv[2]); LCount++)
   {
      strcpy(TheName, Part1[rand()%Point1]);
      strcat(TheName, Part2[rand()%Point2]);
      strcat(TheName, Part3[rand()%Point3]);
      printf("%s, ", TheName);
   }
   
   printf("\n\n");
   
   return;
}

/* Function to get one line at a time from a file */

ReadLine()
{
   /*Declare some local variables */
   
   int StPt = 0;  
   char Getting;
   
   /* Read a line from the file while filtering out LF and CR */
   
   do
   {
      Getting = fgetc(NmFlPt);
      if((Getting != 10) && (Getting != 13))
      {
         TempString[StPt] = Getting;
         StPt = StPt + 1;
      }
   }while(Getting != 10);
   TempString[StPt] = NULL;
   
   return (0);
}

