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