Repository navigation
Expand file tree
/
Copy pathInput.cpp
More file actions
158 lines (113 loc) · 2.94 KB
/
Copy pathInput.cpp
File metadata and controls
158 lines (113 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* \file Input.cpp
* \author Jan Milik <milikjan@fit.cvut.cz>
* \date 2012-03-12
*
* \brief Implementation file for the Input class.
*/
#include "Input.h"
#include <libgen.h>
namespace cppapp {
/**
* \fn string pathBasename(const string &path)
*
* \note The function uses the basename function from the libgen.h header.
* For more information, see <tt>man 3 basename</tt>.
*/
string pathBasename(const string &path)
{
char *p = new char[path.size() + 1];
path.copy(p, path.size());
p[path.size()] = 0;
char *bn = basename(p);
string result(bn);
delete [] p;
return result;
}
string pathWithoutExtension(const string &path)
{
std::size_t dotIndex = path.find_last_of(".");
if (dotIndex == string::npos)
return path;
return path.substr(0, dotIndex);
}
string pathWithExtension(const string &path, const string &extension)
{
string result = pathWithoutExtension(path);
result += ".";
result += extension;
return result;
}
///////////////////////////////////////////////////////////////////////////////
// FILE INPUT
///////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
FileInput::FileInput(string fileName) :
fileName_(fileName),
stream_(fileName.c_str())
{
}
/**
* Close file input.
*/
void FileInput::close()
{
if (!stream_.is_open()) return;
stream_.close();
}
bool FileInput::exists()
{
struct stat fileInfo;
return (stat(fileName_.c_str(), &fileInfo) == 0);
}
bool FileInput::hasExtension(string extension)
{
return FileInput::hasExtension(fileName_, extension);
}
bool FileInput::hasExtension(string fileName, string extension)
{
if (extension.size() < 1) return false;
if (extension[0] != '.') extension = '.' + extension;
if (extension.size() > fileName.size()) return false;
return (fileName.substr(fileName.size() - extension.size(), extension.size()) == extension);
}
///////////////////////////////////////////////////////////////////////////////
// STREAM INPUT
///////////////////////////////////////////////////////////////////////////////
StreamInput::StreamInput(std::string name, std::istream *stream, bool ownsStream) :
name_(name), stream_(stream), ownsStream_(ownsStream)
{
}
StreamInput::StreamInput(std::string name, std::istream *stream) :
name_(name), stream_(stream), ownsStream_(true)
{
}
StreamInput::StreamInput(std::string name, std::istream &stream) :
name_(name), stream_(&stream), ownsStream_(false)
{
}
StreamInput::StreamInput(std::string name, std::string str) :
name_(name), stream_(new stringstream(str)), ownsStream_(true)
{
}
StreamInput::StreamInput(std::string str) :
name_("<string>"), stream_(new stringstream(str)), ownsStream_(true)
{
}
StreamInput::~StreamInput()
{
if (ownsStream_ && (stream_ != NULL)) {
//stream_->close();
delete stream_;
}
stream_ = NULL;
ownsStream_ = false;
}
void StreamInput::close()
{
//if (ownsStream_ && (stream_ != NULL))
// stream_->close();
}
} // namespace cppapp