/**********************************************
 **************    files.c   ******************
 **********************************************/

#define INTUI_V36_NAMES_ONLY

#include <exec/exec.h>

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

#include "consts.h"
#include "structs.h"
#include "proto.h"

extern prj_p prj;

/*
Function : void file_readnotes()
Purpose : Reads in the notes from the filename given in prj->prefs.notefile.
	If the file can't be opened an error is given and the program doesn't
	quit.
*/

void file_readnotes()
{
	struct note temp_note;
	note_p new_note;

	FILE *infp;

	char error_msg[STRLEN_ERRMSG],fontstyles[STRLEN_FONTSTYLES];
	int note_position;
	int temp_ints[4];

	/* Block input to windows */

	note_blockall();
	commod_block();

	/* Open the file */

	infp = fopen(prj->prefs.notefile,"r");

	if (!infp) {
		sprintf(error_msg,"Can't open file \"%s\"",prj->prefs.notefile);
		error(error_msg,ERR_NOTEFILEOPEN,__LINE__,__FILE__);

		/* Unblock input to windows */

		commod_blockclear();
		note_blockclearall();

		return;
	}

	/* Read in each note */

	while(TRUE) {
		note_position = next_free_note();

		/* The the info, if at any time an incomplete record is */
		/* found then stop the loop and don't create a note.    */

		/* Xpos,Ypos,Width,Height */

		if (4 != fscanf(infp,"%d,%d,%d,%d\n",&temp_ints[0],
&temp_ints[1],&temp_ints[2],&temp_ints[3]))
			break;

		temp_note.leftedge = (WORD)temp_ints[0];
		temp_note.topedge = (WORD)temp_ints[1];
		temp_note.width = (WORD)temp_ints[2];
		temp_note.height = (WORD)temp_ints[3];

		/* backcolour,textcolour,caratcolour */

		if (3 != fscanf(infp,"%d,%d,%d\n",&temp_ints[0],
&temp_ints[1],&temp_ints[2]))
			break;

		temp_note.backcolour = (UBYTE)temp_ints[0];
		temp_note.textcolour = (UBYTE)temp_ints[1];
		temp_note.caratcolour = (UBYTE)temp_ints[2];

		/* Public screen name */

		fgets(temp_note.pubscreen,STRLEN_PUBSCREEN,infp);
		replace_chrs(temp_note.pubscreen,'\n','\0'); /* Strip \n */

		/* Fontname (could be empty if default font) */

		fgets(temp_note.fontname,STRLEN_FONTNAME,infp);
		replace_chrs(temp_note.fontname,'\n','\0'); /* Strip \n */

		/* Font size */

		if (1 != fscanf(infp,"%du\n",&temp_ints[0]))
			break;

		temp_note.textattr.ta_YSize = (UWORD)temp_ints[0];

		/* Text styles */

		temp_note.textattr.ta_Style = NULL;

		fgets(temp_note.title,STRLEN_NOTETITLE,infp); /* Rogue \n */

		fgets(fontstyles,STRLEN_FONTSTYLES,infp);

		if (strchr(fontstyles,'B'))
			temp_note.textattr.ta_Style |= FSF_BOLD;

		if (strchr(fontstyles,'I'))
			temp_note.textattr.ta_Style |= FSF_ITALIC;

		if (strchr(fontstyles,'U'))
			temp_note.textattr.ta_Style |= FSF_UNDERLINED;

		/* Title bar text */

		fgets(temp_note.title,STRLEN_NOTETITLE,infp);
		replace_chrs(temp_note.title,'\n','\0');

		if (!strlen(temp_note.title))
			break;

		/* Main note text */

		fgets(temp_note.text,STRLEN_NOTETEXT,infp);
		replace_chrs(temp_note.text,'\n','\0');

		if (!strlen(temp_note.text))
			break;

		/* Now we have all the note info, could we add it ? */

		if (note_position == NO_FREE_NOTES) {
			sprintf(error_msg,"Too many notes in notefile.\n"
"Max. allowed = %d",NO_NOTES);
			error(error_msg,ERR_WARNING,__LINE__,__FILE__);

			return;
		}

		/* We now have enough for a note - yippee! */

		new_note = note_create(note_position);

		/* copy info into new note */

		new_note->leftedge = temp_note.leftedge;
		new_note->topedge = temp_note.topedge;
		new_note->width = temp_note.width;
		new_note->height = temp_note.height;

		new_note->backcolour = temp_note.backcolour;
		new_note->textcolour = temp_note.textcolour;
		new_note->caratcolour = temp_note.caratcolour;

		strncpy(new_note->fontname,&temp_note.fontname[1],STRLEN_FONTNAME);

		new_note->textattr.ta_YSize = temp_note.textattr.ta_YSize;
		new_note->textattr.ta_Style = temp_note.textattr.ta_Style;

		build_fontstring(new_note);

		strncpy(new_note->title,&temp_note.title[1],STRLEN_NOTETITLE);
		strncpy(new_note->text,&temp_note.text[1],STRLEN_NOTETEXT);
		strncpy(new_note->pubscreen,&temp_note.pubscreen[1],STRLEN_PUBSCREEN);
	}

	/* Close file */

	if (infp)
		fclose(infp);

	/* Unblock input to windows */

	commod_blockclear();
	note_blockclearall();

}

