Advertisement
Guest User

Untitled

a guest
Apr 10th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. // p_trans.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <io.h>
  9. #include <ctype.h>
  10.  
  11. void ResourceTranslate(char *sName,int cPass);
  12. void SourceTranslate(char *sName);
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16. FILE *f;
  17. f=fopen("t.txt","r");
  18. char s[80];
  19. fflush(f);
  20. fgets(s,80,f);
  21. puts(s);
  22. int nChoice;
  23. int nPass;
  24. char sName[256];
  25. if(argc==3)
  26. {
  27. if(!((strcmp(argv[1],"1")==0)*(strcmp(argv[1],"2"))))
  28. {
  29. nChoice=argv[1][0]-48;
  30. nPass=1;
  31. }
  32. else
  33. {
  34. nChoice=1;
  35. nPass=2;
  36. }
  37. strcpy(sName,argv[2]);
  38. }
  39. else
  40. {
  41. printf("Project translation program. (C) zHz, 2012\nChoose kind of work:\n1. Resource file translation\n2. Source file translation\nEnter 1 or 2: >");
  42. scanf("%d",&nChoice);
  43.  
  44. switch(nChoice)
  45. {
  46. case 1:
  47. printf("Enter resource file name: >");
  48. fflush(stdin);
  49. gets(sName);
  50. printf("Enter pass: 1: 1st, 2: 2nd >");
  51. scanf("%d",&nPass);
  52. break;
  53. case 2:
  54. printf("Enter folder name: >");
  55. scanf("%s",sName);
  56. break;
  57. }
  58. }
  59. if(nChoice==1)
  60. ResourceTranslate(sName,nPass);
  61. else
  62. SourceTranslate(sName);
  63. return 0;
  64. }
  65.  
  66. #define LINE_LENGTH 512
  67.  
  68. void ResourceTranslate(char *sName,int nPass)
  69. {
  70. FILE *fin, *ftl, *fout;
  71. int nx, nb=-1,ne=-1;
  72. bool bNext=false;
  73. char sLine[LINE_LENGTH],sToken[LINE_LENGTH],sTemp[LINE_LENGTH];
  74. fin=fopen(sName,"r");
  75. ftl=fopen("rc_tl.txt",nPass==1?"w":"r");
  76. fout=fopen("temp.rc3","w");
  77. if(!(fin&&ftl&&fout))
  78. {
  79. printf("Error opening file");
  80. exit(1);
  81. }
  82. while(!feof(fin))
  83. {
  84. fgets(sLine,LINE_LENGTH-1,fin);
  85. if(sLine[0]!='#')
  86. for(nx=0;nx<strlen(sLine);nx++)
  87. if(((sLine[nx]=='\"')&&(sLine[nx-1]!='\\'))||((sLine[nx]=='\'')&&(nx+1==strlen(sLine))))
  88. {
  89. ((nb==-1)?nb:ne)=nx;
  90. if((nb!=-1)&&(ne!=-1))
  91. {
  92. if(nPass==1)
  93. {
  94. strncpy(sToken,sLine+nb,ne-nb+1);
  95. sToken[ne-nb+1]='\0';
  96. fprintf(ftl,"%s\n",sToken);
  97. }
  98. else
  99. {
  100. strcpy(sTemp,sLine);strcat(sTemp,sLine);
  101. sTemp[0]='\0';
  102. fgets(sToken,LINE_LENGTH-1,ftl);
  103. strncpy(sTemp,sLine,nb);
  104. sTemp[nb]='\0';
  105. strcat(sTemp,sToken);
  106. sTemp[nb+strlen(sToken)-1]='\0';
  107. strcat(sTemp,sLine+ne+1);
  108. strcpy(sLine,sTemp);
  109. nx=nb+strlen(sToken)-2;
  110. }
  111. nb=ne=-1;
  112. if((sLine[nx]=='\\')&&((nx+1)==strlen(sLine)))
  113. nb=0;
  114. }
  115. }
  116. if(nPass==2)
  117. fputs(sLine,fout);
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124. fclose(fin);
  125. fclose(ftl);
  126. fclose(fout);
  127.  
  128. }
  129.  
  130. int ProcessToken(char *sToken)
  131. {
  132. static int nID=0;
  133. char sID[10];
  134. int nx;
  135. for(nx=0;nx<strlen(sToken);nx++)
  136. {
  137. if(sToken[nx]<0)
  138. sToken[nx]='_';
  139. else
  140. if(isalpha(sToken[nx]))
  141. sToken[nx]=toupper(sToken[nx]);
  142. else
  143. if(!isdigit(sToken[nx]))
  144. sToken[nx]='_';
  145. }
  146. sprintf(sID,"%d",nID);
  147. strcat(sToken,sID);
  148. nID++;
  149. return nID-1;
  150.  
  151. }
  152.  
  153.  
  154. void SourceTranslate(char *sName)
  155. {
  156. FILE *fin, *fh, *fout, *fin_out;
  157. _finddata_t t;
  158. int nSearchID;
  159. int nPhraseID;
  160. int nx, nb=-1,ne=-1;
  161. char sLine[LINE_LENGTH],sToken[LINE_LENGTH],sTemp[LINE_LENGTH],sMacro[LINE_LENGTH];
  162. char sPath[256];
  163. strcpy(sPath, sName);
  164. strcat(sPath,"*.cpp");
  165. nSearchID=_findfirst(sPath,&t);
  166. fh=fopen("tl.h","w");
  167. fputs("#ifndef __TL_H__\n#define __TL_H__\n",fh);
  168. fout=fopen("russian.txt","w");
  169. if(nSearchID==-1)
  170. {
  171. printf("Files absent.\n");
  172. exit(1);
  173. }
  174. strcpy(sPath,sName);
  175. do
  176. {
  177. strcpy(sName,sPath);
  178. strcat(sName,t.name);
  179. sTemp[0]='\0';
  180. fin=fopen(sName,"r");
  181. fin_out=fopen("1.tmp","w");
  182. //fprintf(fin_out,"#include \"translation.h\"\n");
  183.  
  184. if((fin&&fout&&fh)==NULL)
  185. {
  186. printf("Error opening file!");
  187. exit(1);
  188. }
  189. int ln=0;
  190. fprintf(fh,"// FILE : %s\n",t.name);
  191. fprintf(fout,"// FILE : %s\n",t.name);
  192. while(!feof(fin))
  193. {
  194. if(!fgets(sLine,LINE_LENGTH-1,fin))
  195. {
  196. printf("Error!\n");
  197. break;
  198. }
  199. ln++;
  200. if(sLine[0]!='#')
  201. {
  202. if((nb>0)||(ne>0))
  203. nb=ne=-1;
  204. for(nx=0;nx<strlen(sLine);nx++)
  205. if((sLine[nx]=='\"')||((sLine[nx]=='\\')&&((nx+2)==strlen(sLine))))
  206. {
  207. if(nx>1)
  208. if((sLine[nx-1]=='\\')&&(sLine[nx-2]!='\\'))
  209. continue;
  210.  
  211. ((nb==-1)?nb:ne)=nx;
  212. if((nb!=-1)&&(ne!=-1))
  213. {
  214. strncpy(sToken,sLine+nb,ne-nb+1);
  215. sToken[ne-nb+1]='\0';
  216. strcpy(sMacro,sToken);
  217. nPhraseID=ProcessToken(sMacro);
  218. fprintf(fh,"#define %s %d\n",sMacro,nPhraseID);
  219. fprintf(fout,"%s=%s\n",sMacro,sToken);
  220.  
  221. sTemp[0]='\0';
  222. strncpy(sTemp,sLine,nb);
  223. sTemp[nb]='\0';
  224. strcat(sTemp,"tr(");
  225. strcat(sTemp,sMacro);
  226. strcat(sTemp,")");
  227. sTemp[nb+strlen(sMacro)+4]='\0';
  228. strcat(sTemp,sLine+ne+1);
  229. strcpy(sLine,sTemp);
  230. nx=nb+strlen(sToken)-2;
  231.  
  232. nb=ne=-1;
  233. if((sLine[nx]=='\\')&&((nx+2)==strlen(sLine)))
  234. nb=0;
  235.  
  236. }
  237. }
  238.  
  239. }
  240. fputs(sLine,fin_out);
  241. }
  242. fclose(fin);
  243. fclose(fin_out);
  244. remove(sName);
  245. rename("1.tmp",sName);
  246. }while(!_findnext(nSearchID,&t));
  247.  
  248.  
  249. fprintf(fh,"\n\n#define MAX_PHRASES %d\n\n",nPhraseID+1);
  250. fputs("#endif\n",fh);
  251. fclose(fh);
  252. fclose(fout);
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement