Skip to content

Commit 9024538

Browse files
committed
feat: added some extra functions. Did not mean to edit on master, but this is a class that is not used by anyone else.
1 parent 5e52630 commit 9024538

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

include/vb_util_lib/file_spec.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ class FileSpec
2323
void setType(FileType type);
2424
Directory getDir() const;
2525
void setDir(Directory dir);
26+
void setPath(std::string& base, std::string param, std::string env_name);
27+
void getPath(std::string& full_name);
28+
std::string getPrefix() {
29+
return prefix_;
30+
}
2631

2732
private:
2833
Directory dir_;
2934
FileType type_;
3035
std::string name_;
36+
std::string prefix_ {""};
3137
std::string md5_;
3238
boost::posix_time::ptime date_;
3339
int size_;

src/vb_util_lib/file_spec.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <vb_util_lib/file_spec.h>
2+
#include <cstdlib>
23

34
const boost::posix_time::ptime& FileSpec::getDate() const {
45
return date_;
@@ -47,3 +48,61 @@ FileSpec::Directory FileSpec::getDir() const {
4748
void FileSpec::setDir(FileSpec::Directory dir) {
4849
dir_ = dir;
4950
}
51+
52+
void FileSpec::setPath(std::string& base, std::string param, std::string env_name) {
53+
char sep = '/';
54+
std::string base_dir = "";
55+
prefix_ = "";
56+
name_ = "";
57+
58+
size_t i = base.rfind(sep, base.length());
59+
if (i != std::string::npos)
60+
{
61+
name_ = base.substr(i+1, base.length() - i);
62+
base_dir = base.substr(0, i+1);
63+
}
64+
else
65+
{
66+
name_= base;
67+
base_dir = "";
68+
}
69+
70+
if (base_dir.length() != 0 && base_dir.at(0) == sep)
71+
{
72+
// Absolute path name
73+
prefix_ = base_dir;
74+
return;
75+
}
76+
77+
// Append the separator to the base_dir
78+
if (base_dir.length() > 0 && base_dir.at(base_dir.length()-1) != sep)
79+
{
80+
base_dir += "/";
81+
}
82+
83+
if (param.length() > 0)
84+
{
85+
// use the parameter as the directory name.
86+
prefix_ = param;
87+
} else if (env_name.length() > 0)
88+
{
89+
// use the environment variable
90+
if(const char* env_p = std::getenv(env_name.c_str()))
91+
{
92+
prefix_ = env_p;
93+
}
94+
}
95+
96+
// Append the separator to the prefix
97+
if (prefix_.length() > 0 && prefix_.at(prefix_.length()-1) != sep)
98+
{
99+
prefix_ += "/";
100+
}
101+
prefix_ += base_dir;
102+
103+
return;
104+
}
105+
106+
void FileSpec::getPath(std::string& full_name) {
107+
full_name = prefix_ + name_;
108+
}

0 commit comments

Comments
 (0)