home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1999 June
/
Chip_1999-06_cd.bin
/
ctenari
/
Kvarda
/
source
/
FILE_HTMLADD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-30
|
8KB
|
282 lines
/************************************************************************\
*
* MODULE : FILE_HTMLADD.C
*
* PROGRAMS : HTMLADD
*
* PURPOSE : Conversion of source file to target file replacing
* source code page characters with codes >127 by
* HTML character entities
*
\************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "CONV_SUB.H"
/************************************************************************\
* 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 CodePage[_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 (CodePage,cp,_MAX_FNAME);
CodePage[_MAX_FNAME-1]='\0';
}
/************************************************************************\
* Directory for temporary files
\************************************************************************/
static char TempDir[_MAX_PATH]={""};
/************************************************************************\
* Conversion tables
* Conv0,nConv0 structure for creation of conversion table
* Conv,nConv conversion table
\************************************************************************/
static struct {
char entity[20];
char description[30];
int ichar;
BOOL use;
} Conv0[256];
static int nConv0=0;
static char Conv[256][20];
static int nChar[256];
/************************************************************************\
*
* FUNCTION : InitConversionTable
*
* LOCAL VARS : fname - code page table filename
* f - code page table file handle
* line - buffer for code page table line
* entity - HTML entity
* description - character description
* ichar - character code
*
\************************************************************************/
BOOL InitConversionTable (void)
{
int i,j,n,ichar;
char entity[20],description[30],line[100];
char fname[_MAX_FNAME];
FILE *f;
if (GetTempPath((DWORD)sizeof(TempDir),TempDir)==0)
return (FALSE);
// HTML entities table reading
strcat(strcpy(fname,EncDir),"\\HTML.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(entity,0,20);
memset(description,0,30);
sscanf(line,"%s %s",entity,description);
n=strlen(entity);
if (entity[0]!='&' || entity[n-1]!=';')
continue;
if (strcmp(entity,"&")==0 || strcmp(entity,"<")==0 ||
strcmp(entity,""")==0)
continue;
Conv0[nConv0].use=FALSE;
strcpy(Conv0[nConv0].entity,entity);
strcpy(Conv0[nConv0].description,description);
Conv0[nConv0].ichar=32;
nConv0++;
}
fclose(f);
// source code page table reading and assignment to the HTML entities
strcat(strcat(strcat(strcpy(fname,EncDir),"\\"),CodePage),".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;
for(i=0; i<nConv0; i++)
{
if (Conv0[i].use==TRUE)
continue;
if (strcmp(description,Conv0[i].description)==0)
{
Conv0[i].ichar=ichar;
Conv0[i].use=TRUE;
}
}
}
fclose(f);
// finishing file conversion table
for (i=0; i<256; i++)
{
Conv[i][0]=(char)i;
Conv[i][1]='\0';
nChar[i]=1;
}
for (i=0; i<nConv0; i++)
if (Conv0[i].use==TRUE && Conv0[i].ichar>127)
{
j=Conv0[i].ichar;
strcpy (Conv[j],Conv0[i].entity);
nChar[j]=strlen(Conv[j]);
}
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
* ssize - source file size
* stime - source file time
* buff - buffer for source file content
*
\************************************************************************/
void ConvertFile(char *SFileName, char *TFileName)
{
HANDLE hSFile, hTFile, hTempFile;
char buff[16384];
char TempName[_MAX_FNAME];
DWORD dwBytesRead,dwBytesWritten;
DWORD ssize;
FILETIME stime;
int i,j;
if (GetTempFileName(TempDir,"HTA",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;
hSFile= CreateFile(SFileName,GENERIC_READ,0,
(LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
if (hSFile==INVALID_HANDLE_VALUE)
return;
ssize=GetFileSize(hSFile,NULL);
GetFileTime(hSFile,NULL,NULL,&stime);
do
{
if (ReadFile(hSFile, (LPSTR)buff, 16384, &dwBytesRead, NULL))
{
for (i=0; i<(int)dwBytesRead; i++)
{
j=(int)buff[i];
if (j<0)
j+=256;
WriteFile(hTempFile,(LPSTR)Conv[j],nChar[j],&dwBytesWritten,NULL);
}
}
}
while (dwBytesRead==16384);
CloseHandle(hSFile);
CloseHandle(hTempFile);
CopyFile(TempName,TFileName,FALSE);
DeleteFile(TempName);
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);
printf("\n Converted target file: %s", TFileName);
return;
}