1 /*
2  * Modification History
3  *
4  * 2001-February-12		Jason Rohrer
5  * Created.
6  *
7  * 2002-March-29    Jason Rohrer
8  * Added Fortify inclusion.
9  *
10  * 2002-March-31    Jason Rohrer
11  * Made destructor virtual so it works with subclasses.
12  * Fixed several bugs in deletion of mLastError.
13  */
14 
15 #include "minorGems/common.h"
16 
17 
18 #ifndef STREAM_CLASS_INCLUDED
19 #define STREAM_CLASS_INCLUDED
20 
21 
22 #include <string.h>
23 
24 
25 
26 #ifdef FORTIFY
27 #include "minorGems/util/development/fortify/fortify.h"
28 #endif
29 
30 
31 
32 /**
33  * Base class for all streams.
34  *
35  * @author Jason Rohrer
36  */
37 class Stream {
38 
39 	public:
40 
41 		/**
42 		 * Constructs a stream.
43 		 */
44 		Stream();
45 
46 		virtual ~Stream();
47 
48 		/**
49 		 * Gets the last error associated with this stream.
50 		 * Calling this function clears the error until
51 		 * another error occurs.
52 		 *
53 		 * @return the last stream error in human-readable form.
54 		 *   Must be destroyed by caller.  Returns NULL if
55 		 *   there is no error.  Note that this string is '\0' terminated.
56 		 */
57 		char *getLastError();
58 
59 
60 	protected:
61 
62 		/**
63 		 * Called by subclasses to specify a new last error.  Useful
64 		 * when error doesn't contain information specifiable by
65 		 * a constant string.
66 		 *
67 		 * @param inString human-readable string representing the error.
68 		 *   Note that this string must be '\0' terminated.
69          *   Will be destroyed by this class.
70 		 */
71 		void setNewLastError( char *inString );
72 
73 
74 		/**
75 		 * Called by subclasses to specify a new last error.  Useful
76 		 * when the error can be described by a constant string
77 		 *
78 		 * @param inString human-readable constant string representing
79 		 *   the error.
80 		 *   Note that this string must be '\0' terminated.
81 		 */
82 		void setNewLastErrorConst( const char *inString );
83 
84 
85 	private:
86 		// set to NULL when there is no error
87 		char *mLastError;
88 	};
89 
90 
91 
Stream()92 inline Stream::Stream()
93 	: mLastError( NULL ) {
94 
95 	}
96 
97 
98 
~Stream()99 inline Stream::~Stream() {
100 	if( mLastError != NULL ) {
101 		delete [] mLastError;
102 		}
103 	}
104 
105 
106 
getLastError()107 inline char *Stream::getLastError() {
108 
109 	char *returnString = mLastError;
110 	mLastError = NULL;
111 
112 	return returnString;
113 	}
114 
115 
116 
setNewLastError(char * inString)117 inline void Stream::setNewLastError( char *inString ) {
118 	if( mLastError != NULL ) {
119 		delete [] mLastError;
120 		}
121 
122 	mLastError = inString;
123 	}
124 
125 
126 
setNewLastErrorConst(const char * inString)127 inline void Stream::setNewLastErrorConst( const char *inString ) {
128 
129 	int length = 0;
130 	char lastChar = 'a';
131 	while( lastChar != '\0' ) {
132 		lastChar = inString[length];
133 		length++;
134 		}
135 
136 	// add one more to length to accommodate '\0' terminination
137 	length++;
138 
139 	if( mLastError != NULL ) {
140 		delete [] mLastError;
141 		}
142 
143 	mLastError = new char[ length ];
144 
145 	memcpy( mLastError, inString, length );
146 	}
147 
148 
149 
150 #endif
151