/*
Function : void file_writenotes()
Purpose : Writes the notes to the filename given in prj->prefs.notefile.
	If the file can't be opened an error is given and the program doesn't
	quit.
*/

void file_writenotes()
{
	note_p curr_note;

	FILE *outfp;

	char error_msg[STRLEN_ERRMSG];

	int l;

	/* Block input to windows */

	note_blockall();
	commod_block();

	/* Open the file */

	outfp = fopen(prj->prefs.notefile,"w");

	if (!outfp) {
		sprintf(error_msg,"Can't save notes to file \"%s\"",
prj->prefs.notefile);
		error(error_msg,ERR_NOTEFILEOPEN,__LINE__,__FILE__);

		/* Unblock input to windows */

		commod_blockclear();
		note_blockclearall();

		return;
	}

	/* Write each note */

	for (l = 0; ((l < NO_NOTES) && (prj->notes[l])); l++) {
		curr_note = prj->notes[l];

		/* Xpos,Ypos,Width,Height */

		fprintf(outfp,"%d,%d,%d,%d\n",curr_note->leftedge,
curr_note->topedge,curr_note->width,curr_note->height);

		/* backcolour,textcolour,caratcolour */

		fprintf(outfp,"%d,%d,%d\n",curr_note->backcolour,
curr_note->textcolour,curr_note->caratcolour);

		/* Public screen name */

		fprintf(outfp,"-%s\n",curr_note->pubscreen);

		/* Fontname (could be empty if default font) */

		fprintf(outfp,"-%s\n",curr_note->fontname);

		/* Font size */

		fprintf(outfp,"%d\n",curr_note->textattr.ta_YSize);

		/* Text styles */

		fprintf(outfp,"P%s%s%s\n",
(curr_note->textattr.ta_Style & FSF_BOLD) ? "B" : "",
(curr_note->textattr.ta_Style & FSF_ITALIC) ? "I" : "",
(curr_note->textattr.ta_Style & FSF_UNDERLINED) ? "U" : "");

		/* Title bar text */

		fprintf(outfp,"-%s\n",curr_note->title);

		/* Main note text */

		fprintf(outfp,"-%s\n",curr_note->text);
	}

	/* Close file */

	if (outfp)
		fclose(outfp);

	/* Mark the project as being unchanged */

	prj->projectchanged = FALSE;

	/* Clear input to all open windows */
	    
	note_blockclearall();
	commod_blockclear();
}
