home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / unrar_vms_alpha-3.6.5.zip / match.cxx < prev    next >
C/C++ Source or Header  |  2006-06-27  |  7KB  |  263 lines

  1. #include "rar.hpp"
  2.  
  3. static bool match(char *pattern,char *string,bool ForceCase);
  4. static bool match(wchar *pattern,wchar *string,bool ForceCase);
  5.  
  6. static int mstricompc(const char *Str1,const char *Str2,bool ForceCase);
  7. static int mstricompcw(const wchar *Str1,const wchar *Str2,bool ForceCase);
  8. static int mstrnicompc(const char *Str1,const char *Str2,int N,bool ForceCase);
  9. static int mstrnicompcw(const wchar *Str1,const wchar *Str2,int N,bool ForceCase);
  10.  
  11. inline uint toupperc(byte ch,bool ForceCase)
  12. {
  13.   if (ForceCase)
  14.     return(ch);
  15. #ifdef _WIN_32
  16.   return((uint)CharUpper((LPTSTR)(ch)));
  17. #elif defined(_UNIX)
  18.   return(ch);
  19. #else
  20.   return(toupper(ch));
  21. #endif
  22. }
  23.  
  24.  
  25. inline uint touppercw(uint ch,bool ForceCase)
  26. {
  27.   if (ForceCase)
  28.     return(ch);
  29. #if defined(_UNIX)
  30.   return(ch);
  31. #else
  32.   return(toupperw(ch));
  33. #endif
  34. }
  35.  
  36.  
  37. bool CmpName(char *Wildcard,char *Name,int CmpPath)
  38. {
  39.   bool ForceCase=(CmpPath&MATCH_FORCECASESENSITIVE)!=0;
  40.  
  41.   CmpPath&=MATCH_MODEMASK;
  42.   
  43.   if (CmpPath!=MATCH_NAMES)
  44.   {
  45.     int WildLength=strlen(Wildcard);
  46.     if (CmpPath!=MATCH_EXACTPATH && mstrnicompc(Wildcard,Name,WildLength,ForceCase)==0)
  47.     {
  48.       char NextCh=Name[WildLength];
  49.       if (NextCh=='\\' || NextCh=='/' || NextCh==0)
  50.         return(true);
  51.     }
  52.     char Path1[NM],Path2[NM];
  53.     GetFilePath(Wildcard,Path1);
  54.     GetFilePath(Name,Path2);
  55.     if (mstricompc(Wildcard,Path2,ForceCase)==0)
  56.       return(true);
  57.     if ((CmpPath==MATCH_PATH || CmpPath==MATCH_EXACTPATH) && mstricompc(Path1,Path2,ForceCase)!=0)
  58.       return(false);
  59.     if (CmpPath==MATCH_SUBPATH || CmpPath==MATCH_WILDSUBPATH)
  60.       if (IsWildcard(Path1))
  61.         return(match(Wildcard,Name,ForceCase));
  62.       else
  63.         if (CmpPath==MATCH_SUBPATH || IsWildcard(Wildcard))
  64.         {
  65.           if (*Path1 && mstrnicompc(Path1,Path2,strlen(Path1),ForceCase)!=0)
  66.             return(false);
  67.         }
  68.         else
  69.           if (mstricompc(Path1,Path2,ForceCase)!=0)
  70.             return(false);
  71.   }
  72.   char *Name1=PointToName(Wildcard);
  73.   char *Name2=PointToName(Name);
  74.   if (mstrnicompc("__rar_",Name2,6,false)==0)
  75.     return(false);
  76.   return(match(Name1,Name2,ForceCase));
  77. }
  78.  
  79.  
  80. #ifndef SFX_MODULE
  81. bool CmpName(wchar *Wildcard,wchar *Name,int CmpPath)
  82. {
  83.   bool ForceCase=(CmpPath&MATCH_FORCECASESENSITIVE)!=0;
  84.  
  85.   CmpPath&=MATCH_MODEMASK;
  86.  
  87.   if (CmpPath!=MATCH_NAMES)
  88.   {
  89.     int WildLength=strlenw(Wildcard);
  90.     if (CmpPath!=MATCH_EXACTPATH && mstrnicompcw(Wildcard,Name,WildLength,ForceCase)==0)
  91.     {
  92.       wchar NextCh=Name[WildLength];
  93.       if (NextCh==L'\\' || NextCh==L'/' || NextCh==0)
  94.         return(true);
  95.     }
  96.     wchar Path1[NM],Path2[NM];
  97.     GetFilePath(Wildcard,Path1);
  98.     GetFilePath(Name,Path2);
  99.     if ((CmpPath==MATCH_PATH || CmpPath==MATCH_EXACTPATH) && mstricompcw(Path1,Path2,ForceCase)!=0)
  100.       return(false);
  101.     if (CmpPath==MATCH_SUBPATH || CmpPath==MATCH_WILDSUBPATH)
  102.       if (IsWildcard(NULL,Path1))
  103.         return(match(Wildcard,Name,ForceCase));
  104.       else
  105.         if (CmpPath==MATCH_SUBPATH || IsWildcard(NULL,Wildcard))
  106.         {
  107.           if (*Path1 && mstrnicompcw(Path1,Path2,strlenw(Path1),ForceCase)!=0)
  108.             return(false);
  109.         }
  110.         else
  111.           if (mstricompcw(Path1,Path2,ForceCase)!=0)
  112.             return(false);
  113.   }
  114.   wchar *Name1=PointToName(Wildcard);
  115.   wchar *Name2=PointToName(Name);
  116.   if (mstrnicompcw((const wchar *)L"__rar_",Name2,6,false)==0)
  117.     return(false);
  118.   return(match(Name1,Name2,ForceCase));
  119. }
  120. #endif
  121.  
  122.  
  123. bool match(char *pattern,char *string,bool ForceCase)
  124. {
  125.   for (;; ++string)
  126.   {
  127.     char stringc=toupperc(*string,ForceCase);
  128.     char patternc=toupperc(*pattern++,ForceCase);
  129.     switch (patternc)
  130.     {
  131.       case 0:
  132.         return(stringc==0);
  133.       case '?':
  134.         if (stringc == 0)
  135.           return(false);
  136.         break;
  137.       case '*':
  138.         if (*pattern==0)
  139.           return(true);
  140.         if (*pattern=='.')
  141.         {
  142.           if (pattern[1]=='*' && pattern[2]==0)
  143.             return(true);
  144.           char *dot=strchr(string,'.');
  145.           if (pattern[1]==0)
  146.             return (dot==NULL || dot[1]==0);
  147.           if (dot!=NULL)
  148.           {
  149.             string=dot;
  150.             if (strpbrk(pattern,"*?")==NULL && strchr(string+1,'.')==NULL)
  151.               return(mstricompc(pattern+1,string+1,ForceCase)==0);
  152.           }
  153.         }
  154.  
  155.         while (*string)
  156.           if (match(pattern,string++,ForceCase))
  157.             return(true);
  158.         return(false);
  159.       default:
  160.         if (patternc != stringc)
  161.           if (patternc=='.' && stringc==0)
  162.             return(match(pattern,string,ForceCase));
  163.           else
  164.             return(false);
  165.         break;
  166.     }
  167.   }
  168. }
  169.  
  170.  
  171. #ifndef SFX_MODULE
  172. bool match(wchar *pattern,wchar *string,bool ForceCase)
  173. {
  174.   for (;; ++string)
  175.   {
  176.     wchar stringc=touppercw(*string,ForceCase);
  177.     wchar patternc=touppercw(*pattern++,ForceCase);
  178.     switch (patternc)
  179.     {
  180.       case 0:
  181.         return(stringc==0);
  182.       case '?':
  183.         if (stringc == 0)
  184.           return(false);
  185.         break;
  186.       case '*':
  187.         if (*pattern==0)
  188.           return(true);
  189.         if (*pattern=='.')
  190.         {
  191.           if (pattern[1]=='*' && pattern[2]==0)
  192.             return(true);
  193.           wchar *dot=strchrw(string,'.');
  194.           if (pattern[1]==0)
  195.             return (dot==NULL || dot[1]==0);
  196.           if (dot!=NULL)
  197.           {
  198.             string=dot;
  199.             if (strpbrkw(pattern,(const wchar *)L"*?")==NULL && strchrw(string+1,'.')==NULL)
  200.               return(mstricompcw(pattern+1,string+1,ForceCase)==0);
  201.           }
  202.         }
  203.  
  204.         while (*string)
  205.           if (match(pattern,string++,ForceCase))
  206.             return(true);
  207.         return(false);
  208.       default:
  209.         if (patternc != stringc)
  210.           if (patternc=='.' && stringc==0)
  211.             return(match(pattern,string,ForceCase));
  212.           else
  213.             return(false);
  214.         break;
  215.     }
  216.   }
  217. }
  218. #endif
  219.  
  220.  
  221. int mstricompc(const char *Str1,const char *Str2,bool ForceCase)
  222. {
  223.   if (ForceCase)
  224.     return(strcmp(Str1,Str2));
  225.   return(stricompc(Str1,Str2));
  226. }
  227.  
  228.  
  229. #ifndef SFX_MODULE
  230. int mstricompcw(const wchar *Str1,const wchar *Str2,bool ForceCase)
  231. {
  232.   if (ForceCase)
  233.     return(strcmpw(Str1,Str2));
  234.   return(stricompcw(Str1,Str2));
  235. }
  236. #endif
  237.  
  238.  
  239. int mstrnicompc(const char *Str1,const char *Str2,int N,bool ForceCase)
  240. {
  241.   if (ForceCase)
  242.     return(strncmp(Str1,Str2,N));
  243. #if defined(_UNIX)
  244.   return(strncmp(Str1,Str2,N));
  245. #else
  246.   return(strnicomp(Str1,Str2,N));
  247. #endif
  248. }
  249.  
  250.  
  251. #ifndef SFX_MODULE
  252. int mstrnicompcw(const wchar *Str1,const wchar *Str2,int N,bool ForceCase)
  253. {
  254.   if (ForceCase)
  255.     return(strncmpw(Str1,Str2,N));
  256. #if defined(_UNIX)
  257.   return(strncmpw(Str1,Str2,N));
  258. #else
  259.   return(strnicmpw(Str1,Str2,N));
  260. #endif
  261. }
  262. #endif
  263.