vfs: check userland buffers before reading them.
[haiku.git] / src / kits / locale / Locale.cpp
blob34f702f1649f942c6374925c0059a13f49e2f44d
1 /*
2 * Copyright 2003, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de.
4 * Copyright 2012, John Scipione, jscipione@gmail.com
5 * All rights reserved. Distributed under the terms of the MIT License.
6 */
9 #include <Autolock.h>
10 #include <Catalog.h>
11 #include <Locale.h>
12 #include <LocaleRoster.h>
15 BLocale::BLocale(const BLanguage* language,
16 const BFormattingConventions* conventions)
18 if (conventions != NULL)
19 fConventions = *conventions;
20 else
21 BLocale::Default()->GetFormattingConventions(&fConventions);
23 if (language != NULL)
24 fLanguage = *language;
25 else
26 BLocale::Default()->GetLanguage(&fLanguage);
30 BLocale::BLocale(const BLocale& other)
32 fConventions(other.fConventions),
33 fLanguage(other.fLanguage)
38 /*static*/ const BLocale*
39 BLocale::Default()
41 return BLocaleRoster::Default()->GetDefaultLocale();
45 BLocale&
46 BLocale::operator=(const BLocale& other)
48 if (this == &other)
49 return *this;
51 BAutolock lock(fLock);
52 BAutolock otherLock(other.fLock);
53 if (!lock.IsLocked() || !otherLock.IsLocked())
54 return *this;
56 fConventions = other.fConventions;
57 fLanguage = other.fLanguage;
59 return *this;
63 BLocale::~BLocale()
68 status_t
69 BLocale::GetCollator(BCollator* collator) const
71 if (!collator)
72 return B_BAD_VALUE;
74 BAutolock lock(fLock);
75 if (!lock.IsLocked())
76 return B_ERROR;
78 *collator = fCollator;
80 return B_OK;
84 status_t
85 BLocale::GetLanguage(BLanguage* language) const
87 if (!language)
88 return B_BAD_VALUE;
90 BAutolock lock(fLock);
91 if (!lock.IsLocked())
92 return B_ERROR;
94 *language = fLanguage;
96 return B_OK;
100 status_t
101 BLocale::GetFormattingConventions(BFormattingConventions* conventions) const
103 if (!conventions)
104 return B_BAD_VALUE;
106 BAutolock lock(fLock);
107 if (!lock.IsLocked())
108 return B_ERROR;
110 *conventions = fConventions;
112 return B_OK;
116 const char *
117 BLocale::GetString(uint32 id) const
119 // Note: this code assumes a certain order of the string bases
121 BAutolock lock(fLock);
122 if (!lock.IsLocked())
123 return "";
125 if (id >= B_OTHER_STRINGS_BASE) {
126 if (id == B_CODESET)
127 return "UTF-8";
129 return "";
131 return fLanguage.GetString(id);
135 void
136 BLocale::SetFormattingConventions(const BFormattingConventions& conventions)
138 BAutolock lock(fLock);
139 if (!lock.IsLocked())
140 return;
142 fConventions = conventions;
146 void
147 BLocale::SetCollator(const BCollator& newCollator)
149 BAutolock lock(fLock);
150 if (!lock.IsLocked())
151 return;
153 fCollator = newCollator;
157 void
158 BLocale::SetLanguage(const BLanguage& newLanguage)
160 BAutolock lock(fLock);
161 if (!lock.IsLocked())
162 return;
164 fLanguage = newLanguage;