/* *****                                                            *****
** *****           Program created by Ken Farinsky, 1986.           *****
** *****                                                            *****
**
** All material contained herein may be used in any way desired.
**                    >>>>> i.e.  PUBLIC DOMAIN. <<<<<
** Not limited to any single bulletin board - can be freely transferred.
**
** Original distribution through:
**     Slipped Disk, Inc.
**     Madison Heights,  MI  48071
**     (313) 583-9803
**
** Distributed in the hopes of increasing the level of understanding of the
** Amiga personal computer.  Even the best computer can only succeed with
** the proper software support.
*/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <functions.h>
#include "view.h"
#include <libraries/dos.h>
#include <exec/memory.h>

#define FIBSize		(long)(sizeof(struct FileInfoBlock) + 8)

extern struct Library	   *IntuitionBase ;
extern struct Library	   *GfxBase ;
extern struct Library	   *DosBase ;


/*----------------------------------------------------------------------
** read_datafile() - read the data into a newly allocated data area
** and put it into a format usable by the rest of the program (array
** of pointers to the lines of the file...).  memory key allows for
** later de-allocation of the data area and the array of pointers.
*/
void	 read_datafile( file_name, data, memory_key)
UBYTE			 *file_name ;
UBYTE		   ***data ;
struct Remember **memory_key ;
{
UBYTE					*file_store ;
struct FileInfoBlock	*fileinfo_block ;
struct Lock				*file_lock ;
long					 file_length ;
struct FileHandle		*fp ;
UBYTE					*current_position ;
SHORT					 num_lines ;
SHORT					 line_pos ;
UBYTE					*tmp_ptr ;

fileinfo_block =
	(struct FileInfoBlock *)AllocRemember(memory_key, FIBSize, MEMF_PUBLIC);

file_lock = Lock(file_name, ACCESS_READ) ;
if ( NULL == file_lock)
	{
	/* could not lock file - does not exist (or error) */
	*data = NULL ;
	return ;
	}

if ( Examine( file_lock, fileinfo_block))
	{
	if ( fileinfo_block->fib_EntryType == 2)
		{
		/* file is a directory... */
		*data = NULL ;
		UnLock( file_lock) ;
		return ;
		}

	file_store = (UBYTE *)AllocRemember(memory_key,
							fileinfo_block->fib_Size+8L, MEMF_PUBLIC);
	fp = Open(file_name, MODE_OLDFILE);
	file_length = Read(fp, file_store, fileinfo_block->fib_Size);
	Close(fp) ;
	*(file_store + fileinfo_block->fib_Size) = '\0' ;
	current_position = file_store ;
	num_lines = 1 ;
	while ( *current_position != '\0')
		{
		if ( *current_position == '\n')
			num_lines++ ;
		current_position++ ;
		}
	*data = (UBYTE **)AllocRemember(memory_key,
						(long)((8+num_lines)*sizeof(UBYTE *)),MEMF_PUBLIC);
	current_position = file_store ;
	line_pos = 2 ;
	**data = NULL ;
	*((*data) + 1) = file_store ;
	while ( *current_position != '\0')
		{
		if ( *current_position == '\n')
			{
			*current_position = '\0';
			*((*data) + line_pos++) = current_position + 1;
			}
		current_position++ ;
		}
	*((*data) + --line_pos) = NULL ;
	}
UnLock( file_lock) ;
}
