1 /*
2  * HLLib
3  * Copyright (C) 2006-2010 Ryan Gregg
4 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later
9  * version.
10  */
11 
12 #include "Error.h"
13 
14 using namespace HLLib;
15 
CError()16 CError::CError()
17 {
18 	*this->lpError = '\0';
19 	this->uiSystemError = 0;
20 	*this->lpSystemError = '\0';
21 	*this->lpShortFormattedError = '\0';
22 	*this->lpLongFormattedError = '\0';
23 }
24 
~CError()25 CError::~CError()
26 {
27 
28 }
29 
Clear()30 hlVoid CError::Clear()
31 {
32 
33 }
34 
GetErrorMessage() const35 const hlChar *CError::GetErrorMessage() const
36 {
37 	return this->lpError;
38 }
39 
GetSystemError() const40 hlUInt CError::GetSystemError() const
41 {
42 	return this->uiSystemError;
43 }
44 
GetSystemErrorMessage() const45 const hlChar *CError::GetSystemErrorMessage() const
46 {
47 	return this->lpSystemError;
48 }
49 
GetShortFormattedErrorMessage()50 const hlChar *CError::GetShortFormattedErrorMessage()
51 {
52 	if(this->uiSystemError == 0)
53 	{
54 		if(*this->lpError)
55 		{
56 			sprintf(this->lpShortFormattedError, "Error: %s", this->lpError);
57 		}
58 		else
59 		{
60 			strcpy(this->lpShortFormattedError, "<No error reported.>");
61 		}
62 	}
63 	else
64 	{
65 		sprintf(this->lpShortFormattedError, "Error (0x%.8x): %s %s", this->uiSystemError, this->lpError, this->lpSystemError);
66 	}
67 
68 	return this->lpShortFormattedError;
69 }
70 
GetLongFormattedErrorMessage()71 const hlChar *CError::GetLongFormattedErrorMessage()
72 {
73 	if(this->uiSystemError == 0)
74 	{
75 		if(*this->lpError)
76 		{
77 			sprintf(this->lpLongFormattedError, "Error:\n%s", this->lpError);
78 		}
79 		else
80 		{
81 			strcpy(this->lpLongFormattedError, "<No error reported.>");
82 		}
83 	}
84 	else
85 	{
86 		sprintf(this->lpLongFormattedError, "Error:\n%s\n\nSystem Error (0x%.8x):\n%s", this->lpError, this->uiSystemError, this->lpSystemError);
87 	}
88 
89 	return this->lpLongFormattedError;
90 }
91 
SetErrorMessage(const hlChar * lpError)92 hlVoid CError::SetErrorMessage(const hlChar *lpError)
93 {
94 	this->SetErrorMessageFormated("%s", lpError);
95 }
96 
SetErrorMessageFormated(const hlChar * lpFormat,...)97 hlVoid CError::SetErrorMessageFormated(const hlChar *lpFormat, ...)
98 {
99 	va_list ArgumentList;
100 	va_start(ArgumentList, lpFormat);
101 	vsprintf(this->lpError, lpFormat, ArgumentList);
102 	va_end(ArgumentList);
103 
104 	this->uiSystemError = 0;
105 	*this->lpSystemError = '\0';
106 }
107 
SetSystemErrorMessage(const hlChar * lpError)108 hlVoid CError::SetSystemErrorMessage(const hlChar *lpError)
109 {
110 	this->SetSystemErrorMessageFormated("%s", lpError);
111 }
112 
SetSystemErrorMessageFormated(const hlChar * lpFormat,...)113 hlVoid CError::SetSystemErrorMessageFormated(const hlChar *lpFormat, ...)
114 {
115 	va_list ArgumentList;
116 	va_start(ArgumentList, lpFormat);
117 	vsprintf(this->lpError, lpFormat, ArgumentList);
118 	va_end(ArgumentList);
119 
120 #ifdef _WIN32
121 	this->uiSystemError = GetLastError();
122 
123 	LPVOID lpMessage;
124 
125 	if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, this->uiSystemError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMessage, 0, NULL))
126 	{
127 		strcpy(this->lpSystemError, (hlChar *)lpMessage);
128 
129 		LocalFree(lpMessage);
130 #else
131 	this->uiSystemError = (hlUInt)errno;
132 
133 	hlChar *lpMessage = strerror(errno);
134 
135 	if(lpMessage != 0)
136 	{
137 		strcpy(this->lpSystemError, lpMessage);
138 #endif
139 
140 		hlUInt uiLength = (hlUInt)strlen(this->lpSystemError);
141 
142 		while(uiLength >= 0 && isspace(this->lpSystemError[uiLength - 1]))
143 		{
144 			uiLength--;
145 		}
146 		this->lpSystemError[uiLength] = '\0';
147 	}
148 	else
149 	{
150 		strcpy(this->lpSystemError, "<Unable to retrieve system error message string.>");
151 	}
152 }
153