lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include "uemf.h" |
| 2 | |
| 3 | |
| 4 | int emfInst; |
| 5 | extern void defaultErrorHandler(int etype, char_t *buf); |
| 6 | extern void defaultTraceHandler(int level, char_t *buf); |
| 7 | |
| 8 | static void (*traceHandler)(int level, char_t *buf) = defaultTraceHandler; |
| 9 | static void (*errorHandler)(int etype, char_t *msg) = defaultErrorHandler; |
| 10 | |
| 11 | void error(E_ARGS_DEC, int etype, char_t *fmt, ...) |
| 12 | { |
| 13 | va_list args = {0}; |
| 14 | char_t *fmtBuf, *buf; |
| 15 | |
| 16 | va_start(args, fmt); |
| 17 | fmtValloc(&fmtBuf, E_MAX_ERROR, fmt, args); |
| 18 | |
| 19 | if (etype == E_LOG) { |
| 20 | fmtAlloc(&buf, E_MAX_ERROR, T("%s\n"), fmtBuf); |
| 21 | /*#ifdef DEV*/ |
| 22 | } else if (etype == E_ASSERT) { |
| 23 | fmtAlloc(&buf, E_MAX_ERROR, |
| 24 | T("Assertion %s, failed at %s %d\n"), fmtBuf, E_ARGS); |
| 25 | /*#endif*/ |
| 26 | } else if (etype == E_USER) { |
| 27 | fmtAlloc(&buf, E_MAX_ERROR, T("%s\n"), fmtBuf); |
| 28 | } |
| 29 | |
| 30 | else { |
| 31 | fmtAlloc(&buf, E_MAX_ERROR, T("Unknown error")); |
| 32 | } |
| 33 | va_end(args); |
| 34 | |
| 35 | bfree(B_L, fmtBuf); |
| 36 | |
| 37 | printf("%s",buf); |
| 38 | |
| 39 | bfreeSafe(B_L, buf); |
| 40 | } |
| 41 | |
| 42 | void traceRaw(char_t *buf) |
| 43 | { |
| 44 | if(buf) |
| 45 | printf("%s",buf); |
| 46 | } |
| 47 | |
| 48 | void trace(int level, char_t *fmt, ...) |
| 49 | { |
| 50 | va_list args = {0}; |
| 51 | char_t *buf; |
| 52 | |
| 53 | va_start(args, fmt); |
| 54 | fmtValloc(&buf, VALUE_MAX_STRING, fmt, args); |
| 55 | printf("%s",buf); |
| 56 | bfreeSafe(B_L, buf); |
| 57 | va_end(args); |
| 58 | } |
| 59 | |
| 60 | char_t *strlower(char_t *string) |
| 61 | { |
| 62 | char_t *s; |
| 63 | |
| 64 | a_assert(string); |
| 65 | |
| 66 | if (string == NULL) { |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | s = string; |
| 71 | while (*s) { |
| 72 | if (gisupper(*s)) { |
| 73 | *s = (char_t) gtolower(*s); |
| 74 | } |
| 75 | s++; |
| 76 | } |
| 77 | *s = '\0'; |
| 78 | return string; |
| 79 | } |
| 80 | |
| 81 | char_t *strupper(char_t *string) |
| 82 | { |
| 83 | char_t *s; |
| 84 | |
| 85 | a_assert(string); |
| 86 | if (string == NULL) { |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | s = string; |
| 91 | while (*s) { |
| 92 | if (gislower(*s)) { |
| 93 | *s = (char_t) gtoupper(*s); |
| 94 | } |
| 95 | s++; |
| 96 | } |
| 97 | *s = '\0'; |
| 98 | return string; |
| 99 | } |
| 100 | |
| 101 | char_t *basicGetProduct() |
| 102 | { |
| 103 | return T("uemf"); |
| 104 | } |
| 105 | |
| 106 | char_t *basicGetAddress() |
| 107 | { |
| 108 | return T("localhost"); |
| 109 | } |
| 110 | |
| 111 | char_t *stritoa(int n, char_t *string, int width) |
| 112 | { |
| 113 | char_t *cp, *lim, *s; |
| 114 | int next, minus; |
| 115 | char_t tmp_buf[16]; |
| 116 | |
| 117 | a_assert(string && width > 0); |
| 118 | |
| 119 | if (string == NULL) { |
| 120 | if (width == 0) { |
| 121 | width = 10; |
| 122 | } |
| 123 | if ((string = balloc(B_L, width + 1)) == NULL) { |
| 124 | return NULL; |
| 125 | } |
| 126 | } |
| 127 | if (n < 0) { |
| 128 | minus = 1; |
| 129 | n = -n; |
| 130 | width--; |
| 131 | } else { |
| 132 | minus = 0; |
| 133 | } |
| 134 | |
| 135 | cp = tmp_buf; |
| 136 | lim = &tmp_buf[width - 1]; |
| 137 | while (n > 9 && cp < lim) { |
| 138 | next = n; |
| 139 | n /= 10; |
| 140 | *cp++ = (char_t) (next - n * 10 + '0'); |
| 141 | } |
| 142 | if (cp < lim) { |
| 143 | *cp++ = (char_t) (n + '0'); |
| 144 | } |
| 145 | |
| 146 | s = string; |
| 147 | if (minus) { |
| 148 | *s++ = '-'; |
| 149 | } |
| 150 | |
| 151 | while (cp > tmp_buf) { |
| 152 | *s++ = *--cp; |
| 153 | } |
| 154 | |
| 155 | *s++ = '\0'; |
| 156 | return string; |
| 157 | } |
| 158 | |
| 159 | |