Mercurial > repos > blastem
comparison util.c @ 803:236a184bf6f0
Merge
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 26 Jul 2015 16:51:03 -0700 |
parents | 0b692b5d154b |
children | 3eced113081c |
comparison
equal
deleted
inserted
replaced
802:6811f601008f | 803:236a184bf6f0 |
---|---|
1 #include <string.h> | 1 #include <string.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <stdio.h> | 3 #include <stdio.h> |
4 #include <ctype.h> | 4 #include <ctype.h> |
5 #include <stdint.h> | |
5 | 6 |
6 #include <sys/types.h> | 7 #include <sys/types.h> |
7 #include <sys/stat.h> | 8 #include <sys/stat.h> |
8 #include <unistd.h> | 9 #include <unistd.h> |
10 | |
11 #include "blastem.h" //for headless global | |
12 #include "render.h" //for render_errorbox | |
9 | 13 |
10 char * alloc_concat(char * first, char * second) | 14 char * alloc_concat(char * first, char * second) |
11 { | 15 { |
12 int flen = strlen(first); | 16 int flen = strlen(first); |
13 int slen = strlen(second); | 17 int slen = strlen(second); |
66 } | 70 } |
67 *text = 0; | 71 *text = 0; |
68 return text+1; | 72 return text+1; |
69 } | 73 } |
70 | 74 |
75 uint32_t nearest_pow2(uint32_t val) | |
76 { | |
77 uint32_t ret = 1; | |
78 while (ret < val) | |
79 { | |
80 ret = ret << 1; | |
81 } | |
82 return ret; | |
83 } | |
84 | |
71 static char * exe_str; | 85 static char * exe_str; |
72 | 86 |
73 void set_exe_str(char * str) | 87 void set_exe_str(char * str) |
74 { | 88 { |
75 exe_str = str; | 89 exe_str = str; |
76 } | 90 } |
77 | 91 |
92 void fatal_error(char *format, ...) | |
93 { | |
94 va_list args; | |
95 va_start(args, format); | |
96 if (!headless) { | |
97 //take a guess at the final size | |
98 size_t size = strlen(format) * 2; | |
99 char *buf = malloc(size); | |
100 size_t actual = vsnprintf(buf, size, format, args); | |
101 if (actual >= size) { | |
102 actual++; | |
103 free(buf); | |
104 buf = malloc(actual); | |
105 va_end(args); | |
106 va_start(args, format); | |
107 vsnprintf(buf, actual, format, args); | |
108 } | |
109 fputs(buf, stderr); | |
110 render_errorbox("Fatal Error", buf); | |
111 free(buf); | |
112 } else { | |
113 vfprintf(stderr, format, args); | |
114 } | |
115 va_end(args); | |
116 exit(1); | |
117 } | |
118 | |
119 void warning(char *format, ...) | |
120 { | |
121 va_list args; | |
122 va_start(args, format); | |
123 #ifndef _WIN32 | |
124 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) { | |
125 vfprintf(stderr, format, args); | |
126 } else { | |
127 #endif | |
128 size_t size = strlen(format) * 2; | |
129 char *buf = malloc(size); | |
130 size_t actual = vsnprintf(buf, size, format, args); | |
131 if (actual >= size) { | |
132 actual++; | |
133 free(buf); | |
134 buf = malloc(actual); | |
135 va_end(args); | |
136 va_start(args, format); | |
137 vsnprintf(buf, actual, format, args); | |
138 } | |
139 fputs(buf, stderr); | |
140 render_infobox("BlastEm Info", buf); | |
141 free(buf); | |
142 #ifndef _WIN32 | |
143 } | |
144 #endif | |
145 va_end(args); | |
146 } | |
147 | |
148 void info_message(char *format, ...) | |
149 { | |
150 va_list args; | |
151 va_start(args, format); | |
152 #ifndef _WIN32 | |
153 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) { | |
154 vprintf(format, args); | |
155 } else { | |
156 #endif | |
157 size_t size = strlen(format) * 2; | |
158 char *buf = malloc(size); | |
159 size_t actual = vsnprintf(buf, size, format, args); | |
160 if (actual >= size) { | |
161 actual++; | |
162 free(buf); | |
163 buf = malloc(actual); | |
164 va_end(args); | |
165 va_start(args, format); | |
166 vsnprintf(buf, actual, format, args); | |
167 } | |
168 fputs(buf, stdout); | |
169 render_infobox("BlastEm Info", buf); | |
170 free(buf); | |
171 #ifndef _WIN32 | |
172 } | |
173 #endif | |
174 va_end(args); | |
175 } | |
176 | |
78 #ifdef _WIN32 | 177 #ifdef _WIN32 |
79 #include "Shlobj.h" | 178 #include <windows.h> |
80 #include "Windows.h" | 179 #include <shlobj.h> |
81 | 180 |
82 char * get_home_dir() | 181 char * get_home_dir() |
83 { | 182 { |
84 static char path[MAX_PATH]; | 183 static char path[MAX_PATH]; |
85 SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path); | 184 SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path); |
125 linktext = malloc(cursize); | 224 linktext = malloc(cursize); |
126 linksize = readlink(path, linktext, cursize-1); | 225 linksize = readlink(path, linktext, cursize-1); |
127 if (linksize == -1) { | 226 if (linksize == -1) { |
128 perror("readlink"); | 227 perror("readlink"); |
129 free(linktext); | 228 free(linktext); |
130 linktext = NULL; | 229 return NULL; |
131 } | 230 } |
132 } while ((linksize+1) > cursize); | 231 } while ((linksize+1) > cursize); |
133 linktext[linksize] = 0; | 232 linktext[linksize] = 0; |
134 return linktext; | 233 return linktext; |
135 } | 234 } |
136 | 235 |
137 char * get_exe_dir() | 236 char * get_exe_dir() |
138 { | 237 { |
139 static char * exe_dir; | 238 static char * exe_dir; |
140 if (!exe_dir) { | 239 if (!exe_dir) { |
240 char * cur; | |
241 #ifdef HAS_PROC | |
141 char * linktext = readlink_alloc("/proc/self/exe"); | 242 char * linktext = readlink_alloc("/proc/self/exe"); |
142 if (!linktext) { | 243 if (!linktext) { |
143 goto fallback; | 244 goto fallback; |
144 } | 245 } |
145 char * cur; | |
146 int linksize = strlen(linktext); | 246 int linksize = strlen(linktext); |
147 for(cur = linktext + linksize - 1; cur != linktext; cur--) | 247 for(cur = linktext + linksize - 1; cur != linktext; cur--) |
148 { | 248 { |
149 if (*cur == '/') { | 249 if (*cur == '/') { |
150 *cur = 0; | 250 *cur = 0; |
152 } | 252 } |
153 } | 253 } |
154 if (cur == linktext) { | 254 if (cur == linktext) { |
155 free(linktext); | 255 free(linktext); |
156 fallback: | 256 fallback: |
257 #endif | |
157 if (!exe_str) { | 258 if (!exe_str) { |
158 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr); | 259 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr); |
159 } | 260 } |
160 int pathsize = strlen(exe_str); | 261 int pathsize = strlen(exe_str); |
161 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--) | 262 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--) |
165 memcpy(exe_dir, exe_str, cur-exe_str); | 266 memcpy(exe_dir, exe_str, cur-exe_str); |
166 exe_dir[cur-exe_str] = 0; | 267 exe_dir[cur-exe_str] = 0; |
167 break; | 268 break; |
168 } | 269 } |
169 } | 270 } |
271 #ifdef HAS_PROC | |
170 } else { | 272 } else { |
171 exe_dir = linktext; | 273 exe_dir = linktext; |
172 } | 274 } |
275 #endif | |
173 } | 276 } |
174 return exe_dir; | 277 return exe_dir; |
175 } | 278 } |
176 | 279 |
177 #endif | 280 #endif |