home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 June / Chip_1999-06_cd.bin / ctenari / Kvarda / source / HTMLENRM.C < prev   
Encoding:
C/C++ Source or Header  |  1996-01-30  |  8.1 KB  |  312 lines

  1. /************************************************************************\
  2. *
  3. * PROGRAM   : HTMLENRM
  4. *
  5. * PURPOSE   : Remove HTML character entities and replace them by 
  6. *             characters from certain code page
  7. *
  8. \************************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include "FILE_HTMLREMOVE.H"
  15. #include "CONV_SUB.H"
  16. #include "EXTMASK.H"
  17.  
  18.  
  19. /************************************************************************\
  20. *
  21. * FUNCTION    : GetIniSettings
  22. *
  23. *               Get program settings equivalent to command line options
  24. *               from HTMLENRM.INI
  25. *
  26. * INPUTS      : section    - name of section in HTMLENRM.INI
  27. *
  28. \************************************************************************/
  29.  
  30. static void GetIniSettings (char *section)
  31. {
  32.    char buf[256];
  33.  
  34. // code page
  35.  
  36.    GetPrivateProfileString(section,"CodePage","",buf,100,"HTMLENRM.INI");
  37.    if (strlen(buf)>0)
  38.       SetTargetCodePage(buf);
  39.  
  40. // conversion mode
  41.  
  42.    GetPrivateProfileString(section,"Mode","",buf,100,"HTMLENRM.INI");
  43.    if (strcmp(buf,"normal")==0)
  44.       SetNormalConversion();
  45.    else if (strcmp(buf,"loose")==0)
  46.       SetLooseConversion();
  47.    else if (strcmp(buf,"all")==0)
  48.       SetAllConversion();
  49.  
  50. // file time mode
  51.  
  52.    GetPrivateProfileString(section,"FileTime","",buf,100,"HTMLENRM.INI");
  53.    if (strcmp(buf,"current")==0)
  54.       SetCurrentFileTime();
  55.    else if (strcmp(buf,"original")==0)
  56.       SetOriginalFileTime();
  57.  
  58. // subdirs
  59.  
  60.    GetPrivateProfileString(section,"Subdirectories","",buf,100,"HTMLENRM.INI");
  61.    if (strcmp(buf,"yes")==0)
  62.       SetSubdirConversion(TRUE);
  63.    else if (strcmp(buf,"no")==0)
  64.       SetSubdirConversion(FALSE);
  65.  
  66. // encoding directory
  67.  
  68.    GetPrivateProfileString(section,"EncodingDir","",buf,100,"HTMLENRM.INI");
  69.    if (strlen(buf)>0)
  70.       SetEncodingDirectory (buf);
  71.  
  72. // extension masks
  73.  
  74.    GetIniExtensionMasks (section, "HTMLENRM.INI");
  75.  
  76. }
  77.  
  78.  
  79. /************************************************************************\
  80. *
  81. * FUNCTION    : main
  82. *
  83. * INPUTS      : __argc - argument count
  84. *               __argv - array of argument strings
  85. *
  86. * RETURNS     : 0 on success or -1 on failure.
  87. *
  88. * LOCAL VARS  : Source     - source file/directory
  89. *               Target     - target file/directory
  90. *               nSource    - source name length
  91. *               nTarget    - target name length
  92. *               IsDirectory- flag if source is directory
  93. *               hConsole   - handle to the console
  94. *
  95. \************************************************************************/
  96.  
  97. int main( int __argc, char** __argv )
  98. {
  99.   size_t  nSource,nTarget;
  100.   HANDLE  hConsole;
  101.   char    Source[_MAX_PATH],Target[_MAX_PATH];
  102.   char    path[_MAX_PATH],drive[_MAX_DRIVE],dir[_MAX_DIR],
  103.           name[_MAX_FNAME],ext[_MAX_EXT];
  104.   char    *pc;
  105.   int     i;
  106.   BOOL    IsDirectory=FALSE;  
  107.   DWORD   attr;
  108.  
  109. // Check if Win32s. If so, display notice and terminate.
  110.  
  111.   if( GetVersion() & 0x80000000 && (GetVersion() & 0xFF) ==3)
  112.   {
  113.     MessageBoxA( NULL,
  114.       "This application cannot run on Windows 3.1.\n"
  115.       "This application will now terminate.",
  116.       "HTML Entities Remove",
  117.       MB_OK | MB_ICONSTOP | MB_SETFOREGROUND );
  118.       return( -1 );
  119.   }
  120.  
  121.  
  122. // Open the current console input buffer
  123.  
  124.   hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
  125.                         FILE_SHARE_READ | FILE_SHARE_WRITE,
  126.                         0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
  127.   if (hConsole == INVALID_HANDLE_VALUE ) 
  128.     {
  129.      printf("\n Error: Unable to open console.\n");
  130.      return( -1 );
  131.     }
  132.  
  133.   printf("\nHTML entities remove, Version 1.0.1\n(c) J. Kvarda 1996\n");
  134.  
  135.   if (__argc<2)
  136.     {
  137.      printf("\n Error: Not enough parameters.\n");
  138.      return (-1);
  139.     }
  140.  
  141. // Get the zero argument; the program directory and set it as the default
  142. // encoding directory
  143.  
  144.   GetFullPathName(__argv[0],_MAX_PATH,path,&pc);
  145.   _splitpath(path,drive,dir,name,ext); 
  146.   _makepath(path,drive,dir,NULL,NULL);
  147.   if (path[strlen(path)-1]=='\\')
  148.      path[strlen(path)-1]='\0';
  149.   SetEncodingDirectory(path);
  150.  
  151. // Get default setting from INI file
  152.  
  153.   GetIniSettings("Default");
  154.  
  155. // Get the first argument; the source file or directory
  156.  
  157.   strcpy( Source, __argv[1] );
  158.   nSource = strlen( Source );
  159.   if (Source[nSource-1]=='\\')
  160.     {
  161.      Source[nSource-1]='\0';
  162.      nSource=strlen(Source);
  163.     }
  164.   attr=GetFileAttributes(Source);
  165.   if (attr==0xFFFFFFFF)
  166.     {
  167.      printf("\n Error: Source file or directory doesn't exist.\n");
  168.      return (-1);
  169.     }
  170.   if (attr & FILE_ATTRIBUTE_DIRECTORY)
  171.      IsDirectory=TRUE;
  172.   strcpy (Target,Source);
  173.  
  174.  
  175. // Get next arguments
  176.  
  177.   for (i=2; i<__argc; i++)
  178.     {
  179.  
  180. // Get the second argument; the target file or directory
  181.  
  182.      if(i==2 && __argv[2][0]!='/' && __argv[2][0]!='@') 
  183.        {
  184.         strcpy( Target, __argv[2] );
  185.         nTarget = strlen( Target );
  186.         if (Target[nTarget-1]=='\\')
  187.           {
  188.            Target[nTarget-1]='\0';
  189.            nTarget=strlen(Target);
  190.           }
  191.        }
  192.  
  193.  
  194. // Get other arguments. 
  195.  
  196. // section of INI file
  197.  
  198.      else if (__argv[i][0]=='@' && strlen(__argv[i])>1)
  199.        {
  200.         GetIniSettings(&__argv[i][1]);
  201.         goto skipargs;
  202.        }
  203.  
  204.      else if (__argv[i][0]=='/'  && strlen(__argv[i])>1)
  205.        {
  206.         char a1[100],*a2;
  207.         strcpy(a1,&__argv[i][1]);
  208.         a2=strchr(a1,':');
  209.         if (a2)
  210.           {
  211.            *a2='\0';
  212.            a2++;
  213.  
  214. // code page
  215.  
  216.            if (stricmp("cp",a1)==0 || stricmp("c",a1)==0  || 
  217.                stricmp("codepage",a1)==0)
  218.               SetTargetCodePage(a2);
  219.  
  220. // conversion mode
  221.  
  222.            else if (stricmp("mode",a1)==0 || stricmp("m",a1)==0)
  223.              {
  224.               if (stricmp("normal",a2)==0 || stricmp("n",a2)==0) 
  225.                  SetNormalConversion();
  226.               else if (stricmp("loose",a2)==0 || stricmp("l",a2)==0)
  227.                  SetLooseConversion();
  228.               else if (stricmp("all",a2)==0 || stricmp("a",a2)==0)
  229.                  SetAllConversion();
  230.              }
  231.  
  232. // file time mode
  233.  
  234.            else if (stricmp("time:",a1)==0 || stricmp("t",a1)==0)
  235.              {
  236.               if (stricmp("current",a2)==0 || stricmp("c",a2)==0)
  237.                  SetCurrentFileTime();
  238.               else if (stricmp("original",a2)==0 || stricmp("o",a2)==0)
  239.                  SetOriginalFileTime();
  240.              }
  241.  
  242. // encoding directory
  243.  
  244.            else if (stricmp("dir",a1)==0 || stricmp("d",a1)==0)
  245.               SetEncodingDirectory(a2);
  246.  
  247. // extension masks
  248.  
  249.            else if (stricmp("ext",a1)==0 || stricmp("e",a1)==0)
  250.               SetValidExtension(a2);
  251.            else if (stricmp("noext",a1)==0 || stricmp("n",a1)==0)
  252.               SetInvalidExtension(a2);
  253.           }
  254.  
  255.  
  256.         else 
  257.           {
  258.            strcpy(a1,&__argv[i][1]);
  259.  
  260. // subdirectories
  261.  
  262.            if (stricmp("subdir+",a1)==0 || stricmp("s+",a1)==0)
  263.               SetSubdirConversion(TRUE);
  264.            else if (stricmp("subdir-",a1)==0 || stricmp("s-",a1)==0)
  265.               SetSubdirConversion(FALSE);
  266.           }
  267.        }
  268.     }
  269.  
  270. skipargs:
  271.  
  272. // Check if target directory is not part of source directory 
  273.  
  274.   if (IsDirectory && IsConvertSubdirMode() &&
  275.       nTarget>nSource && Target[nSource]=='\\' && 
  276.       strstr(Target, Source)==&Target[0])
  277.     {
  278.      printf("\n Error: Target directory is in the source directory tree"
  279.             "\n        and conversion of subdirectories is on.\n");
  280.      return( -1 );
  281.     }
  282.  
  283.   attr=GetFileAttributes(Target);
  284.   if (attr!=0xFFFFFFFF)
  285.     {
  286.      if ((attr & FILE_ATTRIBUTE_DIRECTORY) && !IsDirectory) 
  287.        {
  288.         printf("\n Error: Target directory of the same name as the target file"
  289.                "\n        already exists.\n");
  290.         return( -1 );
  291.        }
  292.     }  
  293.  
  294. //  Set HTML entities conversion table
  295.  
  296.   if (!InitConversionTable())
  297.      return(-1);
  298.  
  299. // Convert the source file or directory tree to the target
  300. // (removing HTML entities)
  301.  
  302.   if (IsDirectory)
  303.      ConvertDir(Source,Target);
  304.   else
  305.      ConvertFile(Source,Target);
  306.  
  307.   printf("\n");
  308.   return(0);
  309. }
  310.  
  311.  
  312.