getlang.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include "libadmin/libadmin.h"
  44. #include "i18n.h"
  45. /*********************************************************************
  46. strReplace replaces the first instance of from in target with to.
  47. from can be "": to is inserted at start of target.
  48. to can be "": from is removed from target.
  49. if from is not found, 0 is returned; else 1 is returned.
  50. *********************************************************************/
  51. static int
  52. strReplace(char* target,char* from,char* to)
  53. {
  54. /* replace /from/to/ in target */
  55. char* pFrom;
  56. char* pOldTail;
  57. int lenTo;
  58. pFrom = strstr(target,from);
  59. if (pFrom) {
  60. pOldTail = pFrom+strlen(from);
  61. lenTo = strlen(to);
  62. memmove(pFrom+lenTo,pOldTail,strlen(pOldTail)+1);
  63. memcpy(pFrom,to,lenTo);
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. /*********************************************************************
  69. statFileDir is a wrapper to stat() that strips trailing slashes
  70. because stat() on Windows seems to return -1 otherwise.
  71. *********************************************************************/
  72. int
  73. statFileDir(const char *path, struct stat *info) {
  74. int ret, pathlen;
  75. char *newpath = strdup(path);
  76. if(newpath == NULL)
  77. return -1;
  78. for (pathlen = (strlen(newpath) - 1); pathlen >= 0; pathlen--) {
  79. if (newpath[pathlen] == '/' || newpath[pathlen] == '\\') {
  80. newpath[pathlen] = '\0';
  81. } else {
  82. break;
  83. }
  84. }
  85. ret = stat(newpath, info);
  86. if (newpath)
  87. free(newpath);
  88. return ret;
  89. }
  90. /*********************************************************************
  91. GetLanguage is reserved for future use. These APIs are not belong
  92. to this file. It needs to be moved to somewhere which knows what's
  93. the current language setting.
  94. *********************************************************************/
  95. static char emptyString[] = "";
  96. static char client_language[128] = "en";
  97. static char admin_language[128] = "en";
  98. static char default_language[128] = "en";
  99. void
  100. SetLanguage(int type, char *language)
  101. {
  102. switch(type) {
  103. case CLIENT_LANGUAGE:
  104. if (language)
  105. strcpy(client_language, language);
  106. break;
  107. case ADMIN_LANGUAGE:
  108. if (language)
  109. strcpy(admin_language, language);
  110. break;
  111. case DEFAULT_LANGUAGE:
  112. if (language)
  113. strcpy(default_language, language);
  114. break;
  115. }
  116. return ;
  117. }
  118. char*
  119. GetClientLanguage(void)
  120. {
  121. if (client_language)
  122. return client_language;
  123. else
  124. return emptyString;
  125. }
  126. char*
  127. GetAdminLanguage(void)
  128. {
  129. if (admin_language)
  130. return admin_language;
  131. else
  132. return emptyString;
  133. }
  134. char*
  135. GetDefaultLanguage(void)
  136. {
  137. if (default_language)
  138. return default_language;
  139. else
  140. return "en";
  141. }
  142. /*********************************************************************
  143. GetFileForLanguage looks for a file in the appropriate language.
  144. *********************************************************************/
  145. NSAPI_PUBLIC
  146. int
  147. GetFileForLanguage(char* filePath,char* language,char* existingFilePath)
  148. {
  149. /* Input: filePath,language
  150. * filePath is of the form "/xxx/xxx/$$LANGDIR/xxx/xxx/filename"
  151. * or of the form "/xxx/xxx/xxx/xxx/filename".
  152. * filename may or may not have an extension.
  153. * language is an Accept-Language list; each language-range will be
  154. * tried as a subdirectory name and possibly as a filename modifier.
  155. * "*" is ignored - default always provided if needed.
  156. * "-" is replaced by "_".
  157. * $$LANGDIR is a special string replaced by language. It is optional.
  158. * For the default case, $$LANGDIR/ is replaced by nothing
  159. * (so // is not created).
  160. *
  161. * Returned: existingPath
  162. * existingFilePath is the path of a satisfactory, existing file.
  163. * if no file is found, an empty string "" is returned.
  164. *
  165. * int returned: -1 if no file found (existingFilePath = "")
  166. * 0 if default file is returned
  167. * 1 if language file is returned (any in list)
  168. *
  169. * Example:
  170. * filePath = "/path/$$LANGDIR/filename.ext"
  171. * language = "language"
  172. * GetDefaultLanguage() --> "default"
  173. * LANG_DELIMIT = "_"
  174. *
  175. * 1. Try: "/path/language/filename.ext"
  176. * 2. Try: "/path/filename_language.ext"
  177. * 3. Try: "/path/default/filename.ext"
  178. * 4. Try: "/path/filename_default.ext"
  179. * 5. Try: "/path/filename.ext"
  180. * else: ""
  181. *
  182. * Example:
  183. * language = "en-us;q=0.6,ja;q=0.8,en-ca"
  184. *
  185. * 1. Try: "/path/en-ca/filename.ext"
  186. * 2. Try: "/path/filename_en_ca.ext"
  187. * 3. Try: "/path/ja/filename.ext"
  188. * 4. Try: "/path/filename_ja.ext"
  189. * 5. Try: "/path/en_us/filename.ext"
  190. * 6. Try: "/path/filename_en_us.ext"
  191. * 7. Try: "/path/default/filename.ext"
  192. * 8. Try: "/path/filename_default.ext"
  193. * 9. Try: "/path/filename.ext"
  194. * else: ""
  195. *
  196. */
  197. #define LANG_DELIMIT '_'
  198. int pattern;
  199. char* pDot;
  200. char* pSlash;
  201. /* PRFileInfo info; */
  202. struct stat info;
  203. char lang_modifier[MAX_ACCEPT_LENGTH+1];
  204. ACCEPT_LANGUAGE_LIST acceptLanguageList;
  205. int numLang;
  206. int iLang;
  207. int iCase;
  208. /* escape in case XP_InitStringDatabase has not been called */
  209. if (filePath==NULL) {
  210. *existingFilePath = '\0';
  211. return -1;
  212. }
  213. pattern = (strstr(filePath,"$$LANGDIR/")!=NULL);
  214. for ( iCase=1 ; iCase>=0 ; iCase-- ) {
  215. if (iCase==1) { /* iCase=1 tries requested language */
  216. numLang = XP_AccLangList(language,acceptLanguageList);
  217. } else { /* iCase=0 tries default language */
  218. numLang = XP_AccLangList(GetDefaultLanguage(),acceptLanguageList);
  219. }
  220. for ( iLang=0 ; iLang<numLang ; iLang++ ) {
  221. /* Try: /path/language/filename.ext */
  222. if (pattern) {
  223. strcpy(existingFilePath,filePath);
  224. strReplace(existingFilePath,"$$LANGDIR",acceptLanguageList[iLang]);
  225. if (statFileDir(existingFilePath,&info)==0) {
  226. return iCase;
  227. }
  228. /*
  229. if (PR_GetFileInfo(existingFilePath,&info)==PR_SUCCESS) {
  230. return iCase;
  231. }
  232. */
  233. }
  234. /* Try: /path/filename_language.ext */
  235. {
  236. strcpy(existingFilePath,filePath);
  237. strReplace(existingFilePath,"$$LANGDIR/",emptyString);
  238. pDot = strrchr(existingFilePath,'.');
  239. pSlash = strrchr(existingFilePath,'/');
  240. if (pSlash>=pDot) {
  241. pDot = strchr(existingFilePath,'\0');
  242. }
  243. sprintf(lang_modifier,"%c%s",LANG_DELIMIT,acceptLanguageList[iLang]);
  244. strReplace(pDot,emptyString,lang_modifier);
  245. if (statFileDir(existingFilePath,&info)==0) {
  246. return iCase;
  247. }
  248. /*
  249. if (PR_GetFileInfo(existingFilePath,&info)==PR_SUCCESS) {
  250. return iCase;
  251. }
  252. */
  253. }
  254. }
  255. }
  256. /* Try: /path/filename.ext */
  257. {
  258. strcpy(existingFilePath,filePath);
  259. strReplace(existingFilePath,"$$LANGDIR/",emptyString);
  260. if (statFileDir(existingFilePath,&info)==0) {
  261. return 0;
  262. }
  263. /*
  264. if (PR_GetFileInfo(existingFilePath,&info)==PR_SUCCESS) {
  265. return 0;
  266. }
  267. */
  268. }
  269. /* Else: */
  270. *existingFilePath = '\0';
  271. return -1;
  272. }