Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

localemgr.cpp

00001 /******************************************************************************
00002  *  localemgr.cpp - implementation of class LocaleMgr used to interact with
00003  *                              registered locales for a sword installation
00004  *
00005  * $Id: localemgr_8cpp-source.html,v 1.3 2002/06/20 20:23:09 mgruner Exp $
00006  *
00007  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00008  *      CrossWire Bible Society
00009  *      P. O. Box 2528
00010  *      Tempe, AZ  85280-2528
00011  *
00012  * This program is free software; you can redistribute it and/or modify it
00013  * under the terms of the GNU General Public License as published by the
00014  * Free Software Foundation version 2.
00015  *
00016  * This program is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * General Public License for more details.
00020  *
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <fcntl.h>
00026 
00027 #ifndef __GNUC__
00028 #include <io.h>
00029 #else
00030 #include <unistd.h>
00031 #include <unixstr.h>
00032 #endif
00033 #include <sys/stat.h>
00034 #include <dirent.h>
00035 
00036 #include <swmgr.h>
00037 #include <utilfuns.h>
00038 
00039 #include <localemgr.h>
00040 #include <filemgr.h>
00041 
00042 
00043 LocaleMgr LocaleMgr::systemLocaleMgr;
00044 
00045 
00046 LocaleMgr::LocaleMgr(const char *iConfigPath) {
00047         char *prefixPath = 0;
00048         char *configPath = 0;
00049         char configType = 0;
00050         string path;
00051         
00052         defaultLocaleName = 0;
00053         
00054         char *lang = getenv ("LANG");
00055         if (lang) {
00056                 if (strlen(lang) > 0)
00057                         setDefaultLocaleName(lang);
00058                 else setDefaultLocaleName("en_us");
00059         }
00060         else setDefaultLocaleName("en_us");
00061 
00062         if (!iConfigPath)
00063                 SWMgr::findConfig(&configType, &prefixPath, &configPath);
00064         else configPath = (char *)iConfigPath;
00065 
00066         if (prefixPath) {
00067                 switch (configType) {
00068                 case 2:
00069                         int i;
00070                         for (i = strlen(configPath)-1; ((i) && (configPath[i] != '/') && (configPath[i] != '\\')); i--);
00071                         configPath[i] = 0;
00072                         path = configPath;
00073                         path += "/";
00074                         break;
00075                 default:
00076                         path = prefixPath;
00077                         if ((prefixPath[strlen(prefixPath)-1] != '\\') && (prefixPath[strlen(prefixPath)-1] != '/'))
00078                                 path += "/";
00079 
00080                         break;
00081                 }
00082                 if (FileMgr::existsDir(path.c_str(), "locales.d")) {
00083                         path += "locales.d";
00084                         loadConfigDir(path.c_str());
00085                 }
00086         }
00087 
00088         if (prefixPath)
00089                 delete [] prefixPath;
00090 
00091         if (configPath)
00092                 delete [] configPath;
00093 }
00094 
00095 
00096 LocaleMgr::~LocaleMgr() {
00097         if (defaultLocaleName)
00098                 delete [] defaultLocaleName;
00099      deleteLocales();
00100 }
00101 
00102 
00103 void LocaleMgr::loadConfigDir(const char *ipath) {
00104         DIR *dir;
00105         struct dirent *ent;
00106         string newmodfile;
00107         LocaleMap::iterator it;
00108  
00109         if ((dir = opendir(ipath))) {
00110                 rewinddir(dir);
00111                 while ((ent = readdir(dir))) {
00112                         if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
00113                                 newmodfile = ipath;
00114                                 if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
00115                                         newmodfile += "/";
00116                                 newmodfile += ent->d_name;
00117                                 SWLocale *locale = new SWLocale(newmodfile.c_str());
00118                                 if (locale->getName()) {
00119                                         it = locales.find(locale->getName());
00120                                         if (it != locales.end()) {
00121                                                 *((*it).second) += *locale;
00122                                                 delete locale;
00123                                         }
00124                                         else locales.insert(LocaleMap::value_type(locale->getName(), locale));
00125                                 }
00126                     else        delete locale;
00127                         }
00128                 }
00129                 closedir(dir);
00130         }
00131 }
00132 
00133 
00134 void LocaleMgr::deleteLocales() {
00135 
00136         LocaleMap::iterator it;
00137 
00138         for (it = locales.begin(); it != locales.end(); it++)
00139                 delete (*it).second;
00140 
00141         locales.erase(locales.begin(), locales.end());
00142 }
00143 
00144 
00145 SWLocale *LocaleMgr::getLocale(const char *name) {
00146         LocaleMap::iterator it;
00147 
00148         it = locales.find(name);
00149         if (it != locales.end())
00150                 return (*it).second;
00151 
00152         return 0;
00153 }
00154 
00155 
00156 list <string> LocaleMgr::getAvailableLocales() {
00157         list <string> retVal;
00158         for (LocaleMap::iterator it = locales.begin(); it != locales.end(); it++) 
00159                 retVal.push_back((*it).second->getName());
00160 
00161         return retVal;
00162 }
00163 
00164 
00165 const char *LocaleMgr::translate(const char *text, const char *localeName) {
00166         SWLocale *target;
00167         if (!localeName) {
00168                 localeName = getDefaultLocaleName();
00169         }
00170         target = getLocale(localeName);
00171         if (target)
00172                 return target->translate(text);
00173         return text;
00174 }
00175 
00176 
00177 const char *LocaleMgr::getDefaultLocaleName() {
00178         return defaultLocaleName;
00179 }
00180 
00181 
00182 void LocaleMgr::setDefaultLocaleName(const char *name) {
00183         stdstr(&defaultLocaleName, name);
00184 }

Generated on Thu Jun 20 22:12:59 2002 for The Sword Project by doxygen1.2.15