/*
 *  messages.c
 */

#include "gui.h"


/*
 *  gui_messages_read
 */

char **gui_messages_read(const char *file_name)
{
  char *data;
  char **table;
  unsigned int size = 0, count = 0, pos = 0, i, len;
  int c;
  FILE *file;

  file = fopen(file_name, "rb");
  if (file == NULL) gui_raise_error("unable to open file");

  while ((c = fgetc(file)) != EOF)
  {
    size++;
    if (c == '\n') count++;
  }
  size++;

  data = malloc(size);
  if (data == NULL) gui_raise_error("out of memory");
  table = calloc(count, sizeof(char *));
  if (table == NULL) gui_raise_error("out of memory");

  if (fseek(file, 0, SEEK_SET)) gui_raise_error("file seek failed");

  for (i = 0; i < count; i++)
  {
    if (!fgets(data + pos, size - pos, file)) gui_raise_error("fgets failed");
    len = strlen(data + pos);
    data[pos + len - 1] = '\0';
    table[i] = data + pos;
    pos += len;
  }

  fclose(file);

  return table;
}
