#include "stdafx.h"

/*
*	utilities.cpp
*	Developed for usage with my Win32 GUI tutorial,
*		by Chris Sammis
*	Website: http://nicki.ag.iastate.edu/projects/Win32/
*	Contact: csammis@cs.iastate.edu
*/

char** notes;	// An array of C-style strings
int currentLength;	// The number of notes in the array

// void ModifyNote()
// Inputs:
//	index - the index of the note to modify
//	note - the new text to insert into the array
// Outputs:
//	none
void ModifyNote(int index, const char* note)
{
	// Range checking
	if(index > currentLength || index < 0) return;
	if(currentLength <= 0) return;

	// Resize the requested array entry to accomodate the new text
	notes[index] = new char[strlen(note)+1];
	// Copy in the new text
	strncpy(notes[index],note,strlen(note));
	// Ensure that it's terminated properly
	notes[index][strlen(note)] = '\0';
}

// int GetNoteLength()
// Inputs:
//	index - the index of the note in question
// Outputs:
//	(int) length of the note in question
int GetNoteLength(int index)
{
	// Range checking
	if(index > currentLength || index < 0) return -1;
	return strlen(notes[index]);
}

// void GetNote()
// Inputs:
//	index - the index of the note being retrieved
//	buffer - a pointer to the location where copy of the note is to be placed
// Outputs:
//	none
// Assumptions: buffer is appropriately sized before being passed
void GetNote(int index, char* buffer)
{
	// Range checking
	if(index > currentLength || index < 0) return;
	// If the buffer is too small, return
	if(strlen(buffer) < strlen(notes[index])) return;
	// Zero out (empty) the buffer of any fluff
	memset(buffer,0,strlen(buffer)+1);
	// Place a copy of the note into the buffer
	strncpy(buffer,notes[index],strlen(notes[index]));
}

// void GetShortNote()
// Inputs:
//	index - the index of the note being retrieved
//	buffer - a pointer to the location where copy of the note is to be placed
//	length - the number of characters to truncate to
// Outputs:
//	none
// Assumptions: buffer is appropriately sized before being passed
// Note: Maximum return buffer size is length + 3 + 1
void GetShortNote(int index, char* buffer, unsigned int length)
{
	// Range checking
	if(index > currentLength || index < 0) return;
	// If the buffer is too small, return
	if(strlen(buffer) < length + 3 + 1) return;
	// Zero the buffer
	memset(buffer,0,strlen(buffer)+1);
	// Place a shortened copy of the note into the buffer
	strncpy(buffer,notes[index],length);
	// If the note is larger than the buffer, add ellipsis
	if(strlen(notes[index]) > length)
		strncpy(buffer+15,"...",3);
}

// void RemoveNoteFromList()
// Inputs:
//	index - the index of the note to remove
// Outputs:
//	none
void RemoveNoteFromList(int index)
{
	// Range checking
	if(index > currentLength || index < 0) return;
	if(currentLength <= 0) return;

	// Create a slightly smaller buffer to copy the notes into
	char** newNotes = new char*[currentLength-1];
	int j;

	for(int i=0;i<currentLength;i++)
	{
		if(i < index) j = i;
		if(i == index) continue;	// Don't copy the note at "index"
		if(i > index) j = i-1;
		newNotes[j] = new char[strlen(notes[i])];
		strcpy(newNotes[j],notes[i]);
	}

	currentLength--;
	// Recreate notes with the smaller size
	notes = new char*[currentLength];
	// Copy the temporary buffer to the permanent
	memcpy(notes,newNotes,sizeof(char*) * currentLength);
	delete [] newNotes;	// <-- good programming
}

// void InitializeNotes()
// Inputs:
//	hWndCbo - An HWND to the combo box from JotStuffDownPad
// Outputs:
//	none
void InitializeNotesList(HWND hWndCbo)
{
	currentLength = 0;
	notes = NULL;
	// We *must* start off with one note in the list.
	// Doing it in this function was convienient at the time of writing
	AddNoteToList("");
	SendMessage(hWndCbo,CB_ADDSTRING,NULL,(LPARAM)"[new note]");
	SendMessage(hWndCbo,CB_SETCURSEL,(WPARAM)0,NULL);
}

// void AddNoteToList()
// Inputs:
//	note - a pointer to the text to add
// Outputs:
//	none
void AddNoteToList(const char* note)
{
	// Create a temporary space for the notes while "notes" is destroyed and resized
	char** newNotes = new char*[currentLength+1];
	// Copy the notes into the temp buffer
	memcpy(newNotes,notes,sizeof(char*) * currentLength);
	// Destroy and resize notes
	delete [] notes;
	notes = new char*[currentLength+1];
	// Copy buffer back
	memcpy(notes,newNotes,sizeof(char*) * currentLength);
	delete [] newNotes;
	// Add the new note...
	notes[currentLength] = new char[strlen(note)+1];
	// ...and zero it out (set the string to NULL)
	memset(notes[currentLength],0,strlen(note)+1);
	// Copy in the new string
	strncpy(notes[currentLength+1],note,strlen(note));
	currentLength++;
}

