/*******************************************************************
 * File:         file.c
 * Purpose:      Acronyms translation
 * File purpose: File operations
 * Author:       Richard Butler & Gareth Duncan
 * Date:         11 Nov 2001
 ******************************************************************/

/*
    Acronyms - Translation of Internet and Computer-releated Acronyms
    Copyright  1996-1997-1998-1999-2000-2001-2002,
       Richard Butler & Gareth Duncan

    Thanks are due to Gareth Dukes for the sprites used in this program and
    Gareth Duncan for letting me (Rich) develop this program further.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#include "acronyms.h"

// The string read from the file is stored here
char *buffer = 0;

// These store all the data:
extern char *acronyms[MAX_ACRONYMS+1];
extern char *acronyms_answers[MAX_ACRONYMS+1];
extern char *emoticons[MAX_ACRONYMS+1];
extern char *emoticons_answers[MAX_ACRONYMS+1];
extern char *computers[MAX_ACRONYMS+1];
extern char *computers_answers[MAX_ACRONYMS+1];

//***************************************************************************
// INITIALISE_TEXT_FILES
// Get the text files ready
//***************************************************************************
void initialise_text_files(void)
{
  int pass = 0; // Used for counting passes through for(*) {*}'s

  load_text_file("AcronymsData:Acronyms"); // Load file Acronyms
  // Split the acronyms and their answers up
  for(pass = 0; pass <= MAX_ACRONYMS; pass++)
  {
    // Place the split strings into the acronyms array
    acronyms[pass] = strtok(buffer, "\n");
    buffer=0; // Reset to 0 and repeat with the next split string
    // Place the split strings into the answers array
    acronyms_answers[pass] = strtok(buffer, "\n");
    free(buffer); // Frees the memory allocated to buffer
  }

  load_text_file("AcronymsData:Emoticons"); // Load file Emoticons
  // Split the acronyms and their answers up
  for(pass = 0; pass <= MAX_ACRONYMS; pass++)
  {
    // Place the split strings into the acronyms array
    emoticons[pass] = strtok(buffer, "\n");
    buffer=0; // Reset to 0 and repeat with the next split string
    // Place the split strings into the answers array
    emoticons_answers[pass] = strtok(buffer, "\n");
    free(buffer); // Frees the memory allocated to buffer
  }

  load_text_file("AcronymsData:Computers"); // Load file Computers
  // Split the acronyms and their answers up
  for(pass = 0; pass <= MAX_ACRONYMS; pass++)
  {
    // Place the split strings into the acronyms array
    computers[pass] = strtok(buffer, "\n");
    buffer=0; // Reset to 0 and repeat with the next split string
    // Place the split strings into the answers array
    computers_answers[pass] = strtok(buffer, "\n");
    free(buffer); // Frees the memory allocated to buffer
  }
}

//***************************************************************************
// LOAD_TEXT_FILE
// Load text file into memory
//***************************************************************************
void load_text_file(char *file_name)
{
  FILE *file; // A pointer to an open file
  size_t fSize; // The size of the open file

  file = fopen(file_name, "r");

  fseek(file, 0L, SEEK_END);
  fSize = (size_t) ftell(file);
  fseek(file, 0L, SEEK_SET);

  if (buffer != NULL) {
    free(buffer);
  }

  buffer = (char *) malloc(fSize+1);
  fread(buffer, fSize, 1, file);
  fclose(file);
  buffer[fSize] = '\0';
}
