HPCToolkit
FileUtils.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/FileUtils.hpp $
6 // $Id: FileUtils.hpp 4294 2013-07-11 00:15:53Z felipet1326@gmail.com $
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/FileUtils.hpp $
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef FILEUTILS_H_
61 #define FILEUTILS_H_
62 
63 #include "sys/stat.h"
64 #include "dirent.h"
65 #include <iostream>
66 #include <vector>
67 #include <errno.h>
68 #include <string>
69 #include <cstring>
70 #include <stdint.h>
71 
72 using namespace std;
73 namespace TraceviewerServer
74 {
75 #define NO_ERROR 0
76 
77  typedef int FileDescriptor;
78  typedef uint64_t FileOffset;
79 
80  class FileUtils
81  {
82  public:
83  //Combines UNIX-style paths, taking care to ensure that there is always exactly 1 / joining firstPart and secondPart
84  static string combinePaths(string firstPart, string secondPart)
85  {
86  string firstPartWithoutEndingSlash;
87  if (firstPart[firstPart.length()-1]=='/')
88  firstPartWithoutEndingSlash = firstPart.substr(0,firstPart.length()-1);
89  else
90  firstPartWithoutEndingSlash = firstPart;
91  string recondPartWithoutStartingSlash;
92  if (secondPart[0]=='/')
93  recondPartWithoutStartingSlash = secondPart.substr(1, secondPart.length()-1);
94  else
95  recondPartWithoutStartingSlash = secondPart;
96  return firstPartWithoutEndingSlash + "/" + recondPartWithoutStartingSlash;
97 
98  }
99  //Uses stat to check if the specified folder exists and if it is indeed a folder.
100  static bool existsAndIsDir(string p)
101  {
102  struct stat DirInfo;
103  int err = stat(p.c_str(), &DirInfo);
104  bool isDir = S_ISDIR(DirInfo.st_mode);
105  if ((err!=0)|| !isDir)
106  {
107  cerr<<"Either does not exist or is not directory: File " << p<< " Err: "<< err << " isDir: " << isDir << " mode: "<<DirInfo.st_mode<< " Error: " << strerror(errno) << endl;
108  }
109  return (err == 0) && isDir;
110  }
111 
112  //Uses stat to check if the specified file exists
113  static bool exists(string p)
114  {
115  struct stat DirInfo;
116  int err = stat(p.c_str(), &DirInfo);
117  return (err == 0);
118  }
119  //Gets the file size of a file (the file must exist)
120  static FileOffset getFileSize(string p)
121  {
122  struct stat DirInfo;
123  int err = stat(p.c_str(), &DirInfo);
124  if (err != 0)
125  cerr << "Tried to get file size when file does not exist!" << endl;
126  return DirInfo.st_size;
127  }
128  //Gets a list of all files in the directory, excluding any subfolders in the directory
129  static vector<string> getAllFilesInDir(string directory)
130  {
131  vector<string> validFiles;
132  DIR* testDir;
133  dirent* entry;
134  testDir = opendir(directory.c_str());
135  while ((entry = readdir(testDir)))
136  {
137  string fullPath = combinePaths(directory, string(entry->d_name));
138 
139  struct stat dirInfo;
140  bool err = (stat(fullPath.c_str(), &dirInfo) != 0);
141  bool isDir = S_ISDIR(dirInfo.st_mode);
142  bool aFile = !(err || isDir);
143  if (aFile)
144  validFiles.push_back(fullPath);
145 
146  }
147  closedir(testDir);
148  return validFiles;
149  }
150 
151  //Because atoi returns 0 when the string is invalid, there's no easy way
152  //to distinguish between "0" and an invalid string. This method helps with
153  //that by testing the string to ensure it is only whitespace and '0's.
154  static bool stringActuallyZero(string toTest)
155  {
156  for (unsigned int var = 0; var < toTest.length(); var++)
157  {
158  if (toTest[var] != '0' && toTest[var] >= ' ')
159  return false;
160  }
161  return true;
162  }
163  };
164 
165 } /* namespace TraceviewerServer */
166 #endif /* FILEUTILS_H_ */
err
Definition: names.cpp:1
bool isDir(const char *path)
Definition: FileUtil.cpp:182
static bool existsAndIsDir(string p)
Definition: FileUtils.hpp:100
uint64_t FileOffset
Definition: FileUtils.hpp:78
static string combinePaths(string firstPart, string secondPart)
Definition: FileUtils.hpp:84
static bool stringActuallyZero(string toTest)
Definition: FileUtils.hpp:154
static bool exists(string p)
Definition: FileUtils.hpp:113
static vector< string > getAllFilesInDir(string directory)
Definition: FileUtils.hpp:129
static FileOffset getFileSize(string p)
Definition: FileUtils.hpp:120