propset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <sys/stat.h>
  45. #include <ctype.h>
  46. #include "i18n.h"
  47. #include "txtfile.h"
  48. #include "reshash.h"
  49. #include "propset.h"
  50. int PropertiesLoadFileToHash(PropertiesSet *propset, char *language);
  51. char *GetProertiesFilename(char *directory, char *file, char *language);
  52. int PropertiesLanguageStatus(PropertiesSet *propset, char *language);
  53. int PropertiesSetLangStatus(LanguageStatus *langstatus, char *language, int status);
  54. int unicode_to_UTF8(unsigned int wch, char *utf8);
  55. char *decode_ascii(char *src);
  56. PropertiesSet * PropertiesInit(char *directory, char *file)
  57. {
  58. struct stat buf;
  59. char * file_path;
  60. PropertiesSet *propset = NULL;
  61. PropertiesSet *result = NULL;
  62. ResHash *reshash;
  63. file_path = (char *) malloc (strlen(directory) + strlen(file) + 20);
  64. strcpy(file_path, directory);
  65. strcat(file_path, "/");
  66. strcat(file_path, file);
  67. strcat(file_path, ".properties");
  68. if (stat(file_path, &buf) == 0) {
  69. propset = (PropertiesSet *) malloc(sizeof(PropertiesSet));
  70. memset(propset, 0, sizeof(PropertiesSet));
  71. reshash = (ResHash *) ResHashCreate(file);
  72. if (reshash) {
  73. propset->langlist = (LanguageStatus *) malloc(sizeof(LanguageStatus));
  74. memset(propset->langlist, 0, sizeof(LanguageStatus));
  75. propset->res = reshash;
  76. propset->directory = strdup(directory);
  77. propset->filename = strdup(file);
  78. PropertiesLoadFileToHash(propset, NULL);
  79. result = propset;
  80. }
  81. }
  82. if (file_path)
  83. free (file_path);
  84. return result;
  85. }
  86. char *GetProertiesFilename(char *directory, char *file, char *language)
  87. {
  88. char *filepath;
  89. if (language && *language == '\0')
  90. filepath = (char *) malloc(strlen(directory) + strlen(file) + strlen(language) + 20);
  91. else
  92. filepath = (char *) malloc(strlen(directory) + strlen(file) + 20);
  93. strcpy(filepath, directory);
  94. if (filepath[strlen(filepath) - 1] != '/')
  95. strcat(filepath, "/");
  96. strcat(filepath, file);
  97. if (language && *language != '\0') {
  98. strcat(filepath, "_");
  99. strcat(filepath, language);
  100. }
  101. strcat(filepath, ".properties");
  102. return filepath;
  103. }
  104. /*
  105. PropertiesLoadToHash
  106. Opens property file and save data to hash table
  107. Input
  108. propfile: handle
  109. file: full path with file extension
  110. return:
  111. 0: SUCCESS
  112. 1: FAIL
  113. */
  114. int PropertiesLoadFileToHash(PropertiesSet *propset, char *language)
  115. {
  116. TEXTFILE *hfile;
  117. char *filepath;
  118. char *p, *q;
  119. int n;
  120. char linebuf[1000];
  121. int st;
  122. st = PropertiesLanguageStatus(propset, language);
  123. if (st == LANGUAGE_INVALID)
  124. return 1;
  125. else if (st == LANGUAGE_LOAD)
  126. return 0;
  127. filepath = GetProertiesFilename(propset->directory, propset->filename, language);
  128. if ((hfile = OpenTextFile (filepath, TEXT_OPEN_FOR_READ)) == NULL) {
  129. PropertiesSetLangStatus(propset->langlist, language, LANGUAGE_INVALID);
  130. return 1;
  131. }
  132. while ((n = ReadTextLine(hfile, linebuf)) >= 0) {
  133. if (n == 0)
  134. continue;
  135. p = linebuf;
  136. /* strip leading spaces */
  137. while (*p == ' ' || *p == '\t')
  138. p ++;
  139. /* skip comment line */
  140. if (*p == '\0' || *p == '#' || *p == '=')
  141. continue;
  142. q = strchr (linebuf, '=');
  143. if (q) {
  144. char *key, *value, *newvalue;
  145. *q = '\0';
  146. key = p;
  147. value = q + 1;
  148. /* strip trailing space for key */
  149. p = key + strlen(key) - 1;
  150. while (*p == ' ' || *p == '\t') {
  151. *p = '\0';
  152. p --;
  153. }
  154. /* decode Unicode escape value */
  155. newvalue = decode_ascii(value);
  156. if (newvalue) {
  157. ResHashAdd(propset->res, key, newvalue, language);
  158. free(newvalue);
  159. }
  160. else
  161. ResHashAdd(propset->res, key, value, language);
  162. }
  163. }
  164. PropertiesSetLangStatus(propset->langlist, language, LANGUAGE_LOAD);
  165. return 0;
  166. }
  167. /*
  168. PropertiesIsLoaded
  169. Test if current properties associated with language
  170. is loaded or not.
  171. return:
  172. 1: SUCCESS
  173. 0: FAIL
  174. */
  175. int PropertiesLanguageStatus(PropertiesSet *propset, char *language)
  176. {
  177. LanguageStatus *plang;
  178. plang = propset->langlist;
  179. if (language == NULL || *language == '\0') {
  180. return plang->status;
  181. }
  182. plang = plang->next;
  183. while (plang) {
  184. if (strcmp(plang->language, language) == 0) {
  185. return plang->status;
  186. }
  187. plang = plang->next;
  188. }
  189. return LANGUAGE_NONE;
  190. }
  191. int PropertiesSetLangStatus(LanguageStatus *langlist, char *language, int status)
  192. {
  193. LanguageStatus *plang, *prev;
  194. LanguageStatus *langstatus;
  195. if (language == NULL || *language == '\0') {
  196. langlist->status = status;
  197. return 0;
  198. }
  199. prev = plang = langlist;
  200. plang = plang->next;
  201. while (plang) {
  202. if (strcmp(plang->language, language) == 0) {
  203. plang->status = status;
  204. return 0;
  205. }
  206. prev = plang;
  207. plang = plang->next;
  208. }
  209. langstatus = (LanguageStatus *) malloc(sizeof(LanguageStatus));
  210. memset (langstatus, 0, sizeof(LanguageStatus));
  211. langstatus->language = strdup(language);
  212. langstatus->status = status;
  213. prev->next = langstatus;
  214. return 0;
  215. }
  216. /***
  217. PropertiesOpenFile
  218. return 0: loaded
  219. 1: fail to load file associated with the language
  220. */
  221. int PropertiesOpenFile(PropertiesSet *propset, char *language)
  222. {
  223. int status;
  224. status = PropertiesLanguageStatus(propset, language);
  225. if (status == LANGUAGE_NONE)
  226. return PropertiesLoadFileToHash (propset, language);
  227. else if (status == LANGUAGE_INVALID)
  228. return 1;
  229. else
  230. return 0;
  231. }
  232. const char *PropertiesGetString(PropertiesSet *propset, char *key, ACCEPT_LANGUAGE_LIST acceptlangauge)
  233. {
  234. int i;
  235. char *language = NULL;
  236. i = 0;
  237. while (acceptlangauge[i][0]) {
  238. if (PropertiesOpenFile(propset, acceptlangauge[i]) == 0) {
  239. language = acceptlangauge[i];
  240. break;
  241. }
  242. i ++;
  243. }
  244. return ResHashSearch(propset->res, key, language);
  245. }
  246. void PropertiesDestroy(PropertiesSet *propset)
  247. {
  248. LanguageStatus *langattrib, *next;
  249. if (propset) {
  250. if (propset->path)
  251. free(propset->path);
  252. if (propset->directory)
  253. free(propset->directory);
  254. if (propset->filename)
  255. free(propset->filename);
  256. ResHashDestroy(propset->res);
  257. langattrib = propset->langlist;
  258. while (langattrib) {
  259. next = langattrib->next;
  260. if (langattrib->language)
  261. free(langattrib->language);
  262. free(langattrib);
  263. langattrib = next;
  264. }
  265. }
  266. }
  267. char *decode_ascii(char *src)
  268. {
  269. int i;
  270. char utf8[10];
  271. int state = 0;
  272. int digit = 0;
  273. int digit_count = 0;
  274. char *result, *p, *q;
  275. if (src == NULL || *src == '\0')
  276. return NULL;
  277. if (strchr(src, '\\') == NULL)
  278. return NULL;
  279. result = (char *) malloc(strlen(src) + 1);
  280. p = src;
  281. q = result;
  282. for (;*p; p++) {
  283. char ch;
  284. int n;
  285. if (state == BACKSLASH_U) {
  286. ch = toupper(*p);
  287. if (ch >= '0' && ch <= '9') {
  288. digit = digit * 16 + (ch - '0');
  289. digit_count ++;
  290. }
  291. else if (ch >= 'A' && ch <= 'F') {
  292. digit = digit * 16 + (ch - 'A' + 10);
  293. digit_count ++;
  294. }
  295. else {
  296. n = unicode_to_UTF8(digit, utf8);
  297. for (i = 0; i < n; i++)
  298. *q ++ = utf8[i];
  299. *q ++ = *p;
  300. state = 0;
  301. digit_count = 0;
  302. }
  303. if (digit_count == 4) {
  304. n = unicode_to_UTF8(digit, utf8);
  305. for (i = 0; i < n; i++)
  306. *q ++ = utf8[i];
  307. state = 0;
  308. }
  309. }
  310. else if (state == BACKSLASH) {
  311. if (*p == 'u') {
  312. state = BACKSLASH_U;
  313. digit = 0;
  314. digit_count = 0;
  315. continue;
  316. }
  317. else if (*p == 'n') {
  318. *q++ = '\n';
  319. state = 0;
  320. }
  321. else if (*p == 'r') {
  322. *q++ = '\r';
  323. state = 0;
  324. }
  325. else {
  326. *q++ = '\\';
  327. *q++ = *p;
  328. state = 0;
  329. }
  330. }
  331. else if (*p == '\\') {
  332. state = BACKSLASH;
  333. continue;
  334. }
  335. else {
  336. *q++ = *p;
  337. state = 0;
  338. }
  339. }
  340. *q = '\0';
  341. return result;
  342. }
  343. int unicode_to_UTF8(unsigned int wch, char *utf8)
  344. {
  345. unsigned char hibyte, lobyte, mibyte;
  346. if (wch <= 0x7F) {
  347. /* 0000 007F ==> 0xxxxxxx */
  348. utf8[0] = (unsigned char) wch ;
  349. utf8[1] = '\0';
  350. return 1;
  351. }
  352. else if (wch <= 0x7FF) {
  353. /* 0000 07FF ==> 110xxxxx 10xxxxxx */
  354. lobyte = wch & 0x3F;
  355. hibyte = (wch >> 6) & 0x1F;
  356. utf8[0] = 0xC0 | hibyte;
  357. utf8[1] = 0x80 | lobyte;
  358. utf8[2] = '\0';
  359. return 2;
  360. }
  361. else {
  362. /* FFFF ==> 1110xxxx 10xxxxxx 10xxxxxx */
  363. lobyte = wch & 0x3F;
  364. mibyte = (wch >> 6) & 0x3F;
  365. hibyte = (wch >> 12) & 0xF;
  366. utf8[0] = 0xE0 | hibyte;
  367. utf8[1] = 0x80 | mibyte;
  368. utf8[2] = 0x80 | lobyte;
  369. utf8[3] = '\0';
  370. return 3;
  371. }
  372. }