Adding PROX(Packet pROcessing eXecution engine) VNF to sampleVNF
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / path.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <iostream>
18 #include <iomanip>
19 #include <sys/stat.h>
20 #include <sstream>
21 #include <fstream>
22
23 #include "path.hpp"
24
25 bool Path::isDir() const
26 {
27         struct stat s = { 0 };
28
29         if (stat(path.c_str(), &s)) {
30                 return false;
31         }
32
33         return s.st_mode & S_IFDIR;
34 }
35
36 bool Path::isFile() const
37 {
38         struct stat s = { 0 };
39
40         if (stat(path.c_str(), &s)) {
41                 return false;
42         }
43
44         return s.st_mode & S_IFREG;
45 }
46
47 Path Path::add(const string& str) const
48 {
49         stringstream ss;
50
51         ss << path << str;
52
53         return Path(ss.str());
54 }
55
56 Path Path::add(int number) const
57 {
58         stringstream ss;
59
60         ss << path << number;
61
62         return Path(ss.str());
63 }
64
65 Path &Path::concat(const string &add)
66 {
67         stringstream ss;
68
69         ss << path << add;
70         path = ss.str();
71
72         return *this;
73 }
74
75 int Path::mkdir() const
76 {
77         if (!isDir())
78                 return ::mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
79         return 0;
80 }
81
82 std::ostream& operator<<(std::ofstream &stream, const Path &p)
83 {
84         stream << p.path.c_str();
85
86         return stream;
87 }
88
89 string Path::getFileName() const
90 {
91         for (size_t i = path.size() - 1; i >= 0; --i) {
92                 if (path[i] == '/') {
93                         return path.substr(i + 1);
94                 }
95         }
96         return path;
97 }