Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // p_trans.cpp : Defines the entry point for the console application.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <io.h>
- #include <ctype.h>
- void ResourceTranslate(char *sName,int cPass);
- void SourceTranslate(char *sName);
- int main(int argc, char* argv[])
- {
- FILE *f;
- f=fopen("t.txt","r");
- char s[80];
- fflush(f);
- fgets(s,80,f);
- puts(s);
- int nChoice;
- int nPass;
- char sName[256];
- if(argc==3)
- {
- if(!((strcmp(argv[1],"1")==0)*(strcmp(argv[1],"2"))))
- {
- nChoice=argv[1][0]-48;
- nPass=1;
- }
- else
- {
- nChoice=1;
- nPass=2;
- }
- strcpy(sName,argv[2]);
- }
- else
- {
- printf("Project translation program. (C) zHz, 2012\nChoose kind of work:\n1. Resource file translation\n2. Source file translation\nEnter 1 or 2: >");
- scanf("%d",&nChoice);
- switch(nChoice)
- {
- case 1:
- printf("Enter resource file name: >");
- fflush(stdin);
- gets(sName);
- printf("Enter pass: 1: 1st, 2: 2nd >");
- scanf("%d",&nPass);
- break;
- case 2:
- printf("Enter folder name: >");
- scanf("%s",sName);
- break;
- }
- }
- if(nChoice==1)
- ResourceTranslate(sName,nPass);
- else
- SourceTranslate(sName);
- return 0;
- }
- #define LINE_LENGTH 512
- void ResourceTranslate(char *sName,int nPass)
- {
- FILE *fin, *ftl, *fout;
- int nx, nb=-1,ne=-1;
- bool bNext=false;
- char sLine[LINE_LENGTH],sToken[LINE_LENGTH],sTemp[LINE_LENGTH];
- fin=fopen(sName,"r");
- ftl=fopen("rc_tl.txt",nPass==1?"w":"r");
- fout=fopen("temp.rc3","w");
- if(!(fin&&ftl&&fout))
- {
- printf("Error opening file");
- exit(1);
- }
- while(!feof(fin))
- {
- fgets(sLine,LINE_LENGTH-1,fin);
- if(sLine[0]!='#')
- for(nx=0;nx<strlen(sLine);nx++)
- if(((sLine[nx]=='\"')&&(sLine[nx-1]!='\\'))||((sLine[nx]=='\'')&&(nx+1==strlen(sLine))))
- {
- ((nb==-1)?nb:ne)=nx;
- if((nb!=-1)&&(ne!=-1))
- {
- if(nPass==1)
- {
- strncpy(sToken,sLine+nb,ne-nb+1);
- sToken[ne-nb+1]='\0';
- fprintf(ftl,"%s\n",sToken);
- }
- else
- {
- strcpy(sTemp,sLine);strcat(sTemp,sLine);
- sTemp[0]='\0';
- fgets(sToken,LINE_LENGTH-1,ftl);
- strncpy(sTemp,sLine,nb);
- sTemp[nb]='\0';
- strcat(sTemp,sToken);
- sTemp[nb+strlen(sToken)-1]='\0';
- strcat(sTemp,sLine+ne+1);
- strcpy(sLine,sTemp);
- nx=nb+strlen(sToken)-2;
- }
- nb=ne=-1;
- if((sLine[nx]=='\\')&&((nx+1)==strlen(sLine)))
- nb=0;
- }
- }
- if(nPass==2)
- fputs(sLine,fout);
- }
- fclose(fin);
- fclose(ftl);
- fclose(fout);
- }
- int ProcessToken(char *sToken)
- {
- static int nID=0;
- char sID[10];
- int nx;
- for(nx=0;nx<strlen(sToken);nx++)
- {
- if(sToken[nx]<0)
- sToken[nx]='_';
- else
- if(isalpha(sToken[nx]))
- sToken[nx]=toupper(sToken[nx]);
- else
- if(!isdigit(sToken[nx]))
- sToken[nx]='_';
- }
- sprintf(sID,"%d",nID);
- strcat(sToken,sID);
- nID++;
- return nID-1;
- }
- void SourceTranslate(char *sName)
- {
- FILE *fin, *fh, *fout, *fin_out;
- _finddata_t t;
- int nSearchID;
- int nPhraseID;
- int nx, nb=-1,ne=-1;
- char sLine[LINE_LENGTH],sToken[LINE_LENGTH],sTemp[LINE_LENGTH],sMacro[LINE_LENGTH];
- char sPath[256];
- strcpy(sPath, sName);
- strcat(sPath,"*.cpp");
- nSearchID=_findfirst(sPath,&t);
- fh=fopen("tl.h","w");
- fputs("#ifndef __TL_H__\n#define __TL_H__\n",fh);
- fout=fopen("russian.txt","w");
- if(nSearchID==-1)
- {
- printf("Files absent.\n");
- exit(1);
- }
- strcpy(sPath,sName);
- do
- {
- strcpy(sName,sPath);
- strcat(sName,t.name);
- sTemp[0]='\0';
- fin=fopen(sName,"r");
- fin_out=fopen("1.tmp","w");
- //fprintf(fin_out,"#include \"translation.h\"\n");
- if((fin&&fout&&fh)==NULL)
- {
- printf("Error opening file!");
- exit(1);
- }
- int ln=0;
- fprintf(fh,"// FILE : %s\n",t.name);
- fprintf(fout,"// FILE : %s\n",t.name);
- while(!feof(fin))
- {
- if(!fgets(sLine,LINE_LENGTH-1,fin))
- {
- printf("Error!\n");
- break;
- }
- ln++;
- if(sLine[0]!='#')
- {
- if((nb>0)||(ne>0))
- nb=ne=-1;
- for(nx=0;nx<strlen(sLine);nx++)
- if((sLine[nx]=='\"')||((sLine[nx]=='\\')&&((nx+2)==strlen(sLine))))
- {
- if(nx>1)
- if((sLine[nx-1]=='\\')&&(sLine[nx-2]!='\\'))
- continue;
- ((nb==-1)?nb:ne)=nx;
- if((nb!=-1)&&(ne!=-1))
- {
- strncpy(sToken,sLine+nb,ne-nb+1);
- sToken[ne-nb+1]='\0';
- strcpy(sMacro,sToken);
- nPhraseID=ProcessToken(sMacro);
- fprintf(fh,"#define %s %d\n",sMacro,nPhraseID);
- fprintf(fout,"%s=%s\n",sMacro,sToken);
- sTemp[0]='\0';
- strncpy(sTemp,sLine,nb);
- sTemp[nb]='\0';
- strcat(sTemp,"tr(");
- strcat(sTemp,sMacro);
- strcat(sTemp,")");
- sTemp[nb+strlen(sMacro)+4]='\0';
- strcat(sTemp,sLine+ne+1);
- strcpy(sLine,sTemp);
- nx=nb+strlen(sToken)-2;
- nb=ne=-1;
- if((sLine[nx]=='\\')&&((nx+2)==strlen(sLine)))
- nb=0;
- }
- }
- }
- fputs(sLine,fin_out);
- }
- fclose(fin);
- fclose(fin_out);
- remove(sName);
- rename("1.tmp",sName);
- }while(!_findnext(nSearchID,&t));
- fprintf(fh,"\n\n#define MAX_PHRASES %d\n\n",nPhraseID+1);
- fputs("#endif\n",fh);
- fclose(fh);
- fclose(fout);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement