1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #ifndef FILEUTILS_H
27 #define FILEUTILS_H
28 
29 
30 #include <fstream>
31 #include "SysInfo.h"
32 
33 
34 namespace FileUtils {
35     extern const tstring::value_type pathSeparator;
36 
37     // Returns 'true' if the given character is a path separator.
38     bool isDirSeparator(const tstring::value_type c);
39 
40     // returns directory part of the path.
41     // returns empty string if the path contains only filename.
42     // if the path ends with slash/backslash,
43     // returns removeTrailingSlashes(path).
44     tstring dirname(const tstring &path);
45 
46     // returns basename part of the path
47     // if the path ends with slash/backslash, returns empty string.
48     tstring basename(const tstring &path);
49 
50     /**
51      * Translates forward slashes to back slashes and returns lower case version
52      * of the given string.
53      */
54     tstring normalizePath(tstring v);
55 
56     // Returns suffix of the path. If the given path has a suffix the first
57     // character of the return value is '.'.
58     // Otherwise return value if empty string.
59     tstring suffix(const tstring &path);
60 
61     // combines two strings into a path
62     tstring combinePath(const tstring& parent, const tstring& child);
63 
64     // removes trailing slashes and backslashes in the path if any
65     tstring removeTrailingSlash(const tstring& path);
66 
67     /**
68      * Replace file suffix, example replaceSuffix("file/path.txt", ".csv")
69      * @param path file path to replace suffix
70      * @param suffix new suffix for path
71      * @return return file path with new suffix
72      */
73     tstring replaceSuffix(const tstring& path, const tstring& suffix=tstring());
74 
75     // remove the executable suffix if there is one
76     tstring stripExeSuffix(const tstring& path);
77 
78     /**
79      * Returns absolute path of the given path.
80      * If the given string is empty, returns absolute path to the current
81      * directory.
82      */
83     tstring toAbsolutePath(const tstring& path);
84 
85     // Helper to construct path from multiple components.
86     //
87     // Sample usage:
88     //  Construct "c:\Program Files\Java" string from three components
89     //
90     //  tstring path = FileUtils::mkpath()  << _T("c:")
91     //                                      << _T("Program Files")
92     //                                      << _T("Java");
93     //
94     class mkpath {
95     public:
96         operator const tstring& () const {
97             return path;
98         }
99 
100         mkpath& operator << (const tstring& p) {
101             path = combinePath(path, p);
102             return *this;
103         }
104 
105         // mimic std::string
c_str()106         const tstring::value_type* c_str() const {
107             return path.c_str();
108         }
109     private:
110         tstring path;
111     };
112 
113     // checks if the file or directory exists
114     bool isFileExists(const tstring &filePath);
115 
116     // checks is the specified file is a directory
117     // returns false if the path does not exist
118     bool isDirectory(const tstring &filePath);
119 
120     // checks if the specified directory is not empty
121     // returns true if the path is an existing directory and
122     // it contains at least one file other than "." or "..".
123     bool isDirectoryNotEmpty(const tstring &dirPath);
124 
125 } // FileUtils
126 
127 #endif // FILEUTILS_H
128