home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************\
- *
- * PROGRAM : CPCONV
- *
- * PURPOSE : Remove HTML character entities and replace them by
- * characters from certain code page
- *
- \************************************************************************/
-
- #include <windows.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "FILE_CPCONVERT.H"
- #include "CONV_SUB.H"
- #include "EXTMASK.H"
-
-
- /************************************************************************\
- *
- * FUNCTION : GetIniSettings
- *
- * Get program settings equivalent to command line options
- * from CPCONV.INI
- *
- * INPUTS : section - name of section in CPCONV.INI
- *
- \************************************************************************/
-
- static void GetIniSettings (char *section)
- {
- char buf[256];
-
- // source code page
-
- GetPrivateProfileString(section,"SourceCodePage","",buf,100,"CPCONV.INI");
- if (strlen(buf)>0)
- SetSourceCodePage(buf);
-
- // target code page
-
- GetPrivateProfileString(section,"TargetCodePage","",buf,100,"CPCONV.INI");
- if (strlen(buf)>0)
- SetTargetCodePage(buf);
-
- // one-to-more
-
- GetPrivateProfileString(section,"OneToMore","",buf,100,"CPCONV.INI");
- if (strcmp(buf,"yes")==0)
- SetOneToMoreConversion(TRUE);
- else if (strcmp(buf,"no")==0)
- SetOneToMoreConversion(FALSE);
-
- // default character
-
- GetPrivateProfileString(section,"DefChar","",buf,100,"CPCONV.INI");
- if (strlen(buf)>0)
- SetDefaultCharacter (buf[0]);
-
- // file time mode
-
- GetPrivateProfileString(section,"FileTime","",buf,100,"CPCONV.INI");
- if (strcmp(buf,"current")==0)
- SetCurrentFileTime();
- else if (strcmp(buf,"original")==0)
- SetOriginalFileTime();
-
- // subdirs
-
- GetPrivateProfileString(section,"Subdirectories","",buf,100,"CPCONV.INI");
- if (strcmp(buf,"yes")==0)
- SetSubdirConversion(TRUE);
- else if (strcmp(buf,"no")==0)
- SetSubdirConversion(FALSE);
-
- // encoding directory
-
- GetPrivateProfileString(section,"EncodingDir","",buf,100,"CPCONV.INI");
- if (strlen(buf)>0)
- SetEncodingDirectory (buf);
-
- // extension masks
-
- GetIniExtensionMasks (section, "CPCONV.INI");
-
- if (stricmp(section,"Default")==0)
- return;
-
- GetPrivateProfileString(section,"DifferentTimeOnly","",buf,100,"CPCONV.INI");
- if (strcmp(buf,"yes")==0)
- SetDifferentTimeOnly(TRUE);
- else if (strcmp(buf,"no")==0)
- SetDifferentTimeOnly(FALSE);
- }
-
-
- /************************************************************************\
- *
- * FUNCTION : main
- *
- * INPUTS : __argc - argument count
- * __argv - array of argument strings
- *
- * RETURNS : 0 on success or -1 on failure.
- *
- * LOCAL VARS : Source - source file/directory
- * Target - target file/directory
- * nSource - source name length
- * nTarget - target name length
- * IsDirectory- flag if source is directory
- * hConsole - handle to the console
- *
- \************************************************************************/
-
- int main( int __argc, char** __argv )
- {
- size_t nSource,nTarget;
- HANDLE hConsole;
- char Source[_MAX_PATH],Target[_MAX_PATH];
- char path[_MAX_PATH],drive[_MAX_DRIVE],dir[_MAX_DIR],
- name[_MAX_FNAME],ext[_MAX_EXT];
- char *pc;
- int i;
- BOOL IsDirectory=FALSE;
- DWORD attr;
-
- // Check if Win32s. If so, display notice and terminate.
-
- if( GetVersion() & 0x80000000 && (GetVersion() & 0xFF) ==3)
- {
- MessageBoxA( NULL,
- "This application cannot run on Windows 3.1.\n"
- "This application will now terminate.",
- "HTML Entities Remove",
- MB_OK | MB_ICONSTOP | MB_SETFOREGROUND );
- return( -1 );
- }
-
-
- // Open the current console input buffer
-
- hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
- if (hConsole == INVALID_HANDLE_VALUE )
- {
- printf("\n Error: Unable to open console.\n");
- return( -1 );
- }
-
- printf("\nCode page convert, Version 1.0.1\n(c) J. Kvarda 1996\n");
-
- if (__argc<2)
- {
- printf("\n Error: Not enough parameters.\n");
- return (-1);
- }
-
- // Get the zero argument; the program directory and set it as the default
- // encoding directory
-
- GetFullPathName(__argv[0],_MAX_PATH,path,&pc);
- _splitpath(path,drive,dir,name,ext);
- _makepath(path,drive,dir,NULL,NULL);
- if (path[strlen(path)-1]=='\\')
- path[strlen(path)-1]='\0';
- SetEncodingDirectory(path);
-
- // Get default setting from INI file
-
- GetIniSettings("Default");
-
- // Get the first argument; the source file or directory
-
- strcpy( Source, __argv[1] );
- nSource = strlen( Source );
- if (Source[nSource-1]=='\\')
- {
- Source[nSource-1]='\0';
- nSource=strlen(Source);
- }
- attr=GetFileAttributes(Source);
- if (attr==0xFFFFFFFF)
- {
- printf("\n Error: Source file or directory doesn't exist.\n");
- return (-1);
- }
- if (attr & FILE_ATTRIBUTE_DIRECTORY)
- IsDirectory=TRUE;
- strcpy (Target,Source);
-
-
- // Get next arguments
-
- for (i=2; i<__argc; i++)
- {
-
- // Get the second argument; the target file or directory
-
- if(i==2 && __argv[2][0]!='/' && __argv[2][0]!='@')
- {
- strcpy( Target, __argv[2] );
- nTarget = strlen( Target );
- if (Target[nTarget-1]=='\\')
- {
- Target[nTarget-1]='\0';
- nTarget=strlen(Target);
- }
- }
-
-
- // Get other arguments.
-
- // section of INI file
-
- else if (__argv[i][0]=='@' && strlen(__argv[i])>1)
- {
- GetIniSettings(&__argv[i][1]);
- goto skipargs;
- }
-
- else if (__argv[i][0]=='/' && strlen(__argv[i])>1)
- {
- char a1[100],*a2;
- strcpy(a1,&__argv[i][1]);
- a2=strchr(a1,':');
- if (a2)
- {
- *a2='\0';
- a2++;
-
- // source code page
-
- if (stricmp("scp",a1)==0 || stricmp("s",a1)==0 ||
- stricmp("sourcecodepage",a1)==0)
- SetSourceCodePage(a2);
-
- // target code page
-
- if (stricmp("tcp",a1)==0 || stricmp("t",a1)==0 ||
- stricmp("targetcodepage",a1)==0)
- SetTargetCodePage(a2);
-
- // file time mode
-
- else if (stricmp("time:",a1)==0 || stricmp("t",a1)==0)
- {
- if (stricmp("current",a2)==0 || stricmp("c",a2)==0)
- SetCurrentFileTime();
- else if (stricmp("original",a2)==0 || stricmp("o",a2)==0)
- SetOriginalFileTime();
- }
-
- // encoding directory
-
- else if (stricmp("dir",a1)==0 || stricmp("d",a1)==0)
- SetEncodingDirectory(a2);
-
- // extension masks
-
- else if (stricmp("ext",a1)==0 || stricmp("e",a1)==0)
- SetValidExtension(a2);
- else if (stricmp("noext",a1)==0 || stricmp("n",a1)==0)
- SetInvalidExtension(a2);
- }
-
-
- else
- {
-
- // subdirectories
-
- if (stricmp("subdir+",a1)==0 || stricmp("s+",a1)==0)
- SetSubdirConversion(TRUE);
- else if (stricmp("subdir-",a1)==0 || stricmp("s-",a1)==0)
- SetSubdirConversion(FALSE);
-
- // one-to-more
-
- else if (stricmp("onetomore",a1)==0)
- SetOneToMoreConversion(TRUE);
- else if (stricmp("onetoone",a1)==0)
- SetOneToMoreConversion(FALSE);
- }
- }
- }
-
- skipargs:
-
- // Check if target directory is not part of source directory
-
- if (IsDirectory && IsConvertSubdirMode() &&
- nTarget>nSource && Target[nSource]=='\\' &&
- strstr(Target, Source)==&Target[0])
- {
- printf("\n Error: Target directory is in the source directory tree"
- "\n and conversion of subdirectories is on.\n");
- return( -1 );
- }
-
- attr=GetFileAttributes(Target);
- if (attr!=0xFFFFFFFF)
- {
- if ((attr & FILE_ATTRIBUTE_DIRECTORY) && !IsDirectory)
- {
- printf("\n Error: Target directory of the same name as the target file"
- "\n already exists.\n");
- return( -1 );
- }
- }
-
- // Set code page conversion table
-
- if (!InitConversionTable())
- return(-1);
-
- // Convert the source file or directory tree to the target
- // (changing code page)
-
- if (IsDirectory)
- ConvertDir(Source,Target);
- else
- ConvertFile(Source,Target);
-
- printf("\n");
- return(0);
- }
-
-
-