/src/mozilla-central/xpcom/io/nsNativeCharsetUtils.cpp
Line | Count | Source |
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/. */ |
6 | | |
7 | | #include "xpcom-private.h" |
8 | | |
9 | | //----------------------------------------------------------------------------- |
10 | | // Non-Windows |
11 | | //----------------------------------------------------------------------------- |
12 | | #ifndef XP_WIN |
13 | | |
14 | | #include "nsAString.h" |
15 | | #include "nsReadableUtils.h" |
16 | | #include "nsString.h" |
17 | | |
18 | | nsresult |
19 | | NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) |
20 | 370 | { |
21 | 370 | CopyUTF8toUTF16(aInput, aOutput); |
22 | 370 | return NS_OK; |
23 | 370 | } |
24 | | |
25 | | nsresult |
26 | | NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput) |
27 | 376 | { |
28 | 376 | CopyUTF16toUTF8(aInput, aOutput); |
29 | 376 | return NS_OK; |
30 | 376 | } |
31 | | |
32 | | //----------------------------------------------------------------------------- |
33 | | // XP_WIN |
34 | | //----------------------------------------------------------------------------- |
35 | | #else |
36 | | |
37 | | #include <windows.h> |
38 | | #include "nsString.h" |
39 | | #include "nsAString.h" |
40 | | #include "nsReadableUtils.h" |
41 | | |
42 | | using namespace mozilla; |
43 | | |
44 | | nsresult |
45 | | NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) |
46 | | { |
47 | | uint32_t inputLen = aInput.Length(); |
48 | | |
49 | | nsACString::const_iterator iter; |
50 | | aInput.BeginReading(iter); |
51 | | |
52 | | const char* buf = iter.get(); |
53 | | |
54 | | // determine length of result |
55 | | uint32_t resultLen = 0; |
56 | | int n = ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, nullptr, 0); |
57 | | if (n > 0) { |
58 | | resultLen += n; |
59 | | } |
60 | | |
61 | | // allocate sufficient space |
62 | | if (!aOutput.SetLength(resultLen, fallible)) { |
63 | | return NS_ERROR_OUT_OF_MEMORY; |
64 | | } |
65 | | if (resultLen > 0) { |
66 | | char16ptr_t result = aOutput.BeginWriting(); |
67 | | ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, result, resultLen); |
68 | | } |
69 | | return NS_OK; |
70 | | } |
71 | | |
72 | | nsresult |
73 | | NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput) |
74 | | { |
75 | | uint32_t inputLen = aInput.Length(); |
76 | | |
77 | | nsAString::const_iterator iter; |
78 | | aInput.BeginReading(iter); |
79 | | |
80 | | char16ptr_t buf = iter.get(); |
81 | | |
82 | | // determine length of result |
83 | | uint32_t resultLen = 0; |
84 | | |
85 | | int n = ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, nullptr, 0, |
86 | | nullptr, nullptr); |
87 | | if (n > 0) { |
88 | | resultLen += n; |
89 | | } |
90 | | |
91 | | // allocate sufficient space |
92 | | if (!aOutput.SetLength(resultLen, fallible)) { |
93 | | return NS_ERROR_OUT_OF_MEMORY; |
94 | | } |
95 | | if (resultLen > 0) { |
96 | | char* result = aOutput.BeginWriting(); |
97 | | |
98 | | // default "defaultChar" is '?', which is an illegal character on windows |
99 | | // file system. That will cause file uncreatable. Change it to '_' |
100 | | const char defaultChar = '_'; |
101 | | |
102 | | ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, result, resultLen, |
103 | | &defaultChar, nullptr); |
104 | | } |
105 | | return NS_OK; |
106 | | } |
107 | | |
108 | | #endif |