intl/locale/src/nsCharsetAlias.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bcde1166e909
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "mozilla/ArrayUtils.h"
7
8 #include "nsCharsetAlias.h"
9
10 // for NS_ERROR_UCONV_NOCONV
11 #include "nsCharsetConverterManager.h"
12
13 #include "nsUConvPropertySearch.h"
14
15 using namespace mozilla;
16
17 //
18 static const char* kAliases[][3] = {
19 #include "charsetalias.properties.h"
20 };
21
22 //--------------------------------------------------------------
23 // static
24 nsresult
25 nsCharsetAlias::GetPreferredInternal(const nsACString& aAlias,
26 nsACString& oResult)
27 {
28 nsAutoCString key(aAlias);
29 ToLowerCase(key);
30
31 return nsUConvPropertySearch::SearchPropertyValue(kAliases,
32 ArrayLength(kAliases), key, oResult);
33 }
34
35 //--------------------------------------------------------------
36 // static
37 nsresult
38 nsCharsetAlias::GetPreferred(const nsACString& aAlias,
39 nsACString& oResult)
40 {
41 if (aAlias.IsEmpty()) return NS_ERROR_NULL_POINTER;
42
43 nsresult res = GetPreferredInternal(aAlias, oResult);
44 if (NS_FAILED(res))
45 return res;
46
47 if (nsCharsetConverterManager::IsInternal(oResult))
48 return NS_ERROR_UCONV_NOCONV;
49
50 return res;
51 }
52
53 //--------------------------------------------------------------
54 // static
55 nsresult
56 nsCharsetAlias::Equals(const nsACString& aCharset1,
57 const nsACString& aCharset2, bool* oResult)
58 {
59 nsresult res = NS_OK;
60
61 if(aCharset1.Equals(aCharset2, nsCaseInsensitiveCStringComparator())) {
62 *oResult = true;
63 return res;
64 }
65
66 if(aCharset1.IsEmpty() || aCharset2.IsEmpty()) {
67 *oResult = false;
68 return res;
69 }
70
71 *oResult = false;
72 nsAutoCString name1;
73 res = GetPreferredInternal(aCharset1, name1);
74 if (NS_FAILED(res))
75 return res;
76
77 nsAutoCString name2;
78 res = GetPreferredInternal(aCharset2, name2);
79 if (NS_FAILED(res))
80 return res;
81
82 *oResult = name1.Equals(name2);
83 return NS_OK;
84 }

mercurial