Home | History | Annotate | Download | only in tsan
      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 
     29 void Report(const char *format, ...);
     30 
     31 bool StringMatch(const string& wildcard, const string& text) {
     32   const char* c_text = text.c_str();
     33   const char* c_wildcard = wildcard.c_str();
     34   // Start of the current look-ahead. Everything before these positions is a
     35   // definite, optimal match.
     36   const char* c_text_last = NULL;
     37   const char* c_wildcard_last = NULL;
     38 
     39   char last_wc_char = wildcard[wildcard.size() - 1];
     40 
     41   if (last_wc_char == '*' && wildcard.size() == 1) {
     42     return true;  // '*' matches everything.
     43   }
     44 
     45   if (last_wc_char != '*' && last_wc_char != '?'
     46       && last_wc_char != text[text.size() - 1]) {
     47     // short cut for the case when the wildcard does not end with '*' or '?'
     48     // and the last characters of wildcard and text do not match.
     49     return false;
     50   }
     51 
     52   while (*c_text) {
     53     if (*c_wildcard == '*') {
     54       while (*++c_wildcard == '*') {
     55         // Skip all '*'.
     56       }
     57       if (!*c_wildcard) {
     58         // Ends with a series of '*'.
     59         return true;
     60       }
     61       c_text_last = c_text;
     62       c_wildcard_last = c_wildcard;
     63     } else if ((*c_text == *c_wildcard) || (*c_wildcard == '?')) {
     64       ++c_text;
     65       ++c_wildcard;
     66     } else if (c_text_last) {
     67       // No match. But we have seen at least one '*', so rollback and try at the
     68       // next position.
     69       c_wildcard = c_wildcard_last;
     70       c_text = c_text_last++;
     71     } else {
     72       return false;
     73     }
     74   }
     75 
     76   // Skip all '*' at the end of the wildcard.
     77   while (*c_wildcard == '*') {
     78     ++c_wildcard;
     79   }
     80 
     81   return !*c_wildcard;
     82 }
     83 
     84 string ConvertToPlatformIndependentPath(const string &s) {
     85   string ret = s;
     86 #ifdef _MSC_VER
     87   // TODO(timurrrr): do we need anything apart from s/\\///g?
     88   size_t it = 0;
     89   while ((it = ret.find("\\", it)) != string::npos) {
     90     ret.replace(it, 1, "/");
     91   }
     92 #endif // _MSC_VER
     93   return ret;
     94 }
     95 
     96 TS_FILE OpenFileReadOnly(const string &file_name, bool die_if_failed) {
     97   TS_FILE ret = TS_FILE_INVALID;
     98 #ifdef TS_VALGRIND
     99   SysRes sres = VG_(open)((const Char*)file_name.c_str(), VKI_O_RDONLY, 0);
    100   if (!sr_isError(sres))
    101     ret = sr_Res(sres);
    102 #elif defined(_MSC_VER)
    103   ret = fopen(file_name.c_str(), "r");
    104 #else // no TS_VALGRIND
    105   ret = open(file_name.c_str(), O_RDONLY);
    106 #endif
    107   if (ret == TS_FILE_INVALID && die_if_failed) {
    108     Report("ERROR: can not open file %s\n", file_name.c_str());
    109     exit(1);
    110   }
    111   return ret;
    112 }
    113 
    114 // Read the contents of a file to string. Valgrind version.
    115 string ReadFileToString(const string &file_name, bool die_if_failed) {
    116   TS_FILE fd = OpenFileReadOnly(file_name, die_if_failed);
    117   if (fd == TS_FILE_INVALID) {
    118     return string();
    119   }
    120   char buff[257] = {0};
    121   int n_read;
    122   string res;
    123   while ((n_read = read(fd, buff, sizeof(buff) - 1)) > 0) {
    124     buff[n_read] = 0;
    125     res.append(buff, n_read);
    126   }
    127   close(fd);
    128   return res;
    129 }
    130