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

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>

#if OPT_MACOS == 1

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

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

/*$	XGPostMacOSError::XGPostMacOSError
 *
 *		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														*/
/*																		*/
/************************************************************************/

/*$	XGPostMacOSError::GetLongError
 *
 *		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",buffer,buf2);
}	

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

/*$	XGPostMacOSError::GetErrorString
 *
 *		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
