annotate Isolated/tErrorLib.c @ 79:358b0bd5df43 tip

Added support for Apportable's alcSuspend()/alcResume() in BeginInterruption()/EndInterruption. You must define ALMIXER_USE_APPORTABLE_OPENAL_EXTENSIONS to compile in this support.
author Eric Wing <ewing@coronalabs.com>
date Tue, 30 Oct 2012 16:01:30 -0700
parents aace3301c8d1
children
rev   line source
38
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1 /* See header file for license information. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
2
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
3 #include "tErrorLib.h"
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
4 #include <stdlib.h> /* for malloc */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
5 #include <string.h>
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
6 #include <stdarg.h> /* for vasprintf */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
7 #include <stdio.h> /* also for vasprintf and for printf family */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
8 #include <stddef.h> /* size_t */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
9
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
10 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
11 * For string-only based usage, this implementation
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
12 * still expects an actual error number to be set.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
13 * I am defining 1 as that error value. This might be changable,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
14 * but it is untested. If you change this value, you must recompile
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
15 * the entire library. This can really be any integer except what
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
16 * TERROR_NOERROR_VALUE (in header) is set to.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
17 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
18 #define TERROR_ERROR_VALUE 1
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
19
61
72570fcd30e8 Fixed #define DONT_USE_VASPRINT(F) typo for Thanks to Johnson Lin for testing and reporting these issues!
Eric Wing <ewing . public |-at-| gmail . com>
parents: 39
diff changeset
20 #ifdef DONT_USE_VASPRINTF
38
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
21 #define TERROR_DEFAULT_STRING_LENGTH 128
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
22 /* Visual Studio doesn't define snprintf but _snprintf */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
23 #ifdef _MSC_VER
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
24 #define snprintf _snprintf
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
25 #define vsnprintf _vsnprintf
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
26 #endif
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
27 #endif
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
28
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
29
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
30 #if defined(_WIN32) && !defined(__CYGWIN32__)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
31 #include <windows.h>
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
32 #include <winbase.h> /* For CreateMutex(), LockFile() */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
33
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
34 static void* Internal_CreateMutex()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
35 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
36 return((void*)CreateMutex(NULL, FALSE, NULL));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
37 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
38 static void Internal_DestroyMutex(void* mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
39 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
40 if(NULL != mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
41 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
42 CloseHandle( (HANDLE)mutex );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
43 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
44 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
45 /* This will return true if locking is successful, false if not.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
46 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
47 static int Internal_LockMutex(void* mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
48 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
49 return(
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
50 WaitForSingleObject(
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
51 (HANDLE)mutex,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
52 INFINITE
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
53 ) != WAIT_FAILED
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
54 );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
55 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
56 static void Internal_UnlockMutex(void* mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
57 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
58 ReleaseMutex(
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
59 (HANDLE)mutex
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
60 );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
61 }
64
aace3301c8d1 Fixed misspelled function name in _WIN32 section 'Internal_PlatformPlatformGetThreadID' (looks like search & replace bug). Thanks to Johnson Lin reporting this issue!
Eric Wing <ewing . public |-at-| gmail . com>
parents: 61
diff changeset
62 size_t Internal_PlatformGetThreadID(void)
38
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
63 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
64 return((size_t)GetCurrentThreadId());
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
65 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
66 #else /* Assuming POSIX...maybe not a good assumption. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
67 #include <pthread.h>
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
68 static void* Internal_CreateMutex()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
69 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
70 int ret_val;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
71 pthread_mutex_t* m = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
72 if(NULL == m)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
73 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
74 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
75 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
76 ret_val = pthread_mutex_init(m, NULL);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
77 if(0 != ret_val)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
78 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
79 free(m);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
80 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
81 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
82 return((void*)m);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
83 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
84 static void Internal_DestroyMutex(void* mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
85 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
86 if(NULL != mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
87 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
88 pthread_mutex_destroy((pthread_mutex_t*) (mutex));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
89 free(mutex);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
90 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
91 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
92 /* This will return true if locking is successful, false if not.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
93 * (This is the opposite of pthread_mutex_lock which returns
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
94 * 0 for success.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
95 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
96 static int Internal_LockMutex(void* mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
97 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
98 return(
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
99 pthread_mutex_lock(
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
100 (pthread_mutex_t*)mutex
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
101 ) == 0
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
102 );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
103 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
104 static void Internal_UnlockMutex(void* mutex)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
105 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
106 pthread_mutex_unlock(
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
107 (pthread_mutex_t*)mutex
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
108 );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
109 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
110
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
111 size_t Internal_PlatformGetThreadID()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
112 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
113 /* Basically, we need to convert a pthread_t into an id number. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
114 return (size_t)pthread_self();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
115 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
116 #endif
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
117
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
118
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
119 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
120 * Copies a source string, potentially to a target string, and returns
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
121 * the pointer to the copied string.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
122 * This function is a intended to be an efficient string copy function.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
123 * It's purpose is to copy a string into a string with preallocated memory
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
124 * and avoid dynamic memory allocation if possible. If memory must
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
125 * be allocated, then the old string will be destroyed.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
126 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
127 * This is only to be used where target_string was created with dynamic
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
128 * memory. This function will destroy the memory and allocate new memory
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
129 * if there is not enough space in the target string.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
130 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
131 * @param target_string This is the string you would like to try
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
132 * to copy into. If there is not enough space, a new string will
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
133 * be created and the target_string will be freed. This string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
134 * must have been created dynamically. This may be NULL if you
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
135 * wish for this function to dynamically create a new string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
136 * for you.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
137 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
138 * @param target_max_buffer_size This is a pointer that points to
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
139 * an address containing the size of the preallocated target_string.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
140 * This size is the maximum buffer length which includes the '\\0'
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
141 * character as part of that count. This pointer may not be NULL.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
142 * If you pass in NULL for the target_string (indicating you want
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
143 * a new string allocated for you), then the size should be set to 0.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
144 * When the function completes, the size will be set to the new
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
145 * max buffer size of the string if the string needed to be reallocated.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
146 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
147 * @param source_string This is the string you want to copy. If it's NULL,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
148 * the target_string will have it's memory freed.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
149 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
150 * @return Will return a pointer to the duplicated string. Be aware
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
151 * of several things:
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
152 * - The returned pointer address may not be the same address as the
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
153 * target string passed in (due to a possible reallocation).
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
154 * - If the pointer to the source and target string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
155 * are the same, the pointer to the target string will be returned.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
156 * - If the source string is NULL, the target string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
157 * will be freed and will return NULL.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
158 * - If an error occurs, NULL will be returned.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
159 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
160 * Also note that the value at the address target_max_buffer_size points
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
161 * to will be filled with the new max buffer size for the string.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
162 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
163 * Example:
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
164 * @code
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
165 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
166 * int main()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
167 * {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
168 * const char* original1 = "Hello World";
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
169 * const char* original2 = "Smaller";
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
170 * const char* original3 = "Good-Bye World";
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
171 * char* ret_val;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
172 * char* target = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
173 * size_t target_max_buffer_size = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
174 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
175 * ret_val = CopyDynamicString(target, &target_max_buffer_size, original1);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
176 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
177 * if(ret_val)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
178 * {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
179 * fprintf(stderr, "Target is '%s' with max size = %d\n", ret_val, target_max_buffer_size);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
180 * }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
181 * else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
182 * {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
183 * fprintf(stderr, "Error in function\n");
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
184 * }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
185 * target = ret_val;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
186 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
187 * ret_val = CopyDynamicString(target, &target_max_buffer_size, original2);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
188 * fprintf(stderr, "Target is '%s' with max size = %d\n", ret_val, target_max_buffer_size);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
189 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
190 * target = ret_val; *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
191 * ret_val = CopyDynamicString(target, &target_max_buffer_size, original3);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
192 * fprintf(stderr, "Target is '%s' with max size = %d\n", ret_val, target_max_buffer_size);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
193 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
194 * return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
195 * }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
196 * @endcode
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
197 * This outputs:
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
198 * @code
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
199 * Target is 'Hello World' with max size = 12
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
200 * Target is 'Smaller' with max size = 12
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
201 * Target is 'Good-Bye World' with max size = 15
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
202 * @endcode
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
203 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
204 static char* Internal_CopyDynamicString(char* target_string, size_t* target_max_buffer_size, const char* source_string)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
205 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
206 /* If the pointers are the same, no copy is needed. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
207 if(source_string == target_string)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
208 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
209 /* I don't feel like asserting if the sizes are the same. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
210 /* Return 1 instead of 0 because maybe this isn't an error?
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
211 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
212 return target_string;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
213 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
214
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
215 /* Make sure the size pointer is valid. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
216 if(NULL == target_max_buffer_size)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
217 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
218 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
219 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
220
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
221 /* Yikes, if the string is NULL, should we make the target string NULL?
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
222 * For now, yes, we destroy the string. If you change this, realize that
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
223 * their is code that depends on this behavior.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
224 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
225 if(NULL == source_string)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
226 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
227 *target_max_buffer_size = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
228 free(target_string);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
229 target_string = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
230 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
231 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
232
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
233 /* If target_string is NULL, the *target_max_buffer_size should also be 0.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
234 * Technically, the user should set this and this would be an error,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
235 * but I'll be nice for now. An alternate implementation might suggest
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
236 * that the size would be the desired size the user wants for a new string.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
237 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
238 if( (NULL == target_string) && (0 != *target_max_buffer_size) )
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
239 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
240 *target_max_buffer_size = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
241 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
242
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
243 /* If there is not enough preallocated memory in the target string,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
244 * then we need to reallocate enough memory.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
245 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
246 if( *target_max_buffer_size < (strlen(source_string) + 1) )
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
247 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
248 *target_max_buffer_size = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
249 if(NULL != target_string)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
250 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
251 free(target_string);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
252 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
253 target_string = (char*)calloc( (strlen(source_string) + 1), sizeof(char) );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
254 if(NULL == target_string)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
255 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
256 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
257 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
258 *target_max_buffer_size = strlen(source_string) + 1;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
259 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
260
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
261 /* At this point, there should be enough preallocated
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
262 * memory to call strncpy.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
263 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
264 strncpy(target_string, source_string, *target_max_buffer_size);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
265
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
266 return target_string;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
267 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
268
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
269 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
270 * This is a structure that contains everything needed for an
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
271 * error message entry (per thread). The linked list stuff
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
272 * is fused in with it because I didn't want to write an entire
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
273 * linked list class.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
274 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
275 typedef struct TErrorMessageStructType
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
276 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
277 size_t threadID; /** ThreadID for associated message. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
278 int errorAvailable; /** 1 if an error has been set and not been checked. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
279 int errorNumber; /**< For the user error number. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
280 char* errorString; /**< For the user error message. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
281 size_t errorMaxStringLength; /**< Max size of string buffer including \\0. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
282 struct TErrorMessageStructType* nextItem; /**< Pointer to next error message in list. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
283 } TErrorMessage;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
284
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
285 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
286 * This is a private struct that contains all private data for an
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
287 * ErrorPool. Currently it is a linked list containing all error message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
288 * structs for every thread.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
289 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
290 typedef struct
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
291 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
292 TErrorMessage* errorMessageListHead; /**< Head of the error list. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
293 TErrorMessage* lastErrorMessage; /**< Points to the last set element in the list for GetLastError. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
294 /* Mutex */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
295 } TErrorPoolOpaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
296
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
297 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
298 * This is a private helper function that creates a new TErrorMessage
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
299 * and initializes all its values.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
300 * @return Returns a pointer to a newly allocated and initialized
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
301 * TErrorMessage or NULL on failure.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
302 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
303 static TErrorMessage* Internal_CreateErrorMessageStructure()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
304 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
305 TErrorMessage* new_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
306 /* Use calloc to create a fully cleared structure,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
307 * so I don't have to set/clear each member.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
308 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
309 new_message = (TErrorMessage*)calloc(1, sizeof(TErrorMessage));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
310 if(NULL == new_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
311 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
312 /* Very bad, but not sure what to do. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
313 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
314 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
315 new_message->errorNumber = TERROR_NOERROR_VALUE;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
316 return new_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
317 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
318
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
319 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
320 * This is a private helper function that frees a TErrorMessage.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
321 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
322 * @param err_mesg The pointer to the TErrorMessage to be freed.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
323 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
324 static void Internal_FreeErrorMessageStructure(TErrorMessage* err_mesg)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
325 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
326 if(NULL == err_mesg)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
327 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
328 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
329 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
330 if(NULL != err_mesg->errorString)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
331 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
332 free(err_mesg->errorString);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
333 err_mesg->errorString = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
334 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
335 err_mesg->nextItem = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
336 free(err_mesg);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
337 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
338
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
339 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
340 * This is a private helper function that will search the error pool
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
341 * for the last set error message structure in the Linked list.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
342 * If the last error message was on a different thread, the error
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
343 * data will be copied to the current thread's memory and the
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
344 * lastErrorMessage pointer will be set to the current thread's message.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
345 * (This is because I expect this message to be marked as cleared/read.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
346 * This function does its own mutex locking.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
347 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
348 * @param err_pool The error pool to be used.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
349 * @return Returns the a pointer to the TErrorMessage if found,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
350 * NULL if not found.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
351 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
352 static TErrorMessage* Internal_GetLastError(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
353 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
354 size_t thread_id;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
355 TErrorMessage* current_thread_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
356 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
357
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
358 thread_id = Internal_PlatformGetThreadID();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
359
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
360 Internal_LockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
361
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
362 err_pool_data = err_pool->opaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
363
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
364 if(NULL == err_pool_data->errorMessageListHead)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
365 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
366 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
367 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
368 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
369
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
370 /* I think this is actually an assertion failure.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
371 * I do the check here so I don't have to keep checking below.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
372 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
373 if(NULL == err_pool_data->lastErrorMessage)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
374 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
375 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
376 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
377 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
378
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
379 /* We need to determine if the lastMessage pointer is pointing
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
380 * to data on the current thread. If it is we can just return it.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
381 * Otherwise, we need to copy the message to the current thread's
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
382 * error message memory area.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
383 * We should also update the lastMessage pointer to point
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
384 * to this message since it will likely be marked cleared once read.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
385 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
386 if(thread_id == err_pool_data->lastErrorMessage->threadID)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
387 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
388 /* Not copy is needed. The last error message already
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
389 * points to the memory on the current thread.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
390 * We can short-circuit and return.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
391 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
392 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
393 return err_pool_data->lastErrorMessage;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
394 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
395
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
396 /* Sigh, I really should have a dedicated linked list structure,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
397 * but I don't feel like writing it right now.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
398 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
399 for(current_thread_err_mesg = err_pool_data->errorMessageListHead; current_thread_err_mesg != NULL; current_thread_err_mesg = current_thread_err_mesg->nextItem)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
400 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
401 /* First find the message (memory) for the current thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
402 if(thread_id == current_thread_err_mesg->threadID)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
403 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
404 /* Now we need to copy the message data from the lastErrorMessage
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
405 * to this thread's message (memory).
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
406 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
407 current_thread_err_mesg->errorNumber = err_pool_data->lastErrorMessage->errorNumber;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
408 current_thread_err_mesg->errorAvailable = err_pool_data->lastErrorMessage->errorAvailable;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
409 /* This will copy the string and set the new errorMaxStringLength as needed. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
410 current_thread_err_mesg->errorString = Internal_CopyDynamicString(current_thread_err_mesg->errorString, &current_thread_err_mesg->errorMaxStringLength, err_pool_data->lastErrorMessage->errorString);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
411
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
412
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
413 /* Finally, change the last error message to point to
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
414 * the current thread since I expect the message to be
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
415 * marked cleared and we don't want to accidentally refetched
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
416 * the stale, uncleared entry.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
417 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
418 err_pool_data->lastErrorMessage = current_thread_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
419
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
420 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
421 return current_thread_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
422 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
423 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
424 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
425 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
426 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
427
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
428 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
429 * This is a private helper function that will search the error pool
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
430 * for an error message structure in the Linked list (by thread ID)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
431 * and return the pointer if found. This function does its own mutex
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
432 * locking.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
433 * @param err_pool The error pool to be used.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
434 * @return Returns the a pointer to the TErrorMessage if found,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
435 * NULL if not found.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
436 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
437 static TErrorMessage* Internal_GetErrorOnCurrentThread(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
438 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
439 size_t thread_id;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
440 TErrorMessage* current_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
441 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
442
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
443 thread_id = Internal_PlatformGetThreadID();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
444
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
445 Internal_LockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
446
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
447 err_pool_data = err_pool->opaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
448
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
449 if(NULL == err_pool_data->errorMessageListHead)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
450 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
451 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
452 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
453 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
454
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
455 /* Sigh, I really should have a dedicated linked list structure,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
456 * but I don't feel like writing it right now.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
457 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
458 for(current_err_mesg = err_pool_data->errorMessageListHead; current_err_mesg != NULL; current_err_mesg = current_err_mesg->nextItem)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
459 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
460 if(thread_id == current_err_mesg->threadID)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
461 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
462 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
463 return current_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
464 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
465 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
466 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
467 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
468 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
469
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
470 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
471 * Given a specific TErrorMessage*, will set the lastErrorMessage pointer to
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
472 * the provided error message.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
473 * This function locks.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
474 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
475 * @param err_pool The error pool to be used.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
476 * @param error_message The error message to set the lastErrorMessage pointer to
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
477 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
478 static void Internal_SetLastErrorMessagePointerToErrorMessage(TErrorPool* err_pool, TErrorMessage* error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
479 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
480 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
481 Internal_LockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
482 err_pool_data = err_pool->opaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
483 err_pool_data->lastErrorMessage = error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
484 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
485 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
486
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
487
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
488 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
489 * This is a private helper function that creates a new error message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
490 * structure for the current thread.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
491 * This currently does not check if an error already exists
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
492 * before creating a new entry. Call GetErrorOnCurrentThread first
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
493 * to make sure nothing exists or duplicate entries will be created.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
494 * This function does its own mutex locking.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
495 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
496 * @param err_pool The error pool to be used.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
497 * @return Returns the a pointer to the TErrorMessage if found,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
498 * NULL if there was an allocation error.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
499 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
500 static TErrorMessage* Internal_CreateErrorOnCurrentThread(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
501 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
502 TErrorMessage* new_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
503 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
504
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
505 new_err_mesg = Internal_CreateErrorMessageStructure();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
506 if(NULL == new_err_mesg)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
507 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
508 /* Serious problem, not sure what to do. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
509 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
510 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
511 /* Copy the thread id so we can distinguish between entries. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
512 new_err_mesg->threadID = Internal_PlatformGetThreadID();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
513
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
514 Internal_LockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
515
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
516 err_pool_data = err_pool->opaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
517 /* Add the new message to the top of the list by making
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
518 * its next pointer point to the head of the current list.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
519 * (A formal linked list implementation would make this feel
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
520 * less hacky.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
521 * This also (should) handle the case where errorMessageListHead
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
522 * is NULL.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
523 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
524 new_err_mesg->nextItem = err_pool_data->errorMessageListHead;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
525 /* Now set the head of the list to the new message.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
526 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
527 err_pool_data->errorMessageListHead = new_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
528
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
529 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
530
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
531 return new_err_mesg;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
532 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
533
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
534 /**
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
535 * This is a private helper function that will clean up all the
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
536 * error message structures in the list. This function does its
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
537 * own locking.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
538 * @param err_pool The error pool to be used.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
539 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
540 static void Internal_FreeErrorMessageList(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
541 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
542 TErrorMessage* current_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
543 TErrorMessage* next_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
544 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
545
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
546 Internal_LockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
547
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
548 err_pool_data = err_pool->opaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
549
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
550 if(NULL == err_pool_data->errorMessageListHead)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
551 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
552 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
553 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
554 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
555
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
556 /* Sigh, I really should have a dedicated linked list structure,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
557 * but I don't feel like writing it right now.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
558 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
559 for(current_message = err_pool_data->errorMessageListHead;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
560 current_message != NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
561 current_message = next_message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
562 )
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
563 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
564 next_message = current_message->nextItem;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
565 Internal_FreeErrorMessageStructure(current_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
566 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
567 err_pool_data->errorMessageListHead = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
568 err_pool_data->lastErrorMessage = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
569
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
570 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
571 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
572
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
573 /*
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
574 * API functions start below.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
575 *
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
576 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
577
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
578
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
579 void TError_DeleteEntryOnCurrentThread(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
580 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
581 TErrorMessage* prev_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
582 TErrorMessage* current_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
583 TErrorMessage* next_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
584 size_t thread_id;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
585 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
586
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
587 thread_id = Internal_PlatformGetThreadID();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
588
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
589 Internal_LockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
590
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
591 err_pool_data = err_pool->opaqueData;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
592
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
593 if(NULL == err_pool_data->errorMessageListHead)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
594 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
595 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
596 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
597 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
598
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
599 /* Sigh, I really should have a dedicated linked list structure,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
600 * but I don't feel like writing it right now.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
601 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
602 for(current_message = err_pool_data->errorMessageListHead;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
603 current_message != NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
604 /* I'm not going to increment here because I
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
605 * may delete the item below which would probably
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
606 * cause bad things to happen here.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
607 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
608 /* current_message = current_message->nextItem */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
609 )
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
610 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
611 next_message = current_message->nextItem;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
612
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
613 if(thread_id == current_message->threadID)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
614 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
615 /* Special case, current is only item in list:
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
616 * Both next and prev are NULL in this case.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
617 * We should delete the item and set the errorMessageListHead
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
618 * to NULL.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
619 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
620 if((NULL == prev_message) && (NULL == next_message))
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
621 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
622 Internal_FreeErrorMessageStructure(current_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
623 current_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
624 err_pool_data->errorMessageListHead = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
625 err_pool_data->lastErrorMessage = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
626 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
627 /* Special case, current is at head:
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
628 * Prev is NULL but next is not NULL in this case.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
629 * We should delete the item and set the errorMessageListHead
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
630 * to point to next.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
631 * (The code for the above case would probably work for
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
632 * this case too, but for clarity, this remains.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
633 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
634 else if(NULL == prev_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
635 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
636 /* If the current message happened to be the last message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
637 * set, we need to change the lastErrorMessage pointer
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
638 * so it is not dangling.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
639 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
640 if(current_message == err_pool_data->lastErrorMessage)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
641 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
642 err_pool_data->lastErrorMessage = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
643 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
644 Internal_FreeErrorMessageStructure(current_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
645 current_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
646 err_pool_data->errorMessageListHead = next_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
647 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
648 /* Special case, current is at tail.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
649 * Prev is not NULL, but next is NULL in this case.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
650 * We should delete the item and set prev->next to NULL.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
651 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
652 else if(NULL == next_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
653 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
654 /* If the current message happened to be the last message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
655 * set, we need to change the lastErrorMessage pointer
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
656 * so it is not dangling.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
657 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
658 if(current_message == err_pool_data->lastErrorMessage)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
659 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
660 err_pool_data->lastErrorMessage = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
661 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
662 Internal_FreeErrorMessageStructure(current_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
663 current_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
664 prev_message->nextItem = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
665 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
666 /* Normal case, current is somewhere in the middle of the list.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
667 * The item should be deleted and
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
668 * the prev_message->next should connect to
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
669 * the next_message.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
670 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
671 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
672 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
673 /* If the current message happened to be the last message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
674 * set, we need to change the lastErrorMessage pointer
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
675 * so it is not dangling.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
676 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
677 if(current_message == err_pool_data->lastErrorMessage)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
678 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
679 err_pool_data->lastErrorMessage = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
680 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
681 Internal_FreeErrorMessageStructure(current_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
682 current_message = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
683 prev_message->nextItem = next_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
684 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
685 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
686 /* It's not this thread, so increment everything for the next loop. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
687 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
688 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
689 prev_message = current_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
690 current_message = next_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
691 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
692 } /* End for-loop */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
693
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
694 Internal_UnlockMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
695 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
696
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
697
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
698 void TError_GetLinkedVersion(TErrorVersion* ver)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
699 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
700 /* Check the pointer */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
701 if(NULL == ver)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
702 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
703 /* Do nothing */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
704 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
705 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
706 ver->major = TERROR_MAJOR_VERSION;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
707 ver->minor = TERROR_MINOR_VERSION;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
708 ver->patch = TERROR_PATCH_VERSION;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
709 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
710
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
711
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
712 #if 0
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
713 /* This is for global initialization, not pool initialization. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
714 int TError_Init()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
715 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
716 /* initialize platform? */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
717 /* initialize mutexes? */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
718
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
719 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
720 #endif
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
721
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
722 TErrorPool* TError_CreateErrorPool()
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
723 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
724 TErrorPool* err_pool;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
725 TErrorPoolOpaqueData* err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
726
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
727 err_pool = (TErrorPool*)calloc(1, sizeof(TErrorPool));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
728 if(NULL == err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
729 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
730 /* Very bad, but not sure what to do here. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
731 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
732 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
733 err_pool_data = (TErrorPoolOpaqueData*)calloc(1, sizeof(TErrorPoolOpaqueData));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
734 if(NULL == err_pool_data)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
735 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
736 /* Very bad, but not sure what to do here. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
737 free(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
738 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
739 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
740
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
741 /* Create mutex */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
742 err_pool->mutexLock = Internal_CreateMutex();
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
743
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
744 if(NULL == err_pool->mutexLock)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
745 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
746 /* Very bad, but not sure what to do here. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
747 free(err_pool_data);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
748 free(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
749 return NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
750 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
751
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
752 /* Attach the opaque data to the error pool. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
753 err_pool->opaqueData = err_pool_data;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
754
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
755 /* The OpaqueData will hold the error message list, but it is
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
756 * allowed to be NULL for an empty list so we don't have to allocate
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
757 * it here.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
758 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
759
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
760 return err_pool;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
761 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
762
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
763 /* There better not be any contention when this is called. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
764 void TError_FreeErrorPool(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
765 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
766 if(NULL == err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
767 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
768 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
769 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
770 /* Free all the error messages for each thread.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
771 * This locks and unlocks as it needs.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
772 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
773 Internal_FreeErrorMessageList(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
774
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
775 /* Free opaque data structure. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
776 free(err_pool->opaqueData);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
777
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
778 /* Delete mutex after all locking functions. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
779 Internal_DestroyMutex(err_pool->mutexLock);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
780
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
781 /* Free main data structure. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
782 free(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
783 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
784
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
785 void TError_SetError(TErrorPool* err_pool, int err_num, const char* err_str, ...)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
786 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
787 va_list argp;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
788 va_start(argp, err_str);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
789 TError_SetErrorv(err_pool, err_num, err_str, argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
790 va_end(argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
791 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
792
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
793 void TError_SetErrorv(TErrorPool* err_pool, int err_num, const char* err_str, va_list argp)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
794 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
795 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
796 int ret_num_chars;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
797
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
798 if(NULL == err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
799 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
800 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
801 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
802
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
803 error_message = Internal_GetErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
804 /* If no error message was found, that means we must allocate
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
805 * a new entry for this entry.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
806 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
807 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
808 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
809 error_message = Internal_CreateErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
810 /* If this fails, this is bad...not sure what to do though. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
811 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
812 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
813 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
814 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
815 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
816
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
817 /*
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
818 * I don't think I have to lock here. The [Get|Create]ErrorOnCurrentThread
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
819 * functions lock err_pool as they need access. Here, I don't access
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
820 * err_pool (which is shared) and error_message should be unique for
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
821 * each thread so I don't think there is any contention. (Remember that
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
822 * simultaneous calls to SetError would only happen if they are in
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
823 * different threads.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
824 * There *might* be a problem with library calls (strncpy, calloc).
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
825 * I'm not sure if the various platforms are reentrant.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
826 * I guess for now, I will assume they won't bite me.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
827 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
828
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
829 /* If the err_str is NULL, we need to free our current string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
830 * for consistency. More aggressive optimizations to hold the
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
831 * memory might be considered in the future.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
832 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
833 if(NULL == err_str)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
834 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
835 if(NULL != error_message->errorString)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
836 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
837 free(error_message->errorString);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
838 error_message->errorString = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
839 error_message->errorMaxStringLength = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
840 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
841 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
842 /* Else, copy the string */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
843 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
844 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
845 /* I am using vasprintf which is a GNU extension so it is not
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
846 * portable. However, vasprintf makes certain things possible
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
847 * which would not be otherwise, which is the reason for my
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
848 * use. The main benefit of asprintf/vasprintf is that you can
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
849 * create a string using printf style formatters without
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
850 * worrying about the buffer size. sprintf should never be
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
851 * used because of potential buffer overflows. snprintf
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
852 * is safer, but you are limited to a fixed size string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
853 * which from time-to-time, I have exceeded unless you make
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
854 * the number really big.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
855 * Furthermore, snprintf itself is not currently terribly portable
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
856 * because it is specified only for C99 which some compilers
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
857 * still have not have embraced.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
858 * If you can't use the vasprintf implementation,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
859 * you must add -DDONT_USE_VASPRINTF to your compile flags.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
860 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
861 #ifdef DONT_USE_VASPRINTF
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
862 /* This implementation uses vsnprintf instead of
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
863 * vasprintf. It is strongly recommended you use
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
864 * the vasprintf implmententation instead.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
865 * Never use vsprintf unless you like
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
866 * buffer overflows and security exploits.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
867 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
868
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
869 /* If the string was set to NULL, we must reallocate memory first. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
870 if(NULL == error_message->errorString)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
871 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
872 error_message->errorString = (char*)calloc(TERROR_DEFAULT_STRING_LENGTH, sizeof(char));
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
873 if(NULL == error_message->errorString)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
874 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
875 /* Very bad...what should I do?
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
876 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
877 error_message->errorMaxStringLength = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
878 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
879 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
880 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
881 error_message->errorMaxStringLength = TERROR_DEFAULT_STRING_LENGTH;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
882 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
883 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
884 /* Because of the "Very Bad" situation directly above,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
885 * I need to check again to make sure the string isn't NULL.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
886 * This will let the very bad situation continue on so va_end
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
887 * can be called and the error_number still has a chance to be set.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
888 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
889 if(NULL != error_message->errorString)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
890 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
891 ret_num_chars = vsnprintf(error_message->errorString,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
892 error_message->errorMaxStringLength,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
893 err_str,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
894 argp
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
895 );
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
896 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
897
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
898 #else /* DONT_USE_VASPRINTF */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
899 /* You might be wondering why the #ifdef logic assumes
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
900 * asprintf is available instead of requiring an explicit
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
901 * #define for that. The reason is because asprintf is the
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
902 * better option and I want you to realize that you are not
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
903 * using it. Typically, nobody knows or understands the build
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
904 * system and/or files get copied into new projects with a
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
905 * entirely new build system, so it is easy to forget to
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
906 * add a -D flag. So if you compile without asprintf,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
907 * you are encouraged to explicitly know this.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
908 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
909 /* There may be a slight performance advantage to using snprintf
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
910 * over asprintf depending how asprintf is written. But this
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
911 * implementation will completely destroy and reallocate a
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
912 * string regardless if a string is set to NULL, so there will
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
913 * actually be no performance gains for these cases.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
914 * (This could be optimized but some additional bookkeeping
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
915 * might be needed which might not be worth the effort and
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
916 * code clutter.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
917 * As for memory allocation safety, because new messages for
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
918 * different threads must be allocated dynamically, there is no
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
919 * way for this library to use purely static memory.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
920 * So I don't believe there is anything to be gained using
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
921 * snprintf over asprintf and you lose out on arbitrary lengthed
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
922 * error messages.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
923 * If memory allocation must be minimized, I recommend just
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
924 * using the error number interface by itself which
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
925 * will always keep the strings at NULL, and don't mess
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
926 * with the asprintf/sprintf code.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
927 */
39
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
928 if(NULL != error_message->errorString)
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
929 {
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
930 /* Need to free errorString from previous pass otherwise we leak.
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
931 * Maybe there is a smarter way to avoid the free/malloc,
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
932 * but this would probably require determining the length of the
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
933 * final string beforehand which probably implies two
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
934 * *printf calls which may or may not be better.
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
935 */
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
936 free(error_message->errorString);
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
937 error_message->errorString = NULL;
c07dbd386ded fixed memory leak in tErrorLib
Eric Wing <ewing@anscamobile.com>
parents: 38
diff changeset
938 }
38
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
939
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
940 ret_num_chars = vasprintf(&error_message->errorString, err_str, argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
941 /* vasprintf returns -1 as an error */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
942 if(-1 == ret_num_chars)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
943 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
944 /* Very bad, but not sure what to do here. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
945 if(NULL != error_message->errorString)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
946 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
947 free(error_message->errorString);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
948 error_message->errorString = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
949 error_message->errorMaxStringLength = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
950 /* Don't return here. Still need to va_end, and
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
951 * there is a chance that the err_num might work.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
952 * Plus the availability needs to be set.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
953 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
954 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
955 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
956 /* else vasprint returns the number of characters in the string
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
957 * not including the \0 character.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
958 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
959 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
960 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
961 /* I actually don't know how much memory vasprintf allocated
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
962 * for the string. But it is at least ret_num_chars+1, so
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
963 * I will use that as my max string length (which is
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
964 * mainly used by CopyDynamicString() for efficiency
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
965 * which is becoming less used in this code).
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
966 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
967 error_message->errorMaxStringLength = ret_num_chars+1;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
968 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
969 #endif /* DONT_USE_VASPRINTF */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
970 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
971
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
972 /* I'm allowing for a user to explicitly clear an error message by
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
973 * clearing both attributes.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
974 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
975 if((TERROR_NOERROR_VALUE == err_num) && (NULL == err_str))
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
976 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
977 error_message->errorNumber = TERROR_NOERROR_VALUE;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
978 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
979 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
980 /* This is the normal case, copy the error number
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
981 * and mark the error as unread.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
982 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
983 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
984 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
985 error_message->errorNumber = err_num;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
986 error_message->errorAvailable = 1;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
987 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
988
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
989 /* Now that the data is set, we also want to denote that this
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
990 * thread is the last error message. We need to lock for this
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
991 * since the lastError pointer is shared across threads.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
992 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
993 Internal_SetLastErrorMessagePointerToErrorMessage(err_pool, error_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
994 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
995
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
996 void TError_SetErrorNoFormat(TErrorPool* err_pool, int err_num, const char* err_str)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
997 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
998 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
999 if(NULL == err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1000 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1001 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1002 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1003
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1004 error_message = Internal_GetErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1005 /* If no error message was found, that means we must allocate
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1006 * a new entry for this entry.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1007 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1008 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1009 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1010 error_message = Internal_CreateErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1011 /* If this fails, this is bad...not sure what to do though. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1012 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1013 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1014 return;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1015 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1016 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1017
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1018 /*
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1019 * I don't think I have to lock here. The [Get|Create]ErrorOnCurrentThread
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1020 * functions lock err_pool as they need access. Here, I don't access
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1021 * err_pool (which is shared) and error_message should be unique for
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1022 * each thread so I don't think there is any contention. (Remember that
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1023 * simultaneous calls to SetError would only happen if they are in
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1024 * different threads.)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1025 * There *might* be a problem with library calls (strncpy, calloc).
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1026 * I'm not sure if the various platforms are reentrant.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1027 * I guess for now, I will assume they won't bite me.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1028 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1029 error_message->errorNumber = err_num;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1030 /* This will copy the string and set the new errorMaxStringLength as needed. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1031 error_message->errorString = Internal_CopyDynamicString(error_message->errorString, &error_message->errorMaxStringLength, err_str);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1032 /* I'm allowing for a user to explicitly clear an error message by
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1033 * clearing both attributes.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1034 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1035 if((TERROR_NOERROR_VALUE == err_num) && (NULL == err_str))
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1036 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1037 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1038 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1039 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1040 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1041 error_message->errorAvailable = 1;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1042 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1043
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1044 /* Now that the data is set, we also want to denote that this
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1045 * thread is the last error message. We need to lock for this
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1046 * since the lastError pointer is shared across threads.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1047 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1048 Internal_SetLastErrorMessagePointerToErrorMessage(err_pool, error_message);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1049 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1050
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1051 void TError_SetErrorNum(TErrorPool* err_pool, int err_num)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1052 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1053 TError_SetErrorNoFormat(err_pool, err_num, NULL);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1054 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1055
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1056 void TError_SetErrorStr(TErrorPool* err_pool, const char* err_str, ...)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1057 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1058 va_list argp;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1059 va_start(argp, err_str);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1060 if(NULL == err_str)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1061 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1062 TError_SetErrorv(err_pool, TERROR_NOERROR_VALUE, err_str, argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1063 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1064 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1065 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1066 TError_SetErrorv(err_pool, TERROR_ERROR_VALUE, err_str, argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1067 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1068 va_end(argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1069 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1070
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1071 void TError_SetErrorStrv(TErrorPool* err_pool, const char* err_str, va_list argp)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1072 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1073 if(NULL == err_str)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1074 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1075 TError_SetErrorv(err_pool, TERROR_NOERROR_VALUE, err_str, argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1076 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1077 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1078 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1079 TError_SetErrorv(err_pool, TERROR_ERROR_VALUE, err_str, argp);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1080 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1081 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1082
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1083 /* If a NULL string is set, then it is presumed no error actually occurred
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1084 * and this is a reset. So the err_num will be implicitly set to 0. Otherwise
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1085 * the err_num will be set to 1 (for internal consistency and conventions).
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1086 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1087 void TError_SetErrorStrNoFormat(TErrorPool* err_pool, const char* err_str)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1088 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1089 if(NULL == err_str)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1090 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1091 TError_SetErrorNoFormat(err_pool, TERROR_NOERROR_VALUE, err_str);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1092 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1093 else
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1094 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1095 TError_SetErrorNoFormat(err_pool, TERROR_ERROR_VALUE, err_str);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1096 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1097 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1098
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1099 /* This currently returns 0 as a "no error found" value.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1100 * This could potentially conflict with a user. For now, users
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1101 * shouldn't use 0 to represent an error. If this becomes a
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1102 * problem, we could introduce a magic number like -999999 and
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1103 * define TERROR_NO_ERROR_FOUND.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1104 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1105 int TError_GetErrorNumOnCurrentThread(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1106 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1107 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1108
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1109 error_message = Internal_GetErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1110
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1111 /* If no error message was found for the thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1112 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1113 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1114 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1115 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1116 /* If an error message was found for the thread, but
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1117 * it has already been read/cleared.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1118 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1119 if(0 == error_message->errorAvailable)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1120 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1121 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1122 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1123 /* We found a legitimate error message, clear it and return it. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1124 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1125 return error_message->errorNumber;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1126 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1127
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1128 const char* TError_GetErrorStrOnCurrentThread(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1129 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1130 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1131
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1132 error_message = Internal_GetErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1133
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1134 /* If no error message was found for the thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1135 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1136 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1137 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1138 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1139 /* If an error message was found for the thread, but
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1140 * it has already been read/cleared.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1141 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1142 if(0 == error_message->errorAvailable)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1143 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1144 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1145 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1146 /* We found a legitimate error message, clear it and return it. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1147 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1148 return error_message->errorString;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1149 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1150
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1151 TErrorStatus TError_GetErrorOnCurrentThread(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1152 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1153 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1154 TErrorStatus error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1155 error_container.errorNumber = TERROR_NOERROR_VALUE;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1156 error_container.errorString = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1157
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1158 error_message = Internal_GetErrorOnCurrentThread(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1159
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1160 /* If no error message was found for the thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1161 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1162 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1163 return error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1164 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1165 /* If an error message was found for the thread, but
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1166 * it has already been read/cleared.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1167 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1168 if(0 == error_message->errorAvailable)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1169 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1170 return error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1171 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1172 /* We found a legitimate error message, clear it and return it. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1173 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1174
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1175 error_container.errorNumber = error_message->errorNumber;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1176 error_container.errorString = error_message->errorString;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1177 return error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1178 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1179
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1180 /* This function is for alternative usage where you just want one error
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1181 * for all threads. The backend will still work the same, but when you
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1182 * call this function, it will look up the last set error, copy (with locking)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1183 * the last error to the current thread's memory, and return the object.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1184 * As always, since the returned object is only accessed on this thread, you
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1185 * don't have to worry about locking.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1186 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1187 TErrorStatus TError_GetLastError(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1188 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1189
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1190 // Lock the error pool to get the lastMessage pointer
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1191 // if the lastMessage pointer is pointing to data on the current thread,
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1192 // we can just return it.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1193 // Otherwise, we need to copy the message
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1194
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1195 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1196 TErrorStatus error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1197 error_container.errorNumber = TERROR_NOERROR_VALUE;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1198 error_container.errorString = NULL;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1199
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1200 error_message = Internal_GetLastError(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1201
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1202 /* If no error message was found for the thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1203 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1204 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1205 return error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1206 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1207 /* If an error message was found for the thread, but
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1208 * it has already been read/cleared.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1209 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1210 if(0 == error_message->errorAvailable)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1211 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1212 return error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1213 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1214 /* We found a legitimate error message, clear it and return it. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1215 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1216
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1217 error_container.errorNumber = error_message->errorNumber;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1218 error_container.errorString = error_message->errorString;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1219 return error_container;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1220 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1221
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1222 /* This currently returns 0 as a "no error found" value.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1223 * This could potentially conflict with a user. For now, users
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1224 * shouldn't use 0 to represent an error. If this becomes a
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1225 * problem, we could introduce a magic number like -999999 and
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1226 * define TERROR_NO_ERROR_FOUND.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1227 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1228 int TError_GetLastErrorNum(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1229 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1230 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1231
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1232 error_message = Internal_GetLastError(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1233
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1234 /* If no error message was found for the thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1235 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1236 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1237 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1238 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1239 /* If an error message was found for the thread, but
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1240 * it has already been read/cleared.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1241 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1242 if(0 == error_message->errorAvailable)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1243 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1244 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1245 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1246 /* We found a legitimate error message, clear it and return it. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1247 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1248 return error_message->errorNumber;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1249 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1250
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1251 const char* TError_GetLastErrorStr(TErrorPool* err_pool)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1252 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1253 TErrorMessage* error_message;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1254
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1255 error_message = Internal_GetLastError(err_pool);
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1256
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1257 /* If no error message was found for the thread. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1258 if(NULL == error_message)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1259 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1260 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1261 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1262 /* If an error message was found for the thread, but
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1263 * it has already been read/cleared.
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1264 */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1265 if(0 == error_message->errorAvailable)
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1266 {
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1267 return 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1268 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1269 /* We found a legitimate error message, clear it and return it. */
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1270 error_message->errorAvailable = 0;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1271 return error_message->errorString;
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1272 }
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1273
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1274
71b465ff0622 Added support files.
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1275