langtobin.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:3k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: langtobin.c 271 2005-08-09 08:31:35Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #define MAXLINE 1024
  28. #define FOURCC(a,b,c,d) 
  29. (((unsigned char)a << 0) | ((unsigned char)b << 8) | 
  30. ((unsigned char)c << 16) | ((unsigned char)d << 24))
  31. #define LANG_DEFAULT FOURCC('E','N','_','_')
  32. static void Filter(char* i)
  33. {
  34. char* j=i;
  35. for (;*i;++i,++j)
  36. {
  37. if (i[0]=='\' && i[1]=='n')
  38. {
  39. *j=10;
  40. ++i;
  41. }
  42. else
  43. *j=*i;
  44. }
  45. *j=0;
  46. }
  47. int ToFourCC(const char* In)
  48. {
  49. char s[4+1];
  50. int i;
  51. for (i=0;i<4;++i)
  52. if (!In[i])
  53. {
  54. for (;i<4;++i)
  55. s[i] = '_';
  56. }
  57. else
  58. s[i] = (char)toupper(In[i]);
  59. return FOURCC(s[0],s[1],s[2],s[3]);
  60. }
  61. int ToInt(const char* In, int Hex)
  62. {
  63. int v=0;
  64. sscanf(In,Hex ? "%X":"%d",&v);
  65. return v;
  66. }
  67. void Convert(const char* p, int n,FILE* out)
  68. {
  69. char* s = (char*)malloc(MAXLINE*sizeof(char));
  70. int CodePage = -1;
  71. int Id,Len,Lang = 0;
  72. char* q;
  73. int i,No;
  74. if (s)
  75. {
  76. while (n>0)
  77. {
  78. for (i=0;n>0 && *p!=13 && *p!=10;++p,--n)
  79. if (i<MAXLINE-4)
  80. s[i++] = *p;
  81. for (;n>0 && (*p==13 || *p==10);++p,--n)
  82. s[i]=0;
  83. s[i+1]=0;
  84. s[i+2]=0;
  85. s[i+3]=0;
  86. for (i=0;isspace(s[i]);++i);
  87. if (s[i]==0) continue;
  88. if (s[i]==';')
  89. {
  90. sscanf(s+i,";CODEPAGE = %d",&CodePage);
  91. continue;
  92. }
  93. if (s[i]=='[')
  94. {
  95. ++i;
  96. q = strchr(s+i,']');
  97. if (!q || CodePage<0) break; // invalid language file
  98. *q = 0;
  99. Lang = ToFourCC(s+i);
  100. if (Lang == FOURCC('D','E','F','A'))
  101. Lang = LANG_DEFAULT;
  102. fwrite(&Lang,1,sizeof(int),out);
  103. fwrite(&CodePage,1,sizeof(int),out);
  104. }
  105. else
  106. {
  107. q = strchr(s+i,'=');
  108. if (!q || !Lang) break; // invalid language file
  109. *q = 0;
  110. ++q;
  111. if (strlen(s+i)>4)
  112. {
  113. if (strlen(s+i)<8)
  114. No = ToFourCC(s+i+4);
  115. else
  116. {
  117. No = ToInt(s+i+4,1);
  118. if (No >= 32768)
  119. No -= 65536;
  120. }
  121. }
  122. else
  123. No = 0;
  124. Filter(q);
  125. Id = ToFourCC(s+i);
  126. fwrite(&Id,1,sizeof(int),out);
  127. fwrite(&No,1,sizeof(int),out);
  128. Len = strlen(q)+1;
  129. Len = (Len+sizeof(int)-1) & ~(sizeof(int)-1);
  130. fwrite(q,1,Len,out);
  131. }
  132. }
  133. }
  134. free(s);
  135. }
  136. int main(int pc,const char** ps)
  137. {
  138. FILE* in = fopen(ps[1],"rb");
  139. FILE* out = fopen(ps[2],"wb+");
  140. if (in && out)
  141. {
  142. int len = 0x10000;
  143. char* data = (char*)malloc(len);
  144. if (data)
  145. {
  146. len = fread(data,1,len,in);
  147. Convert(data,len,out);
  148. free(data);
  149. }
  150. fclose(in);
  151. fclose(out);
  152. }
  153. return 0;
  154. }