diff options
Diffstat (limited to 'windows.c')
-rw-r--r-- | windows.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -124,3 +124,57 @@ void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar, divelist_font = DIVELIST_DEFAULT_FONT; gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0); } + +/* barely documented API */ +extern int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, int *); + +/* expand-convert the UTF-16 argument list to a list of UTF-8 strings */ +void subsurface_command_line_init(gint *argc, gchar ***argv) +{ + wchar_t **wargv, **wenviron; + gchar **argv_new; + gchar *s; + /* for si we assume that a struct address will equal the address + * of its first and only int member */ + gint i, n, ret, si; + + /* memory leak tools may reports a potential issue here at a call + * to strcpy_s in msvcrt, wich should be a false positive. but even if there + * is some kind of a leak, it should be unique and have the same + * lifespan as the process heap. */ + ret = __wgetmainargs(&n, &wargv, &wenviron, TRUE, &si); + if (ret < 0) { + g_warning("Cannot convert command line"); + return; + } + argv_new = g_malloc(sizeof(gchar *) * (n + 1)); + + for (i = 0; i < n; ++i) { + s = g_utf16_to_utf8((gunichar2 *)wargv[i], -1, NULL, NULL, NULL); + if (!s) { + g_warning("Cannot convert command line argument (%d) to UTF-8", (i + 1)); + s = "\0"; + } else if (!g_utf8_validate(s, -1, NULL)) { + g_warning("Cannot validate command line argument '%s' (%d)", s, (i + 1)); + g_free(s); + s = "\0"; + } + argv_new[i] = s; + } + argv_new[n] = NULL; + + /* update the argument list and count */ + if (argv && argc) { + *argv = argv_new; + *argc = n; + } +} + +/* once done, free the argument list */ +void subsurface_command_line_exit(gint *argc, gchar ***argv) +{ + int i; + for (i = 0; i < *argc; i++) + g_free((*argv)[i]); + g_free(*argv); +} |