ROSE  0.11.145.0
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 
8 namespace 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.
13 namespace 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 
25 enum 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 
52 typedef std::vector<StringWithLineNumber> FileWithLineNumbers;
53 
55 ROSE_UTIL_API OSType getOSType();
56 
64 ROSE_UTIL_API void writeFile(const std::string& outputString, const std::string& fileNameString,
65  const std::string& directoryName);
66 
73 ROSE_UTIL_API std::string readFile(const std::string& fileName);
74 
83 ROSE_UTIL_API FileWithLineNumbers readFileWithPos(const std::string& fileName);
84 
89 ROSE_UTIL_API void homeDir(std::string& dir);
90 
96 ROSE_UTIL_API std::string stripPathFromFileName(const std::string &fileNameWithPath);
97 
105 ROSE_UTIL_API std::string getPathFromFileName(const std::string &fileNameWithPath);
106 
111 ROSE_UTIL_API std::string stripFileSuffixFromFileName(const std::string & fileNameWithSuffix);
112 
117 ROSE_UTIL_API std::string getAbsolutePathFromRelativePath(const std::string &relativePath, bool printErrorIfAny = false);
118 
126 ROSE_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 
143 ROSE_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 */
150 enum FileNameLocation {
151  FILENAME_LOCATION_UNKNOWN,
152  FILENAME_LOCATION_USER,
153  FILENAME_LOCATION_LIBRARY,
154  FILENAME_LOCATION_NOT_EXIST
155 };
156 
157 static const std::string FILENAME_LIBRARY_UNKNOWN = "Unknown";
158 static const std::string FILENAME_LIBRARY_USER = "User";
159 static const std::string FILENAME_LIBRARY_C = "C";
160 static const std::string FILENAME_LIBRARY_STDCXX = "C++";
161 static const std::string FILENAME_LIBRARY_STL = "STL";
162 static const std::string FILENAME_LIBRARY_LINUX = "Linux";
163 static const std::string FILENAME_LIBRARY_GCC = "GCC";
164 static const std::string FILENAME_LIBRARY_BOOST = "Boost";
165 static const std::string FILENAME_LIBRARY_ROSE = "Rose";
166 
167 // CH (2/16/2010): Use this typedef to avoid following changes
168 typedef std::string FileNameLibrary;
169 
170 /* This is the return type of classifyFileName, which provides all the details it infers */
172 private:
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 
179 public:
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 
228 ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath);
229 
234 ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath, OSType os);
235 
241 ROSE_UTIL_API FileNameClassification classifyFileName(const std::string& fileName, const std::string& appPath,
242  const std::map<std::string, std::string>& libPathCollection);
243 
248 ROSE_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 
255 ROSE_UTIL_API const std::string stripDotsFromHeaderFileName(const std::string& name);
256 
265 ROSE_UTIL_API int directoryDistance(const std::string& left, const std::string& right);
266 
272 ROSE_UTIL_API std::vector<std::string> readWordsInFile(std::string filename);
273 
274 // popen_wrapper is defined in sage_support.C
275 
281 bool popen_wrapper(const std::string &command, std::vector<std::string> &result);
282 
284 inline std::ostream& operator<<(std::ostream& os, const StringWithLineNumber& s) {
285  os << s.toString();
286  return os;
287 }
288 
298 ROSE_UTIL_API std::string toString(const FileWithLineNumbers& strings, const std::string& filename = "<unknown>", int line = 1);
299 
303 inline FileWithLineNumbers& operator+=(FileWithLineNumbers& a, const FileWithLineNumbers& b) {
304  a.insert(a.end(), b.begin(), b.end());
305  return a;
306 }
307 
311 inline FileWithLineNumbers operator+(const FileWithLineNumbers& a, const FileWithLineNumbers& b) {
312  FileWithLineNumbers f = a;
313  f += b;
314  return f;
315 }
316 
330 inline 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 
339 inline FileWithLineNumbers& operator<<(FileWithLineNumbers& f, const char* str) {
340  f << std::string(str);
341  return f;
342 }
351 ROSE_UTIL_API std::string copyEdit(const std::string& inputString, const std::string & oldToken, const std::string & newToken);
352 ROSE_UTIL_API FileWithLineNumbers copyEdit(const FileWithLineNumbers& inputString, const std::string& oldToken,
353  const std::string& newToken);
354 ROSE_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 void homeDir(std::string &dir)
Name of the home directory.
ROSE_UTIL_API FileWithLineNumbers readFileWithPos(const std::string &fileName)
Reads an entire text file.
ROSE_UTIL_API std::string readFile(const std::string &fileName)
Reads entire text file.
FileWithLineNumbers operator+(const FileWithLineNumbers &a, const FileWithLineNumbers &b)
Concatenate vectors of strings with source location.
Definition: FileUtility.h:311
ROSE_UTIL_API std::string getPathFromFileName(const std::string &fileNameWithPath)
Returns all but the last component of a path in a filesystem.
ROSE_UTIL_API OSType getOSType()
Obtain operating system type information.
ROSE_UTIL_API std::string stripFileSuffixFromFileName(const std::string &fileNameWithSuffix)
Get the file name without the ".suffix".
std::vector< StringWithLineNumber > FileWithLineNumbers
A vector of strings with line numbers and file names.
Definition: FileUtility.h:52
Main namespace for the ROSE library.
FileWithLineNumbers & operator+=(FileWithLineNumbers &a, const FileWithLineNumbers &b)
Append strings with source location information to vector of such.
Definition: FileUtility.h:303
ROSE_UTIL_API std::vector< std::string > readWordsInFile(std::string filename)
Reads words from a file.
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.
String with source location information.
Definition: FileUtility.h:40
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::list< std::string > findfile(std::string patternString, std::string pathString) __attribute__((deprecated))
Find file names non-recursively.
ROSE_UTIL_API int directoryDistance(const std::string &left, const std::string &right)
Edit distance between two directory names.
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 stripPathFromFileName(const std::string &fileNameWithPath)
Returns the last component of a path in a filesystem.
std::ostream & operator<<(std::ostream &os, const StringWithLineNumber &s)
Prints a StringWithLineNumber.
Definition: FileUtility.h:284
ROSE_UTIL_API std::string getAbsolutePathFromRelativePath(const std::string &relativePath, bool printErrorIfAny=false)
Get the absolute path from the relative path.
bool popen_wrapper(const std::string &command, std::vector< std::string > &result)
Simple wrapper for Unix popen command.
ROSE_UTIL_API const std::string stripDotsFromHeaderFileName(const std::string &name)
Remove leading dots.
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 void writeFile(const std::string &outputString, const std::string &fileNameString, const std::string &directoryName)
Create a file.