PageRenderTime 68ms CodeModel.GetById 59ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/main.cpp

http://github.com/mozy/mordor
C++ | 43 lines | 38 code | 4 blank | 1 comment | 11 complexity | 7aa9e9642bd23d62571bb41221428083 MD5 | raw file
 1// Copyright (c) 2010 - Mozy, Inc.
 2
 3#include "main.h"
 4
 5#ifdef WINDOWS
 6namespace Mordor {
 7
 8char **CommandLineToUtf8(int argc, wchar_t **argvW)
 9{
10    DWORD totalLength = (argc + 1) * sizeof(char *);
11    DWORD wcFlags = WC_ERR_INVALID_CHARS;
12    for (int i = 0; i < argc; ++i) {
13        int ret = WideCharToMultiByte(CP_UTF8, wcFlags, argvW[i], -1, NULL, 0, NULL, NULL);
14        if (ret == 0 && GetLastError() == ERROR_INVALID_FLAGS) {
15            wcFlags = 0;
16            ret = WideCharToMultiByte(CP_UTF8, wcFlags, argvW[i], -1, NULL, 0, NULL, NULL);
17        }
18        if (ret == 0)
19            return NULL;
20        totalLength += ret;
21    }
22    char **result = (char **)LocalAlloc(LMEM_FIXED, totalLength);
23    if (!result)
24        return NULL;
25    char *strings = (char *)result + (argc + 1) * sizeof(char *);
26    totalLength -= (argc + 1) * sizeof(char *);
27    for (int i = 0; i < argc; ++i) {
28        result[i] = strings;
29        int ret = WideCharToMultiByte(CP_UTF8, wcFlags, argvW[i], -1, strings,
30            totalLength, NULL, NULL);
31        if (ret == 0) {
32            LocalFree(result);
33            return NULL;
34        }
35        strings += ret;
36        totalLength -= ret;
37    }
38    result[argc] = NULL;
39    return result;
40}
41
42}
43#endif