home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************\
- *
- * MODULE : FILE_CPCONVERT.C
- *
- * PROGRAMS : CPCONV
- *
- * PURPOSE : Conversion of source file in source encoding (code page)
- * to target file in target encoding (code page) and
- * supporting variables and functions
- *
- \************************************************************************/
-
- #include <windows.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "FILE_CPCONVERT.H"
- #include "CONV_SUB.H"
-
- /************************************************************************\
- * Conversion mode setting
- * DifferentTimeOnly=TRUE convert only files with different date/time
- * (if Target<>Source)
- \************************************************************************/
-
- static int DifferentTimeOnly = FALSE;
-
- void SetDifferentTimeOnly (BOOL different)
- {
- DifferentTimeOnly=different;
- }
-
- /************************************************************************\
- * Time mode setting
- * current set date/time of the target file to the current file
- * original set date/time of the target file to be the same as
- * of the source (original) file
- \************************************************************************/
-
- enum {original, current};
-
- static int TimeMode = original;
- FILETIME CurrentTime;
-
- void SetOriginalFileTime (void)
- {
- TimeMode=original;
- }
-
- void SetCurrentFileTime (void)
- {
- TimeMode=current;
- CoFileTimeNow(&CurrentTime);
- }
-
- /************************************************************************\
- * Encoding setting
- * EncDir encoding directory (directory of the code page tables)
- * CodePage target code page
- \************************************************************************/
-
- static char EncDir[_MAX_PATH];
- static char SourceCodePage[_MAX_FNAME]={"CP1250"};
- static char TargetCodePage[_MAX_FNAME]={"CP1250"};
-
- void SetEncodingDirectory (char *directory)
- {
- int n;
- strncpy(EncDir,directory,_MAX_PATH);
- EncDir[_MAX_PATH-1]='\0';
- n=strlen(EncDir);
- while (n>0 && EncDir[n]=='\\')
- {
- EncDir[n]='\0';
- n--;
- }
- }
-
- void SetSourceCodePage (char *cp)
- {
- strncpy (SourceCodePage,cp,_MAX_FNAME);
- SourceCodePage[_MAX_FNAME-1]='\0';
- }
-
- void SetTargetCodePage (char *cp)
- {
- strncpy (TargetCodePage,cp,_MAX_FNAME);
- TargetCodePage[_MAX_FNAME-1]='\0';
- }
-
- /************************************************************************\
- * Directory for temporary files
- \************************************************************************/
-
- static char TempDir[_MAX_PATH]={""};
-
- /************************************************************************\
- * Conversion tables
- * Conv0,nConv0 structure for creation of conversion table
- * Conv conversion character string for one-to-one conversion
- * ConvTable,nChar conversion string table for one-to-more conversion
- * DefChar default replace character
- * OneToMore TRUE if one-to-more allowed
- * UseTable TRUE if one-to-more table has to be used
- * Copy TRUE if there is no difference between source and
- * target code page
- \************************************************************************/
-
- static struct {
- int ischar;
- char description[30];
- char tchar;
- char tstring[20];
- BOOL set;
- } Conv0[384];
-
- static int nConv0=0;
-
- static char Conv[258];
- static char ConvTable[256][20];
- static int nChar[256];
-
- static char DefChar=' ';
- static BOOL OneToMore=FALSE;
- static BOOL UseTable=FALSE;
- static BOOL Copy=FALSE;
-
- void SetOneToMoreConversion (BOOL onetomore)
- {
- OneToMore=onetomore;
- }
-
- void SetDefaultCharacter (char defchar)
- {
- DefChar=defchar;
- }
-
- /************************************************************************\
- *
- * FUNCTION : InitConversionTable
- *
- * LOCAL VARS : fname - code page table filename
- * f - code page table file handle
- * line - buffer for code page table line
- * description - character description
- * noaccentstring - string for character replace if there
- * isn't target character
- * ichar - character code
- *
- \************************************************************************/
-
- BOOL InitConversionTable (void)
- {
- int i,j,n,ichar;
- char description[30],noaccentstring[20],line[100];
- char fname[_MAX_FNAME];
- FILE *f;
-
- // check if source and target CP are different
-
- if (stricmp(SourceCodePage,TargetCodePage)==0)
- {
- Copy=TRUE;
- printf("\n Warning: Source and target code page are the same.");
- }
-
- if (GetTempPath((DWORD)sizeof(TempDir),TempDir)==0)
- return (FALSE);
-
- // source code page reading
-
- strcat(strcat(strcat(strcpy(fname,EncDir),"\\"),SourceCodePage),".ENC");
- f=fopen(fname,"rt");
- if (f==NULL)
- {
- printf("\n Error: Unable to find %s",fname);
- return(FALSE);
- }
- while(!feof(f))
- {
- if (fgets(line,100,f)==NULL)
- break;
- if (line[0]==';')
- continue;
- line[99]='\0';
- memset(description,0,30);
- sscanf(line,"%d %s",&ichar,description);
- if (strlen(description)<1)
- continue;
- Conv0[nConv0].ischar=ichar;
- strcpy(Conv0[nConv0].description,description);
- Conv0[nConv0].tchar=DefChar;
- memset(Conv0[nConv0].tstring,0,20);
- Conv0[nConv0].tstring[0]=DefChar;
- Conv0[nConv0].set=FALSE;
- nConv0++;
- }
- fclose(f);
-
- // target code page table reading and assignment to the source
- // code page characters
-
- strcat(strcat(strcat(strcpy(fname,EncDir),"\\"),TargetCodePage),".ENC");
- f=fopen(fname,"rt");
- if (f==NULL)
- {
- printf("\nError: Unable to find %s",fname);
- return (FALSE);
- }
- while(!feof(f))
- {
- if (fgets(line,100,f)==NULL)
- break;
- if (line[0]==';')
- continue;
- line[99]='\0';
- memset(description,0,30);
- sscanf(line,"%d %s",&ichar,description);
- if (strlen(description)<1)
- continue;
- for(i=0; i<nConv0; i++)
- {
- if (Conv0[i].set==TRUE)
- continue;
- if (strcmp(description,Conv0[i].description)==0)
- {
- Conv0[i].tchar=(char)ichar;
- Conv0[i].tstring[0]=(char)ichar;
- Conv0[i].set=TRUE;
- break;
- }
- }
- }
- fclose(f);
-
- // not-accented strings assignment to source code page characters
- // if there is no other assignment
-
- strcat(strcpy(fname,EncDir),"\\ACCENT.CONV");
- f=fopen(fname,"rt");
- if (f==NULL)
- {
- printf("\n Warning: Unable to find %s."
- "\n Remaining accented characters cannot be replaced"
- "\n by non-accented characters.",fname);
- goto finish;
- }
- while(!feof(f))
- {
- if (fgets(line,100,f)==NULL)
- break;
- if (line[0]==';')
- continue;
- line[99]='\0';
- memset(description,0,30);
- memset(noaccentstring,0,20);
- sscanf(line,"%s %s",description,noaccentstring);
- if (strlen(description)<1)
- continue;
- for (i=0; i<nConv0; i++)
- {
- if (Conv0[i].set==TRUE)
- continue;
- if (strcmp(description,Conv0[i].description)==0)
- {
- n=strlen(noaccentstring);
- if (n==1)
- {
- Conv0[i].tchar=noaccentstring[0];
- Conv0[i].tstring[0]=noaccentstring[0];
- Conv0[i].set=TRUE;
- }
- else if (n>1 && OneToMore)
- {
- strcpy(Conv0[i].tstring,noaccentstring);
- UseTable=TRUE;
- Conv0[i].set=TRUE;
- }
- }
- }
- }
- fclose(f);
-
- // finishing file conversion tables
-
- finish:
- if (UseTable)
- {
- for (i=0; i<256; i++)
- {
- ConvTable[i][0]=(char)i;
- ConvTable[i][1]='\0';
- nChar[i]=1;
- }
- for (i=0; i<nConv0; i++)
- {
- j=Conv0[i].ischar;
- strcpy(ConvTable[j],Conv0[i].tstring);
- nChar[j]=strlen(ConvTable[j]);
- }
- }
- else
- {
- BOOL Convert=FALSE;
- for (i=0; i<256; i++)
- Conv[i]=(char)i;
- Conv[256]='\0';
- for (i=0; i<nConv0; i++)
- {
- j=Conv0[i].ischar;
- if (Conv[j]!=Conv0[i].tchar)
- {
- Convert=TRUE;
- Conv[j]=Conv0[i].tchar;
- }
- }
- if (Convert==FALSE)
- Copy=TRUE;
- }
-
- return (TRUE);
- }
-
- /************************************************************************\
- *
- * FUNCTION : ConvertFile
- *
- * INPUTS : SourceDir - source file name
- * TargetDir - target file name
- *
- * RETURNS : None
- *
- * LOCAL VARS : hSFile - source file handle
- * hTFile - target file handle
- * hTempFile - temporary file handle
- * TempName - temporary file name
- * stime - source file time
- * ttime - target file time
- * buff - buffer
- * dwBytesRead - number of read bytes
- * dwBytesWritten - number of written bytes
- * dwPos - file pointer
- *
- \************************************************************************/
-
- void ConvertFile(char *SFileName, char *TFileName)
- {
- HANDLE hSFile, hTFile, hTempFile;
- char TempName[_MAX_FNAME];
- char buff[16384];
- DWORD dwBytesRead,dwBytesWritten,dwPos;
- FILETIME stime,ttime;
- int i,j;
-
- hSFile= CreateFile(SFileName,GENERIC_READ,0,
- (LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
- if (hSFile==INVALID_HANDLE_VALUE)
- return;
- GetFileTime(hSFile,NULL,NULL,&stime);
-
- if (DifferentTimeOnly && stricmp(SFileName,TFileName)!=0)
- {
- hTFile= CreateFile(TFileName,GENERIC_READ,0,
- (LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
- if (hTFile!=INVALID_HANDLE_VALUE)
- {
- GetFileTime(hTFile,NULL,NULL,&ttime);
- CloseHandle(hTFile);
-
- if (stime.dwLowDateTime==ttime.dwLowDateTime &&
- stime.dwHighDateTime==ttime.dwHighDateTime)
- return;
- }
- }
-
- if (Copy)
- {
- CloseHandle(hSFile);
- CopyFile(SFileName,TFileName,FALSE);
- printf("\n Copied target file: %s", TFileName);
- goto settime;
- }
-
- if (GetTempFileName(TempDir,"CPC",0,TempName)==0)
- return;
- hTempFile= CreateFile(TempName,GENERIC_WRITE,0,
- (LPSECURITY_ATTRIBUTES)NULL,CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
- if (hTempFile==INVALID_HANDLE_VALUE)
- return;
-
- do
- {
- if (ReadFile(hSFile, (LPSTR)buff, 16384, &dwBytesRead, NULL))
- {
- if (!UseTable)
- {
- for (i=0; i<(int)dwBytesRead; i++)
- {
- j=(int)buff[i];
- if (j<0)
- j+=256;
- buff[i]=Conv[j];
- }
- dwPos=SetFilePointer(hTempFile,0,(LPLONG)NULL,FILE_END);
- LockFile(hTempFile,dwPos,0,dwPos+dwBytesRead,0);
- WriteFile(hTempFile,(LPSTR)buff,dwBytesRead,&dwBytesWritten,NULL);
- UnlockFile(hTempFile,dwPos,0,dwPos+dwBytesRead,0);
- }
- else
- {
- for (i=0; i<(int)dwBytesRead; i++)
- {
- j=(int)buff[i];
- if (j<0)
- j+=256;
- WriteFile(hTempFile,(LPSTR)ConvTable[j],nChar[j],
- &dwBytesWritten,NULL);
- }
- }
- }
- }
- while (dwBytesRead==16384);
- CloseHandle(hSFile);
- CloseHandle(hTempFile);
-
- CopyFile(TempName,TFileName,FALSE);
- DeleteFile(TempName);
- printf("\n Converted target file: %s", TFileName);
-
- settime:
- hTFile= CreateFile(TFileName,GENERIC_WRITE,0,
- (LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
- if (hTFile==INVALID_HANDLE_VALUE)
- return;
- if (TimeMode==current)
- SetFileTime(hTFile,NULL,NULL,&CurrentTime);
- else
- SetFileTime(hTFile,NULL,NULL,&stime);
- CloseHandle(hTFile);
- return;
- }
-
-