cotn25/area/
cotn25/src/
#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#endif

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include "merc.h"

#define MAX_PROJECT 20

PROJECT_DATA *project_list = NULL;
PROJECT_DATA *project_last = NULL;
PROJECT_DATA *project_free = NULL;


bool remove_project   args((int i));


void load_project()
{
  PROJECT_DATA *project;
  FILE *fp;
  char *name;

  if ((fp = fopen("../txt/project.txt", "r")) == NULL)
  {
    log_string("Non-fatal error: project.txt not found!");
    return;
  }
  name = fread_word(fp);
  while (str_cmp(name, END_MARKER))
  {
    project = alloc_perm(sizeof(PROJECT_DATA));
    project->imm  = str_dup(name);
    project->date = fread_string(fp);
    project->text = fread_string(fp);

    if (project_list)
      project_list->prev = project;
    project->next = project_list;
    project_list = project;

    if (!project_last) project_last = project;
    name = fread_word(fp);
  }

  fclose(fp);
}

void do_addproject( CHAR_DATA *ch, char *argument )
{
  PROJECT_DATA *project;
  PROJECT_DATA *cproject;
  char *strtime;
  char buf[50];
  int i;

  if (IS_NPC(ch)) return;

  /* we need something to add to the list */
  if (argument[0] == '\0' || strlen(argument) < 5)
  {
    send_to_char("What is your project?\n\r", ch);
    return;
  }
  /* Mainly to avoid that the list looks ugly */
  if (strlen(argument) > 60)
  {
    send_to_char("Keep it under 60 chars please.\n\r", ch);
    return;
  }

  /* Set the current time */
  strtime = ctime(&current_time);
  for (i = 0; i < 6; i++)
  {
    buf[i] = strtime[i + 4];
  }
  buf[6] = '\0';

  /* If we have a free project, we reuse it */
  if (project_free)
  {
    project = project_free;
    project_free = project_free->next;
    project->next = NULL;
    if (project_free) project_free->prev = NULL;
  }
  else
    project = alloc_perm(sizeof(*project));

  /* set the strings for the project */
  project->imm = str_dup(ch->name);
  project->text = str_dup(argument);
  project->date = str_dup(buf);

  /* If theres already a project, just add to the list */
  if (project_last)
  {
    project_last->next = project;
    project->prev = project_last;
    project->next = NULL;
    project_last = project;
  }
  else // there are no project.
  {
    project->next = project_list;
    project_list = project;
    project_last = project;
  }

  /* Removing the oldest project if the list has gone beyond the max */
  cproject = project_list; i = 0;
  while ((cproject = cproject->next) != NULL) i++;
  if (i >= MAX_PROJECT)
    remove_project(1);

  send_to_char("Project added.\n\r", ch );  
  save_project();
  return;
}

/*
 * The player function.
 * simply lists the last MAX_PROJECT projects
 */
void do_project(CHAR_DATA *ch, char *argument)
{
  PROJECT_DATA *project;
  char buf[MAX_STRING_LENGTH];
  char tempbuf[MAX_STRING_LENGTH];
  bool found = FALSE;
  int i = 0;

  if (IS_NPC(ch)) return;

  sprintf(buf, " #R[#0*******#R] #yNeeded projects to be done #R[#0*******#R]#n\n\r\n\r");
  for (project = project_list; project; project = project->next)
  {
    found = TRUE;
    ++i;
    sprintf(tempbuf, " #R[#0%3d#R] #G%-6s  #L%-9s #C%s#n\n\r",
      i, project->date, project->imm, project->text);
    strcat(buf, tempbuf);
  }
  if (found) send_to_char(buf, ch);
  else send_to_char("No projects.\n\r", ch);
  return;
}
void do_delproject(CHAR_DATA *ch, char *argument)
{
  char arg[MAX_INPUT_LENGTH];
  bool found = FALSE;
  int i;

  if (IS_NPC(ch)) return;

  one_argument(argument, arg);

  if ((i = atoi(arg)) < 1)
  {
    send_to_char("Which project did you want to remove ?\n\r", ch);
    return;
  }
  found = remove_project(i);
  if (!found) send_to_char("No such project.\n\r", ch);
  else send_to_char("Project removed.\n\r", ch);
  save_project();
  return;
}
bool remove_project(int i)
{
  PROJECT_DATA *project;
  bool found = FALSE;

  for (project = project_list; project; project = project->next)
  {
    if (--i > 0) continue;
    found = TRUE;

    /* clearing out the strings */
    free_string(project->imm);
    free_string(project->text);
    free_string(project->date);

    /* update the pointer to the last project if needed */
    if (project == project_last) project_last = project->prev;

    /* handle the special case of the first project */
    if (project == project_list)
    {
      project_list = project->next;
      if (project->next) project->next->prev = NULL;
    }
    else
    {
      project->prev->next = project->next;
      if (project->next)
        project->next->prev = project->prev;
    }

    /* Handle the free list */
    project->next = project_free;
    project->prev = NULL;
    if (project_free) project_free->prev = project;
    project_free = project;

    /* terminate the loop */
    break;
  }

  /* did we remove anything ? */
  return found;
}
void save_project()
{
  FILE *fp;
  PROJECT_DATA *project;
  int i = 0;

  if ((fp = fopen("../txt/project.txt","w")) == NULL)
  {
    log_string("Error writing to project.txt");
    return;
  }
  for (project = project_last; project; project = project->prev)
  {
    if (++i > MAX_PROJECT) break;
    fprintf(fp, "%s\n", project->imm);
    fprintf(fp, "%s~\n", project->date);
    fprintf(fp, "%s~\n", project->text);
  }
  fprintf(fp, "%s\n", END_MARKER);
  fclose(fp);
}