xref: /haiku/headers/os/locale/Locale.h (revision a906d0a031e721e2f2ec9d95274103e74a3a774f)
1 /*
2  * Copyright 2003-2010, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _B_LOCALE_H_
6 #define _B_LOCALE_H_
7 
8 
9 #include <Collator.h>
10 #include <FormattingConventions.h>
11 #include <Language.h>
12 #include <Locker.h>
13 
14 
15 namespace icu_44 {
16 	class DateFormat;
17 }
18 
19 
20 class BCatalog;
21 class BString;
22 class BTimeZone;
23 
24 
25 typedef enum {
26 	B_DATE_ELEMENT_INVALID = B_BAD_DATA,
27 	B_DATE_ELEMENT_YEAR = 0,
28 	B_DATE_ELEMENT_MONTH,
29 	B_DATE_ELEMENT_DAY,
30 	B_DATE_ELEMENT_AM_PM,
31 	B_DATE_ELEMENT_HOUR,
32 	B_DATE_ELEMENT_MINUTE,
33 	B_DATE_ELEMENT_SECOND
34 } BDateElement;
35 
36 typedef enum {
37 	B_NUMBER_ELEMENT_INVALID = B_BAD_DATA,
38 	B_NUMBER_ELEMENT_INTEGER = 0,
39 	B_NUMBER_ELEMENT_FRACTIONAL,
40 	B_NUMBER_ELEMENT_CURRENCY
41 } BNumberElement;
42 
43 
44 class BLocale {
45 public:
46 								BLocale(const BLanguage* language = NULL,
47 									const BFormattingConventions* conventions
48 										= NULL);
49 								BLocale(const BLocale& other);
50 								~BLocale();
51 
52 	static	const BLocale*		Default();
53 
54 			BLocale&			operator=(const BLocale& other);
55 
56 			status_t			GetCollator(BCollator* collator) const;
57 			status_t			GetLanguage(BLanguage* language) const;
58 			status_t			GetFormattingConventions(
59 									BFormattingConventions* conventions) const;
60 
61 			void				SetFormattingConventions(
62 									const BFormattingConventions& conventions);
63 			void				SetCollator(const BCollator& newCollator);
64 			void				SetLanguage(const BLanguage& newLanguage);
65 
66 			// see definitions in LocaleStrings.h
67 			const char*			GetString(uint32 id) const;
68 
69 			void				FormatString(char* target, size_t maxSize,
70 									char* fmt, ...) const;
71 			void				FormatString(BString* buffer, char* fmt,
72 									...) const;
73 
74 								// DateTime
75 
76 								// TODO: drop some of these once BDateTimeFormat
77 								//       has been implemented!
78 			ssize_t				FormatDateTime(char* target, size_t maxSize,
79 									time_t time, BDateFormatStyle dateStyle,
80 									BTimeFormatStyle timeStyle) const;
81 			status_t			FormatDateTime(BString* buffer, time_t time,
82 									BDateFormatStyle dateStyle,
83 									BTimeFormatStyle timeStyle,
84 									const BTimeZone* timeZone = NULL) const;
85 
86 								// Date
87 
88 								// TODO: drop some of these once BDateFormat
89 								//       has been implemented!
90 			ssize_t				FormatDate(char* string, size_t maxSize,
91 									time_t time, BDateFormatStyle style) const;
92 			status_t			FormatDate(BString* string, time_t time,
93 									BDateFormatStyle style,
94 									const BTimeZone* timeZone = NULL) const;
95 			status_t			FormatDate(BString* string,
96 									int*& fieldPositions, int& fieldCount,
97 									time_t time, BDateFormatStyle style) const;
98 			status_t			GetDateFields(BDateElement*& fields,
99 									int& fieldCount, BDateFormatStyle style
100 									) const;
101 
102 			int					StartOfWeek() const;
103 
104 								// Time
105 
106 								// TODO: drop some of these once BTimeFormat
107 								//       has been implemented!
108 			ssize_t				FormatTime(char* string, size_t maxSize,
109 									time_t time, BTimeFormatStyle style) const;
110 			status_t			FormatTime(BString* string, time_t time,
111 									BTimeFormatStyle style,
112 									const BTimeZone* timeZone = NULL) const;
113 			status_t			FormatTime(BString* string,
114 									int*& fieldPositions, int& fieldCount,
115 									time_t time, BTimeFormatStyle style) const;
116 			status_t			GetTimeFields(BDateElement*& fields,
117 									int& fieldCount, BTimeFormatStyle style
118 									) const;
119 
120 								// numbers
121 
122 			ssize_t				FormatNumber(char* string, size_t maxSize,
123 									double value) const;
124 			status_t			FormatNumber(BString* string,
125 									double value) const;
126 			ssize_t				FormatNumber(char* string, size_t maxSize,
127 									int32 value) const;
128 			status_t			FormatNumber(BString* string,
129 									int32 value) const;
130 
131 								// monetary
132 
133 			ssize_t				FormatMonetary(char* string, size_t maxSize,
134 									double value) const;
135 			status_t			FormatMonetary(BString* string,
136 									double value) const;
137 
138 			// Collator short-hands
139 			int					StringCompare(const char* s1,
140 									const char* s2) const;
141 			int					StringCompare(const BString* s1,
142 									const BString* s2) const;
143 
144 			void				GetSortKey(const char* string,
145 									BString* sortKey) const;
146 
147 private:
148 			icu_44::DateFormat*	_CreateDateFormatter(
149 									const BString& format) const;
150 			icu_44::DateFormat*	_CreateTimeFormatter(
151 									const BString& format) const;
152 
153 	mutable	BLocker				fLock;
154 			BCollator			fCollator;
155 			BFormattingConventions	fConventions;
156 			BLanguage			fLanguage;
157 };
158 
159 
160 //--- collator short-hands inlines ---
161 //	#pragma mark -
162 
163 inline int
164 BLocale::StringCompare(const char* s1, const char* s2) const
165 {
166 	return fCollator.Compare(s1, s2);
167 }
168 
169 
170 inline int
171 BLocale::StringCompare(const BString* s1, const BString* s2) const
172 {
173 	return fCollator.Compare(s1->String(), s2->String());
174 }
175 
176 
177 inline void
178 BLocale::GetSortKey(const char* string, BString* sortKey) const
179 {
180 	fCollator.GetSortKey(string, sortKey);
181 }
182 
183 
184 #endif	/* _B_LOCALE_H_ */
185