/***********************************************
 **************   utility.c   ******************
 ***********************************************/

#define INTUI_V36_NAMES_ONLY

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

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

extern prj_p prj;

/*
Function : void remove_char_from_string(char *c)
Purpose : c is a pointer to a character in a note->text string. The operation
	is as follows:
			string\0 	   tring\0
		     cp-^	   ---> cp-^
*/

void remove_char_from_string(char *cp)
{
	int l = 0;

	/* Move characters down */

	while (cp[l] != '\0') {
		cp[l] = cp[l + 1];
		l++;
	}
}

/*
Function : void add_char_to_string(char *cp,char c)
Purpose : Given a pointer to a character in a string, adds the character c
	in that position and moves the rest of the characters along. As
	follows:
		    tring\0		    string\0
		 cp-^          --->      cp-^
*/

void add_char_to_string(char *cp,char c)
{
	int l;

	for (l = strlen(cp); l >= 0; l--)
		cp[l + 1] = cp[l];

	cp[0] = c;
}

/*
Function : int next_free_note()
Purpose : Returns the position in the note pointer array of the next free note
	position. Returns NO_FREE_NOTES if unsuccessful.
*/

int next_free_note()
{
	int l;

	for (l = 0; l < NO_NOTES; l++)
		if (!(prj->notes[l]))
			return(l);

	/* No notes free */

	return(NO_FREE_NOTES);
}

/*
Function : void replace_chrs(char *str,char chr_old,char chr_new)
Purpose : Given a string, will replace the first occurance of chr_old with
	chr_new.
*/

void replace_chrs(char *str,char chr_old,char chr_new)
{
	char *c;

	for (c = str; ((*c != '\0') && (*c != chr_old)); c++);

	if (*c == '\0')
		return;

	if (*c == chr_old)
		*c = chr_new;
}

/*
Function : void build_fontstring(note_p curr_note)
Purpose : Build nice fontstring.
*/

void build_fontstring(note_p curr_note)
{
	char *c,temp_str[STRLEN_FONTNAME];
	int n;

	if (curr_note->fontname[0] == '\0') {
		curr_note->fontstring[0] = '\0';
		return;
	}

	c = strstr(curr_note->fontname,".font");

	if (!c) {
		curr_note->fontstring[0] = '\0';
		return;
	}

	n = c-curr_note->fontname;
	strncpy(temp_str,curr_note->fontname,n);
	temp_str[n] = '\0';

	sprintf(curr_note->fontstring,"%s %d",temp_str,
curr_note->textattr.ta_YSize);
}

/*
Function : int positionfromnote(note_p curr_note)
Purpose : Given an array pointer, returns the position in the prj->notes[]
	array. Returns NOT_IN_ARRAY if can't find it.
*/

int positionfromnote(note_p curr_note)
{
	int l;

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

	/* Can't find it */

	return(NOT_IN_ARRAY);
}
