1 martin 1.7 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.8 //
|
3 martin 1.7 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.8 //
|
10 martin 1.7 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.8 //
|
17 martin 1.7 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.8 //
|
20 martin 1.7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.8 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.7 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.8 //
|
28 martin 1.7 //////////////////////////////////////////////////////////////////////////
|
29 kumpf 1.1 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #include <Pegasus/Common/LanguageTag.h>
33 #include <Pegasus/Common/LanguageParser.h>
34 #include <Pegasus/Common/AutoPtr.h>
35 #include <Pegasus/Common/InternalException.h>
36
37 PEGASUS_NAMESPACE_BEGIN
38
39 class LanguageTagRep
40 {
41 public:
|
42 mike 1.5 LanguageTagRep() : refs(1) { }
43 AtomicInt refs;
|
44 kumpf 1.1 String tag; // complete language tag
45 String language; // language part of language tag
46 String country; // country code part of the language tag
47 String variant; // language variant part of the language tag
48 };
49
|
50 kumpf 1.6 static inline void _ref(LanguageTagRep* rep)
|
51 mike 1.5 {
52 if (rep)
|
53 kumpf 1.6 (reinterpret_cast<LanguageTagRep*>(rep))->refs++;
|
54 mike 1.5 }
55
|
56 kumpf 1.6 static inline void _unref(LanguageTagRep* rep)
|
57 mike 1.5 {
|
58 kumpf 1.6 if (rep &&
59 (reinterpret_cast<LanguageTagRep*>(rep))->refs.decAndTestIfZero())
60 {
61 delete reinterpret_cast<LanguageTagRep*>(rep);
62 }
|
63 mike 1.5 }
64
|
65 kumpf 1.1 LanguageTag::LanguageTag()
66 {
67 _rep = 0;
68 }
69
70 LanguageTag::LanguageTag(const String& languageTagString)
71 {
72 _rep = new LanguageTagRep();
73
|
74 mike 1.5 try
75 {
76 LanguageParser::parseLanguageTag(
77 languageTagString,
78 _rep->language,
79 _rep->country,
80 _rep->variant);
81 }
82 catch (...)
83 {
84 _unref(_rep);
85 throw;
86 }
|
87 kumpf 1.1
88 _rep->tag = languageTagString;
89 }
90
|
91 mike 1.5 LanguageTag::LanguageTag(const LanguageTag& x)
|
92 kumpf 1.1 {
|
93 mike 1.5 _ref(_rep = x._rep);
|
94 kumpf 1.1 }
95
96 LanguageTag::~LanguageTag()
97 {
|
98 mike 1.5 _unref(_rep);
|
99 kumpf 1.1 }
100
|
101 mike 1.5 LanguageTag& LanguageTag::operator=(const LanguageTag& x)
|
102 kumpf 1.1 {
|
103 mike 1.5 if (_rep != x._rep)
|
104 kumpf 1.1 {
|
105 mike 1.5 _unref(_rep);
106 _ref(_rep = x._rep);
107 }
|
108 kumpf 1.1
109 return *this;
110 }
111
112 String LanguageTag::getLanguage() const
113 {
|
114 marek 1.4 CheckRep(_rep);
|
115 kumpf 1.1 return _rep->language;
116 }
117
118 String LanguageTag::getCountry() const
119 {
|
120 marek 1.4 CheckRep(_rep);
|
121 kumpf 1.1 return _rep->country;
122 }
123
124 String LanguageTag::getVariant() const
125 {
|
126 marek 1.4 CheckRep(_rep);
|
127 kumpf 1.1 return _rep->variant;
128 }
129
130 String LanguageTag::toString() const
131 {
|
132 marek 1.4 CheckRep(_rep);
|
133 kumpf 1.1 return _rep->tag;
134 }
135
136 Boolean LanguageTag::operator==(const LanguageTag& languageTag) const
137 {
138 return String::equalNoCase(toString(), languageTag.toString());
139 }
140
141 Boolean LanguageTag::operator!=(const LanguageTag& languageTag) const
142 {
143 return !String::equalNoCase(toString(), languageTag.toString());
144 }
145
146 PEGASUS_NAMESPACE_END
|