home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 June / Chip_1999-06_cd.bin / ctenari / Kvarda / source / EXTMASK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-12  |  2.4 KB  |  127 lines

  1. /************************************************************************\
  2. *
  3. * MODULE    : EXTMASK.C
  4. *
  5. * PROGRAMS  : HTMLENRM, HTMLENAD, CPCONV, UNWIACZ
  6. *
  7. * PURPOSE   : Check of filename against list of valid/invalid extensions
  8. *             for conversion and supporting variables and functions
  9. *
  10. \************************************************************************/
  11.  
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #include "EXTMASK.H"
  18.  
  19. static struct {
  20.   char mask[11];
  21.   BOOL valid;
  22.  } Exts[50] = {{"*",TRUE}};
  23. static int nExt=1;
  24. static BOOL first=TRUE;
  25.  
  26. void SetValidExtension (char *ext)
  27.    int n;
  28.  
  29.    n=strlen(ext);
  30.    if (n<1 || n>10)
  31.       return;
  32.    if (first)
  33.      {
  34.       Exts[0].valid=FALSE;
  35.       first=FALSE;
  36.       nExt=1;
  37.      }
  38.    if (stricmp(ext,"*")==0)
  39.       nExt=0;
  40.    if (nExt>=50)
  41.       return;
  42.    strcpy(Exts[nExt].mask,ext);
  43.    Exts[nExt].valid=TRUE;
  44.    nExt++;
  45. }
  46.  
  47. void SetInvalidExtension (char *ext)
  48. {
  49.    int n;
  50.  
  51.    n=strlen(ext);
  52.    if (n<1 || n>10)
  53.       return;
  54.    if (stricmp(ext,"*")==0)
  55.       nExt=0;
  56.    if (nExt>=50)
  57.       return;
  58.    first=FALSE;
  59.    strcpy(Exts[nExt].mask,ext);
  60.    Exts[nExt].valid=FALSE;
  61.    nExt++;
  62. }
  63.  
  64. void GetIniExtensionMasks (char *section, char *inifile)
  65. {
  66.    char item[30],ext[12];
  67.    char *c;
  68.    int  i=1;
  69.  
  70. next:
  71.    sprintf(item,"Extension%d",i);
  72.    GetPrivateProfileString(section,item,"",ext,11,inifile);
  73.    if (strlen(ext)>0)
  74.      {
  75.       if (i==1)
  76.         {
  77.          strcpy(Exts[0].mask,"*");
  78.          Exts[0].valid=FALSE;
  79.          nExt=1;
  80.         }
  81.       c=strchr(ext,',');
  82.       Exts[nExt].valid=TRUE;
  83.       if (c!=NULL)
  84.         {
  85.          *c='\0';
  86.          c++;
  87.          if (*c=='0')
  88.             Exts[nExt].valid=FALSE;
  89.         }
  90.       strcpy(Exts[nExt].mask,ext);
  91.       nExt++;
  92.       i++;
  93.       if (nExt<50)
  94.          goto next;
  95.      }
  96. }
  97.  
  98. BOOL CheckFilenameExtension (char *filename)
  99. {
  100.    int i,n;
  101.    char *c,ext[11];
  102.    BOOL valid;
  103.  
  104.    c=strrchr(filename,'.');
  105.    if (c==NULL)
  106.       strcpy(ext,"");
  107.    else
  108.      {
  109.       i=strlen(filename);
  110.       n=&filename[i]-c;
  111.       if (n<1 || n>10)
  112.          strcpy(ext,"");
  113.       else
  114.          strcpy(ext,&c[1]);
  115.      }
  116.  
  117.    for (i=0;i<nExt;i++)
  118.      {
  119.       if (stricmp(Exts[i].mask,"*")==0)
  120.          valid=Exts[i].valid;
  121.       else if (stricmp(Exts[i].mask,ext)==0)
  122.          valid=Exts[i].valid;
  123.      }
  124.    return (valid);
  125. }
  126.