1 /*
2 Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
3 All Rights Reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
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 copyright
11   notice, this list of conditions and the following disclaimer in the
12   documentation and/or other materials provided with the distribution.
13 * Neither the name of Sony Pictures Imageworks nor the names of its
14   contributors may be used to endorse or promote products derived from
15   this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 
30 #ifndef INCLUDED_OCIO_PATHUTILS_H
31 #define INCLUDED_OCIO_PATHUTILS_H
32 
33 #include <OpenColorIO/OpenColorIO.h>
34 
35 #include <map>
36 
37 OCIO_NAMESPACE_ENTER
38 {
39     namespace pystring
40     {
41     namespace os
42     {
43     namespace path
44     {
45         // This is not currently included in pystring, but we need it
46         // So let's define it locally for now
47 
48         std::string abspath(const std::string & path);
49     }
50     }
51     }
52 
53     // The EnvMap is ordered by the length of the keys (long -> short). This
54     // is so that recursive string expansion will deal with similar prefixed
55     // keys as expected.
56     // ie. '$TEST_$TESTING_$TE' will expand in this order '2 1 3'
57     template <class T>
58     struct EnvMapKey : std::binary_function <T, T, bool>
59     {
60         bool
operatorEnvMapKey61         operator() (const T &x, const T &y) const
62         {
63             // If the lengths are unequal, sort by length
64             if(x.length() != y.length())
65             {
66                 return (x.length() > y.length());
67             }
68             // Otherwise, use the standard string sort comparison
69             else
70             {
71                 return (x<y);
72             }
73         }
74     };
75     typedef std::map< std::string, std::string, EnvMapKey< std::string > > EnvMap;
76 
77     // Get map of current env key = value, or update the existing entries
78     void LoadEnvironment(EnvMap & map, bool update = false);
79 
80     // Expand a string with $VAR, ${VAR} or %VAR% with the keys passed
81     // in the EnvMap.
82     std::string EnvExpand(const std::string & str, const EnvMap & map);
83 
84     // Check if a file exists
85     bool FileExists(const std::string & filename);
86 
87     // Get a fast hash for a file, without reading all the contents.
88     // Currently, this checks the mtime and the inode number.
89     std::string GetFastFileHash(const std::string & filename);
90 
91     void ClearPathCaches();
92 }
93 OCIO_NAMESPACE_EXIT
94 
95 #endif
96