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

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