00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "fsLangMgr.h"
00009
00010 #ifdef _DEBUG
00011 #undef THIS_FILE
00012 static char THIS_FILE[]=__FILE__;
00013 #define new DEBUG_NEW
00014 #endif
00015
00016 fsLangMgr::fsLangMgr()
00017 {
00018 m_iCurLng = -1;
00019 }
00020
00021 fsLangMgr::~fsLangMgr()
00022 {
00023
00024 }
00025
00026 #define LNG_COMMENT_CHAR ';'
00027
00028 BOOL fsLangMgr::Initialize()
00029 {
00030 LOG ("fsLM::I: start" << nl);
00031
00032 LoadBuiltInLngStrings ();
00033
00034 WIN32_FIND_DATA wfd;
00035
00036 m_strLngFolder = ((CFdmApp*)AfxGetApp ())->m_strAppPath;
00037 m_strLngFolder += "Language\\";
00038
00039 CString strMask = m_strLngFolder;
00040 strMask += "*.lng";
00041
00042 LOG ("fsLM::I: searching for " << strMask << nl);
00043
00044 HANDLE hFind = FindFirstFile (strMask, &wfd);
00045 BOOL bFind = hFind != INVALID_HANDLE_VALUE;
00046
00047 while (bFind)
00048 {
00049 try {
00050 fsLngFileInfo info;
00051 info.strFileName = wfd.cFileName;
00052
00053 LOG ("fsLM::I: found " << wfd.cFileName << nl);
00054
00055 CStdioFile file (m_strLngFolder + info.strFileName, CFile::modeRead | CFile::typeText | CFile::shareDenyNone);
00056
00057 LOG ("fsLM::I: was opened ok." << nl);
00058
00059
00060 while (file.ReadString (info.strLngName))
00061 {
00062 if (info.strLngName.GetLength () && info.strLngName [0] != LNG_COMMENT_CHAR)
00063 {
00064 LOG ("fsLM::I: it's a valid lng file. add it." << nl);
00065 AddLngFileInfo (info);
00066 break;
00067 }
00068 }
00069 }
00070 catch (...){
00071 LOG ("fsLM::I: some file error occurred." << nl);
00072 }
00073
00074 bFind = FindNextFile (hFind, &wfd);
00075 }
00076
00077 if (hFind != INVALID_HANDLE_VALUE)
00078 FindClose (hFind);
00079
00080 LOG ("fsLM::I: there are " << m_vLngFiles.size () << " languages found" << nl);
00081
00082 return m_vLngFiles.size () != 0;
00083 }
00084
00085 int fsLangMgr::GetLngCount()
00086 {
00087 return m_vLngFiles.size ();
00088 }
00089
00090 LPCSTR fsLangMgr::GetLngName(int iIndex)
00091 {
00092 static char szBuiltIn [] = "Built In";
00093 if (iIndex < 0 || iIndex >= m_vLngFiles.size ())
00094 return szBuiltIn;
00095 return m_vLngFiles [iIndex].strLngName;
00096 }
00097
00098 BOOL fsLangMgr::LoadLng(int iIndex)
00099 {
00100 try {
00101
00102 CStdioFile file (m_strLngFolder + m_vLngFiles [iIndex].strFileName, CFile::modeRead | CFile::typeText | CFile::shareDenyNone);
00103 CString str;
00104
00105 do
00106 {
00107 if (FALSE == file.ReadString (str))
00108 return FALSE;
00109
00110 if (str.GetLength () && str [0] != LNG_COMMENT_CHAR)
00111 break;
00112 }
00113 while (TRUE);
00114
00115 m_vStrings.clear ();
00116
00117 while (file.ReadString (str))
00118 {
00119 if (str.GetLength () == 0 || str [0] == LNG_COMMENT_CHAR)
00120 continue;
00121
00122 PreprocessLanguageString (str);
00123
00124 m_vStrings.add (str);
00125 }
00126
00127 m_iCurLng = iIndex;
00128
00129 return m_vStrings.size () != 0;
00130
00131 }
00132 catch (...)
00133 {
00134 return FALSE;
00135 }
00136 }
00137
00138 int fsLangMgr::FindLngByName(LPCSTR pszName)
00139 {
00140 LOG ("fsLM::FLbN: find for " << pszName << nl);
00141
00142 for (int i = 0; i < m_vLngFiles.size (); i++)
00143 {
00144 if (m_vLngFiles [i].strLngName == pszName ||
00145 lstrcmpi (m_vLngFiles [i].strFileName, pszName) == 0)
00146 {
00147 LOG ("fsLM::FLbN: was found." << nl);
00148 return i;
00149 }
00150 }
00151
00152 LOG ("fsLM::FLbN: was not found." << nl);
00153 return -1;
00154 }
00155
00156 int fsLangMgr::GetStringCount()
00157 {
00158 return m_vStrings.size ();
00159 }
00160
00161 LPCSTR fsLangMgr::GetString(int iIndex)
00162 {
00163 static char szNull [1] = {0};
00164
00165 try{
00166
00167 if (iIndex < m_vStrings.size ())
00168 return m_vStrings [iIndex];
00169 else if (iIndex < m_vBuiltInStrings.size ())
00170 return m_vBuiltInStrings [iIndex];
00171 else
00172 return szNull;
00173
00174 }
00175 catch (...) {ASSERT (FALSE); return szNull;}
00176 }
00177
00178 int fsLangMgr::GetCurLng()
00179 {
00180 return m_iCurLng;
00181 }
00182
00183 CString fsLangMgr::GetStringNP(int iIndex)
00184 {
00185 CString str = GetString (iIndex);
00186 str.Remove ('&');
00187 return str;
00188 }
00189
00190 void fsLangMgr::AddLngFileInfo(fsLngFileInfo &info)
00191 {
00192 for (int i = 0; i < m_vLngFiles.size (); i++)
00193 {
00194 if (stricmp (m_vLngFiles [i].strLngName, info.strLngName) > 0)
00195 {
00196 m_vLngFiles.insert (i, info);
00197 return;
00198 }
00199 }
00200
00201 m_vLngFiles.add (info);
00202 }
00203
00204 void fsLangMgr::LoadBuiltInLngStrings()
00205 {
00206 HGLOBAL hResource;
00207 HRSRC hRes;
00208 LPBYTE pbRes;
00209
00210 hRes = FindResource (NULL, MAKEINTRESOURCE (IDR_ENG_LNGSTRINGS), "RT_LNGSTRINGS");
00211 if (hRes == NULL)
00212 return;
00213
00214 hResource = LoadResource (NULL, hRes);
00215 pbRes = (LPBYTE) LockResource (hResource);
00216 LPBYTE pbResEnd = pbRes + SizeofResource (NULL, hRes);
00217
00218 LPCSTR psz = (LPCSTR) pbRes;
00219 LPCSTR pszE = (LPCSTR) pbResEnd;
00220 bool bLngNamePassed = false;
00221
00222 while (psz < pszE)
00223 {
00224 CString str;
00225 while (psz < pszE && *psz != '\n' && *psz != '\r')
00226 str += *psz++;
00227 while (psz < pszE && (*psz == '\n' || *psz == '\r'))
00228 psz++;
00229
00230 if (str == "" || str [0] == LNG_COMMENT_CHAR)
00231 continue;
00232
00233 if (bLngNamePassed == false) {
00234 bLngNamePassed = true;
00235 continue;
00236 }
00237
00238 PreprocessLanguageString (str);
00239 m_vBuiltInStrings.add (str);
00240 }
00241 }
00242
00243 void fsLangMgr::PreprocessLanguageString(CString &str)
00244 {
00245 str.Replace ("\\n", "\n");
00246 }