Locale.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/Locale.cpp#11 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/common/Locale.h"
00025 
00026 #include <string>
00027 #include <cassert>
00028 
00029 using namespace std;
00030 
00031 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/common/Locale.cpp#11 $");
00032 
00033 
00034 #define DEFAULT_LOCALE_DISPLAY ("[default]")
00035 
00036 
00037 // TODO: base this on the system
00038 Locale Locale::DEFAULT = Locale("en", "US");
00039 
00040 
00041 Locale::Locale(const string &language)
00042     : _lang(language)
00043 {
00044     assert(language.empty() || language.length() == 2);
00045 }
00046 
00047 Locale::Locale(const string &language, const string &country)
00048     : _lang(language), _country(country)
00049 {
00050     // either both language and country are empty or language must be non-empty
00051     assert((language.empty() && country.empty()) || !language.empty());
00052 
00053     assert(language.empty() || language.length() == 2);
00054     assert(country.empty() || country.length() == 2);
00055 }
00056 
00057 Locale::Locale(
00058     const string &language,
00059     const string &country,
00060     const string &variant)
00061     : _lang(language), _country(country), _variant(variant)
00062 {
00063     // either language, country and variant are empty or language must
00064     // be non-empty
00065     assert((language.empty() && country.empty() && variant.empty()) ||
00066            !language.empty());
00067 
00068     assert(language.empty() || language.length() == 2);
00069     assert(country.empty() || country.length() == 2);
00070 }
00071 
00072 Locale::~Locale()
00073 {
00074 }
00075 
00076 bool Locale::operator==(const Locale &rhs) const
00077 {
00078     return (_lang == rhs._lang &&
00079             _country == rhs._country &&
00080             _variant == rhs._variant);
00081 }
00082 
00083 bool Locale::operator!=(const Locale &rhs) const {
00084     return (_lang != rhs._lang ||
00085             _country != rhs._country ||
00086             _variant != rhs._variant);
00087 }
00088 
00089 bool Locale::operator<(const Locale &rhs) const
00090 {
00091     int langCmp = this->getLanguage().compare(rhs.getLanguage());
00092     if (langCmp < 0) {
00093         return true;
00094     }
00095     if (langCmp > 0) {
00096         return false;
00097     }
00098 
00099     // same language, check country
00100     int countryCmp = this->getCountry().compare(rhs.getCountry());
00101     if (countryCmp < 0) {
00102         return true;
00103     }
00104     if (countryCmp > 0) {
00105         return false;
00106     }
00107 
00108     // same country, check variant
00109     int variantCmp = this->getVariant().compare(rhs.getVariant());
00110     if (variantCmp < 0) {
00111         return true;
00112     }
00113     return false;
00114 }
00115 
00116 const string &Locale::getLanguage() const
00117 {
00118     return _lang;
00119 }
00120 
00121 const string &Locale::getCountry() const
00122 {
00123     return _country;
00124 }
00125 
00126 const string &Locale::getVariant() const
00127 {
00128     return _variant;
00129 }
00130 
00131 string Locale::getDisplayName() const
00132 {
00133     if (_lang.empty()) {
00134         return DEFAULT_LOCALE_DISPLAY;
00135     }
00136 
00137     return
00138         _lang
00139         + (_country.empty() ? "" : (string("_") + _country))
00140         + (_variant.empty() ? "" : (string("_") + _variant));
00141 }
00142 
00143 bool Locale::hasParentLocale() const
00144 {
00145     return !(_country.empty() && _variant.empty());
00146 }
00147 
00148 Locale Locale::getParentLocale() const
00149 {
00150     if (hasParentLocale()) {
00151         // one or both of _country and _variant is non-empty
00152         if (_country.empty() ^ _variant.empty()) {
00153             return Locale(_lang);
00154         }
00155 
00156         return Locale(_lang, _country);
00157     } else {
00158         // TODO: gotta give them something -- maybe a locale with no language?
00159         return *this;
00160     }
00161 }
00162 
00163 const Locale &Locale::getDefault()
00164 {
00165     return DEFAULT;
00166 }
00167 
00168 void Locale::setDefault(const Locale &locale)
00169 {
00170     DEFAULT = locale;
00171 }
00172 
00173 ostream &operator<<(ostream &str, const Locale &loc)
00174 {
00175     return str << loc.getDisplayName();
00176 }
00177 
00178 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/common/Locale.cpp#11 $");
00179 
00180 // End Locale.cpp

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1