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

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