propset.c 11 KB

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