bpp-core3  3.0.0
FileTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <sys/stat.h>
6 
7 #include "../Text/TextTools.h"
8 #include "FileTools.h" // class's header file
9 
10 using namespace bpp;
11 using namespace std;
12 
13 /******************************************************************************/
14 
15 char FileTools::DIR_SEP = '/';
16 
17 /******************************************************************************/
18 
19 bool FileTools::fileExists(const std::string& filename)
20 {
21  ifstream file(filename.c_str());
22  bool test = file.good(); // needed for CLang.
23  file.close();
24  return test;
25 }
26 
27 /******************************************************************************/
28 
29 bool FileTools::directoryExists(const std::string& path)
30 {
31  ifstream file(path.c_str());
32  bool test = file.good(); // needed for CLang.
33  file.close();
34  return test;
35 }
36 
37 /******************************************************************************/
38 
39 std::string FileTools::getFileName(const std::string& path, char dirSep)
40 {
41  ptrdiff_t end = static_cast<ptrdiff_t>(path.find_last_of("."));
42  ptrdiff_t begin = static_cast<ptrdiff_t>(path.find_last_of(dirSep) + 1);
43 
44  // Return an empty string if specified string isn't a path
45  if (begin > end)
46  return "";
47 
48  // Copy path and deletion of directories and extension
49  string result(path);
50  result.erase(result.begin() + end, result.end());
51  result.erase(result.begin(), result.begin() + begin);
52 
53  // Send file name
54  return result;
55 }
56 
57 /******************************************************************************/
58 
59 streampos FileTools::getFileSize(const std::string& filename)
60 {
61  std::ifstream stream;
62  streampos size;
63  stream.open(filename.c_str(), std::ios::ate);
64  size = stream.tellg();
65  stream.close();
66  return size;
67 }
68 
69 /******************************************************************************/
70 
71 std::string FileTools::getParent(const std::string& path, char dirSep)
72 {
73  // Position of file name:
74  ptrdiff_t begin = static_cast<ptrdiff_t>(path.find_last_of(dirSep));
75 
76  // Copy string and delte filename:
77  string result(path);
78  result.erase(result.begin() + begin, result.end());
79 
80  // Send directories
81  return result;
82 }
83 
84 /******************************************************************************/
85 
86 std::string FileTools::getExtension(const std::string& path)
87 {
88  size_t end = path.find_last_of(".");
89  return path.substr(end + 1);
90 }
91 
92 /******************************************************************************/
93 
94 std::vector<std::string> FileTools::putStreamIntoVectorOfStrings(std::istream& input)
95 {
96  vector<string> vs;
97  string s = "";
98  while (input)
99  {
100  getline(input, s, '\n');
101  vs.push_back(s);
102  }
103  return vs;
104 }
105 
106 /******************************************************************************/
107 
108 std::string FileTools::getNextLine(std::istream& in)
109 {
110  if (in.eof())
111  return string("");
112  string temp("");
113  while (!in.eof() && TextTools::isEmpty(temp))
114  {
115  getline(in, temp, '\n');
116  }
117  return temp;
118 }
119 
120 /******************************************************************************/
static std::streampos getFileSize(const std::string &filename)
Get the size of a file.
Definition: FileTools.cpp:59
static std::string getExtension(const std::string &path)
Get the extension of a file.
Definition: FileTools.cpp:86
STL namespace.
static char DIR_SEP
Definition: FileTools.h:34
static std::vector< std::string > putStreamIntoVectorOfStrings(std::istream &input)
Reads a stream and write each line in a vector.
Definition: FileTools.cpp:94
static bool directoryExists(const std::string &path)
Tells if a directory exist.
Definition: FileTools.cpp:29
static std::string getNextLine(std::istream &in)
Get the next non-blanck line of a stream.
Definition: FileTools.cpp:108
static std::string getParent(const std::string &path, char dirSep=DIR_SEP)
Get the path of the parent directry of the given file/dir.
Definition: FileTools.cpp:71
static bool fileExists(const std::string &filename)
Tells if a file exist.
Definition: FileTools.cpp:19
static std::string getFileName(const std::string &path, char dirSep=DIR_SEP)
Get the name of of a file, without extension.
Definition: FileTools.cpp:39
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be &#39;empty&#39; if it is only made of white spaces...
Definition: TextTools.cpp:20