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 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
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
);
21 nsresult
NS_CopyUnicodeToNative(const nsAString
& aInput
, nsACString
& aOutput
) {
22 CopyUTF16toUTF8(aInput
, aOutput
);
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
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);
53 // allocate sufficient space
54 if (!aOutput
.SetLength(resultLen
, fallible
)) {
55 return NS_ERROR_OUT_OF_MEMORY
;
58 char16ptr_t result
= aOutput
.BeginWriting();
59 ::MultiByteToWideChar(CP_ACP
, 0, buf
, inputLen
, result
, resultLen
);
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,
81 // allocate sufficient space
82 if (!aOutput
.SetLength(resultLen
, fallible
)) {
83 return NS_ERROR_OUT_OF_MEMORY
;
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);