PageRenderTime 2135ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/googleclient/third_party/icu38/files/source/tools/genrb/wrtxml.c

http://o3d.googlecode.com/
C | 1165 lines | 1100 code | 25 blank | 40 comment | 30 complexity | b200b74279a723bce4aed75433af5f1f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, GPL-2.0, Apache-2.0, MIT, CPL-1.0
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 2002-2007, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. *******************************************************************************
  8. *
  9. * File wrtxml.c
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 10/01/02 Ram Creation.
  15. *******************************************************************************
  16. */
  17. #include "reslist.h"
  18. #include "unewdata.h"
  19. #include "unicode/ures.h"
  20. #include "errmsg.h"
  21. #include "filestrm.h"
  22. #include "cstring.h"
  23. #include "unicode/ucnv.h"
  24. #include "genrb.h"
  25. #include "rle.h"
  26. #include "ucol_tok.h"
  27. #include "uhash.h"
  28. #include "uresimp.h"
  29. #include "unicode/ustring.h"
  30. #include "unicode/uchar.h"
  31. #include "ustr.h"
  32. #include "prscmnts.h"
  33. #include <time.h>
  34. static int tabCount = 0;
  35. static FileStream* out=NULL;
  36. static struct SRBRoot* srBundle ;
  37. static const char* outDir = NULL;
  38. static const char* enc ="";
  39. static UConverter* conv = NULL;
  40. const char* const* ISOLanguages;
  41. const char* const* ISOCountries;
  42. const char* textExt = ".txt";
  43. const char* xliffExt = ".xlf";
  44. /*write indentation for formatting*/
  45. static void write_tabs(FileStream* os){
  46. int i=0;
  47. for(;i<=tabCount;i++){
  48. T_FileStream_write(os," ",4);
  49. }
  50. }
  51. /*get ID for each element. ID is globally unique.*/
  52. static char* getID(const char* id, char* curKey, char* result) {
  53. if(curKey == NULL) {
  54. result = uprv_malloc(sizeof(char)*uprv_strlen(id) + 1);
  55. uprv_memset(result, 0, sizeof(char)*uprv_strlen(id) + 1);
  56. uprv_strcpy(result, id);
  57. } else {
  58. result = uprv_malloc(sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1);
  59. uprv_memset(result, 0, sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1);
  60. if(id[0]!='\0'){
  61. uprv_strcpy(result, id);
  62. uprv_strcat(result, "_");
  63. }
  64. uprv_strcat(result, curKey);
  65. }
  66. return result;
  67. }
  68. /*compute CRC for binary code*/
  69. /* The code is from http://www.theorem.com/java/CRC32.java
  70. * Calculates the CRC32 - 32 bit Cyclical Redundancy Check
  71. * <P> This check is used in numerous systems to verify the integrity
  72. * of information. It's also used as a hashing function. Unlike a regular
  73. * checksum, it's sensitive to the order of the characters.
  74. * It produces a 32 bit
  75. *
  76. * @author Michael Lecuyer (mjl@theorem.com)
  77. * @version 1.1 August 11, 1998
  78. */
  79. /* ICU is not endian portable, because ICU data generated on big endian machines can be
  80. * ported to big endian machines but not to little endian machines and vice versa. The
  81. * conversion is not portable across platforms with different endianess.
  82. */
  83. static uint32_t computeCRC(char *ptr, uint32_t len, uint32_t lastcrc){
  84. int32_t crc;
  85. uint32_t temp1;
  86. uint32_t temp2;
  87. int32_t crc_ta[256];
  88. int i = 0;
  89. int j = 0;
  90. uint32_t crc2 = 0;
  91. #define CRC32_POLYNOMIAL 0xEDB88320
  92. /*build crc table*/
  93. for (i = 0; i <= 255; i++) {
  94. crc2 = i;
  95. for (j = 8; j > 0; j--) {
  96. if ((crc2 & 1) == 1) {
  97. crc2 = (crc2 >> 1) ^ CRC32_POLYNOMIAL;
  98. } else {
  99. crc2 >>= 1;
  100. }
  101. }
  102. crc_ta[i] = crc2;
  103. }
  104. crc = lastcrc;
  105. while(len--!=0) {
  106. temp1 = (uint32_t)crc>>8;
  107. temp2 = crc_ta[(crc^*ptr) & 0xFF];
  108. crc = temp1^temp2;
  109. ptr++;
  110. }
  111. return(crc);
  112. }
  113. static void strnrepchr(char* src, int32_t srcLen, char s, char r){
  114. int32_t i = 0;
  115. for(i=0;i<srcLen;i++){
  116. if(src[i]==s){
  117. src[i]=r;
  118. }
  119. }
  120. }
  121. /* Parse the filename, and get its language information.
  122. * If it fails to get the language information from the filename,
  123. * use "en" as the default value for language
  124. */
  125. static char* parseFilename(const char* id, char* lang) {
  126. int idLen = (int) uprv_strlen(id);
  127. char* localeID = (char*) uprv_malloc(idLen);
  128. int pos = 0;
  129. int canonCapacity = 0;
  130. char* canon = NULL;
  131. int canonLen = 0;
  132. /*int i;*/
  133. UErrorCode status = U_ZERO_ERROR;
  134. char *ext = uprv_strchr(id, '.');
  135. if(ext != NULL){
  136. pos = (int) (ext - id);
  137. } else {
  138. pos = idLen;
  139. }
  140. uprv_memcpy(localeID, id, pos);
  141. localeID[pos]=0; /* NUL terminate the string */
  142. canonCapacity =pos*3;
  143. canon = (char*) uprv_malloc(canonCapacity);
  144. canonLen = uloc_canonicalize(localeID, canon, canonCapacity, &status);
  145. if(U_FAILURE(status)){
  146. fprintf(stderr, "Could not canonicalize the locale ID: %s. Error: %s\n", localeID, u_errorName(status));
  147. exit(status);
  148. }
  149. strnrepchr(canon, canonLen, '_', '-');
  150. return canon;
  151. }
  152. static const char* xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  153. #if 0
  154. static const char* bundleStart = "<xliff version = \"1.2\" "
  155. "xmlns='urn:oasis:names:tc:xliff:document:1.2' "
  156. "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  157. "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd'>\n";
  158. #else
  159. static const char* bundleStart = "<xliff version = \"1.1\" "
  160. "xmlns='urn:oasis:names:tc:xliff:document:1.1' "
  161. "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  162. "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.1 http://www.oasis-open.org/committees/xliff/documents/xliff-core-1.1.xsd'>\n";
  163. #endif
  164. static const char* bundleEnd = "</xliff>\n";
  165. void res_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status);
  166. static char* convertAndEscape(char** pDest, int32_t destCap, int32_t* destLength,
  167. const UChar* src, int32_t srcLen, UErrorCode* status){
  168. int32_t srcIndex=0;
  169. char* dest=NULL;
  170. char* temp=NULL;
  171. int32_t destLen=0;
  172. UChar32 c = 0;
  173. if(status==NULL || U_FAILURE(*status) || pDest==NULL || srcLen==0 || src == NULL){
  174. return NULL;
  175. }
  176. dest =*pDest;
  177. if(dest==NULL || destCap <=0){
  178. destCap = srcLen * 8;
  179. dest = (char*) uprv_malloc(sizeof(char) * destCap);
  180. if(dest==NULL){
  181. *status=U_MEMORY_ALLOCATION_ERROR;
  182. return NULL;
  183. }
  184. }
  185. dest[0]=0;
  186. while(srcIndex<srcLen){
  187. U16_NEXT(src, srcIndex, srcLen, c);
  188. if (U16_IS_LEAD(c) || U16_IS_TRAIL(c)) {
  189. *status = U_ILLEGAL_CHAR_FOUND;
  190. fprintf(stderr, "Illegal Surrogate! \n");
  191. uprv_free(dest);
  192. return NULL;
  193. }
  194. if((destLen+UTF8_CHAR_LENGTH(c)) < destCap){
  195. /* ASCII Range */
  196. if(c <=0x007F){
  197. switch(c) {
  198. case '&':
  199. uprv_strcpy(dest+( destLen),"&amp;");
  200. destLen+=(int32_t)uprv_strlen("&amp;");
  201. break;
  202. case '<':
  203. uprv_strcpy(dest+(destLen),"&lt;");
  204. destLen+=(int32_t)uprv_strlen("&lt;");
  205. break;
  206. case '>':
  207. uprv_strcpy(dest+(destLen),"&gt;");
  208. destLen+=(int32_t)uprv_strlen("&gt;");
  209. break;
  210. case '"':
  211. uprv_strcpy(dest+(destLen),"&quot;");
  212. destLen+=(int32_t)uprv_strlen("&quot;");
  213. break;
  214. case '\'':
  215. uprv_strcpy(dest+(destLen),"&apos;");
  216. destLen+=(int32_t)uprv_strlen("&apos;");
  217. break;
  218. /* Disallow C0 controls except TAB, CR, LF*/
  219. case 0x00:
  220. case 0x01:
  221. case 0x02:
  222. case 0x03:
  223. case 0x04:
  224. case 0x05:
  225. case 0x06:
  226. case 0x07:
  227. case 0x08:
  228. /*case 0x09:*/
  229. /*case 0x0A: */
  230. case 0x0B:
  231. case 0x0C:
  232. /*case 0x0D:*/
  233. case 0x0E:
  234. case 0x0F:
  235. case 0x10:
  236. case 0x11:
  237. case 0x12:
  238. case 0x13:
  239. case 0x14:
  240. case 0x15:
  241. case 0x16:
  242. case 0x17:
  243. case 0x18:
  244. case 0x19:
  245. case 0x1A:
  246. case 0x1B:
  247. case 0x1C:
  248. case 0x1D:
  249. case 0x1E:
  250. case 0x1F:
  251. *status = U_ILLEGAL_CHAR_FOUND;
  252. fprintf(stderr, "Illegal Character \\u%04X!\n",(int)c);
  253. uprv_free(dest);
  254. return NULL;
  255. default:
  256. dest[destLen++]=(char)c;
  257. }
  258. }else{
  259. UBool isError = FALSE;
  260. U8_APPEND((unsigned char*)dest,destLen,destCap,c,isError);
  261. if(isError){
  262. *status = U_ILLEGAL_CHAR_FOUND;
  263. fprintf(stderr, "Illegal Character \\U%08X!\n",(int)c);
  264. uprv_free(dest);
  265. return NULL;
  266. }
  267. }
  268. }else{
  269. destCap += destLen;
  270. temp = (char*) uprv_malloc(sizeof(char)*destCap);
  271. if(temp==NULL){
  272. *status=U_MEMORY_ALLOCATION_ERROR;
  273. uprv_free(dest);
  274. return NULL;
  275. }
  276. uprv_memmove(temp,dest,destLen);
  277. destLen=0;
  278. uprv_free(dest);
  279. dest=temp;
  280. temp=NULL;
  281. }
  282. }
  283. *destLength = destLen;
  284. return dest;
  285. }
  286. #define ASTERISK 0x002A
  287. #define SPACE 0x0020
  288. #define CR 0x000A
  289. #define LF 0x000D
  290. #define AT_SIGN 0x0040
  291. static void
  292. trim(char **src, int32_t *len){
  293. char *s = NULL;
  294. int32_t i = 0;
  295. if(src == NULL || *src == NULL){
  296. return;
  297. }
  298. s = *src;
  299. /* trim from the end */
  300. for( i=(*len-1); i>= 0; i--){
  301. switch(s[i]){
  302. case ASTERISK:
  303. case SPACE:
  304. case CR:
  305. case LF:
  306. s[i] = 0;
  307. continue;
  308. default:
  309. break;
  310. }
  311. break;
  312. }
  313. *len = i+1;
  314. }
  315. static void
  316. print(UChar* src, int32_t srcLen,const char *tagStart,const char *tagEnd, UErrorCode *status){
  317. int32_t bufCapacity = srcLen*4;
  318. char *buf = NULL;
  319. int32_t bufLen = 0;
  320. if(U_FAILURE(*status)){
  321. return;
  322. }
  323. buf = (char*) (uprv_malloc(bufCapacity));
  324. if(buf==0){
  325. fprintf(stderr, "Could not allocate memory!!");
  326. exit(U_MEMORY_ALLOCATION_ERROR);
  327. }
  328. buf = convertAndEscape(&buf, bufCapacity, &bufLen, src, srcLen,status);
  329. if(U_SUCCESS(*status)){
  330. trim(&buf,&bufLen);
  331. T_FileStream_write(out,tagStart, (int32_t)uprv_strlen(tagStart));
  332. T_FileStream_write(out, buf, bufLen);
  333. T_FileStream_write(out,tagEnd, (int32_t)uprv_strlen(tagEnd));
  334. T_FileStream_write(out,"\n",1);
  335. }
  336. }
  337. static void
  338. printNoteElements(struct UString *src, UErrorCode *status){
  339. #if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
  340. int32_t capacity = 0;
  341. UChar* note = NULL;
  342. int32_t noteLen = 0;
  343. int32_t count = 0,i;
  344. if(src == NULL){
  345. return;
  346. }
  347. capacity = src->fLength;
  348. note = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
  349. count = getCount(src->fChars,src->fLength, UPC_NOTE, status);
  350. if(U_FAILURE(*status)){
  351. return;
  352. }
  353. for(i=0; i < count; i++){
  354. noteLen = getAt(src->fChars,src->fLength, &note, capacity, i, UPC_NOTE, status);
  355. if(U_FAILURE(*status)){
  356. return;
  357. }
  358. if(noteLen > 0){
  359. write_tabs(out);
  360. print(note, noteLen,"<note>", "</note>", status);
  361. }
  362. }
  363. uprv_free(note);
  364. #else
  365. fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n");
  366. #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
  367. }
  368. static void printAttribute(const char *name, const char *value, int32_t len)
  369. {
  370. T_FileStream_write(out, " ", 1);
  371. T_FileStream_write(out, name, (int32_t) uprv_strlen(name));
  372. T_FileStream_write(out, " = \"", 4);
  373. T_FileStream_write(out, value, (int32_t) len);
  374. T_FileStream_write(out, "\"", 1);
  375. }
  376. static void
  377. printComments(struct UString *src, const char *resName, UBool printTranslate, UErrorCode *status){
  378. #if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
  379. int32_t capacity = src->fLength;
  380. char* buf = NULL;
  381. int32_t bufLen = 0;
  382. UChar* desc = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
  383. UChar* trans = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
  384. int32_t descLen = 0, transLen=0;
  385. if(status==NULL || U_FAILURE(*status)){
  386. uprv_free(desc);
  387. uprv_free(trans);
  388. return;
  389. }
  390. if(desc==NULL || trans==NULL){
  391. *status = U_MEMORY_ALLOCATION_ERROR;
  392. uprv_free(desc);
  393. uprv_free(trans);
  394. return;
  395. }
  396. src->fLength = removeCmtText(src->fChars, src->fLength, status);
  397. descLen = getDescription(src->fChars,src->fLength, &desc, capacity, status);
  398. transLen = getTranslate(src->fChars,src->fLength, &trans, capacity, status);
  399. /* first print translate attribute */
  400. if(transLen > 0){
  401. if(printTranslate){
  402. /* print translate attribute */
  403. buf = convertAndEscape(&buf, 0, &bufLen, trans, transLen, status);
  404. if(U_SUCCESS(*status)){
  405. printAttribute("translate", buf, bufLen);
  406. T_FileStream_write(out,">\n", 2);
  407. }
  408. }else if(getShowWarning()){
  409. fprintf(stderr, "Warning: Tranlate attribute for resource %s cannot be set. XLIFF prohibits it.\n", resName);
  410. /* no translate attribute .. just close the tag */
  411. T_FileStream_write(out,">\n", 2);
  412. }
  413. }else{
  414. /* no translate attribute .. just close the tag */
  415. T_FileStream_write(out,">\n", 2);
  416. }
  417. if(descLen > 0){
  418. write_tabs(out);
  419. print(desc, descLen, "<!--", "-->", status);
  420. }
  421. #else
  422. fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n");
  423. #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
  424. }
  425. /*
  426. * Print out a containing element, like:
  427. * <trans-unit id = "blah" resname = "blah" restype = "x-id-alias" translate = "no">
  428. * <group id "calendar_gregorian" resname = "gregorian" restype = "x-icu-array">
  429. */
  430. static char *printContainer(struct SResource *res, const char *container, const char *restype, const char *mimetype, const char *id, UErrorCode *status)
  431. {
  432. char *resname = NULL;
  433. char *sid = NULL;
  434. write_tabs(out);
  435. if (res->fKey >= 0 && uprv_strcmp(srBundle->fKeys + res->fKey, "") != 0) {
  436. resname = srBundle->fKeys + res->fKey;
  437. sid = getID(id, resname, sid);
  438. } else {
  439. sid = getID(id, NULL, sid);
  440. }
  441. T_FileStream_write(out, "<", 1);
  442. T_FileStream_write(out, container, (int32_t) uprv_strlen(container));
  443. printAttribute("id", sid, (int32_t) uprv_strlen(sid));
  444. if (resname != NULL) {
  445. printAttribute("resname", resname, (int32_t) uprv_strlen(resname));
  446. }
  447. if (mimetype != NULL) {
  448. printAttribute("mime-type", mimetype, (int32_t) uprv_strlen(mimetype));
  449. }
  450. if (restype != NULL) {
  451. printAttribute("restype", restype, (int32_t) uprv_strlen(restype));
  452. }
  453. tabCount += 1;
  454. if (res->fComment != NULL && res->fComment->fChars != NULL) {
  455. /* printComments will print the closing ">\n" */
  456. printComments(res->fComment, resname, TRUE, status);
  457. } else {
  458. T_FileStream_write(out, ">\n", 2);
  459. }
  460. return sid;
  461. }
  462. /* Writing Functions */
  463. static const char *trans_unit = "trans-unit";
  464. static const char *close_trans_unit = "</trans-unit>\n";
  465. static const char *source = "<source>";
  466. static const char *close_source = "</source>\n";
  467. static const char *group = "group";
  468. static const char *close_group = "</group>\n";
  469. static const char *bin_unit = "bin-unit";
  470. static const char *close_bin_unit = "</bin-unit>\n";
  471. static const char *bin_source = "<bin-source>\n";
  472. static const char *close_bin_source = "</bin-source>\n";
  473. static const char *external_file = "<external-file";
  474. /*static const char *close_external_file = "</external-file>\n";*/
  475. static const char *internal_file = "<internal-file";
  476. static const char *close_internal_file = "</internal-file>\n";
  477. static const char *application_mimetype = "application"; /* add "/octet-stream"? */
  478. static const char *alias_restype = "x-icu-alias";
  479. static const char *array_restype = "x-icu-array";
  480. static const char *binary_restype = "x-icu-binary";
  481. static const char *integer_restype = "x-icu-integer";
  482. static const char *intvector_restype = "x-icu-intvector";
  483. static const char *table_restype = "x-icu-table";
  484. static void
  485. string_write_xml(struct SResource *res, const char* id, const char* language, UErrorCode *status) {
  486. char *sid = NULL;
  487. char* buf = NULL;
  488. int32_t bufLen = 0;
  489. if(status==NULL || U_FAILURE(*status)){
  490. return;
  491. }
  492. sid = printContainer(res, trans_unit, NULL, NULL, id, status);
  493. write_tabs(out);
  494. T_FileStream_write(out, source, (int32_t) uprv_strlen(source));
  495. buf = convertAndEscape(&buf, 0, &bufLen, res->u.fString.fChars, res->u.fString.fLength, status);
  496. if (U_FAILURE(*status)) {
  497. return;
  498. }
  499. T_FileStream_write(out, buf, bufLen);
  500. T_FileStream_write(out, close_source, (int32_t) uprv_strlen(close_source));
  501. printNoteElements(res->fComment, status);
  502. tabCount -= 1;
  503. write_tabs(out);
  504. T_FileStream_write(out, close_trans_unit, (int32_t) uprv_strlen(close_trans_unit));
  505. uprv_free(buf);
  506. uprv_free(sid);
  507. }
  508. static void
  509. alias_write_xml(struct SResource *res, const char* id, const char* language, UErrorCode *status) {
  510. char *sid = NULL;
  511. char* buf = NULL;
  512. int32_t bufLen=0;
  513. sid = printContainer(res, trans_unit, alias_restype, NULL, id, status);
  514. write_tabs(out);
  515. T_FileStream_write(out, source, (int32_t) uprv_strlen(source));
  516. buf = convertAndEscape(&buf, 0, &bufLen, res->u.fString.fChars, res->u.fString.fLength, status);
  517. if(U_FAILURE(*status)){
  518. return;
  519. }
  520. T_FileStream_write(out, buf, bufLen);
  521. T_FileStream_write(out, close_source, (int32_t)uprv_strlen(close_source));
  522. printNoteElements(res->fComment, status);
  523. tabCount -= 1;
  524. write_tabs(out);
  525. T_FileStream_write(out, close_trans_unit, (int32_t)uprv_strlen(close_trans_unit));
  526. uprv_free(buf);
  527. uprv_free(sid);
  528. }
  529. static void
  530. array_write_xml( struct SResource *res, const char* id, const char* language, UErrorCode *status) {
  531. char* sid = NULL;
  532. int index = 0;
  533. struct SResource *current = NULL;
  534. struct SResource *first =NULL;
  535. sid = printContainer(res, group, array_restype, NULL, id, status);
  536. current = res->u.fArray.fFirst;
  537. first=current;
  538. while (current != NULL) {
  539. char c[256] = {0};
  540. char* subId = NULL;
  541. itostr(c, index, 10, 0);
  542. index += 1;
  543. subId = getID(sid, c, subId);
  544. res_write_xml(current, subId, language, FALSE, status);
  545. uprv_free(subId);
  546. subId = NULL;
  547. if(U_FAILURE(*status)){
  548. return;
  549. }
  550. current = current->fNext;
  551. }
  552. tabCount -= 1;
  553. write_tabs(out);
  554. T_FileStream_write(out, close_group, (int32_t) uprv_strlen(close_group));
  555. uprv_free(sid);
  556. }
  557. static void
  558. intvector_write_xml( struct SResource *res, const char* id, const char* language, UErrorCode *status) {
  559. char* sid = NULL;
  560. char* ivd = NULL;
  561. uint32_t i=0;
  562. uint32_t len=0;
  563. char buf[256] = {'0'};
  564. sid = printContainer(res, group, intvector_restype, NULL, id, status);
  565. for(i = 0; i < res->u.fIntVector.fCount; i += 1) {
  566. char c[256] = {0};
  567. itostr(c, i, 10, 0);
  568. ivd = getID(sid, c, ivd);
  569. len = itostr(buf, res->u.fIntVector.fArray[i], 10, 0);
  570. write_tabs(out);
  571. T_FileStream_write(out, "<", 1);
  572. T_FileStream_write(out, trans_unit, (int32_t)uprv_strlen(trans_unit));
  573. printAttribute("id", ivd, (int32_t)uprv_strlen(ivd));
  574. printAttribute("restype", integer_restype, (int32_t) strlen(integer_restype));
  575. T_FileStream_write(out,">\n", 2);
  576. tabCount += 1;
  577. write_tabs(out);
  578. T_FileStream_write(out, source, (int32_t)uprv_strlen(source));
  579. T_FileStream_write(out, buf, len);
  580. T_FileStream_write(out, close_source, (int32_t)uprv_strlen(close_source));
  581. tabCount -= 1;
  582. write_tabs(out);
  583. T_FileStream_write(out, close_trans_unit, (int32_t)uprv_strlen(close_trans_unit));
  584. uprv_free(ivd);
  585. ivd = NULL;
  586. }
  587. tabCount -= 1;
  588. write_tabs(out);
  589. T_FileStream_write(out, close_group, (int32_t)uprv_strlen(close_group));
  590. uprv_free(sid);
  591. sid = NULL;
  592. }
  593. static void
  594. int_write_xml(struct SResource *res, const char* id, const char* language, UErrorCode *status) {
  595. char* sid = NULL;
  596. char buf[256] = {0};
  597. uint32_t len = 0;
  598. sid = printContainer(res, trans_unit, integer_restype, NULL, id, status);
  599. write_tabs(out);
  600. T_FileStream_write(out, source, (int32_t) uprv_strlen(source));
  601. len = itostr(buf, res->u.fIntValue.fValue, 10, 0);
  602. T_FileStream_write(out, buf, len);
  603. T_FileStream_write(out, close_source, (int32_t)uprv_strlen(close_source));
  604. printNoteElements(res->fComment, status);
  605. tabCount -= 1;
  606. write_tabs(out);
  607. T_FileStream_write(out, close_trans_unit, (int32_t)uprv_strlen(close_trans_unit));
  608. uprv_free(sid);
  609. sid = NULL;
  610. }
  611. static void
  612. bin_write_xml( struct SResource *res, const char* id, const char* language, UErrorCode *status) {
  613. const char* m_type = application_mimetype;
  614. char* sid = NULL;
  615. uint32_t crc = 0xFFFFFFFF;
  616. char fileName[1024] ={0};
  617. int32_t tLen = ( outDir == NULL) ? 0 :(int32_t)uprv_strlen(outDir);
  618. char* fn = (char*) uprv_malloc(sizeof(char) * (tLen+1024 +
  619. (res->u.fBinaryValue.fFileName !=NULL ?
  620. uprv_strlen(res->u.fBinaryValue.fFileName) :0)));
  621. const char* ext = NULL;
  622. char* f = NULL;
  623. fn[0]=0;
  624. if(res->u.fBinaryValue.fFileName != NULL){
  625. uprv_strcpy(fileName, res->u.fBinaryValue.fFileName);
  626. f = uprv_strrchr(fileName, '\\');
  627. if (f != NULL) {
  628. f++;
  629. } else {
  630. f = fileName;
  631. }
  632. ext = uprv_strrchr(fileName, '.');
  633. if (ext == NULL) {
  634. fprintf(stderr, "Error: %s is an unknown binary filename type.\n", fileName);
  635. exit(U_ILLEGAL_ARGUMENT_ERROR);
  636. }
  637. if(uprv_strcmp(ext, ".jpg")==0 || uprv_strcmp(ext, ".jpeg")==0 || uprv_strcmp(ext, ".gif")==0 ){
  638. m_type = "image";
  639. } else if(uprv_strcmp(ext, ".wav")==0 || uprv_strcmp(ext, ".au")==0 ){
  640. m_type = "audio";
  641. } else if(uprv_strcmp(ext, ".avi")==0 || uprv_strcmp(ext, ".mpg")==0 || uprv_strcmp(ext, ".mpeg")==0){
  642. m_type = "video";
  643. } else if(uprv_strcmp(ext, ".txt")==0 || uprv_strcmp(ext, ".text")==0){
  644. m_type = "text";
  645. }
  646. sid = printContainer(res, bin_unit, binary_restype, m_type, id, status);
  647. write_tabs(out);
  648. T_FileStream_write(out, bin_source, (int32_t)uprv_strlen(bin_source));
  649. tabCount+= 1;
  650. write_tabs(out);
  651. T_FileStream_write(out, external_file, (int32_t)uprv_strlen(external_file));
  652. printAttribute("href", f, (int32_t)uprv_strlen(f));
  653. T_FileStream_write(out, "/>\n", 3);
  654. tabCount -= 1;
  655. write_tabs(out);
  656. T_FileStream_write(out, close_bin_source, (int32_t)uprv_strlen(close_bin_source));
  657. printNoteElements(res->fComment, status);
  658. tabCount -= 1;
  659. write_tabs(out);
  660. T_FileStream_write(out, close_bin_unit, (int32_t)uprv_strlen(close_bin_unit));
  661. } else {
  662. char temp[256] = {0};
  663. uint32_t i = 0;
  664. int32_t len=0;
  665. sid = printContainer(res, bin_unit, binary_restype, m_type, id, status);
  666. write_tabs(out);
  667. T_FileStream_write(out, bin_source, (int32_t)uprv_strlen(bin_source));
  668. tabCount += 1;
  669. write_tabs(out);
  670. T_FileStream_write(out, internal_file, (int32_t)uprv_strlen(internal_file));
  671. printAttribute("form", application_mimetype, (int32_t) uprv_strlen(application_mimetype));
  672. while(i <res->u.fBinaryValue.fLength){
  673. len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2);
  674. crc = computeCRC(temp, len, crc);
  675. i++;
  676. }
  677. len = itostr(temp, crc, 10, 0);
  678. printAttribute("crc", temp, len);
  679. T_FileStream_write(out, ">", 1);
  680. i = 0;
  681. while(i <res->u.fBinaryValue.fLength){
  682. len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2);
  683. T_FileStream_write(out ,temp ,len);
  684. i += 1;
  685. }
  686. T_FileStream_write(out, close_internal_file, (int32_t)uprv_strlen(close_internal_file));
  687. tabCount -= 2;
  688. write_tabs(out);
  689. T_FileStream_write(out, close_bin_source, (int32_t)uprv_strlen(close_bin_source));
  690. printNoteElements(res->fComment, status);
  691. tabCount -= 1;
  692. write_tabs(out);
  693. T_FileStream_write(out, close_bin_unit, (int32_t)uprv_strlen(close_bin_unit));
  694. uprv_free(sid);
  695. sid = NULL;
  696. }
  697. uprv_free(fn);
  698. }
  699. static void
  700. table_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) {
  701. uint32_t i = 0;
  702. struct SResource *current = NULL;
  703. struct SResource *save = NULL;
  704. char* sid = NULL;
  705. if (U_FAILURE(*status)) {
  706. return ;
  707. }
  708. sid = printContainer(res, group, table_restype, NULL, id, status);
  709. if(isTopLevel) {
  710. sid[0] = '\0';
  711. }
  712. save = current = res->u.fTable.fFirst;
  713. i = 0;
  714. while (current != NULL) {
  715. res_write_xml(current, sid, language, FALSE, status);
  716. if(U_FAILURE(*status)){
  717. return;
  718. }
  719. i += 1;
  720. current = current->fNext;
  721. }
  722. tabCount -= 1;
  723. write_tabs(out);
  724. T_FileStream_write(out, close_group,(int32_t)uprv_strlen(close_group));
  725. uprv_free(sid);
  726. sid = NULL;
  727. }
  728. void
  729. res_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) {
  730. if (U_FAILURE(*status)) {
  731. return ;
  732. }
  733. if (res != NULL) {
  734. switch (res->fType) {
  735. case URES_STRING:
  736. string_write_xml (res, id, language, status);
  737. return;
  738. case URES_ALIAS:
  739. alias_write_xml (res, id, language, status);
  740. return;
  741. case URES_INT_VECTOR:
  742. intvector_write_xml (res, id, language, status);
  743. return;
  744. case URES_BINARY:
  745. bin_write_xml (res, id, language, status);
  746. return;
  747. case URES_INT:
  748. int_write_xml (res, id, language, status);
  749. return;
  750. case URES_ARRAY:
  751. array_write_xml (res, id, language, status);
  752. return;
  753. case URES_TABLE:
  754. case URES_TABLE32:
  755. table_write_xml (res, id, language, isTopLevel, status);
  756. return;
  757. default:
  758. break;
  759. }
  760. }
  761. *status = U_INTERNAL_PROGRAM_ERROR;
  762. }
  763. void
  764. bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, const char* filename,
  765. char *writtenFilename, int writtenFilenameLen,
  766. const char* language, const char* outFileName, UErrorCode *status) {
  767. char* xmlfileName = NULL;
  768. char* outputFileName = NULL;
  769. char* originalFileName = NULL;
  770. const char* fileStart = "<file xml:space = \"preserve\" source-language = \"";
  771. const char* file1 = "\" datatype = \"x-icu-resource-bundle\" ";
  772. const char* file2 = "original = \"";
  773. const char* file4 = "\" date = \"";
  774. const char* fileEnd = "</file>\n";
  775. const char* headerStart = "<header>\n";
  776. const char* headerEnd = "</header>\n";
  777. const char* bodyStart = "<body>\n";
  778. const char* bodyEnd = "</body>\n";
  779. const char *tool_start = "<tool";
  780. const char *tool_id = "genrb-" GENRB_VERSION "-icu-" U_ICU_VERSION;
  781. const char *tool_name = "genrb";
  782. char* pid = NULL;
  783. char* temp = NULL;
  784. char* lang = NULL;
  785. char* pos;
  786. int32_t first, index;
  787. time_t currTime;
  788. char timeBuf[128];
  789. outDir = outputDir;
  790. srBundle = bundle;
  791. pos = uprv_strrchr(filename, '\\');
  792. if(pos != NULL) {
  793. first = (int32_t)(pos - filename + 1);
  794. } else {
  795. first = 0;
  796. }
  797. index = (int32_t)(uprv_strlen(filename) - uprv_strlen(textExt) - first);
  798. originalFileName = uprv_malloc(sizeof(char)*index+1);
  799. uprv_memset(originalFileName, 0, sizeof(char)*index+1);
  800. uprv_strncpy(originalFileName, filename + first, index);
  801. if(uprv_strcmp(originalFileName, srBundle->fLocale) != 0) {
  802. fprintf(stdout, "Warning: The file name is not same as the resource name!\n");
  803. }
  804. temp = originalFileName;
  805. originalFileName = uprv_malloc(sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1);
  806. uprv_memset(originalFileName, 0, sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1);
  807. uprv_strcat(originalFileName, temp);
  808. uprv_strcat(originalFileName, textExt);
  809. uprv_free(temp);
  810. temp = NULL;
  811. if (language == NULL) {
  812. /* lang = parseFilename(filename, lang);
  813. if (lang == NULL) {*/
  814. /* now check if locale name is valid or not
  815. * this is to cater for situation where
  816. * pegasusServer.txt contains
  817. *
  818. * en{
  819. * ..
  820. * }
  821. */
  822. lang = parseFilename(srBundle->fLocale, lang);
  823. /*
  824. * Neither the file name nor the table name inside the
  825. * txt file contain a valid country and language codes
  826. * throw an error.
  827. * pegasusServer.txt contains
  828. *
  829. * testelements{
  830. * ....
  831. * }
  832. */
  833. if(lang==NULL){
  834. fprintf(stderr, "Error: The file name and table name do not contain a valid language code. Please use -l option to specify it.\n");
  835. exit(U_ILLEGAL_ARGUMENT_ERROR);
  836. }
  837. /* }*/
  838. } else {
  839. lang = uprv_malloc(sizeof(char)*uprv_strlen(language) +1);
  840. uprv_memset(lang, 0, sizeof(char)*uprv_strlen(language) +1);
  841. uprv_strcpy(lang, language);
  842. }
  843. if(outFileName) {
  844. outputFileName = uprv_malloc(sizeof(char)*uprv_strlen(outFileName) + 1);
  845. uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(outFileName) + 1);
  846. uprv_strcpy(outputFileName,outFileName);
  847. } else {
  848. outputFileName = uprv_malloc(sizeof(char)*uprv_strlen(srBundle->fLocale) + 1);
  849. uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(srBundle->fLocale) + 1);
  850. uprv_strcpy(outputFileName,srBundle->fLocale);
  851. }
  852. if(outputDir) {
  853. xmlfileName = uprv_malloc(sizeof(char)*(uprv_strlen(outputDir) + uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1);
  854. uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputDir)+ uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1);
  855. } else {
  856. xmlfileName = uprv_malloc(sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1);
  857. uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1);
  858. }
  859. if(outputDir){
  860. uprv_strcpy(xmlfileName, outputDir);
  861. if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){
  862. uprv_strcat(xmlfileName,U_FILE_SEP_STRING);
  863. }
  864. }
  865. uprv_strcat(xmlfileName,outputFileName);
  866. uprv_strcat(xmlfileName,xliffExt);
  867. if (writtenFilename) {
  868. uprv_strncpy(writtenFilename, xmlfileName, writtenFilenameLen);
  869. }
  870. if (U_FAILURE(*status)) {
  871. goto cleanup_bundle_write_xml;
  872. }
  873. out= T_FileStream_open(xmlfileName,"w");
  874. if(out==NULL){
  875. *status = U_FILE_ACCESS_ERROR;
  876. goto cleanup_bundle_write_xml;
  877. }
  878. T_FileStream_write(out,xmlHeader, (int32_t)uprv_strlen(xmlHeader));
  879. if(outputEnc && *outputEnc!='\0'){
  880. /* store the output encoding */
  881. enc = outputEnc;
  882. conv=ucnv_open(enc,status);
  883. if(U_FAILURE(*status)){
  884. goto cleanup_bundle_write_xml;
  885. }
  886. }
  887. T_FileStream_write(out,bundleStart, (int32_t)uprv_strlen(bundleStart));
  888. write_tabs(out);
  889. T_FileStream_write(out, fileStart, (int32_t)uprv_strlen(fileStart));
  890. /* check if lang and language are the same */
  891. if(language != NULL && uprv_strcmp(lang, srBundle->fLocale)!=0){
  892. fprintf(stderr,"Warning: The top level tag in the resource and language specified are not the same. Please check the input.\n");
  893. }
  894. T_FileStream_write(out,lang, (int32_t)uprv_strlen(lang));
  895. T_FileStream_write(out,file1, (int32_t)uprv_strlen(file1));
  896. T_FileStream_write(out,file2, (int32_t)uprv_strlen(file2));
  897. T_FileStream_write(out,originalFileName, (int32_t)uprv_strlen(originalFileName)); T_FileStream_write(out,file4, (int32_t)uprv_strlen(file4));
  898. time(&currTime);
  899. strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&currTime));
  900. T_FileStream_write(out,timeBuf, (int32_t)uprv_strlen(timeBuf));
  901. T_FileStream_write(out,"\">\n", 3);
  902. tabCount += 1;
  903. write_tabs(out);
  904. T_FileStream_write(out,headerStart, (int32_t)uprv_strlen(headerStart));
  905. tabCount += 1;
  906. write_tabs(out);
  907. T_FileStream_write(out, tool_start, (int32_t) uprv_strlen(tool_start));
  908. printAttribute("tool-id", tool_id, (int32_t) uprv_strlen(tool_id));
  909. printAttribute("tool-name", tool_name, (int32_t) uprv_strlen(tool_name));
  910. T_FileStream_write(out, "/>\n", 3);
  911. tabCount -= 1;
  912. write_tabs(out);
  913. T_FileStream_write(out,headerEnd, (int32_t)uprv_strlen(headerEnd));
  914. write_tabs(out);
  915. tabCount += 1;
  916. T_FileStream_write(out,bodyStart, (int32_t)uprv_strlen(bodyStart));
  917. res_write_xml(bundle->fRoot, bundle->fLocale, lang, TRUE, status);
  918. tabCount -= 1;
  919. write_tabs(out);
  920. T_FileStream_write(out,bodyEnd, (int32_t)uprv_strlen(bodyEnd));
  921. tabCount--;
  922. write_tabs(out);
  923. T_FileStream_write(out,fileEnd, (int32_t)uprv_strlen(fileEnd));
  924. tabCount--;
  925. write_tabs(out);
  926. T_FileStream_write(out,bundleEnd,(int32_t)uprv_strlen(bundleEnd));
  927. T_FileStream_close(out);
  928. ucnv_close(conv);
  929. cleanup_bundle_write_xml:
  930. if(originalFileName!= NULL) {
  931. uprv_free(originalFileName);
  932. originalFileName = NULL;
  933. }
  934. if(lang != NULL) {
  935. uprv_free(lang);
  936. lang = NULL;
  937. }
  938. if(pid != NULL) {
  939. uprv_free(pid);
  940. pid = NULL;
  941. }
  942. if(xmlfileName != NULL) {
  943. uprv_free(xmlfileName);
  944. pid = NULL;
  945. }
  946. if(outputFileName != NULL){
  947. uprv_free(outputFileName);
  948. pid = NULL;
  949. }
  950. }