/* main.c from 'xferdemo' */

#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "event.h"
#include "baricon.h"
#include "resspr.h"
#include "res.h"
#include "menu.h"
#include "template.h"
#include "dbox.h"
#include "flex.h"
#include "akbd.h"
#include "xferrecv.h"
#include "saveas.h"
#include "bbc.h"
#include "werr.h"
#include "txt.h"
#include "kernel.h"

#include "showtext.h"

#include <stdlib.h>
#include <string.h>

/*************************** Statics etc. *************************/

#define Osfile_Savestamped 10
#define Osfile_Readinfo    17
#define Osfile_Load        255

/* Menu entires */
enum Iconbarmenu { Iconbarmenu_Info = 1, Iconbarmenu_Quit };

enum Textmenu { Textmenu_Save = 1 };

/* Dialogue box fields */
#define ProgInfo_Field    4

/* Text file type */
#define FileType_Text     0xfff

/* Version for info box */
static char *Version_String = "1.00 (19th July 1994)";

static menu menu_iconbar;

static char **text_buffer;

/************************* File functions *************************/

/* Save procedure for saving */
static BOOL save_proc(char *name, void *t)
{
  txt text = *(txt *)t;
  char *buffer;
  int n, res;
  _kernel_osfile_block block;

  txt_arrayseg(text, 0, &buffer, &n);
                           
  block.load = FileType_Text;
  block.start = (int)buffer;
  block.end = (int)buffer + n;
  
  if (res = _kernel_osfile(Osfile_Savestamped, name, &block), res == _kernel_ERROR || res == 0)
    return FALSE;
  else
    return TRUE;
}

/* RAM send procedure for saving */
static BOOL send_proc(void *t, int *maxsize)
{
  txt text = *(txt *)t;
  char *buffer;
  int n, length;

  txt_arrayseg(text, 0, &buffer, &n);
  
  while (n)
  {
    length = (n>*maxsize) ? *maxsize : n;
    if (!xfersend_sendbuf(buffer, length))
      return FALSE;
    else
      {
        buffer += length;
        n -= length;
      }
  }
  return TRUE;
}
