src/osd/sdl/sdlclip.c
| r7340 | r7341 | |
| 1 | | #include <stdlib.h> |
| 2 | | #include <fcntl.h> |
| 3 | | #include <unistd.h> |
| 4 | | #include <ctype.h> |
| 5 | | #include <sys/stat.h> |
| 6 | | #include <sys/types.h> |
| 7 | | #include <ctype.h> |
| 8 | | #include <errno.h> |
| 9 | | |
| 10 | | #ifdef SDLMAME_WIN32 |
| 11 | | #define WIN32_LEAN_AND_MEAN |
| 12 | | #include <windows.h> |
| 13 | | #include <tchar.h> |
| 14 | | #include "strconv.h" |
| 15 | | #endif |
| 16 | | |
| 17 | | #ifdef SDLMAME_MACOSX |
| 18 | | #include <Carbon/Carbon.h> |
| 19 | | #endif |
| 20 | | |
| 21 | | |
| 22 | | #ifdef SDLMAME_MACOSX |
| 23 | | //============================================================ |
| 24 | | // osd_get_clipboard_text |
| 25 | | //============================================================ |
| 26 | | |
| 27 | | char *osd_get_clipboard_text(void) |
| 28 | | { |
| 29 | | char *result = NULL; /* core expects a malloced C string of uft8 data */ |
| 30 | | |
| 31 | | PasteboardRef pasteboard_ref; |
| 32 | | OSStatus err; |
| 33 | | PasteboardSyncFlags sync_flags; |
| 34 | | PasteboardItemID item_id; |
| 35 | | CFIndex flavor_count; |
| 36 | | CFArrayRef flavor_type_array; |
| 37 | | CFIndex flavor_index; |
| 38 | | ItemCount item_count; |
| 39 | | UInt32 item_index; |
| 40 | | Boolean success = false; |
| 41 | | |
| 42 | | err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref); |
| 43 | | |
| 44 | | if (!err) |
| 45 | | { |
| 46 | | sync_flags = PasteboardSynchronize( pasteboard_ref ); |
| 47 | | |
| 48 | | err = PasteboardGetItemCount(pasteboard_ref, &item_count ); |
| 49 | | |
| 50 | | for (item_index=1; item_index<=item_count; item_index++) |
| 51 | | { |
| 52 | | err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id); |
| 53 | | |
| 54 | | if (!err) |
| 55 | | { |
| 56 | | err = PasteboardCopyItemFlavors(pasteboard_ref, item_id, &flavor_type_array); |
| 57 | | |
| 58 | | if (!err) |
| 59 | | { |
| 60 | | flavor_count = CFArrayGetCount(flavor_type_array); |
| 61 | | |
| 62 | | for (flavor_index = 0; flavor_index < flavor_count; flavor_index++) |
| 63 | | { |
| 64 | | CFStringRef flavor_type; |
| 65 | | CFDataRef flavor_data; |
| 66 | | CFStringEncoding encoding; |
| 67 | | CFStringRef string_ref; |
| 68 | | CFDataRef data_ref; |
| 69 | | CFIndex length; |
| 70 | | CFRange range; |
| 71 | | |
| 72 | | flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index); |
| 73 | | |
| 74 | | if (UTTypeConformsTo (flavor_type, kUTTypeUTF16PlainText)) |
| 75 | | encoding = kCFStringEncodingUTF16; |
| 76 | | else if (UTTypeConformsTo (flavor_type, kUTTypeUTF8PlainText)) |
| 77 | | encoding = kCFStringEncodingUTF8; |
| 78 | | else if (UTTypeConformsTo (flavor_type, kUTTypePlainText)) |
| 79 | | encoding = kCFStringEncodingMacRoman; |
| 80 | | else |
| 81 | | continue; |
| 82 | | |
| 83 | | err = PasteboardCopyItemFlavorData(pasteboard_ref, item_id, flavor_type, &flavor_data); |
| 84 | | |
| 85 | | if( !err ) |
| 86 | | { |
| 87 | | string_ref = CFStringCreateFromExternalRepresentation (kCFAllocatorDefault, flavor_data, encoding); |
| 88 | | data_ref = CFStringCreateExternalRepresentation (kCFAllocatorDefault, string_ref, kCFStringEncodingUTF8, '?'); |
| 89 | | |
| 90 | | length = CFDataGetLength (data_ref); |
| 91 | | range = CFRangeMake (0,length); |
| 92 | | |
| 93 | | result = (char *)malloc (length+1); |
| 94 | | if (result != NULL) |
| 95 | | { |
| 96 | | CFDataGetBytes (data_ref, range, (unsigned char *)result); |
| 97 | | result[length] = 0; |
| 98 | | success = true; |
| 99 | | break; |
| 100 | | } |
| 101 | | |
| 102 | | CFRelease(data_ref); |
| 103 | | CFRelease(string_ref); |
| 104 | | CFRelease(flavor_data); |
| 105 | | } |
| 106 | | } |
| 107 | | |
| 108 | | CFRelease(flavor_type_array); |
| 109 | | } |
| 110 | | } |
| 111 | | |
| 112 | | if (success) |
| 113 | | break; |
| 114 | | } |
| 115 | | |
| 116 | | CFRelease(pasteboard_ref); |
| 117 | | } |
| 118 | | |
| 119 | | return result; |
| 120 | | } |
| 121 | | #endif |
| 122 | | |
| 123 | | #ifdef SDLMAME_WIN32 |
| 124 | | //============================================================ |
| 125 | | // get_clipboard_text_by_format |
| 126 | | //============================================================ |
| 127 | | |
| 128 | | static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID data)) |
| 129 | | { |
| 130 | | char *result = NULL; |
| 131 | | HANDLE data_handle; |
| 132 | | LPVOID data; |
| 133 | | |
| 134 | | // check to see if this format is available |
| 135 | | if (IsClipboardFormatAvailable(format)) |
| 136 | | { |
| 137 | | // open the clipboard |
| 138 | | if (OpenClipboard(NULL)) |
| 139 | | { |
| 140 | | // try to access clipboard data |
| 141 | | data_handle = GetClipboardData(format); |
| 142 | | if (data_handle != NULL) |
| 143 | | { |
| 144 | | // lock the data |
| 145 | | data = GlobalLock(data_handle); |
| 146 | | if (data != NULL) |
| 147 | | { |
| 148 | | // invoke the convert |
| 149 | | result = (*convert)(data); |
| 150 | | |
| 151 | | // unlock the data |
| 152 | | GlobalUnlock(data_handle); |
| 153 | | } |
| 154 | | } |
| 155 | | |
| 156 | | // close out the clipboard |
| 157 | | CloseClipboard(); |
| 158 | | } |
| 159 | | } |
| 160 | | return result; |
| 161 | | } |
| 162 | | |
| 163 | | |
| 164 | | |
| 165 | | //============================================================ |
| 166 | | // convert_wide |
| 167 | | //============================================================ |
| 168 | | |
| 169 | | static char *convert_wide(LPCVOID data) |
| 170 | | { |
| 171 | | return utf8_from_wstring((LPCWSTR) data); |
| 172 | | } |
| 173 | | |
| 174 | | |
| 175 | | |
| 176 | | //============================================================ |
| 177 | | // convert_ansi |
| 178 | | //============================================================ |
| 179 | | |
| 180 | | static char *convert_ansi(LPCVOID data) |
| 181 | | { |
| 182 | | return utf8_from_astring((LPCSTR) data); |
| 183 | | } |
| 184 | | |
| 185 | | |
| 186 | | |
| 187 | | //============================================================ |
| 188 | | // osd_get_clipboard_text |
| 189 | | //============================================================ |
| 190 | | |
| 191 | | char *osd_get_clipboard_text(void) |
| 192 | | { |
| 193 | | char *result = NULL; |
| 194 | | |
| 195 | | // try to access unicode text |
| 196 | | if (result == NULL) |
| 197 | | result = get_clipboard_text_by_format(CF_UNICODETEXT, convert_wide); |
| 198 | | |
| 199 | | // try to access ANSI text |
| 200 | | if (result == NULL) |
| 201 | | result = get_clipboard_text_by_format(CF_TEXT, convert_ansi); |
| 202 | | |
| 203 | | return result; |
| 204 | | } |
| 205 | | #endif |
| 206 | | |
| 207 | | #if defined(SDL_VIDEO_DRIVER_X11) |
| 208 | | //============================================================ |
| 209 | | // osd_get_clipboard_text |
| 210 | | //============================================================ |
| 211 | | |
| 212 | | char *osd_get_clipboard_text(void) |
| 213 | | { |
| 214 | | SDL_SysWMinfo info; |
| 215 | | Display* display; |
| 216 | | Window our_win; |
| 217 | | Window selection_win; |
| 218 | | Atom data_type; |
| 219 | | int data_format; |
| 220 | | unsigned long nitems; |
| 221 | | unsigned long bytes_remaining; |
| 222 | | unsigned char* prop; |
| 223 | | char* result; |
| 224 | | XEvent event; |
| 225 | | Uint32 t0, t1; |
| 226 | | Atom types[2]; |
| 227 | | int i; |
| 228 | | |
| 229 | | /* get & validate SDL sys-wm info */ |
| 230 | | SDL_VERSION(&info.version); |
| 231 | | if ( ! SDL_GetWMInfo( &info ) ) |
| 232 | | return NULL; |
| 233 | | if ( info.subsystem != SDL_SYSWM_X11 ) |
| 234 | | return NULL; |
| 235 | | if ( (display = info.info.x11.display) == NULL ) |
| 236 | | return NULL; |
| 237 | | if ( (our_win = info.info.x11.window) == None ) |
| 238 | | return NULL; |
| 239 | | |
| 240 | | /* request data to owner */ |
| 241 | | selection_win = XGetSelectionOwner( display, XA_PRIMARY ); |
| 242 | | if ( selection_win == None ) |
| 243 | | return NULL; |
| 244 | | |
| 245 | | /* first, try UTF-8, then latin-1 */ |
| 246 | | types[0] = XInternAtom( display, "UTF8_STRING", False ); |
| 247 | | types[1] = XA_STRING; /* latin-1 */ |
| 248 | | |
| 249 | | for ( i = 0; i < ARRAY_LENGTH(types); i++ ) |
| 250 | | { |
| 251 | | |
| 252 | | XConvertSelection( display, XA_PRIMARY, types[i], types[i], our_win, CurrentTime ); |
| 253 | | |
| 254 | | /* wait for SelectionNotify, but no more than 100 ms */ |
| 255 | | t0 = t1 = SDL_GetTicks(); |
| 256 | | while ( 1 ) |
| 257 | | { |
| 258 | | if ( XCheckTypedWindowEvent( display, our_win, SelectionNotify, &event ) ) break; |
| 259 | | SDL_Delay( 1 ); |
| 260 | | t1 = SDL_GetTicks(); |
| 261 | | if ( t1 - t0 > 100 ) |
| 262 | | return NULL; |
| 263 | | } |
| 264 | | if ( event.xselection.property == None ) |
| 265 | | continue; |
| 266 | | |
| 267 | | /* get property & check its type */ |
| 268 | | if ( XGetWindowProperty( display, our_win, types[i], 0, 65536, False, types[i], |
| 269 | | &data_type, &data_format, &nitems, &bytes_remaining, &prop ) |
| 270 | | != Success ) |
| 271 | | continue; |
| 272 | | if ( ! prop ) |
| 273 | | continue; |
| 274 | | if ( (data_format != 8) || (data_type != types[i]) ) |
| 275 | | { |
| 276 | | XFree( prop ); |
| 277 | | continue; |
| 278 | | } |
| 279 | | |
| 280 | | /* return a copy & free original */ |
| 281 | | result = core_strdup( (char*) prop ); |
| 282 | | XFree( prop ); |
| 283 | | return result; |
| 284 | | } |
| 285 | | |
| 286 | | return NULL; |
| 287 | | } |
| 288 | | |
| 289 | | #endif |
| 290 | | |
| 291 | | #if defined(SDLMAME_UNIX) && !defined(SDLMAME_MACOSX) && !defined(SDL_VIDEO_DRIVER_X11) |
| 292 | | //============================================================ |
| 293 | | // osd_get_clipboard_text |
| 294 | | //============================================================ |
| 295 | | |
| 296 | | char *osd_get_clipboard_text(void) |
| 297 | | { |
| 298 | | char *result = NULL; |
| 299 | | |
| 300 | | return result; |
| 301 | | } |
| 302 | | #endif |
src/osd/sdl/sdlos_macosx.c
| r7340 | r7341 | |
| 16 | 16 | |
| 17 | 17 | #include <mach/mach.h> |
| 18 | 18 | #include <mach/mach_time.h> |
| 19 | #include <Carbon/Carbon.h> |
| 19 | 20 | |
| 20 | 21 | // MAME headers |
| 21 | 22 | #include "osdcore.h" |
| r7340 | r7341 | |
| 204 | 205 | { |
| 205 | 206 | return setenv(name, value, overwrite); |
| 206 | 207 | } |
| 208 | |
| 209 | |
| 210 | //============================================================ |
| 211 | // osd_get_clipboard_text |
| 212 | //============================================================ |
| 213 | |
| 214 | char *osd_get_clipboard_text(void) |
| 215 | { |
| 216 | char *result = NULL; /* core expects a malloced C string of uft8 data */ |
| 217 | |
| 218 | PasteboardRef pasteboard_ref; |
| 219 | OSStatus err; |
| 220 | PasteboardSyncFlags sync_flags; |
| 221 | PasteboardItemID item_id; |
| 222 | CFIndex flavor_count; |
| 223 | CFArrayRef flavor_type_array; |
| 224 | CFIndex flavor_index; |
| 225 | ItemCount item_count; |
| 226 | UInt32 item_index; |
| 227 | Boolean success = false; |
| 228 | |
| 229 | err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref); |
| 230 | |
| 231 | if (!err) |
| 232 | { |
| 233 | sync_flags = PasteboardSynchronize( pasteboard_ref ); |
| 234 | |
| 235 | err = PasteboardGetItemCount(pasteboard_ref, &item_count ); |
| 236 | |
| 237 | for (item_index=1; item_index<=item_count; item_index++) |
| 238 | { |
| 239 | err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id); |
| 240 | |
| 241 | if (!err) |
| 242 | { |
| 243 | err = PasteboardCopyItemFlavors(pasteboard_ref, item_id, &flavor_type_array); |
| 244 | |
| 245 | if (!err) |
| 246 | { |
| 247 | flavor_count = CFArrayGetCount(flavor_type_array); |
| 248 | |
| 249 | for (flavor_index = 0; flavor_index < flavor_count; flavor_index++) |
| 250 | { |
| 251 | CFStringRef flavor_type; |
| 252 | CFDataRef flavor_data; |
| 253 | CFStringEncoding encoding; |
| 254 | CFStringRef string_ref; |
| 255 | CFDataRef data_ref; |
| 256 | CFIndex length; |
| 257 | CFRange range; |
| 258 | |
| 259 | flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index); |
| 260 | |
| 261 | if (UTTypeConformsTo (flavor_type, kUTTypeUTF16PlainText)) |
| 262 | encoding = kCFStringEncodingUTF16; |
| 263 | else if (UTTypeConformsTo (flavor_type, kUTTypeUTF8PlainText)) |
| 264 | encoding = kCFStringEncodingUTF8; |
| 265 | else if (UTTypeConformsTo (flavor_type, kUTTypePlainText)) |
| 266 | encoding = kCFStringEncodingMacRoman; |
| 267 | else |
| 268 | continue; |
| 269 | |
| 270 | err = PasteboardCopyItemFlavorData(pasteboard_ref, item_id, flavor_type, &flavor_data); |
| 271 | |
| 272 | if( !err ) |
| 273 | { |
| 274 | string_ref = CFStringCreateFromExternalRepresentation (kCFAllocatorDefault, flavor_data, encoding); |
| 275 | data_ref = CFStringCreateExternalRepresentation (kCFAllocatorDefault, string_ref, kCFStringEncodingUTF8, '?'); |
| 276 | |
| 277 | length = CFDataGetLength (data_ref); |
| 278 | range = CFRangeMake (0,length); |
| 279 | |
| 280 | result = (char *)malloc (length+1); |
| 281 | if (result != NULL) |
| 282 | { |
| 283 | CFDataGetBytes (data_ref, range, (unsigned char *)result); |
| 284 | result[length] = 0; |
| 285 | success = true; |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | CFRelease(data_ref); |
| 290 | CFRelease(string_ref); |
| 291 | CFRelease(flavor_data); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | CFRelease(flavor_type_array); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (success) |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | CFRelease(pasteboard_ref); |
| 304 | } |
| 305 | |
| 306 | return result; |
| 307 | } |
| 308 | |
| 309 | //============================================================ |
| 310 | // osd_stat |
| 311 | //============================================================ |
| 312 | |
| 313 | osd_directory_entry *osd_stat(const char *path) |
| 314 | { |
| 315 | int err; |
| 316 | osd_directory_entry *result = NULL; |
| 317 | #if defined(SDLMAME_DARWIN) || defined(SDLMAME_NO64BITIO) |
| 318 | struct stat st; |
| 319 | #else |
| 320 | struct stat64 st; |
| 321 | #endif |
| 322 | |
| 323 | #if defined(SDLMAME_DARWIN) || defined(SDLMAME_NO64BITIO) |
| 324 | err = stat(path, &st); |
| 325 | #else |
| 326 | err = stat64(path, &st); |
| 327 | #endif |
| 328 | |
| 329 | if( err == -1) return NULL; |
| 330 | |
| 331 | // create an osd_directory_entry; be sure to make sure that the caller can |
| 332 | // free all resources by just freeing the resulting osd_directory_entry |
| 333 | result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1); |
| 334 | strcpy(((char *) result) + sizeof(*result), path); |
| 335 | result->name = ((char *) result) + sizeof(*result); |
| 336 | result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE; |
| 337 | result->size = (UINT64)st.st_size; |
| 338 | |
| 339 | return result; |
| 340 | } |
| 341 | |
| 342 | //============================================================ |
| 343 | // osd_get_volume_name |
| 344 | //============================================================ |
| 345 | |
| 346 | const char *osd_get_volume_name(int idx) |
| 347 | { |
| 348 | if (idx!=0) return NULL; |
| 349 | return "/"; |
| 350 | } |
| 351 | |
| 352 | //============================================================ |
| 353 | // osd_get_full_path |
| 354 | //============================================================ |
| 355 | |
| 356 | file_error osd_get_full_path(char **dst, const char *path) |
| 357 | { |
| 358 | file_error err; |
| 359 | char path_buffer[512]; |
| 360 | |
| 361 | err = FILERR_NONE; |
| 362 | |
| 363 | if (getcwd(path_buffer, 511) == NULL) |
| 364 | { |
| 365 | printf("osd_get_full_path: failed!\n"); |
| 366 | err = FILERR_FAILURE; |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3); |
| 371 | |
| 372 | // if it's already a full path, just pass it through |
| 373 | if (path[0] == '/') |
| 374 | { |
| 375 | strcpy(*dst, path); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return err; |
| 384 | } |
| 385 | |
| 386 | //============================================================ |
| 387 | // osd_get_emulator_directory |
| 388 | //============================================================ |
| 389 | |
| 390 | void osd_get_emulator_directory(char *dir, size_t dir_size) |
| 391 | { |
| 392 | strncpy(dir, sdl_cwd, dir_size); |
| 393 | } |
src/osd/sdl/sdlos_win32.c
| r7340 | r7341 | |
| 18 | 18 | |
| 19 | 19 | // MAME headers |
| 20 | 20 | #include "osdcore.h" |
| 21 | #include "strconv.h" |
| 21 | 22 | |
| 22 | 23 | //============================================================ |
| 23 | 24 | // PROTOTYPES |
| r7340 | r7341 | |
| 271 | 272 | |
| 272 | 273 | return result; |
| 273 | 274 | } |
| 275 | |
| 276 | |
| 277 | //============================================================ |
| 278 | // get_clipboard_text_by_format |
| 279 | //============================================================ |
| 280 | |
| 281 | static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID data)) |
| 282 | { |
| 283 | char *result = NULL; |
| 284 | HANDLE data_handle; |
| 285 | LPVOID data; |
| 286 | |
| 287 | // check to see if this format is available |
| 288 | if (IsClipboardFormatAvailable(format)) |
| 289 | { |
| 290 | // open the clipboard |
| 291 | if (OpenClipboard(NULL)) |
| 292 | { |
| 293 | // try to access clipboard data |
| 294 | data_handle = GetClipboardData(format); |
| 295 | if (data_handle != NULL) |
| 296 | { |
| 297 | // lock the data |
| 298 | data = GlobalLock(data_handle); |
| 299 | if (data != NULL) |
| 300 | { |
| 301 | // invoke the convert |
| 302 | result = (*convert)(data); |
| 303 | |
| 304 | // unlock the data |
| 305 | GlobalUnlock(data_handle); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // close out the clipboard |
| 310 | CloseClipboard(); |
| 311 | } |
| 312 | } |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | //============================================================ |
| 319 | // convert_wide |
| 320 | //============================================================ |
| 321 | |
| 322 | static char *convert_wide(LPCVOID data) |
| 323 | { |
| 324 | return utf8_from_wstring((LPCWSTR) data); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | //============================================================ |
| 330 | // convert_ansi |
| 331 | //============================================================ |
| 332 | |
| 333 | static char *convert_ansi(LPCVOID data) |
| 334 | { |
| 335 | return utf8_from_astring((LPCSTR) data); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | //============================================================ |
| 341 | // osd_get_clipboard_text |
| 342 | //============================================================ |
| 343 | |
| 344 | char *osd_get_clipboard_text(void) |
| 345 | { |
| 346 | char *result = NULL; |
| 347 | |
| 348 | // try to access unicode text |
| 349 | if (result == NULL) |
| 350 | result = get_clipboard_text_by_format(CF_UNICODETEXT, convert_wide); |
| 351 | |
| 352 | // try to access ANSI text |
| 353 | if (result == NULL) |
| 354 | result = get_clipboard_text_by_format(CF_TEXT, convert_ansi); |
| 355 | |
| 356 | return result; |
| 357 | } |
| 358 | |
| 359 | //============================================================ |
| 360 | // astring_from_utf8 |
| 361 | //============================================================ |
| 362 | |
| 363 | CHAR *astring_from_utf8(const char *utf8string) |
| 364 | { |
| 365 | WCHAR *wstring; |
| 366 | int char_count; |
| 367 | CHAR *result; |
| 368 | |
| 369 | // convert MAME string (UTF-8) to UTF-16 |
| 370 | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 371 | wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); |
| 372 | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count); |
| 373 | |
| 374 | // convert UTF-16 to "ANSI code page" string |
| 375 | char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL); |
| 376 | result = (CHAR *)malloc(char_count * sizeof(*result)); |
| 377 | if (result != NULL) |
| 378 | WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL); |
| 379 | |
| 380 | return result; |
| 381 | } |
| 382 | |
| 383 | //============================================================ |
| 384 | // wstring_from_utf8 |
| 385 | //============================================================ |
| 386 | |
| 387 | WCHAR *wstring_from_utf8(const char *utf8string) |
| 388 | { |
| 389 | int char_count; |
| 390 | WCHAR *result; |
| 391 | |
| 392 | // convert MAME string (UTF-8) to UTF-16 |
| 393 | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 394 | result = (WCHAR *)malloc(char_count * sizeof(*result)); |
| 395 | if (result != NULL) |
| 396 | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count); |
| 397 | |
| 398 | return result; |
| 399 | } |
| 400 | |
| 401 | //============================================================ |
| 402 | // win_attributes_to_entry_type |
| 403 | //============================================================ |
| 404 | |
| 405 | static osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes) |
| 406 | { |
| 407 | if (attributes == 0xFFFFFFFF) |
| 408 | return ENTTYPE_NONE; |
| 409 | else if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 410 | return ENTTYPE_DIR; |
| 411 | else |
| 412 | return ENTTYPE_FILE; |
| 413 | } |
| 414 | |
| 415 | //============================================================ |
| 416 | // osd_stat |
| 417 | //============================================================ |
| 418 | |
| 419 | osd_directory_entry *osd_stat(const char *path) |
| 420 | { |
| 421 | osd_directory_entry *result = NULL; |
| 422 | TCHAR *t_path; |
| 423 | HANDLE find = INVALID_HANDLE_VALUE; |
| 424 | WIN32_FIND_DATA find_data; |
| 425 | |
| 426 | // convert the path to TCHARs |
| 427 | t_path = tstring_from_utf8(path); |
| 428 | if (t_path == NULL) |
| 429 | goto done; |
| 430 | |
| 431 | // attempt to find the first file |
| 432 | find = FindFirstFile(t_path, &find_data); |
| 433 | if (find == INVALID_HANDLE_VALUE) |
| 434 | goto done; |
| 435 | |
| 436 | // create an osd_directory_entry; be sure to make sure that the caller can |
| 437 | // free all resources by just freeing the resulting osd_directory_entry |
| 438 | result = (osd_directory_entry *) malloc(sizeof(*result) + strlen(path) + 1); |
| 439 | if (!result) |
| 440 | goto done; |
| 441 | strcpy(((char *) result) + sizeof(*result), path); |
| 442 | result->name = ((char *) result) + sizeof(*result); |
| 443 | result->type = win_attributes_to_entry_type(find_data.dwFileAttributes); |
| 444 | result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32); |
| 445 | |
| 446 | done: |
| 447 | if (t_path) |
| 448 | free(t_path); |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | //============================================================ |
| 453 | // osd_get_volume_name |
| 454 | //============================================================ |
| 455 | |
| 456 | const char *osd_get_volume_name(int idx) |
| 457 | { |
| 458 | static char szBuffer[128]; |
| 459 | const char *p; |
| 460 | |
| 461 | GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer); |
| 462 | |
| 463 | p = szBuffer; |
| 464 | while(idx--) { |
| 465 | p += strlen(p) + 1; |
| 466 | if (!*p) return NULL; |
| 467 | } |
| 468 | |
| 469 | return p; |
| 470 | } |
| 471 | |
| 472 | //============================================================ |
| 473 | // win_error_to_mame_file_error |
| 474 | //============================================================ |
| 475 | |
| 476 | static file_error win_error_to_mame_file_error(DWORD error) |
| 477 | { |
| 478 | file_error filerr; |
| 479 | |
| 480 | // convert a Windows error to a file_error |
| 481 | switch (error) |
| 482 | { |
| 483 | case ERROR_SUCCESS: |
| 484 | filerr = FILERR_NONE; |
| 485 | break; |
| 486 | |
| 487 | case ERROR_OUTOFMEMORY: |
| 488 | filerr = FILERR_OUT_OF_MEMORY; |
| 489 | break; |
| 490 | |
| 491 | case ERROR_FILE_NOT_FOUND: |
| 492 | case ERROR_PATH_NOT_FOUND: |
| 493 | filerr = FILERR_NOT_FOUND; |
| 494 | break; |
| 495 | |
| 496 | case ERROR_ACCESS_DENIED: |
| 497 | filerr = FILERR_ACCESS_DENIED; |
| 498 | break; |
| 499 | |
| 500 | case ERROR_SHARING_VIOLATION: |
| 501 | filerr = FILERR_ALREADY_OPEN; |
| 502 | break; |
| 503 | |
| 504 | default: |
| 505 | filerr = FILERR_FAILURE; |
| 506 | break; |
| 507 | } |
| 508 | return filerr; |
| 509 | } |
| 510 | |
| 511 | //============================================================ |
| 512 | // osd_get_full_path |
| 513 | //============================================================ |
| 514 | |
| 515 | file_error osd_get_full_path(char **dst, const char *path) |
| 516 | { |
| 517 | file_error err; |
| 518 | TCHAR *t_path; |
| 519 | TCHAR buffer[MAX_PATH]; |
| 520 | |
| 521 | // convert the path to TCHARs |
| 522 | t_path = tstring_from_utf8(path); |
| 523 | if (t_path == NULL) |
| 524 | { |
| 525 | err = FILERR_OUT_OF_MEMORY; |
| 526 | goto done; |
| 527 | } |
| 528 | |
| 529 | // cannonicalize the path |
| 530 | if (!GetFullPathName(t_path, ARRAY_LENGTH(buffer), buffer, NULL)) |
| 531 | { |
| 532 | err = win_error_to_mame_file_error(GetLastError()); |
| 533 | goto done; |
| 534 | } |
| 535 | |
| 536 | // convert the result back to UTF-8 |
| 537 | *dst = utf8_from_tstring(buffer); |
| 538 | if (!*dst) |
| 539 | { |
| 540 | err = FILERR_OUT_OF_MEMORY; |
| 541 | goto done; |
| 542 | } |
| 543 | |
| 544 | err = FILERR_NONE; |
| 545 | |
| 546 | done: |
| 547 | if (t_path != NULL) |
| 548 | free(t_path); |
| 549 | return err; |
| 550 | } |
| 551 | |
| 552 | //============================================================ |
| 553 | // win_get_module_file_name_utf8 |
| 554 | //============================================================ |
| 555 | |
| 556 | static DWORD win_get_module_file_name_utf8(HMODULE module, char *filename, DWORD size) |
| 557 | { |
| 558 | TCHAR t_filename[MAX_PATH]; |
| 559 | char *utf8_filename; |
| 560 | |
| 561 | if (GetModuleFileName(module, t_filename, ARRAY_LENGTH(t_filename)) == 0) |
| 562 | return 0; |
| 563 | |
| 564 | utf8_filename = utf8_from_tstring(t_filename); |
| 565 | if (!utf8_filename) |
| 566 | return 0; |
| 567 | |
| 568 | size = (DWORD) snprintf(filename, size, "%s", utf8_filename); |
| 569 | free(utf8_filename); |
| 570 | return size; |
| 571 | } |
| 572 | |
| 573 | //============================================================ |
| 574 | // osd_get_emulator_directory |
| 575 | //============================================================ |
| 576 | |
| 577 | void osd_get_emulator_directory(char *dir, size_t dir_size) |
| 578 | { |
| 579 | char *s; |
| 580 | win_get_module_file_name_utf8(NULL, dir, dir_size); |
| 581 | s = strrchr(dir, '\\'); |
| 582 | if (s) |
| 583 | s[1] = '\0'; |
| 584 | } |
| | No newline at end of file |
src/osd/sdl/sdlos_unix.c
| r7340 | r7341 | |
| 15 | 15 | #include <time.h> |
| 16 | 16 | #include <sys/time.h> |
| 17 | 17 | |
| 18 | #include <SDL/SDL.h> |
| 19 | #include <SDL/SDL_syswm.h> |
| 20 | |
| 18 | 21 | // MAME headers |
| 19 | 22 | #include "osdcore.h" |
| 20 | 23 | |
| r7340 | r7341 | |
| 119 | 122 | { |
| 120 | 123 | return setenv(name, value, overwrite); |
| 121 | 124 | } |
| 125 | |
| 126 | |
| 127 | #if defined(SDL_VIDEO_DRIVER_X11) && defined(SDLMAME_X11) |
| 128 | |
| 129 | //============================================================ |
| 130 | // osd_get_clipboard_text |
| 131 | //============================================================ |
| 132 | |
| 133 | char *osd_get_clipboard_text(void) |
| 134 | { |
| 135 | SDL_SysWMinfo info; |
| 136 | Display* display; |
| 137 | Window our_win; |
| 138 | Window selection_win; |
| 139 | Atom data_type; |
| 140 | int data_format; |
| 141 | unsigned long nitems; |
| 142 | unsigned long bytes_remaining; |
| 143 | unsigned char* prop; |
| 144 | char* result; |
| 145 | XEvent event; |
| 146 | Uint32 t0, t1; |
| 147 | Atom types[2]; |
| 148 | int i; |
| 149 | |
| 150 | /* get & validate SDL sys-wm info */ |
| 151 | SDL_VERSION(&info.version); |
| 152 | if ( ! SDL_GetWMInfo( &info ) ) |
| 153 | return NULL; |
| 154 | if ( info.subsystem != SDL_SYSWM_X11 ) |
| 155 | return NULL; |
| 156 | if ( (display = info.info.x11.display) == NULL ) |
| 157 | return NULL; |
| 158 | if ( (our_win = info.info.x11.window) == None ) |
| 159 | return NULL; |
| 160 | |
| 161 | /* request data to owner */ |
| 162 | selection_win = XGetSelectionOwner( display, XA_PRIMARY ); |
| 163 | if ( selection_win == None ) |
| 164 | return NULL; |
| 165 | |
| 166 | /* first, try UTF-8, then latin-1 */ |
| 167 | types[0] = XInternAtom( display, "UTF8_STRING", False ); |
| 168 | types[1] = XA_STRING; /* latin-1 */ |
| 169 | |
| 170 | for ( i = 0; i < ARRAY_LENGTH(types); i++ ) |
| 171 | { |
| 172 | |
| 173 | XConvertSelection( display, XA_PRIMARY, types[i], types[i], our_win, CurrentTime ); |
| 174 | |
| 175 | /* wait for SelectionNotify, but no more than 100 ms */ |
| 176 | t0 = t1 = SDL_GetTicks(); |
| 177 | while ( 1 ) |
| 178 | { |
| 179 | if ( XCheckTypedWindowEvent( display, our_win, SelectionNotify, &event ) ) break; |
| 180 | SDL_Delay( 1 ); |
| 181 | t1 = SDL_GetTicks(); |
| 182 | if ( t1 - t0 > 100 ) |
| 183 | return NULL; |
| 184 | } |
| 185 | if ( event.xselection.property == None ) |
| 186 | continue; |
| 187 | |
| 188 | /* get property & check its type */ |
| 189 | if ( XGetWindowProperty( display, our_win, types[i], 0, 65536, False, types[i], |
| 190 | &data_type, &data_format, &nitems, &bytes_remaining, &prop ) |
| 191 | != Success ) |
| 192 | continue; |
| 193 | if ( ! prop ) |
| 194 | continue; |
| 195 | if ( (data_format != 8) || (data_type != types[i]) ) |
| 196 | { |
| 197 | XFree( prop ); |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | /* return a copy & free original */ |
| 202 | if (prop != NULL) |
| 203 | { |
| 204 | result = (char *) osd_malloc(strlen((char *)prop)+1); |
| 205 | strcpy(result, (char *)prop); |
| 206 | } |
| 207 | else |
| 208 | result = NULL; |
| 209 | XFree( prop ); |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | #else |
| 217 | //============================================================ |
| 218 | // osd_get_clipboard_text |
| 219 | //============================================================ |
| 220 | |
| 221 | char *osd_get_clipboard_text(void) |
| 222 | { |
| 223 | char *result = NULL; |
| 224 | |
| 225 | return result; |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | //============================================================ |
| 230 | // osd_stat |
| 231 | //============================================================ |
| 232 | |
| 233 | osd_directory_entry *osd_stat(const char *path) |
| 234 | { |
| 235 | int err; |
| 236 | osd_directory_entry *result = NULL; |
| 237 | #if defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) |
| 238 | struct stat st; |
| 239 | #else |
| 240 | struct stat64 st; |
| 241 | #endif |
| 242 | |
| 243 | #if defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) |
| 244 | err = stat(path, &st); |
| 245 | #else |
| 246 | err = stat64(path, &st); |
| 247 | #endif |
| 248 | |
| 249 | if( err == -1) return NULL; |
| 250 | |
| 251 | // create an osd_directory_entry; be sure to make sure that the caller can |
| 252 | // free all resources by just freeing the resulting osd_directory_entry |
| 253 | result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1); |
| 254 | strcpy(((char *) result) + sizeof(*result), path); |
| 255 | result->name = ((char *) result) + sizeof(*result); |
| 256 | result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE; |
| 257 | result->size = (UINT64)st.st_size; |
| 258 | |
| 259 | return result; |
| 260 | } |
| 261 | |
| 262 | //============================================================ |
| 263 | // osd_get_volume_name |
| 264 | //============================================================ |
| 265 | |
| 266 | const char *osd_get_volume_name(int idx) |
| 267 | { |
| 268 | if (idx!=0) return NULL; |
| 269 | return "/"; |
| 270 | } |
| 271 | |
| 272 | //============================================================ |
| 273 | // osd_get_full_path |
| 274 | //============================================================ |
| 275 | |
| 276 | file_error osd_get_full_path(char **dst, const char *path) |
| 277 | { |
| 278 | file_error err; |
| 279 | char path_buffer[512]; |
| 280 | |
| 281 | err = FILERR_NONE; |
| 282 | |
| 283 | if (getcwd(path_buffer, 511) == NULL) |
| 284 | { |
| 285 | printf("osd_get_full_path: failed!\n"); |
| 286 | err = FILERR_FAILURE; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3); |
| 291 | |
| 292 | // if it's already a full path, just pass it through |
| 293 | if (path[0] == '/') |
| 294 | { |
| 295 | strcpy(*dst, path); |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return err; |
| 304 | } |
| 305 | |
| 306 | //============================================================ |
| 307 | // osd_get_emulator_directory |
| 308 | //============================================================ |
| 309 | |
| 310 | void osd_get_emulator_directory(char *dir, size_t dir_size) |
| 311 | { |
| 312 | strncpy(dir, sdl_cwd, dir_size); |
| 313 | } |
src/osd/sdl/sdlfile.c
| r7340 | r7341 | |
| 395 | 395 | /* osd_is_path_separator */ |
| 396 | 396 | /*============================================================ */ |
| 397 | 397 | |
| 398 | | int osd_is_path_separator(char c) |
| 398 | static int osd_is_path_separator(char c) |
| 399 | 399 | { |
| 400 | 400 | return (c == '/') || (c == '\\'); |
| 401 | 401 | } |
| r7340 | r7341 | |
| 422 | 422 | result = FALSE; |
| 423 | 423 | return result; |
| 424 | 424 | } |
| 425 | | |
| 426 | | /* not used anywhere */ |
| 427 | | #if 0 |
| 428 | | //============================================================ |
| 429 | | // osd_mkdir |
| 430 | | //============================================================ |
| 431 | | |
| 432 | | file_error osd_mkdir(const char *dir) |
| 433 | | { |
| 434 | | #ifdef SDLMAME_WIN32 |
| 435 | | if (mkdir(dir) != 0) |
| 436 | | #else |
| 437 | | if (mkdir(dir, 0666) != 0) |
| 438 | | #endif |
| 439 | | { |
| 440 | | return error_to_file_error(errno); |
| 441 | | } |
| 442 | | |
| 443 | | return FILERR_NONE; |
| 444 | | } |
| 445 | | |
| 446 | | //============================================================ |
| 447 | | // osd_rmdir |
| 448 | | //============================================================ |
| 449 | | |
| 450 | | static file_error osd_rmdir(const char *dir) |
| 451 | | { |
| 452 | | if (rmdir(dir) != 0) |
| 453 | | { |
| 454 | | return error_to_file_error(errno); |
| 455 | | } |
| 456 | | |
| 457 | | return FILERR_NONE; |
| 458 | | } |
| 459 | | #endif |
| 460 | | |
| 461 | | // these are MESS specific - DO NOT TOUCH!!@! |
| 462 | | #ifdef MESS |
| 463 | | |
| 464 | | //============================================================ |
| 465 | | // osd_copyfile |
| 466 | | // FIXME: this will not work with blanks in filename ... |
| 467 | | //============================================================ |
| 468 | | |
| 469 | | file_error osd_copyfile(const char *destfile, const char *srcfile) |
| 470 | | { |
| 471 | | char command[1024]; |
| 472 | | |
| 473 | | sprintf(command, "cp %s %s\n", srcfile, destfile); |
| 474 | | system(command); |
| 475 | | |
| 476 | | return FILERR_NONE; |
| 477 | | } |
| 478 | | |
| 479 | | //============================================================ |
| 480 | | // osd_stat |
| 481 | | //============================================================ |
| 482 | | |
| 483 | | #ifndef SDLMAME_WIN32 |
| 484 | | osd_directory_entry *osd_stat(const char *path) |
| 485 | | { |
| 486 | | int err; |
| 487 | | osd_directory_entry *result = NULL; |
| 488 | | #if defined(SDLMAME_DARWIN) || defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) || defined(SDLMAME_OS2) |
| 489 | | struct stat st; |
| 490 | | #else |
| 491 | | struct stat64 st; |
| 492 | | #endif |
| 493 | | |
| 494 | | #if defined(SDLMAME_DARWIN) || defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) || defined(SDLMAME_OS2) |
| 495 | | err = stat(path, &st); |
| 496 | | #else |
| 497 | | err = stat64(path, &st); |
| 498 | | #endif |
| 499 | | |
| 500 | | if( err == -1) return NULL; |
| 501 | | |
| 502 | | // create an osd_directory_entry; be sure to make sure that the caller can |
| 503 | | // free all resources by just freeing the resulting osd_directory_entry |
| 504 | | result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1); |
| 505 | | strcpy(((char *) result) + sizeof(*result), path); |
| 506 | | result->name = ((char *) result) + sizeof(*result); |
| 507 | | result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE; |
| 508 | | result->size = (UINT64)st.st_size; |
| 509 | | |
| 510 | | return result; |
| 511 | | } |
| 512 | | #endif |
| 513 | | #endif |
| 514 | | |
| 515 | | //============================================================ |
| 516 | | // osd_getcurdir |
| 517 | | //============================================================ |
| 518 | | |
| 519 | | file_error osd_getcurdir(char *buffer, size_t buffer_len) |
| 520 | | { |
| 521 | | file_error filerr = FILERR_NONE; |
| 522 | | |
| 523 | | if (getcwd(buffer, buffer_len) == NULL) |
| 524 | | { |
| 525 | | filerr = FILERR_FAILURE; |
| 526 | | } |
| 527 | | |
| 528 | | if( filerr != FILERR_FAILURE ) |
| 529 | | { |
| 530 | | if( strcmp( &buffer[strlen(buffer)-1], PATH_SEPARATOR ) != 0 ) |
| 531 | | { |
| 532 | | strncat( buffer, PATH_SEPARATOR, buffer_len ); |
| 533 | | } |
| 534 | | } |
| 535 | | |
| 536 | | return filerr; |
| 537 | | } |
| 538 | | |
| 539 | | //============================================================ |
| 540 | | // osd_get_full_path |
| 541 | | //============================================================ |
| 542 | | |
| 543 | | file_error osd_get_full_path(char **dst, const char *path) |
| 544 | | { |
| 545 | | file_error err; |
| 546 | | char path_buffer[512]; |
| 547 | | |
| 548 | | err = FILERR_NONE; |
| 549 | | |
| 550 | | if (getcwd(path_buffer, 511) == NULL) |
| 551 | | { |
| 552 | | printf("osd_get_full_path: failed!\n"); |
| 553 | | err = FILERR_FAILURE; |
| 554 | | } |
| 555 | | else |
| 556 | | { |
| 557 | | *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3); |
| 558 | | |
| 559 | | // if it's already a full path, just pass it through |
| 560 | | if (path[0] == '/') |
| 561 | | { |
| 562 | | strcpy(*dst, path); |
| 563 | | } |
| 564 | | else |
| 565 | | { |
| 566 | | sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path); |
| 567 | | } |
| 568 | | } |
| 569 | | |
| 570 | | return err; |
| 571 | | } |
| 572 | | |
| 573 | | //============================================================ |
| 574 | | // osd_setcurdir |
| 575 | | //============================================================ |
| 576 | | |
| 577 | | file_error osd_setcurdir(const char *dir) |
| 578 | | { |
| 579 | | file_error filerr = FILERR_NONE; |
| 580 | | |
| 581 | | if (chdir(dir) != 0) |
| 582 | | { |
| 583 | | filerr = FILERR_FAILURE; |
| 584 | | } |
| 585 | | |
| 586 | | return filerr; |
| 587 | | } |
| 588 | | |
| 589 | | //============================================================ |
| 590 | | // osd_basename |
| 591 | | //============================================================ |
| 592 | | |
| 593 | | char *osd_basename(char *filename) |
| 594 | | { |
| 595 | | char *c; |
| 596 | | |
| 597 | | // NULL begets NULL |
| 598 | | if (!filename) |
| 599 | | return NULL; |
| 600 | | |
| 601 | | // start at the end and return when we hit a slash or colon |
| 602 | | for (c = filename + strlen(filename) - 1; c >= filename; c--) |
| 603 | | if (*c == '\\' || *c == '/' || *c == ':') |
| 604 | | return c + 1; |
| 605 | | |
| 606 | | // otherwise, return the whole thing |
| 607 | | return filename; |
| 608 | | } |
| 609 | | |
| 610 | | //============================================================ |
| 611 | | // osd_dirname |
| 612 | | //============================================================ |
| 613 | | |
| 614 | | char *osd_dirname(const char *filename) |
| 615 | | { |
| 616 | | char *dirname; |
| 617 | | char *c; |
| 618 | | |
| 619 | | // NULL begets NULL |
| 620 | | if (!filename) |
| 621 | | return NULL; |
| 622 | | |
| 623 | | // allocate space for it |
| 624 | | dirname = (char*)malloc(strlen(filename) + 1); |
| 625 | | if (!dirname) |
| 626 | | return NULL; |
| 627 | | |
| 628 | | // copy in the name |
| 629 | | strcpy(dirname, filename); |
| 630 | | |
| 631 | | // search backward for a slash or a colon |
| 632 | | for (c = dirname + strlen(dirname) - 1; c >= dirname; c--) |
| 633 | | if (*c == '\\' || *c == '/' || *c == ':') |
| 634 | | { |
| 635 | | // found it: NULL terminate and return |
| 636 | | *(c + 1) = 0; |
| 637 | | return dirname; |
| 638 | | } |
| 639 | | |
| 640 | | // otherwise, return an empty string |
| 641 | | dirname[0] = 0; |
| 642 | | return dirname; |
| 643 | | } |
| 644 | | |
| 645 | | // no idea what these are for, just cheese them |
| 646 | | int osd_num_devices(void) |
| 647 | | { |
| 648 | | return 1; |
| 649 | | } |
| 650 | | |
| 651 | | const char *osd_get_device_name(int idx) |
| 652 | | { |
| 653 | | return "/"; |
| 654 | | } |
| 655 | | |
| 656 | | //============================================================ |
| 657 | | // osd_get_temp_filename |
| 658 | | //============================================================ |
| 659 | | |
| 660 | | file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename) |
| 661 | | { |
| 662 | | char tempbuf[512]; |
| 663 | | |
| 664 | | if (!basename) |
| 665 | | basename = "tempfile"; |
| 666 | | |
| 667 | | sprintf(tempbuf, "/tmp/%s", basename); |
| 668 | | unlink(tempbuf); |
| 669 | | |
| 670 | | strncpy(buffer, tempbuf, buffer_len); |
| 671 | | |
| 672 | | return FILERR_NONE; |
| 673 | | } |
| 674 | | |
| 675 | | //============================================================ |
| 676 | | // osd_mkdir |
| 677 | | //============================================================ |
| 678 | | |
| 679 | | file_error osd_mkdir(const char *dir) |
| 680 | | { |
| 681 | | file_error filerr = FILERR_NONE; |
| 682 | | |
| 683 | | #ifdef SDLMAME_WIN32 |
| 684 | | if (mkdir(dir) != 0) |
| 685 | | #else |
| 686 | | if (mkdir(dir, 0700) != 0) |
| 687 | | #endif |
| 688 | | { |
| 689 | | filerr = FILERR_FAILURE; |
| 690 | | } |
| 691 | | |
| 692 | | return filerr; |
| 693 | | } |
| 694 | | |
| 695 | | //============================================================ |
| 696 | | // osd_rmdir |
| 697 | | //============================================================ |
| 698 | | |
| 699 | | file_error osd_rmdir(const char *dir) |
| 700 | | { |
| 701 | | file_error filerr = FILERR_NONE; |
| 702 | | |
| 703 | | if (rmdir(dir) != 0) |
| 704 | | { |
| 705 | | filerr = FILERR_FAILURE; |
| 706 | | } |
| 707 | | |
| 708 | | return filerr; |
| 709 | | } |
| 710 | | |
| 711 | | |
| 712 | | //============================================================ |
| 713 | | // osd_get_emulator_directory |
| 714 | | //============================================================ |
| 715 | | |
| 716 | | void osd_get_emulator_directory(char *dir, size_t dir_size) |
| 717 | | { |
| 718 | | strncpy(dir, sdl_cwd, dir_size); |
| 719 | | } |
src/osd/osdcore.h
| r7340 | r7341 | |
| 50 | 50 | |
| 51 | 51 | #include "osdcomm.h" |
| 52 | 52 | |
| 53 | |
| 54 | |
| 53 | 55 | /*************************************************************************** |
| 54 | 56 | FILE I/O INTERFACES |
| 55 | 57 | ***************************************************************************/ |
| r7340 | r7341 | |
| 84 | 86 | /* osd_file is an opaque type which represents an open file */ |
| 85 | 87 | typedef struct _osd_file osd_file; |
| 86 | 88 | |
| 87 | | enum |
| 88 | | { |
| 89 | | OSD_FOPEN_READ, |
| 90 | | OSD_FOPEN_WRITE, |
| 91 | | OSD_FOPEN_RW, |
| 92 | | OSD_FOPEN_RW_CREATE |
| 93 | | }; |
| 94 | | |
| 95 | | |
| 96 | | struct _mame_file; |
| 97 | | |
| 98 | | |
| 99 | 89 | /*----------------------------------------------------------------------------- |
| 100 | 90 | osd_open: open a new file. |
| 101 | 91 | |
| r7340 | r7341 | |
| 819 | 809 | -----------------------------------------------------------------------------*/ |
| 820 | 810 | void osd_break_into_debugger(const char *message); |
| 821 | 811 | |
| 822 | | /*************************************************************************** |
| 823 | | FILE I/O INTERFACES |
| 824 | | ***************************************************************************/ |
| 825 | 812 | |
| 826 | | /*----------------------------------------------------------------------------- |
| 827 | | osd_copyfile: copies a file |
| 828 | | |
| 829 | | Parameters: |
| 830 | | |
| 831 | | destfile - path to destination |
| 832 | | |
| 833 | | srcfile - path to source file |
| 834 | | |
| 835 | | Return value: |
| 836 | | |
| 837 | | a file_error describing any error that occurred while copying |
| 838 | | the file, or FILERR_NONE if no error occurred |
| 839 | | -----------------------------------------------------------------------------*/ |
| 840 | | file_error osd_copyfile(const char *destfile, const char *srcfile); |
| 841 | | |
| 842 | | |
| 843 | | /*----------------------------------------------------------------------------- |
| 844 | | osd_get_temp_filename: given a filename, return a full path with that |
| 845 | | filename but in a temporary directory |
| 846 | | |
| 847 | | Parameters: |
| 848 | | |
| 849 | | buffer - pointer to buffer |
| 850 | | |
| 851 | | buffer_len - length of buffer |
| 852 | | |
| 853 | | basename - basename of file to create |
| 854 | | |
| 855 | | Return value: |
| 856 | | |
| 857 | | a file_error describing any error that occurred while copying |
| 858 | | the file, or FILERR_NONE if no error occurred |
| 859 | | -----------------------------------------------------------------------------*/ |
| 860 | | file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename); |
| 861 | | |
| 862 | | |
| 863 | 813 | /*************************************************************************** |
| 864 | 814 | DIRECTORY INTERFACES |
| 865 | 815 | ***************************************************************************/ |
| r7340 | r7341 | |
| 879 | 829 | -----------------------------------------------------------------------------*/ |
| 880 | 830 | osd_directory_entry *osd_stat(const char *path); |
| 881 | 831 | |
| 882 | | |
| 883 | | /*----------------------------------------------------------------------------- |
| 884 | | osd_mkdir: creates a directory |
| 885 | | |
| 886 | | Parameters: |
| 887 | | |
| 888 | | dir - path to directory to create |
| 889 | | |
| 890 | | Return value: |
| 891 | | |
| 892 | | a file_error describing any error that occurred while creating |
| 893 | | the directory, or FILERR_NONE if no error occurred |
| 894 | | -----------------------------------------------------------------------------*/ |
| 895 | | file_error osd_mkdir(const char *dir); |
| 896 | | |
| 897 | | |
| 898 | | /*----------------------------------------------------------------------------- |
| 899 | | osd_rmdir: removes a directory |
| 900 | | |
| 901 | | Parameters: |
| 902 | | |
| 903 | | dir - path to directory to removes |
| 904 | | |
| 905 | | Return value: |
| 906 | | |
| 907 | | a file_error describing any error that occurred while deleting |
| 908 | | the directory, or FILERR_NONE if no error occurred |
| 909 | | -----------------------------------------------------------------------------*/ |
| 910 | | file_error osd_rmdir(const char *dir); |
| 911 | | |
| 912 | | |
| 913 | | /*----------------------------------------------------------------------------- |
| 914 | | osd_getcurdir: retrieves the current working directory |
| 915 | | |
| 916 | | Parameters: |
| 917 | | |
| 918 | | buffer - place to store current working directory |
| 919 | | |
| 920 | | buffer_len - length of buffer |
| 921 | | |
| 922 | | Return value: |
| 923 | | |
| 924 | | a file_error describing any error that occurred while deleting |
| 925 | | the directory, or FILERR_NONE if no error occurred |
| 926 | | -----------------------------------------------------------------------------*/ |
| 927 | | file_error osd_getcurdir(char *buffer, size_t buffer_len); |
| 928 | | |
| 929 | | |
| 930 | | /*----------------------------------------------------------------------------- |
| 931 | | osd_setcurdir: sets the current working directory |
| 932 | | |
| 933 | | Parameters: |
| 934 | | |
| 935 | | dir - path to directory to which to change |
| 936 | | |
| 937 | | Return value: |
| 938 | | |
| 939 | | a file_error describing any error that occurred while deleting |
| 940 | | the directory, or FILERR_NONE if no error occurred |
| 941 | | -----------------------------------------------------------------------------*/ |
| 942 | | file_error osd_setcurdir(const char *dir); |
| 943 | | |
| 944 | | |
| 945 | 832 | /*************************************************************************** |
| 946 | 833 | PATH INTERFACES |
| 947 | 834 | ***************************************************************************/ |
| r7340 | r7341 | |
| 959 | 846 | void osd_get_emulator_directory(char *dir, size_t dir_size); |
| 960 | 847 | |
| 961 | 848 | /*----------------------------------------------------------------------------- |
| 962 | | osd_is_path_separator: returns whether a character is a path separator |
| 963 | | |
| 964 | | Parameters: |
| 965 | | |
| 966 | | c - the character in question |
| 967 | | |
| 968 | | Return value: |
| 969 | | |
| 970 | | non-zero if the character is a path separator, zero otherwise |
| 971 | | |
| 972 | | -----------------------------------------------------------------------------*/ |
| 973 | | int osd_is_path_separator(char c); |
| 974 | | |
| 975 | | |
| 976 | | /*----------------------------------------------------------------------------- |
| 977 | | osd_dirname: returns the base directory of a file path |
| 978 | | |
| 979 | | Parameters: |
| 980 | | |
| 981 | | filename - the path in question |
| 982 | | |
| 983 | | Return value: |
| 984 | | |
| 985 | | an allocated path to the directory containing this file |
| 986 | | |
| 987 | | -----------------------------------------------------------------------------*/ |
| 988 | | char *osd_dirname(const char *filename); |
| 989 | | |
| 990 | | |
| 991 | | /*----------------------------------------------------------------------------- |
| 992 | | osd_basename: returns the file or directory name from a full path |
| 993 | | |
| 994 | | Parameters: |
| 995 | | |
| 996 | | filename - the path in question |
| 997 | | |
| 998 | | Return value: |
| 999 | | |
| 1000 | | a pointer to the base name of the file |
| 1001 | | |
| 1002 | | -----------------------------------------------------------------------------*/ |
| 1003 | | char *osd_basename(char *filename); |
| 1004 | | |
| 1005 | | |
| 1006 | | /*----------------------------------------------------------------------------- |
| 1007 | 849 | osd_get_full_path: retrieves the full path |
| 1008 | 850 | |
| 1009 | 851 | Parameters: |
| r7340 | r7341 | |
| 1019 | 861 | file_error osd_get_full_path(char **dst, const char *path); |
| 1020 | 862 | |
| 1021 | 863 | |
| 1022 | | |
| 1023 | 864 | /*************************************************************************** |
| 1024 | 865 | UNCATEGORIZED INTERFACES |
| 1025 | 866 | ***************************************************************************/ |
| 1026 | 867 | |
| 868 | /*----------------------------------------------------------------------------- |
| 869 | osd_get_clipboard_text: retrieves text from the clipboard |
| 1027 | 870 | |
| 1028 | | /* reads text from the clipboard - the returned string needs to be free()-ed! */ |
| 1029 | | char *osd_get_clipboard_text(void); |
| 871 | Return value: |
| 1030 | 872 | |
| 873 | the returned string needs to be free()-ed! |
| 1031 | 874 | |
| 875 | -----------------------------------------------------------------------------*/ |
| 876 | char *osd_get_clipboard_text(void); |
| 1032 | 877 | |
| 1033 | | /****************************************************************************** |
| 878 | /*----------------------------------------------------------------------------- |
| 879 | osd_get_volume_name: retrieves the volume name |
| 1034 | 880 | |
| 1035 | | Device and file browsing |
| 881 | Parameters: |
| 1036 | 882 | |
| 1037 | | ******************************************************************************/ |
| 883 | idx - order number of volume |
| 1038 | 884 | |
| 1039 | | int osd_num_devices(void); |
| 1040 | | const char *osd_get_device_name(int idx); |
| 885 | Return value: |
| 1041 | 886 | |
| 887 | pointer to volume name |
| 1042 | 888 | |
| 889 | -----------------------------------------------------------------------------*/ |
| 890 | const char *osd_get_volume_name(int idx); |
| 891 | |
| 1043 | 892 | #endif /* __OSDEPEND_H__ */ |
src/osd/windows/winfile.c
| r7340 | r7341 | |
| 413 | 413 | return filerr; |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | | |
| 417 | 416 | //============================================================ |
| 418 | | // osd_get_temp_filename |
| 419 | | //============================================================ |
| 420 | | |
| 421 | | file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename) |
| 422 | | { |
| 423 | | TCHAR tempbuf[MAX_PATH]; |
| 424 | | TCHAR *t_filename; |
| 425 | | char *u_tempbuf; |
| 426 | | |
| 427 | | if (!basename) |
| 428 | | basename = "tempfile.tmp"; |
| 429 | | |
| 430 | | GetTempPath(ARRAY_LENGTH(tempbuf), tempbuf); |
| 431 | | t_filename = tstring_from_utf8(basename); |
| 432 | | _tcscat(tempbuf, t_filename); |
| 433 | | free(t_filename); |
| 434 | | DeleteFile(tempbuf); |
| 435 | | |
| 436 | | u_tempbuf = utf8_from_tstring(tempbuf); |
| 437 | | snprintf(buffer, buffer_len, "%s", u_tempbuf); |
| 438 | | free(u_tempbuf); |
| 439 | | |
| 440 | | return FILERR_NONE; |
| 441 | | } |
| 442 | | |
| 443 | | |
| 444 | | //============================================================ |
| 445 | | // osd_copyfile |
| 446 | | //============================================================ |
| 447 | | |
| 448 | | file_error osd_copyfile(const char *destfile, const char *srcfile) |
| 449 | | { |
| 450 | | // wrapper for win_copy_file_utf8() |
| 451 | | if (!win_copy_file_utf8(srcfile, destfile, TRUE)) |
| 452 | | return win_error_to_mame_file_error(GetLastError()); |
| 453 | | |
| 454 | | return FILERR_NONE; |
| 455 | | } |
| 456 | | |
| 457 | | |
| 458 | | //============================================================ |
| 459 | 417 | // osd_stat |
| 460 | 418 | //============================================================ |
| 461 | 419 | |
| r7340 | r7341 | |
| 502 | 460 | return result; |
| 503 | 461 | } |
| 504 | 462 | |
| 505 | | |
| 506 | | |
| 507 | 463 | //============================================================ |
| 508 | | // osd_is_path_separator |
| 509 | | //============================================================ |
| 510 | | |
| 511 | | int osd_is_path_separator(char c) |
| 512 | | { |
| 513 | | return (c == '/') || (c == '\\'); |
| 514 | | } |
| 515 | | |
| 516 | | |
| 517 | | |
| 518 | | //============================================================ |
| 519 | | // osd_dirname |
| 520 | | //============================================================ |
| 521 | | |
| 522 | | char *osd_dirname(const char *filename) |
| 523 | | { |
| 524 | | char *dirname; |
| 525 | | char *c; |
| 526 | | |
| 527 | | // NULL begets NULL |
| 528 | | if (!filename) |
| 529 | | return NULL; |
| 530 | | |
| 531 | | // allocate space for it |
| 532 | | dirname = (char*)malloc(strlen(filename) + 1); |
| 533 | | if (!dirname) |
| 534 | | return NULL; |
| 535 | | |
| 536 | | // copy in the name |
| 537 | | strcpy(dirname, filename); |
| 538 | | |
| 539 | | // search backward for a slash or a colon |
| 540 | | for (c = dirname + strlen(dirname) - 1; c >= dirname; c--) |
| 541 | | if (*c == '\\' || *c == '/' || *c == ':') |
| 542 | | { |
| 543 | | // found it: NULL terminate and return |
| 544 | | *(c + 1) = 0; |
| 545 | | return dirname; |
| 546 | | } |
| 547 | | |
| 548 | | // otherwise, return an empty string |
| 549 | | dirname[0] = 0; |
| 550 | | return dirname; |
| 551 | | } |
| 552 | | |
| 553 | | |
| 554 | | //============================================================ |
| 555 | | // osd_basename |
| 556 | | //============================================================ |
| 557 | | |
| 558 | | char *osd_basename(char *filename) |
| 559 | | { |
| 560 | | char *c; |
| 561 | | |
| 562 | | // NULL begets NULL |
| 563 | | if (!filename) |
| 564 | | return NULL; |
| 565 | | |
| 566 | | // start at the end and return when we hit a slash or colon |
| 567 | | for (c = filename + strlen(filename) - 1; c >= filename; c--) |
| 568 | | if (*c == '\\' || *c == '/' || *c == ':') |
| 569 | | return c + 1; |
| 570 | | |
| 571 | | // otherwise, return the whole thing |
| 572 | | return filename; |
| 573 | | } |
| 574 | | |
| 575 | | |
| 576 | | //============================================================ |
| 577 | 464 | // osd_get_full_path |
| 578 | 465 | //============================================================ |
| 579 | 466 | |
| r7340 | r7341 | |
| 614 | 501 | return err; |
| 615 | 502 | } |
| 616 | 503 | |
| 617 | | |
| 618 | 504 | //============================================================ |
| 619 | | // osd_mkdir |
| 505 | // osd_get_volume_name |
| 620 | 506 | //============================================================ |
| 621 | 507 | |
| 622 | | file_error osd_mkdir(const char *dir) |
| 508 | const char *osd_get_volume_name(int idx) |
| 623 | 509 | { |
| 624 | | file_error filerr = FILERR_NONE; |
| 625 | | |
| 626 | | TCHAR *tempstr = tstring_from_utf8(dir); |
| 627 | | if (!tempstr) |
| 628 | | { |
| 629 | | filerr = FILERR_OUT_OF_MEMORY; |
| 630 | | goto done; |
| 631 | | } |
| 632 | | |
| 633 | | if (!CreateDirectory(tempstr, NULL)) |
| 634 | | { |
| 635 | | filerr = win_error_to_mame_file_error(GetLastError()); |
| 636 | | goto done; |
| 637 | | } |
| 638 | | |
| 639 | | done: |
| 640 | | if (tempstr) |
| 641 | | free(tempstr); |
| 642 | | return filerr; |
| 643 | | } |
| 644 | | |
| 645 | | |
| 646 | | //============================================================ |
| 647 | | // osd_rmdir |
| 648 | | //============================================================ |
| 649 | | |
| 650 | | file_error osd_rmdir(const char *dir) |
| 651 | | { |
| 652 | | file_error filerr = FILERR_NONE; |
| 653 | | |
| 654 | | TCHAR *tempstr = tstring_from_utf8(dir); |
| 655 | | if (!tempstr) |
| 656 | | { |
| 657 | | filerr = FILERR_OUT_OF_MEMORY; |
| 658 | | goto done; |
| 659 | | } |
| 660 | | |
| 661 | | if (!RemoveDirectory(tempstr)) |
| 662 | | { |
| 663 | | filerr = win_error_to_mame_file_error(GetLastError()); |
| 664 | | goto done; |
| 665 | | } |
| 666 | | |
| 667 | | done: |
| 668 | | if (tempstr) |
| 669 | | free(tempstr); |
| 670 | | return filerr; |
| 671 | | } |
| 672 | | |
| 673 | | |
| 674 | | //============================================================ |
| 675 | | // osd_getcurdir |
| 676 | | //============================================================ |
| 677 | | |
| 678 | | file_error osd_getcurdir(char *buffer, size_t buffer_len) |
| 679 | | { |
| 680 | | file_error filerr = FILERR_NONE; |
| 681 | | char *tempstr = NULL; |
| 682 | | TCHAR path[_MAX_PATH]; |
| 683 | | |
| 684 | | if (!GetCurrentDirectory(ARRAY_LENGTH(path), path)) |
| 685 | | { |
| 686 | | filerr = win_error_to_mame_file_error(GetLastError()); |
| 687 | | goto done; |
| 688 | | } |
| 689 | | |
| 690 | | tempstr = utf8_from_tstring(path); |
| 691 | | if (!tempstr) |
| 692 | | { |
| 693 | | filerr = FILERR_OUT_OF_MEMORY; |
| 694 | | goto done; |
| 695 | | } |
| 696 | | snprintf(buffer, buffer_len, "%s", tempstr); |
| 697 | | |
| 698 | | done: |
| 699 | | if (tempstr) |
| 700 | | free(tempstr); |
| 701 | | return filerr; |
| 702 | | } |
| 703 | | |
| 704 | | |
| 705 | | //============================================================ |
| 706 | | // osd_setcurdir |
| 707 | | //============================================================ |
| 708 | | |
| 709 | | file_error osd_setcurdir(const char *dir) |
| 710 | | { |
| 711 | | file_error filerr = FILERR_NONE; |
| 712 | | |
| 713 | | TCHAR *tempstr = tstring_from_utf8(dir); |
| 714 | | if (!tempstr) |
| 715 | | { |
| 716 | | filerr = FILERR_OUT_OF_MEMORY; |
| 717 | | goto done; |
| 718 | | } |
| 719 | | |
| 720 | | if (!SetCurrentDirectory(tempstr)) |
| 721 | | { |
| 722 | | filerr = win_error_to_mame_file_error(GetLastError()); |
| 723 | | goto done; |
| 724 | | } |
| 725 | | |
| 726 | | done: |
| 727 | | if (tempstr) |
| 728 | | free(tempstr); |
| 729 | | return filerr; |
| 730 | | } |
| 731 | | |
| 732 | | |
| 733 | | //============================================================ |
| 734 | | // DIRECTORIES |
| 735 | | //============================================================ |
| 736 | | |
| 737 | | int osd_num_devices(void) |
| 738 | | { |
| 739 | | char szBuffer[128]; |
| 740 | | char *p; |
| 741 | | int i; |
| 742 | | |
| 743 | | GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer); |
| 744 | | |
| 745 | | i = 0; |
| 746 | | for (p = szBuffer; *p; p += (strlen(p) + 1)) |
| 747 | | i++; |
| 748 | | return i; |
| 749 | | } |
| 750 | | |
| 751 | | const char *osd_get_device_name(int idx) |
| 752 | | { |
| 753 | 510 | static char szBuffer[128]; |
| 754 | 511 | const char *p; |
| 755 | 512 | |
| 756 | 513 | GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer); |
| 757 | 514 | |
| 758 | 515 | p = szBuffer; |
| 759 | | while(idx--) |
| 516 | while(idx--) { |
| 760 | 517 | p += strlen(p) + 1; |
| 518 | if (!*p) return NULL; |
| 519 | } |
| 761 | 520 | |
| 762 | 521 | return p; |
| 763 | 522 | } |
| 764 | 523 | |
| 524 | //============================================================ |
| 525 | // osd_get_emulator_directory |
| 526 | //============================================================ |
| 527 | |
| 765 | 528 | void osd_get_emulator_directory(char *dir, size_t dir_size) |
| 766 | 529 | { |
| 767 | 530 | char *s; |
src/mess/tools/imgtool/windows/wimgtool.c
| r7340 | r7341 | |
| 73 | 73 | return info; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | static DWORD win_get_file_attributes_utf8(const char *filename) |
| 77 | { |
| 78 | DWORD result = ~0; |
| 79 | LPTSTR t_filename; |
| 76 | 80 | |
| 81 | t_filename = tstring_from_utf8(filename); |
| 82 | if (t_filename != NULL) |
| 83 | { |
| 84 | result = GetFileAttributes(t_filename); |
| 85 | free(t_filename); |
| 86 | } |
| 87 | return result; |
| 88 | } |
| 77 | 89 | |
| 78 | 90 | struct foreach_entry |
| 79 | 91 | { |
| r7340 | r7341 | |
| 176 | 188 | switch(ERRORSOURCE(err)) |
| 177 | 189 | { |
| 178 | 190 | case IMGTOOLERR_SRC_IMAGEFILE: |
| 179 | | source = osd_basename((char *) imagename); |
| 191 | source = imgtool_basename((char *) imagename); |
| 180 | 192 | break; |
| 181 | 193 | case IMGTOOLERR_SRC_FILEONIMAGE: |
| 182 | 194 | source = filename; |
| r7340 | r7341 | |
| 657 | 669 | file_title, imageinfo); |
| 658 | 670 | } |
| 659 | 671 | |
| 660 | | statusbar_text[0] = osd_basename((char *) info->filename); |
| 672 | statusbar_text[0] = imgtool_basename((char *) info->filename); |
| 661 | 673 | statusbar_text[1] = imgtool_image_module(info->image)->description; |
| 662 | 674 | |
| 663 | 675 | free(file_title); |
| r7340 | r7341 | |
| 870 | 882 | return NULL; |
| 871 | 883 | } |
| 872 | 884 | |
| 885 | //============================================================ |
| 886 | // win_error_to_mame_file_error |
| 887 | //============================================================ |
| 873 | 888 | |
| 889 | static file_error win_error_to_mame_file_error(DWORD error) |
| 890 | { |
| 891 | file_error filerr; |
| 874 | 892 | |
| 893 | // convert a Windows error to a file_error |
| 894 | switch (error) |
| 895 | { |
| 896 | case ERROR_SUCCESS: |
| 897 | filerr = FILERR_NONE; |
| 898 | break; |
| 899 | |
| 900 | case ERROR_OUTOFMEMORY: |
| 901 | filerr = FILERR_OUT_OF_MEMORY; |
| 902 | break; |
| 903 | |
| 904 | case ERROR_FILE_NOT_FOUND: |
| 905 | case ERROR_PATH_NOT_FOUND: |
| 906 | filerr = FILERR_NOT_FOUND; |
| 907 | break; |
| 908 | |
| 909 | case ERROR_ACCESS_DENIED: |
| 910 | filerr = FILERR_ACCESS_DENIED; |
| 911 | break; |
| 912 | |
| 913 | case ERROR_SHARING_VIOLATION: |
| 914 | filerr = FILERR_ALREADY_OPEN; |
| 915 | break; |
| 916 | |
| 917 | default: |
| 918 | filerr = FILERR_FAILURE; |
| 919 | break; |
| 920 | } |
| 921 | return filerr; |
| 922 | } |
| 923 | |
| 924 | //============================================================ |
| 925 | // win_mkdir |
| 926 | //============================================================ |
| 927 | |
| 928 | file_error win_mkdir(const char *dir) |
| 929 | { |
| 930 | file_error filerr = FILERR_NONE; |
| 931 | |
| 932 | TCHAR *tempstr = tstring_from_utf8(dir); |
| 933 | if (!tempstr) |
| 934 | { |
| 935 | filerr = FILERR_OUT_OF_MEMORY; |
| 936 | goto done; |
| 937 | } |
| 938 | |
| 939 | if (!CreateDirectory(tempstr, NULL)) |
| 940 | { |
| 941 | filerr = win_error_to_mame_file_error(GetLastError()); |
| 942 | goto done; |
| 943 | } |
| 944 | |
| 945 | done: |
| 946 | if (tempstr) |
| 947 | free(tempstr); |
| 948 | return filerr; |
| 949 | } |
| 950 | |
| 951 | |
| 875 | 952 | static imgtoolerr_t get_recursive_directory(imgtool_partition *partition, const char *path, LPCSTR local_path) |
| 876 | 953 | { |
| 877 | 954 | imgtoolerr_t err; |
| r7340 | r7341 | |
| 880 | 957 | const char *subpath; |
| 881 | 958 | char local_subpath[MAX_PATH]; |
| 882 | 959 | |
| 883 | | if (osd_mkdir(local_path) != FILERR_NONE) |
| 960 | if (win_mkdir(local_path) != FILERR_NONE) |
| 884 | 961 | { |
| 885 | 962 | err = IMGTOOLERR_UNEXPECTED; |
| 886 | 963 | goto done; |
| r7340 | r7341 | |
| 1638 | 1715 | |
| 1639 | 1716 | // figure out the file/dir name on the image |
| 1640 | 1717 | snprintf(subpath, ARRAY_LENGTH(subpath), "%s%s", |
| 1641 | | info->current_directory ? info->current_directory : "", osd_basename(filename)); |
| 1718 | info->current_directory ? info->current_directory : "", imgtool_basename(filename)); |
| 1642 | 1719 | |
| 1643 | 1720 | if (GetFileAttributes(buffer) & FILE_ATTRIBUTE_DIRECTORY) |
| 1644 | 1721 | err = put_recursive_directory(info->partition, buffer, subpath); |
src/mess/osd/sdl/w32util.c
| r7340 | r7341 | |
| 1 | | #define WIN32_LEAN_AND_MEAN |
| 2 | | #include <windows.h> |
| 3 | | #include <ctype.h> |
| 4 | | #include <tchar.h> |
| 5 | | |
| 6 | | #include "osdcore.h" |
| 7 | | #include "utils.h" |
| 8 | | #include "strconv.h" |
| 9 | | #include "emu.h" |
| 10 | | #include "unicode.h" |
| 11 | | |
| 12 | | //============================================================ |
| 13 | | // astring_from_utf8 |
| 14 | | //============================================================ |
| 15 | | |
| 16 | | CHAR *astring_from_utf8(const char *utf8string) |
| 17 | | { |
| 18 | | WCHAR *wstring; |
| 19 | | int char_count; |
| 20 | | CHAR *result; |
| 21 | | |
| 22 | | // convert MAME string (UTF-8) to UTF-16 |
| 23 | | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 24 | | wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); |
| 25 | | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count); |
| 26 | | |
| 27 | | // convert UTF-16 to "ANSI code page" string |
| 28 | | char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL); |
| 29 | | result = (CHAR *)malloc(char_count * sizeof(*result)); |
| 30 | | if (result != NULL) |
| 31 | | WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL); |
| 32 | | |
| 33 | | return result; |
| 34 | | } |
| 35 | | |
| 36 | | //============================================================ |
| 37 | | // wstring_from_utf8 |
| 38 | | //============================================================ |
| 39 | | |
| 40 | | WCHAR *wstring_from_utf8(const char *utf8string) |
| 41 | | { |
| 42 | | int char_count; |
| 43 | | WCHAR *result; |
| 44 | | |
| 45 | | // convert MAME string (UTF-8) to UTF-16 |
| 46 | | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 47 | | result = (WCHAR *)malloc(char_count * sizeof(*result)); |
| 48 | | if (result != NULL) |
| 49 | | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count); |
| 50 | | |
| 51 | | return result; |
| 52 | | } |
| 53 | | |
| 54 | | //============================================================ |
| 55 | | // win_attributes_to_entry_type |
| 56 | | //============================================================ |
| 57 | | |
| 58 | | static osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes) |
| 59 | | { |
| 60 | | if (attributes == 0xFFFFFFFF) |
| 61 | | return ENTTYPE_NONE; |
| 62 | | else if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 63 | | return ENTTYPE_DIR; |
| 64 | | else |
| 65 | | return ENTTYPE_FILE; |
| 66 | | } |
| 67 | | |
| 68 | | //============================================================ |
| 69 | | // osd_stat |
| 70 | | //============================================================ |
| 71 | | |
| 72 | | osd_directory_entry *osd_stat(const char *path) |
| 73 | | { |
| 74 | | osd_directory_entry *result = NULL; |
| 75 | | TCHAR *t_path; |
| 76 | | HANDLE find = INVALID_HANDLE_VALUE; |
| 77 | | WIN32_FIND_DATA find_data; |
| 78 | | |
| 79 | | // convert the path to TCHARs |
| 80 | | t_path = tstring_from_utf8(path); |
| 81 | | if (t_path == NULL) |
| 82 | | goto done; |
| 83 | | |
| 84 | | // attempt to find the first file |
| 85 | | find = FindFirstFile(t_path, &find_data); |
| 86 | | if (find == INVALID_HANDLE_VALUE) |
| 87 | | goto done; |
| 88 | | |
| 89 | | // create an osd_directory_entry; be sure to make sure that the caller can |
| 90 | | // free all resources by just freeing the resulting osd_directory_entry |
| 91 | | result = (osd_directory_entry *) malloc(sizeof(*result) + strlen(path) + 1); |
| 92 | | if (!result) |
| 93 | | goto done; |
| 94 | | strcpy(((char *) result) + sizeof(*result), path); |
| 95 | | result->name = ((char *) result) + sizeof(*result); |
| 96 | | result->type = win_attributes_to_entry_type(find_data.dwFileAttributes); |
| 97 | | result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32); |
| 98 | | |
| 99 | | done: |
| 100 | | if (t_path) |
| 101 | | free(t_path); |
| 102 | | return result; |
| 103 | | } |
| 104 | | |
src/mess/zippath.c
| r7340 | r7341 | |
| 57 | 57 | PATH OPERATIONS |
| 58 | 58 | ***************************************************************************/ |
| 59 | 59 | |
| 60 | //============================================================ |
| 61 | // is_path_separator |
| 62 | //============================================================ |
| 63 | |
| 64 | int is_path_separator(char c) |
| 65 | { |
| 66 | return (c == '/') || (c == '\\'); |
| 67 | } |
| 68 | |
| 60 | 69 | /*------------------------------------------------- |
| 61 | 70 | parse_parent_path - parses out the parent path |
| 62 | 71 | -------------------------------------------------*/ |
| r7340 | r7341 | |
| 68 | 77 | |
| 69 | 78 | /* skip over trailing path separators */ |
| 70 | 79 | pos = length - 1; |
| 71 | | while((pos > 0) && osd_is_path_separator(path[pos])) |
| 80 | while((pos > 0) && is_path_separator(path[pos])) |
| 72 | 81 | pos--; |
| 73 | 82 | |
| 74 | 83 | /* return endpos */ |
| r7340 | r7341 | |
| 76 | 85 | *endpos = pos; |
| 77 | 86 | |
| 78 | 87 | /* now skip until we find a path separator */ |
| 79 | | while((pos >= 0) && !osd_is_path_separator(path[pos])) |
| 88 | while((pos >= 0) && !is_path_separator(path[pos])) |
| 80 | 89 | pos--; |
| 81 | 90 | |
| 82 | 91 | /* return beginpos */ |
| r7340 | r7341 | |
| 136 | 145 | { |
| 137 | 146 | result = astring_cpyc(dst, path2); |
| 138 | 147 | } |
| 139 | | else if ((path1[0] != '\0') && !osd_is_path_separator(path1[strlen(path1) - 1])) |
| 148 | else if ((path1[0] != '\0') && !is_path_separator(path1[strlen(path1) - 1])) |
| 140 | 149 | { |
| 141 | 150 | result = astring_assemble_3(dst, path1, PATH_SEPARATOR, path2); |
| 142 | 151 | } |
| r7340 | r7341 | |
| 217 | 226 | |
| 218 | 227 | done: |
| 219 | 228 | if (ptr != NULL) |
| 220 | | free(ptr); |
| 229 | global_free(ptr); |
| 221 | 230 | return filerr; |
| 222 | 231 | } |
| 223 | 232 | |
| r7340 | r7341 | |
| 372 | 381 | i += 2; |
| 373 | 382 | |
| 374 | 383 | /* skip path separators */ |
| 375 | | while (osd_is_path_separator(path[i])) |
| 384 | while (is_path_separator(path[i])) |
| 376 | 385 | i++; |
| 377 | 386 | |
| 378 | 387 | return path[i] == '\0'; |
| r7340 | r7341 | |
| 412 | 421 | |
| 413 | 422 | static int is_zip_path_separator(char c) |
| 414 | 423 | { |
| 415 | | return is_zip_file_separator(c) || osd_is_path_separator(c); |
| 424 | return is_zip_file_separator(c) || is_path_separator(c); |
| 416 | 425 | } |
| 417 | 426 | |
| 418 | 427 | |
| r7340 | r7341 | |
| 537 | 546 | { |
| 538 | 547 | /* trim the path of trailing path separators */ |
| 539 | 548 | i = astring_len(apath); |
| 540 | | while((i > 1) && osd_is_path_separator(astring_c(apath)[i - 1])) |
| 549 | while((i > 1) && is_path_separator(astring_c(apath)[i - 1])) |
| 541 | 550 | i--; |
| 542 | 551 | apath_trimmed = astring_cpysubstr(apath_trimmed, apath, 0, i); |
| 543 | 552 | |
| r7340 | r7341 | |
| 549 | 558 | { |
| 550 | 559 | /* get the entry type and free the stat entry */ |
| 551 | 560 | current_entry_type = current_entry->type; |
| 552 | | free(current_entry); |
| 561 | global_free(current_entry); |
| 553 | 562 | current_entry = NULL; |
| 554 | 563 | } |
| 555 | 564 | else |
| r7340 | r7341 | |
| 703 | 712 | { |
| 704 | 713 | dirlist = directory->returned_dirlist; |
| 705 | 714 | directory->returned_dirlist = directory->returned_dirlist->next; |
| 706 | | free(dirlist); |
| 715 | global_free(dirlist); |
| 707 | 716 | } |
| 708 | 717 | |
| 709 | | free(directory); |
| 718 | global_free(directory); |
| 710 | 719 | } |
| 711 | 720 | |
| 712 | 721 | |