ROSE 0.11.145.192
FileUtility.h
1#ifndef ROSE_FileUtility_H
2#define ROSE_FileUtility_H
3
4#include "commandline_processing.h"
5
6#include <Rose/StringUtility.h>
7
8namespace Rose {
9
10// [Robb P Matzke 2016-06-16]: These file utilities were all originally part of StringUtility. I'm leaving them here for now
11// just so I don't need to make so many changes to projects that use these, but you should expect these to be moved sometime in
12// the future.
13namespace StringUtility {
14
15
17// This part of the StringUtility API deals with file names and should be moved to some other name space. In particular, it
18// provides no definitions for "path", "filename", "extension", etc. and many of these functions won't work properly on a
19// non-POSIX system. Therefore, consider using Rose::FileSystem, which is mostly a thin wrapper around boost::filesystem. The
20// boost::filesystem documentation has good definitions for what the various terms should mean and works on non-POSIX file
21// systems.
23
24
25enum OSType {
26 OS_TYPE_UNKNOWN,
27 OS_TYPE_LINUX,
28 OS_TYPE_OSX,
29 OS_TYPE_WINDOWS,
30 OS_TPYE_WINDOWSXP
31};
32
41 std::string str; // DQ (1/23/2010): this name is difficult to trace within the code.
42 std::string filename; // Empty string means generated code
43 unsigned int line;
44
45 StringWithLineNumber(const std::string& str, const std::string& filename, unsigned int line)
46 : str(str), filename(filename), line(line) {}
47
48 ROSE_UTIL_API std::string toString() const;
49};
50
52typedef std::vector<StringWithLineNumber> FileWithLineNumbers;
53
55ROSE_UTIL_API OSType getOSType();
56
64ROSE_UTIL_API void writeFile(const std::string& outputString, const std::string& fileNameString,
65 const std::string& directoryName);
66
73ROSE_UTIL_API std::string readFile(const std::string& fileName);
74
83ROSE_UTIL_API FileWithLineNumbers readFileWithPos(const std::string& fileName);
84
89ROSE_UTIL_API void homeDir(std::string& dir);
90
96ROSE_UTIL_API std::string stripPathFromFileName(const std::string &fileNameWithPath);
97
105ROSE_UTIL_API std::string getPathFromFileName(const std::string &fileNameWithPath);
106
111ROSE_UTIL_API std::string stripFileSuffixFromFileName(const std::string & fileNameWithSuffix);
112
117ROSE_UTIL_API std::string getAbsolutePathFromRelativePath(const std::string &relativePath, bool printErrorIfAny = false);
118
126ROSE_UTIL_API std::string fileNameSuffix(const std::string &fileName);
127
128
129// [Robb Matzke 2016-05-06]: I am deprecating "findfile" because:
130// 1. It appears to not be used anywhere in ROSE, projects, tests, or documentation.
131// 2. The name is spelled wrong (should be "findFile")
132// 3. File operations should not be in StringUtility since they have nothing to do with string manipulation
133// 4. Rose::FileSystem::findNames does something similar.
134
143ROSE_UTIL_API std::list<std::string> findfile(std::string patternString, std::string pathString)
144 SAWYER_DEPRECATED("use Rose::FileSystem functions instead"); // ROSE_DEPRECATED is not defined here
145
146/* File name location.
147 *
148 * Files can be classified as being in one of three locations: We don't know if it's user or system It is a user (application)
149 * file It is a system library This file does not exist */
150enum FileNameLocation {
151 FILENAME_LOCATION_UNKNOWN,
152 FILENAME_LOCATION_USER,
153 FILENAME_LOCATION_LIBRARY,
154 FILENAME_LOCATION_NOT_EXIST
155};
156
157static const std::string FILENAME_LIBRARY_UNKNOWN = "Unknown";
158static const std::string FILENAME_LIBRARY_USER = "User";
159static const std::string FILENAME_LIBRARY_C = "C";
160static const std::string FILENAME_LIBRARY_STDCXX = "C++";
161static const std::string FILENAME_LIBRARY_STL = "STL";
162static const std::string FILENAME_LIBRARY_LINUX = "Linux";
163static const std::string FILENAME_LIBRARY_GCC = "GCC";
164static const std::string FILENAME_LIBRARY_BOOST = "Boost";
165static const std::string FILENAME_LIBRARY_ROSE = "Rose";
166
167// CH (2/16/2010): Use this typedef to avoid following changes
168typedef std::string FileNameLibrary;
169
170/* This is the return type of classifyFileName, which provides all the details it infers */
172private:
173 FileNameLocation location;
174
175 // CH (2/12/2010): Change 'library' type from enum to string to let user set it
176 FileNameLibrary library;
177 int distance;
178
179public:
180 FileNameClassification(FileNameLocation loc, const FileNameLibrary& lib, int dist)
181 : location(loc), library(lib), distance(dist) {}
182
184 : location(FILENAME_LOCATION_UNKNOWN), library("Unknown"), distance(0) {}
185
186 /* Return the FileNameLocation which is described above with the definition of the enum */
187 FileNameLocation getLocation() const {
188 return location;
189 }
190
191 /* Return the FileNameLibrary which is described above with the definition of the enum */
192 FileNameLibrary getLibrary() const {
193 return library;
194 }
195
196 /* Return the "distance" of the filename from the appPath that was supplied during the call. The distance is defined as
197 * the number of cd's that only move up or down one directory that it would take to move from the directory of the filename
198 * to the directory that was given by appPath. This is intended as a heuristic to gage whether or not one believes that
199 * the filename is related to the source (appPath) directory. Examples:
200 *
201 * Between /a/b/c/file.h and /a/b/d/e/ the distance is 3 because one must cd ..; cd d; cd e; to get to appPath
202 *
203 * *EXCEPTION*: if the appPath is an ancestor of filename then the distance will be 0. The idea being that this filename
204 * is "in" the appPath somewhere and thus part of the application. */
205 int getDistanceFromSourceDirectory() const {
206 return distance;
207 }
208
209 bool isUserCode() const {
210 return location == FILENAME_LOCATION_USER;
211 }
212
213 bool isLibraryCode() const {
214 return location == FILENAME_LOCATION_LIBRARY;
215 }
216
217 /* Return a string name for the library indicated by getLibrary() */
218 std::string getLibraryName() const {
219 return library;
220 }
221};
222
228ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath);
229
234ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath, OSType os);
235
241ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath,
242 const std::map<std::string, std::string>& libPathCollection);
243
248ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath,
249 const std::map<std::string, std::string>& libPathCollection,
250 OSType os);
251
255ROSE_UTIL_API const std::string stripDotsFromHeaderFileName(const std::string& name);
256
265ROSE_UTIL_API int directoryDistance(const std::string& left, const std::string& right);
266
272ROSE_UTIL_API std::vector<std::string> readWordsInFile(std::string filename);
273
274// popen_wrapper is defined in sage_support.C
275
281bool popen_wrapper(const std::string &command, std::vector<std::string> &result);
282
284inline std::ostream& operator<<(std::ostream& os, const StringWithLineNumber& s) {
285 os << s.toString();
286 return os;
287}
288
298ROSE_UTIL_API std::string toString(const FileWithLineNumbers& strings, const std::string& filename = "<unknown>", int line = 1);
299
304 a.insert(a.end(), b.begin(), b.end());
305 return a;
306}
307
313 f += b;
314 return f;
315}
316
330inline FileWithLineNumbers& operator<<(FileWithLineNumbers& f, const std::string& str) {
331 if (!f.empty() && f.back().filename == "") {
332 f.back().str += str;
333 } else {
334 f.push_back(StringWithLineNumber(str, "", 1));
335 }
336 return f;
337}
338
340 f << std::string(str);
341 return f;
342}
351ROSE_UTIL_API std::string copyEdit(const std::string& inputString, const std::string & oldToken, const std::string & newToken);
352ROSE_UTIL_API FileWithLineNumbers copyEdit(const FileWithLineNumbers& inputString, const std::string& oldToken,
353 const std::string& newToken);
354ROSE_UTIL_API FileWithLineNumbers copyEdit(const FileWithLineNumbers& inputString, const std::string& oldToken,
355 const FileWithLineNumbers& newToken);
357} // namespace
358} // namespace
359
360#endif
ROSE_UTIL_API const std::string stripDotsFromHeaderFileName(const std::string &name)
Remove leading dots.
ROSE_UTIL_API std::string fileNameSuffix(const std::string &fileName)
Get the file name suffix (extension) without the leading dot.
ROSE_UTIL_API std::string stripFileSuffixFromFileName(const std::string &fileNameWithSuffix)
Get the file name without the ".suffix".
ROSE_UTIL_API int directoryDistance(const std::string &left, const std::string &right)
Edit distance between two directory names.
ROSE_UTIL_API std::string getPathFromFileName(const std::string &fileNameWithPath)
Returns all but the last component of a path in a filesystem.
std::ostream & operator<<(std::ostream &os, const StringWithLineNumber &s)
Prints a StringWithLineNumber.
ROSE_UTIL_API std::string copyEdit(const std::string &inputString, const std::string &oldToken, const std::string &newToken)
Replace all occurrences of a string with another string.
ROSE_UTIL_API void homeDir(std::string &dir)
Name of the home directory.
ROSE_UTIL_API std::string readFile(const std::string &fileName)
Reads entire text file.
ROSE_UTIL_API std::list< std::string > findfile(std::string patternString, std::string pathString) __attribute__((deprecated))
Find file names non-recursively.
FileWithLineNumbers operator+(const FileWithLineNumbers &a, const FileWithLineNumbers &b)
Concatenate vectors of strings with source location.
ROSE_UTIL_API void writeFile(const std::string &outputString, const std::string &fileNameString, const std::string &directoryName)
Create a file.
ROSE_UTIL_API std::vector< std::string > readWordsInFile(std::string filename)
Reads words from a file.
ROSE_UTIL_API std::string toString(const FileWithLineNumbers &strings, const std::string &filename="<unknown>", int line=1)
Generate C preprocessor #line directives.
ROSE_UTIL_API OSType getOSType()
Obtain operating system type information.
bool popen_wrapper(const std::string &command, std::vector< std::string > &result)
Simple wrapper for Unix popen command.
ROSE_UTIL_API std::string stripPathFromFileName(const std::string &fileNameWithPath)
Returns the last component of a path in a filesystem.
FileWithLineNumbers & operator+=(FileWithLineNumbers &a, const FileWithLineNumbers &b)
Append strings with source location information to vector of such.
std::vector< StringWithLineNumber > FileWithLineNumbers
A vector of strings with line numbers and file names.
Definition FileUtility.h:52
ROSE_UTIL_API FileWithLineNumbers readFileWithPos(const std::string &fileName)
Reads an entire text file.
ROSE_UTIL_API FileNameClassification classifyFileName(const std::string &fileName, const std::string &appPath)
Determine whether a file is source code or system library.
ROSE_UTIL_API std::string getAbsolutePathFromRelativePath(const std::string &relativePath, bool printErrorIfAny=false)
Get the absolute path from the relative path.
The ROSE library.
String with source location information.
Definition FileUtility.h:40