Brian Silverman | 70325d6 | 2015-09-20 17:00:43 -0400 | [diff] [blame] | 1 | // Copyright (c) 2007, 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 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // --- |
| 31 | // Author: csilvers@google.com (Craig Silverstein) |
| 32 | // |
| 33 | // Routines for dealing with filesystem paths. Mostly to make porting |
| 34 | // to windows easier, though it's nice to have an API for this kind of |
| 35 | // thing. |
| 36 | |
| 37 | #include <config.h> |
| 38 | #include <string> |
| 39 | #include <ctype.h> // for isalpha, used on windows |
| 40 | #include <string.h> // for strchr |
| 41 | #include <ctemplate/template_pathops.h> |
| 42 | |
| 43 | #ifndef PATH_SEP |
| 44 | # ifdef _WIN32 |
| 45 | # define PATH_SEP '\\' |
| 46 | # else |
| 47 | # define PATH_SEP '/' // assume a unix-like system |
| 48 | # endif |
| 49 | #endif |
| 50 | |
| 51 | namespace ctemplate { |
| 52 | |
| 53 | using std::string; |
| 54 | |
| 55 | const char kCWD[] = { '.', PATH_SEP, '\0' }; |
| 56 | const char kRootdir[] = { PATH_SEP, '\0' }; |
| 57 | |
| 58 | // Windows is bi-slashual: we always write separators using PATH_SEP (\), |
| 59 | // but accept either PATH_SEP or the unix / as a separator on input. |
| 60 | inline bool IsPathSep(char c) { |
| 61 | #ifdef _WIN32 |
| 62 | if (c == '/') return true; |
| 63 | #endif |
| 64 | return c == PATH_SEP; |
| 65 | } |
| 66 | |
| 67 | // ---------------------------------------------------------------------- |
| 68 | // PathJoin() |
| 69 | // Joins a and b together to form a path. If 'b' starts with '/' |
| 70 | // then we just return b, otherwise a + b. If 'a' does not end in |
| 71 | // a slash we put a slash in the middle. Does *not* resolve ..'s |
| 72 | // and stuff like that, for now. Not very efficient. |
| 73 | // Returns a string which is the joining. |
| 74 | // ---------------------------------------------------------------------- |
| 75 | |
| 76 | string PathJoin(const string& a, const string& b) { |
| 77 | if (b.empty()) return a; // degenerate case 1 |
| 78 | if (a.empty()) return b; // degenerate case 2 |
| 79 | if (IsAbspath(b)) return b; // absolute path |
| 80 | if (IsDirectory(a)) return a + b; // 'well-formed' case |
| 81 | return a + PATH_SEP + b; |
| 82 | } |
| 83 | |
| 84 | bool IsAbspath(const string& path) { |
| 85 | #ifdef _WIN32 |
| 86 | if (path.size() > 2 && // c:\ is an absolute path on windows |
| 87 | path[1] == ':' && IsPathSep(path[2]) && isalpha(path[0])) { |
| 88 | return true; |
| 89 | } |
| 90 | #endif |
| 91 | return !path.empty() && IsPathSep(path[0]); |
| 92 | } |
| 93 | |
| 94 | bool IsDirectory(const string& path) { |
| 95 | return !path.empty() && IsPathSep(path[path.size()-1]); |
| 96 | } |
| 97 | |
| 98 | void NormalizeDirectory(string* dir) { |
| 99 | if (dir->empty()) return; // I guess "" means 'current directory' |
| 100 | if (!IsPathSep((*dir)[dir->size()-1])) |
| 101 | *dir += PATH_SEP; |
| 102 | } |
| 103 | |
| 104 | string Basename(const string& path) { |
| 105 | for (const char* p = path.data() + path.size()-1; p >= path.data(); --p) { |
| 106 | if (IsPathSep(*p)) |
| 107 | return string(p+1, path.data() + path.size() - (p+1)); |
| 108 | } |
| 109 | return path; // no path-separator found, so whole string is the basename |
| 110 | } |
| 111 | |
| 112 | bool ContainsFullWord(const string& text, const string& word) { |
| 113 | // List of delimiter characters to be considered. Please update the comment in |
| 114 | // the header file if you change this list. |
| 115 | static const char* delim = ".,_-#*?:"; |
| 116 | |
| 117 | const int inputlength = text.length(); |
| 118 | const int wordlength = word.length(); |
| 119 | |
| 120 | // corner cases |
| 121 | if (inputlength == 0 || wordlength == 0 || wordlength > inputlength) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | int nextmatchpos = 0; // position from where search in the input string |
| 126 | while (nextmatchpos < inputlength) { |
| 127 | const int pos = text.find(word, nextmatchpos); |
| 128 | if (pos == string::npos) { |
| 129 | return false; // no match at all |
| 130 | } |
| 131 | |
| 132 | // if found, check that it is surrounded by delimiter characters. |
| 133 | bool pre_delimited = (pos == 0) || |
| 134 | (strchr(delim, text.at(pos - 1)) != NULL); |
| 135 | bool post_delimited = (pos >= inputlength - wordlength) || |
| 136 | (strchr(delim, text.at(pos + wordlength)) != NULL); |
| 137 | if (pre_delimited && post_delimited) return true; |
| 138 | |
| 139 | nextmatchpos = (pos + wordlength + 1); |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | } |