BPicture: Fix archive constructor.
[haiku.git] / src / kits / locale / Currency.cpp
blob86e4f34105b42ada34973273739d9d880edd1665
1 #include <Currency.h>
3 // message archive field names
4 static const char *kArchivedCurrencyCodeName = "be:currency code";
5 static const char *kArchivedDefaultSymbol = "be:default symbol";
6 static const char *kArchivedDefaultFractionDigits
7 = "be:default fraction digits";
9 // constructor
10 BCurrency::BCurrency(const BCurrency &other)
11 : fCurrencyCode(),
12 fDefaultSymbol(),
13 fDefaultFractionDigits(B_NO_INIT)
15 *this = other;
18 // constructor
19 BCurrency::BCurrency(BMessage *archive)
20 : fCurrencyCode(),
21 fDefaultSymbol(),
22 fDefaultFractionDigits(B_NO_INIT)
24 if (archive->FindString(kArchivedCurrencyCodeName, &fCurrencyCode)
25 == B_OK
26 && archive->FindString(kArchivedDefaultSymbol, &fDefaultSymbol)
27 == B_OK
28 && archive->FindInt32(kArchivedDefaultFractionDigits,
29 &fDefaultFractionDigits) == B_OK
30 && _CheckData()) {
31 // everything went fine
32 } else
33 _Unset(B_NO_INIT);
36 // constructor
37 BCurrency::BCurrency(const char *currencyCode)
39 // TODO: load currency from disk
42 // destructor
43 BCurrency::~BCurrency()
47 // InitCheck
48 status_t
49 BCurrency::InitCheck() const
51 return (fDefaultFractionDigits < 0 ? fDefaultFractionDigits : B_OK);
54 // Archive
55 status_t
56 BCurrency::Archive(BMessage *archive, bool deep) const
58 status_t error = BArchivable::Archive(archive, deep);
59 if (error == B_OK) {
60 if (archive->AddString(kArchivedCurrencyCodeName, fCurrencyCode)
61 == B_OK
62 && archive->AddString(kArchivedDefaultSymbol, fDefaultSymbol)
63 == B_OK
64 && archive->AddInt32(kArchivedDefaultFractionDigits,
65 fDefaultFractionDigits) == B_OK) {
66 // everything went fine
67 } else
68 error = B_ERROR;
70 return error;
73 // Instantiate
74 _EXPORT
75 BArchivable *
76 BCurrency::Instantiate(BMessage *archive)
78 if (!validate_instantiation(archive, "BCurrency"))
79 return NULL;
80 return new BCurrency(archive);
83 // CurrencyCode
84 const char *
85 BCurrency::CurrencyCode() const
87 return (InitCheck() == B_OK ? fCurrencyCode.String() : NULL);
90 // DefaultSymbol
91 const char *
92 BCurrency::DefaultSymbol() const
94 return (InitCheck() == B_OK ? fDefaultSymbol.String() : NULL);
97 // DefaultFractionDigits
98 int32
99 BCurrency::DefaultFractionDigits() const
101 return (InitCheck() == B_OK ? fDefaultFractionDigits : 0);
104 // GetSymbol
105 status_t
106 BCurrency::GetSymbol(char *symbol, size_t maxSize, BLocale *locale)
108 // check initialization and parameters
109 if (InitCheck() != B_OK)
110 return B_NO_INIT;
111 if (symbol == NULL)
112 return B_BAD_VALUE;
113 // TODO: get symbol from locale
114 // fall back to the default symbol
115 if ((int32)maxSize <= fDefaultSymbol.Length())
116 return EOVERFLOW; // OpenBeOS: B_BUFFER_OVERFLOW
117 strcpy(symbol, fDefaultSymbol.String());
118 return B_OK;
121 // GetSymbol
122 status_t
123 BCurrency::GetSymbol(BString *symbol, BLocale *locale)
125 // check initialization and parameters
126 if (InitCheck() != B_OK)
127 return B_NO_INIT;
128 if (symbol == NULL)
129 return B_BAD_VALUE;
130 // TODO: get symbol from locale
131 // fall back to the default symbol
132 *symbol = fDefaultSymbol;
133 if (symbol->Length() != fDefaultSymbol.Length())
134 return B_NO_MEMORY;
135 return B_OK;
138 // =
139 BCurrency &
140 BCurrency::operator=(const BCurrency &other)
142 if (other.InitCheck() == B_OK) {
143 fCurrencyCode = other.fCurrencyCode;
144 fDefaultSymbol = other.fDefaultSymbol;
145 fDefaultFractionDigits = other.fDefaultFractionDigits;
146 if (!_CheckData())
147 _Unset(B_NO_MEMORY);
148 } else
149 _Unset(B_NO_MEMORY);
150 return *this;
153 // ==
154 bool
155 BCurrency::operator==(const BCurrency &other) const
157 if (InitCheck() != B_OK || other.InitCheck() != B_OK)
158 return false;
159 return (fCurrencyCode == other.fCurrencyCode);
162 // !=
163 bool
164 BCurrency::operator!=(const BCurrency &other) const
166 return !(*this == other);
169 // constructor
170 BCurrency::BCurrency()
172 // privatized to make it unaccessible
175 // _CheckData
176 bool
177 BCurrency::_CheckData() const
179 return (fDefaultFractionDigits >= 0
180 && fCurrencyCode.Length() > 0
181 && fDefaultSymbol.Length() == 0);
184 // _Unset
185 void
186 BCurrency::_Unset(status_t error)
188 fCurrencyCode.Truncate(0);
189 fDefaultSymbol.Truncate(0);
190 fDefaultFractionDigits = error;