00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gamepad.h"
00037
00038 #include "util/file.h"
00039
00040
00041 namespace gamepad {
00042
00043
00044
00045
00046
00047
00048
00049
00050 static smart_ptr<i18n::Manager>
00051 createLocaleMgr
00052 (
00053 IN const char * locale,
00054 IN nstream::Folder * dir,
00055 IN const char * fileroot,
00056 IN const char * importantId
00057 )
00058 {
00059 ASSERT(locale, "null");
00060 ASSERT(dir, "null");
00061 ASSERT(fileroot, "null");
00062 ASSERT(importantId, "null");
00063
00064
00065 smart_ptr<i18n::Manager> mgr = i18n::Manager::create(locale);
00066 ASSERT(mgr, "failed to create base locale manager");
00067
00068
00069 smart_ptr<nstream::Manager> fsMgr = dir->getManager();
00070 ASSERT(fsMgr, "Failed to create filesystem named stream manager");
00071
00072
00073 std::string temp, relpath;
00074 appendPath("strings", locale, temp);
00075 appendPath(temp.c_str(), fileroot, relpath);
00076 relpath += ".txt";
00077
00078
00079
00080 smart_ptr<nstream::Stream> stream;
00081 try {
00082 stream = nstream::openNamedStream(fsMgr, relpath.c_str());
00083 } catch (std::exception& e) {
00084 DPRINTF("Exception: %s", e.what());
00085 }
00086
00087 if (!stream) {
00088 DPRINTF("Unable to open stream at path: '%s'", relpath.c_str());
00089 return NULL;
00090 }
00091
00092
00093 mgr->parseStrings(stream);
00094
00095
00096 if (!mgr->getString(importantId)) {
00097 DPRINTF("Found file '%s', but does not contain id '%s'",
00098 relpath.c_str(), importantId);
00099 return NULL;
00100 }
00101
00102
00103 return mgr;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113 smart_ptr<i18n::Manager>
00114 getConfigLocaleMgr
00115 (
00116 IN const char * locale,
00117 IN const char * defaultLocale,
00118 IN nstream::Folder * dataDirectory
00119 )
00120 {
00121 ASSERT_THROW(locale || defaultLocale,
00122 "At least one locale (primary or backup) should be non-null!");
00123 ASSERT(dataDirectory, "null");
00124
00125
00126 if (!strcmp(locale, defaultLocale)) {
00127 defaultLocale = NULL;
00128 }
00129
00130 smart_ptr<i18n::Manager> mgr;
00131 if (locale) {
00132 DPRINTF("Trying requested locale: '%s'", locale);
00133 mgr = createLocaleMgr(locale, dataDirectory, "config",
00134 "buttonPress");
00135 }
00136
00137 if (!mgr && defaultLocale) {
00138 DPRINTF("Falling back to default locale: '%s'", defaultLocale);
00139 mgr = createLocaleMgr(defaultLocale, dataDirectory,
00140 "config", "buttonPress");
00141 }
00142
00143 ASSERT_THROW(mgr, "Failed to create locale manager for gamepad " <<
00144 "configuration. Bad data directory? " << dataDirectory);
00145
00146 return mgr;
00147 }
00148
00149
00150
00151 smart_ptr<i18n::Manager>
00152 getTypeLocaleMgr
00153 (
00154 IN const Type * type,
00155 IN const char * locale,
00156 IN const char * defaultLocale,
00157 IN nstream::Folder * dataDirectory
00158 )
00159 {
00160 ASSERT(type, "null");
00161 ASSERT_THROW(locale || defaultLocale,
00162 "Either the primary or backup locale should be non-null!");
00163 ASSERT(dataDirectory, "null");
00164
00165
00166 if (locale && defaultLocale && !strcmp(locale, defaultLocale)) {
00167 defaultLocale = NULL;
00168 }
00169
00170
00171 const char * fileroot = type->getName();
00172
00173
00174 smart_ptr<i18n::Manager> mgr;
00175 if (locale) {
00176 mgr = createLocaleMgr(locale, dataDirectory, fileroot,
00177 "friendlyName");
00178 }
00179
00180 if (!mgr && defaultLocale) {
00181 mgr = createLocaleMgr(defaultLocale, dataDirectory, fileroot,
00182 "friendlyName");
00183 }
00184
00185 ASSERT_THROW(mgr, "Failed to create locale manager for gamepad '" <<
00186 fileroot << "'. Bad data directory? " << dataDirectory);
00187
00188 return mgr;
00189 }
00190
00191
00192
00193 };
00194