1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/locale/src/nsCharsetAlias.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,84 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/ArrayUtils.h" 1.10 + 1.11 +#include "nsCharsetAlias.h" 1.12 + 1.13 +// for NS_ERROR_UCONV_NOCONV 1.14 +#include "nsCharsetConverterManager.h" 1.15 + 1.16 +#include "nsUConvPropertySearch.h" 1.17 + 1.18 +using namespace mozilla; 1.19 + 1.20 +// 1.21 +static const char* kAliases[][3] = { 1.22 +#include "charsetalias.properties.h" 1.23 +}; 1.24 + 1.25 +//-------------------------------------------------------------- 1.26 +// static 1.27 +nsresult 1.28 +nsCharsetAlias::GetPreferredInternal(const nsACString& aAlias, 1.29 + nsACString& oResult) 1.30 +{ 1.31 + nsAutoCString key(aAlias); 1.32 + ToLowerCase(key); 1.33 + 1.34 + return nsUConvPropertySearch::SearchPropertyValue(kAliases, 1.35 + ArrayLength(kAliases), key, oResult); 1.36 +} 1.37 + 1.38 +//-------------------------------------------------------------- 1.39 +// static 1.40 +nsresult 1.41 +nsCharsetAlias::GetPreferred(const nsACString& aAlias, 1.42 + nsACString& oResult) 1.43 +{ 1.44 + if (aAlias.IsEmpty()) return NS_ERROR_NULL_POINTER; 1.45 + 1.46 + nsresult res = GetPreferredInternal(aAlias, oResult); 1.47 + if (NS_FAILED(res)) 1.48 + return res; 1.49 + 1.50 + if (nsCharsetConverterManager::IsInternal(oResult)) 1.51 + return NS_ERROR_UCONV_NOCONV; 1.52 + 1.53 + return res; 1.54 +} 1.55 + 1.56 +//-------------------------------------------------------------- 1.57 +// static 1.58 +nsresult 1.59 +nsCharsetAlias::Equals(const nsACString& aCharset1, 1.60 + const nsACString& aCharset2, bool* oResult) 1.61 +{ 1.62 + nsresult res = NS_OK; 1.63 + 1.64 + if(aCharset1.Equals(aCharset2, nsCaseInsensitiveCStringComparator())) { 1.65 + *oResult = true; 1.66 + return res; 1.67 + } 1.68 + 1.69 + if(aCharset1.IsEmpty() || aCharset2.IsEmpty()) { 1.70 + *oResult = false; 1.71 + return res; 1.72 + } 1.73 + 1.74 + *oResult = false; 1.75 + nsAutoCString name1; 1.76 + res = GetPreferredInternal(aCharset1, name1); 1.77 + if (NS_FAILED(res)) 1.78 + return res; 1.79 + 1.80 + nsAutoCString name2; 1.81 + res = GetPreferredInternal(aCharset2, name2); 1.82 + if (NS_FAILED(res)) 1.83 + return res; 1.84 + 1.85 + *oResult = name1.Equals(name2); 1.86 + return NS_OK; 1.87 +}