| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | askf_os.cpp - OS-dialog-based implementation of askfile |
| 15 | Function |
| 16 | Implements file dialog using the OS-provided file dialog |
| 17 | Notes |
| 18 | Only one of askf_tx.c or askf_os.c should be included in a given |
| 19 | executable. For a text-only version, include askf_tx. For a version |
| 20 | where os_askfile() provides a file dialog, use askf_os instead. |
| 21 | |
| 22 | We provide a choice of tio_askfile() implementations in the portable |
| 23 | code (rather than only through the OS code) so that we can call |
| 24 | the formatted text output routines in this version. An OS-layer |
| 25 | implementation could not call the formatted output routines (it would |
| 26 | have to call os_printf directly), which would result in poor prompt |
| 27 | formatting any time a prompt exceeded a single line of text. |
| 28 | Modified |
| 29 | 09/27/99 MJRoberts - Creation |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include "os.h" |
| 34 | #include "t3std.h" |
| 35 | #include "vmglob.h" |
| 36 | #include "vmconsol.h" |
| 37 | #include "charmap.h" |
| 38 | |
| 39 | /* |
| 40 | * formatted text-only file prompt |
| 41 | */ |
| 42 | int CVmConsole::askfile(VMG_ const char *prompt, size_t prompt_len, |
| 43 | char *reply, size_t replen, |
| 44 | int dialog_type, os_filetype_t file_type, |
| 45 | int /*bypass_script*/) |
| 46 | { |
| 47 | int result; |
| 48 | size_t prompt_ui_len; |
| 49 | char prompt_ui[256]; |
| 50 | char fname[OSFNMAX]; |
| 51 | |
| 52 | /* flush any pending output */ |
| 53 | flush(vmg_ VM_NL_INPUT); |
| 54 | |
| 55 | /* translate the prompt to the local UI character set */ |
| 56 | prompt_ui_len = G_cmap_to_ui->map_utf8(prompt_ui, sizeof(prompt_ui) - 1, |
| 57 | prompt, prompt_len, 0); |
| 58 | |
| 59 | /* null-terminate the translated prompt */ |
| 60 | prompt_ui[prompt_ui_len] = '\0'; |
| 61 | |
| 62 | /* let the system handle it */ |
| 63 | result = os_askfile(prompt, fname, sizeof(fname), |
| 64 | dialog_type, file_type); |
| 65 | |
| 66 | /* if we were successful, translate the filename to UTF-8 */ |
| 67 | if (result == OS_AFE_SUCCESS) |
| 68 | { |
| 69 | char *dst; |
| 70 | size_t dstlen; |
| 71 | |
| 72 | /* |
| 73 | * The result string is in the local UI character set, so we |
| 74 | * must translate it to UTF-8 for the caller. Note that we |
| 75 | * reserve one byte of the result buffer for the null |
| 76 | * terminator. |
| 77 | */ |
| 78 | dst = reply; |
| 79 | dstlen = replen - 1; |
| 80 | G_cmap_from_ui->map(&dst, &dstlen, fname, strlen(fname)); |
| 81 | |
| 82 | /* null-terminate the result */ |
| 83 | *dst = '\0'; |
| 84 | } |
| 85 | |
| 86 | /* return the result */ |
| 87 | return result; |
| 88 | } |
| 89 |