VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/simplepattern.cpp@ 25659

Last change on this file since 25659 was 21337, checked in by vboxsync, 15 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* $Id: simplepattern.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
2/** @file
3 * IPRT - RTStrSimplePattern.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/string.h>
36#include "internal/iprt.h"
37
38#include <iprt/assert.h>
39
40
41RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString)
42{
43#if 0
44 return RTStrSimplePatternNMatch(pszPattern, RTSTR_MAX, pszString, RTSTR_MAX);
45#else
46 /* ASSUMES ASCII / UTF-8 */
47 for (;;)
48 {
49 char chPat = *pszPattern;
50 switch (chPat)
51 {
52 default:
53 if (*pszString != chPat)
54 return false;
55 break;
56
57 case '*':
58 {
59 /* collapse '*' and '?', they are supurfluous */
60 while ((chPat = *++pszPattern) == '*' || chPat == '?')
61 /* nothing */;
62
63 /* if no more pattern, we're done now. */
64 if (!chPat)
65 return true;
66
67 /* find chPat in the string and try get a match on the remaining pattern. */
68 for (;;)
69 {
70 char chStr = *pszString++;
71 if ( chStr == chPat
72 && RTStrSimplePatternMatch(pszPattern + 1, pszString))
73 return true;
74 if (!chStr)
75 return false;
76 }
77 /* won't ever get here */
78 break;
79 }
80
81 case '?':
82 if (!*pszString)
83 return false;
84 break;
85
86 case '\0':
87 return !*pszString;
88 }
89 pszString++;
90 pszPattern++;
91 }
92#endif
93}
94RT_EXPORT_SYMBOL(RTStrSimplePatternMatch);
95
96
97RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
98 const char *pszString, size_t cchString)
99{
100 /* ASSUMES ASCII / UTF-8 */
101 for (;;)
102 {
103 char chPat = cchPattern ? *pszPattern : '\0';
104 switch (chPat)
105 {
106 default:
107 {
108 char chStr = cchString ? *pszString : '\0';
109 if (chStr != chPat)
110 return false;
111 break;
112 }
113
114 case '*':
115 {
116 /* Collapse '*' and '?', they are supurfluous. End of the pattern == match. */
117 do
118 {
119 if (!--cchPattern)
120 return true;
121 chPat = *++pszPattern;
122 } while (chPat == '*' || chPat == '?');
123 if (!chPat)
124 return true;
125
126 /* Find chPat in the string and try get a match on the remaining pattern. */
127 for (;;)
128 {
129 if (!cchString--)
130 return false;
131 char chStr = *pszString++;
132 if ( chStr == chPat
133 && RTStrSimplePatternNMatch(pszPattern + 1, cchPattern - 1, pszString, cchString))
134 return true;
135 if (!chStr)
136 return false;
137 }
138 /* won't ever get here */
139 break;
140 }
141
142 case '?':
143 if (!cchString || !*pszString)
144 return false;
145 break;
146
147 case '\0':
148 return cchString == 0 || !*pszString;
149 }
150
151 /* advance */
152 pszString++;
153 cchString--;
154 pszPattern++;
155 cchPattern--;
156 }
157}
158RT_EXPORT_SYMBOL(RTStrSimplePatternNMatch);
159
160
161RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
162 const char *pszString, size_t cchString,
163 size_t *poffMatchingPattern)
164{
165 const char *pszCur = pszPatterns;
166 while (*pszCur && cchPatterns)
167 {
168 /*
169 * Find the end of the current pattern.
170 */
171 unsigned char ch = '\0';
172 const char *pszEnd = pszCur;
173 while (cchPatterns && (ch = *pszEnd) != '\0' && ch != '|')
174 cchPatterns--, pszEnd++;
175
176 /*
177 * Try match it.
178 */
179 if (RTStrSimplePatternNMatch(pszCur, pszEnd - pszCur, pszString, cchString))
180 {
181 if (poffMatchingPattern)
182 *poffMatchingPattern = pszCur - pszPatterns;
183 return true;
184 }
185
186 /* advance */
187 if (!ch || !cchPatterns)
188 break;
189 cchPatterns--;
190 pszCur = pszEnd + 1;
191 }
192
193 if (poffMatchingPattern)
194 *poffMatchingPattern = RTSTR_MAX;
195 return false;
196}
197RT_EXPORT_SYMBOL(RTStrSimplePatternMultiMatch);
198
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette