notepad.c

nakedmudv3.0/src/editor/notepad.c · 3.3 KB

From the archive nakedmudv3.0.tar.tgz

Download the archive   « Back to the archive contents

//*****************************************************************************
//
// notepad.c
//
// Here is the proof of concept for the editor utility. This is a basic text
// editor that allows players to write/read information contained within a
// notepad.
//
//*****************************************************************************

#include "../mud.h"
#include "../utils.h"
#include "../inform.h"
#include "../auxiliary.h"
#include "../socket.h"
#include "../character.h"
#include "editor.h"



//*****************************************************************************
// mandatory modules
//*****************************************************************************
#include "../scripts/script_editor.h"



//*****************************************************************************
// auxiliary data for sockets
//*****************************************************************************
typedef struct notepad_data {
  BUFFER *notepad;
} NOTEPAD_DATA;

NOTEPAD_DATA *newNotepadData() {
  NOTEPAD_DATA *data = malloc(sizeof(NOTEPAD_DATA));
  data->notepad = newBuffer(1);
  return data;
}

void deleteNotepadData(NOTEPAD_DATA *data) {
  if(data->notepad) deleteBuffer(data->notepad);
  free(data);
}



//*****************************************************************************
// local datastructures, functions, and commands for players
//*****************************************************************************
void socketStartNotepad(SOCKET_DATA *sock, EDITOR *editor) {
  NOTEPAD_DATA *data = socketGetAuxiliaryData(sock, "notepad_data");
  socketStartEditor(sock, editor, data->notepad);
}

COMMAND(cmd_write) {
  if(!charGetSocket(ch))
    text_to_char(ch, "You need an attached socket for that!\r\n");
  else {
    message(ch, NULL, NULL, NULL, TRUE, TO_ROOM, 
	    "$n pulls out a pen and begins jotting notes down.");
    if(!strcasecmp(arg, "script"))
      socketStartNotepad(charGetSocket(ch), script_editor);
    else
      socketStartNotepad(charGetSocket(ch), text_editor);
  }
}

COMMAND(cmd_notepad) {
  if(!charGetSocket(ch))
    text_to_char(ch, "You need an attached socket for that!\r\n");
  else {
    SOCKET_DATA *sock = charGetSocket(ch);
    NOTEPAD_DATA *data = socketGetAuxiliaryData(sock, "notepad_data");
    if(!*bufferString(data->notepad))
      text_to_char(ch, "Your notepad is empty.\r\n");
    else
      text_to_char(ch, bufferString(data->notepad));
  }
}

BUFFER *socketGetNotepad(SOCKET_DATA *sock) {
  NOTEPAD_DATA *data = socketGetAuxiliaryData(sock, "notepad_data");
  return data->notepad;
}

void socketSetNotepad(SOCKET_DATA *sock, const char *txt) {
  NOTEPAD_DATA *data = socketGetAuxiliaryData(sock, "notepad_data");
  bufferClear(data->notepad);
  bufferCat(data->notepad, txt);
}



//*****************************************************************************
// setting everything up...
//*****************************************************************************
void init_notepad() {
  // install the editor components
  auxiliariesInstall("notepad_data", 
		     newAuxiliaryFuncs(AUXILIARY_TYPE_SOCKET,
				       newNotepadData, deleteNotepadData,
				       NULL, NULL, NULL, NULL));

  // install our commands
  add_cmd("write", NULL, cmd_write, POS_SITTING, POS_FLYING,
	  "player", FALSE, TRUE);
  add_cmd("notepad", NULL, cmd_notepad, POS_SITTING, POS_FLYING,
	  "player", FALSE, TRUE);
}

Site Stats

Snippets: 1791
Codebases: 571