1 /* Copyright (c) 2010-2011, Google Inc.
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "common_util.h"
28
StringMatch(const string & wildcard,const string & text)29 bool StringMatch(const string& wildcard, const string& text) {
30 const char* c_text = text.c_str();
31 const char* c_wildcard = wildcard.c_str();
32 // Start of the current look-ahead. Everything before these positions is a
33 // definite, optimal match.
34 const char* c_text_last = NULL;
35 const char* c_wildcard_last = NULL;
36
37 char last_wc_char = wildcard[wildcard.size() - 1];
38
39 if (last_wc_char == '*' && wildcard.size() == 1) {
40 return true; // '*' matches everything.
41 }
42
43 if (last_wc_char != '*' && last_wc_char != '?'
44 && last_wc_char != text[text.size() - 1]) {
45 // short cut for the case when the wildcard does not end with '*' or '?'
46 // and the last characters of wildcard and text do not match.
47 return false;
48 }
49
50 while (*c_text) {
51 if (*c_wildcard == '*') {
52 while (*++c_wildcard == '*') {
53 // Skip all '*'.
54 }
55 if (!*c_wildcard) {
56 // Ends with a series of '*'.
57 return true;
58 }
59 c_text_last = c_text;
60 c_wildcard_last = c_wildcard;
61 } else if ((*c_text == *c_wildcard) || (*c_wildcard == '?')) {
62 ++c_text;
63 ++c_wildcard;
64 } else if (c_text_last) {
65 // No match. But we have seen at least one '*', so rollback and try at the
66 // next position.
67 c_wildcard = c_wildcard_last;
68 c_text = c_text_last++;
69 } else {
70 return false;
71 }
72 }
73
74 // Skip all '*' at the end of the wildcard.
75 while (*c_wildcard == '*') {
76 ++c_wildcard;
77 }
78
79 return !*c_wildcard;
80 }
81
ConvertToPlatformIndependentPath(const string & s)82 string ConvertToPlatformIndependentPath(const string &s) {
83 string ret = s;
84 #ifdef _MSC_VER
85 // TODO(timurrrr): do we need anything apart from s/\\///g?
86 size_t it = 0;
87 while ((it = ret.find("\\", it)) != string::npos) {
88 ret.replace(it, 1, "/");
89 }
90 #endif // _MSC_VER
91 return ret;
92 }
93
OpenFileReadOnly(const string & file_name,bool die_if_failed)94 TS_FILE OpenFileReadOnly(const string &file_name, bool die_if_failed) {
95 TS_FILE ret = TS_FILE_INVALID;
96 #ifdef TS_VALGRIND
97 SysRes sres = VG_(open)((const Char*)file_name.c_str(), VKI_O_RDONLY, 0);
98 if (!sr_isError(sres))
99 ret = sr_Res(sres);
100 #elif defined(_MSC_VER)
101 ret = fopen(file_name.c_str(), "r");
102 #else // no TS_VALGRIND
103 ret = open(file_name.c_str(), O_RDONLY);
104 #endif
105 if (ret == TS_FILE_INVALID && die_if_failed) {
106 Printf("ERROR: can not open file %s\n", file_name.c_str());
107 exit(1);
108 }
109 return ret;
110 }
111
112 // Read the contents of a file to string. Valgrind version.
ReadFileToString(const string & file_name,bool die_if_failed)113 string ReadFileToString(const string &file_name, bool die_if_failed) {
114 TS_FILE fd = OpenFileReadOnly(file_name, die_if_failed);
115 if (fd == TS_FILE_INVALID) {
116 return string();
117 }
118 char buff[257] = {0};
119 int n_read;
120 string res;
121 while ((n_read = read(fd, buff, sizeof(buff) - 1)) > 0) {
122 buff[n_read] = 0;
123 res.append(buff, n_read);
124 }
125 close(fd);
126 return res;
127 }
128