#include "CText.h"

CText::CText (void) : line_num (0), filename (PATH_MAX), lines (NULL),
  lines_cur (NULL), line_cur (0)
{
}

CText::~CText (void)
{
  delete_lines ();
}

// read a text file into memory
bool
CText::read_file (c_char name)
{
  char bufread[LINE_SIZE+1];
  filename = name;
  ifstream f ((char *)filename);
  if (!f)
    return 0;
  while (f.getline (bufread, LINE_SIZE))
    add_line (bufread);
  rewind_text ();
  return 1;
}

// move the current line pointer to the beginning
void
CText::rewind_text (void)
{
  lines_cur = lines;
  line_cur = 0;
}

// get the current line and move to the next
c_char 
CText::get_line (void)
{
  char *tmp = NULL;
  if (lines_cur != NULL)
    {
      tmp = lines_cur->text;
      lines_cur = lines_cur->next;
      line_cur++;
    }
  return tmp;
}

// delete all lines
void
CText::delete_lines (void)
{
  line_type *l = lines, *l2;
  while (l != NULL)
    {
      l2 = l->next;
      if (l->text != NULL)
        free (l->text);
      delete l;
      l = l2;
    }
  lines = NULL;
  line_num = line_cur = 0;
}

// add a text line to the end, unless it's an empty line
void
CText::add_line (c_char t)
{
  if (t == NULL || t[0] == 0)
    return;
  if (lines == NULL)				// if it's empty
    {
      lines = new line_type;
      lines->text = NULL;
      strset (&lines->text, t, MSG_SIZE);
      lines->next = NULL;
    }
  else
    {
      line_type *l_buf = lines;
      while (l_buf->next != NULL)		// else find the last
        l_buf = l_buf->next;
      l_buf->next = new line_type;
      l_buf->next->text = NULL;
      strset (&l_buf->next->text, t, MSG_SIZE);
      l_buf->next->next = NULL;
    }
  line_num++;
}

