/*	XGPostMacOSError.cpp
 *
 *		This knows how to deal with the MacOS Errors
 */

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#include <XConfig.h>

#if OPT_MACOS == 1

#include <string.h>
#include <XError.h>
#include <XDataUtil.h>

/************************************************************************/
/*																		*/
/*	Construction														*/
/*																		*/
/************************************************************************/

//	@G"Constructor/Destructor"

/*	@F"XGPostMacOSError::XGPostMacOSError" @I
 *
 *		Create this error, appending the OS Error string to the message
 *	passed to me
 */

XGPostMacOSError::XGPostMacOSError(OSErr err, unsigned long msg, ...)
{
	va_list vlist;
	
	va_start(vlist,msg);
	SetMessage(msg,vlist);
	va_end(vlist);
	
	fErrorID = err;
}

/************************************************************************/
/*																		*/
/*	Error Messages														*/
/*																		*/
/************************************************************************/

// @G"Public Methods"

/*	@F"XGPostMacOSError::GetLongError" @I
 *
 *		Get the message string. This actually looks up the error message
 *	that was passed in the constructor and appends it to the error
 *	description.
 */

void XGPostMacOSError::GetLongError(char *dest)
{
	char buffer[80];
	char buf2[256];
	
	XGPostError::GetLongError(buffer);
	GetErrorString(fErrorID,buf2);
	yformat(dest,"#S: #S",buf2,buffer);
}	

/************************************************************************/
/*																		*/
/*	Error Formatting													*/
/*																		*/
/************************************************************************/

/*	@F"XGPostMacOSError::GetErrorString" @I
 *
 *		Get the error string. Looks up the error message associated with
 *	this Macintosh specific OS error.
 */

void XGPostMacOSError::GetErrorString(OSErr err, char *str)
{
	Handle h;
	short len,i;
	short *ptr;
	short x;
	
	/*
	 *	Load the error string resources
	 */
	
	h = GetResource('OSer',16384);
	if (h) {
		len = *((short *)(*h));
		ptr = ((short *)(*h)) + 1;
		
		for (i = 0; i < len; i++) {
			if (*ptr == err) {
				cvtp2c(str,(unsigned char *)(ptr+1));
				return;
			} else {
				x = 3 + *((unsigned char *)(ptr+1));
				if (x & 1) x++;
				ptr += (x >> 1);
			}
		}
	}
	
	/*
	 *	If I get here, there is no error message. Format internally
	 */
	
	yformat(str,"Error #L",(long)err);
}

#endif
