bpp-core3  3.0.0
FileTools.cpp
Go to the documentation of this file.
1 //
2 // File: FileTools.cpp
3 // Authors:
4 // Guillaume Deuchst
5 // Julien Dutheil
6 // Last modified: 2005-08-23 00:00:00
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12  This software is a computer program whose purpose is to provide utilitary
13  classes. This file belongs to the Bio++ Project.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #include <sys/stat.h>
43 
44 #include "../Text/TextTools.h"
45 #include "FileTools.h" // class's header file
46 
47 using namespace bpp;
48 using namespace std;
49 
50 /******************************************************************************/
51 
52 char FileTools::DIR_SEP = '/';
53 
54 /******************************************************************************/
55 
56 bool FileTools::fileExists(const std::string& filename)
57 {
58  ifstream file(filename.c_str());
59  bool test = file.good(); // needed for CLang.
60  file.close();
61  return test;
62 }
63 
64 /******************************************************************************/
65 
66 bool FileTools::directoryExists(const std::string& path)
67 {
68  ifstream file(path.c_str());
69  bool test = file.good(); // needed for CLang.
70  file.close();
71  return test;
72 }
73 
74 /******************************************************************************/
75 
76 std::string FileTools::getFileName(const std::string& path, char dirSep)
77 {
78  ptrdiff_t end = static_cast<ptrdiff_t>(path.find_last_of("."));
79  ptrdiff_t begin = static_cast<ptrdiff_t>(path.find_last_of(dirSep) + 1);
80 
81  // Return an empty string if specified string isn't a path
82  if (begin > end)
83  return "";
84 
85  // Copy path and deletion of directories and extension
86  string result(path);
87  result.erase(result.begin() + end, result.end());
88  result.erase(result.begin(), result.begin() + begin);
89 
90  // Send file name
91  return result;
92 }
93 
94 /******************************************************************************/
95 
96 streampos FileTools::getFileSize(const std::string& filename)
97 {
98  std::ifstream stream;
99  streampos size;
100  stream.open(filename.c_str(), std::ios::ate);
101  size = stream.tellg();
102  stream.close();
103  return size;
104 }
105 
106 /******************************************************************************/
107 
108 std::string FileTools::getParent(const std::string& path, char dirSep)
109 {
110  // Position of file name:
111  ptrdiff_t begin = static_cast<ptrdiff_t>(path.find_last_of(dirSep));
112 
113  // Copy string and delte filename:
114  string result(path);
115  result.erase(result.begin() + begin, result.end());
116 
117  // Send directories
118  return result;
119 }
120 
121 /******************************************************************************/
122 
123 std::string FileTools::getExtension(const std::string& path)
124 {
125  size_t end = path.find_last_of(".");
126  return path.substr(end + 1);
127 }
128 
129 /******************************************************************************/
130 
131 std::vector<std::string> FileTools::putStreamIntoVectorOfStrings(std::istream& input)
132 {
133  vector<string> vs;
134  string s = "";
135  while (input)
136  {
137  getline(input, s, '\n');
138  vs.push_back(s);
139  }
140  return vs;
141 }
142 
143 /******************************************************************************/
144 
145 std::string FileTools::getNextLine(std::istream& in)
146 {
147  if (in.eof())
148  return string("");
149  string temp("");
150  while (!in.eof() && TextTools::isEmpty(temp))
151  {
152  getline(in, temp, '\n');
153  }
154  return temp;
155 }
156 
157 /******************************************************************************/
static std::string getNextLine(std::istream &in)
Get the next non-blanck line of a stream.
Definition: FileTools.cpp:145
static char DIR_SEP
Definition: FileTools.h:72
static bool fileExists(const std::string &filename)
Tells if a file exist.
Definition: FileTools.cpp:56
static std::string getExtension(const std::string &path)
Get the extension of a file.
Definition: FileTools.cpp:123
static std::streampos getFileSize(const std::string &filename)
Get the size of a file.
Definition: FileTools.cpp:96
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:108
static bool directoryExists(const std::string &path)
Tells if a directory exist.
Definition: FileTools.cpp:66
static std::vector< std::string > putStreamIntoVectorOfStrings(std::istream &input)
Reads a stream and write each line in a vector.
Definition: FileTools.cpp:131
static std::string getFileName(const std::string &path, char dirSep=DIR_SEP)
Get the name of of a file, without extension.
Definition: FileTools.cpp:76
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be 'empty' if it is only made of white spaces.
Definition: TextTools.cpp:58