Bug 1861963 [wpt PR 42839] - Fix accessing update_properties for product, a=testonly
[gecko.git] / xpcom / io / nsNativeCharsetUtils.cpp
blob847ae3666c1e0a8a18cf73317193149c83a52037
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 //-----------------------------------------------------------------------------
8 // Non-Windows
9 //-----------------------------------------------------------------------------
10 #ifndef XP_WIN
12 # include "nsAString.h"
13 # include "nsReadableUtils.h"
14 # include "nsString.h"
16 nsresult NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) {
17 CopyUTF8toUTF16(aInput, aOutput);
18 return NS_OK;
21 nsresult NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput) {
22 CopyUTF16toUTF8(aInput, aOutput);
23 return NS_OK;
26 //-----------------------------------------------------------------------------
27 // XP_WIN
28 //-----------------------------------------------------------------------------
29 #else
31 # include <windows.h>
32 # include "nsString.h"
33 # include "nsAString.h"
34 # include "nsReadableUtils.h"
36 using namespace mozilla;
38 nsresult NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) {
39 uint32_t inputLen = aInput.Length();
41 nsACString::const_iterator iter;
42 aInput.BeginReading(iter);
44 const char* buf = iter.get();
46 // determine length of result
47 uint32_t resultLen = 0;
48 int n = ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, nullptr, 0);
49 if (n > 0) {
50 resultLen += n;
53 // allocate sufficient space
54 if (!aOutput.SetLength(resultLen, fallible)) {
55 return NS_ERROR_OUT_OF_MEMORY;
57 if (resultLen > 0) {
58 char16ptr_t result = aOutput.BeginWriting();
59 ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, result, resultLen);
61 return NS_OK;
64 nsresult NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput) {
65 uint32_t inputLen = aInput.Length();
67 nsAString::const_iterator iter;
68 aInput.BeginReading(iter);
70 char16ptr_t buf = iter.get();
72 // determine length of result
73 uint32_t resultLen = 0;
75 int n = ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, nullptr, 0, nullptr,
76 nullptr);
77 if (n > 0) {
78 resultLen += n;
81 // allocate sufficient space
82 if (!aOutput.SetLength(resultLen, fallible)) {
83 return NS_ERROR_OUT_OF_MEMORY;
85 if (resultLen > 0) {
86 char* result = aOutput.BeginWriting();
88 // default "defaultChar" is '?', which is an illegal character on windows
89 // file system. That will cause file uncreatable. Change it to '_'
90 const char defaultChar = '_';
92 ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, result, resultLen,
93 &defaultChar, nullptr);
95 return NS_OK;
98 #endif