Skip to content

Commit 218c0c1

Browse files
authored
Merge pull request #32 from AutoModality/AM-277
Am 277
2 parents 99fe1b4 + 6c60b37 commit 218c0c1

File tree

3 files changed

+260
-1
lines changed

3 files changed

+260
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ add_library(am_utils
111111
src/vb_util_lib/imu_class.cpp
112112
src/vb_util_lib/am_heartbeat.cpp
113113
src/vb_util_lib/vb_main.cpp
114-
src/vb_util_lib/bag_logger.cpp)
114+
src/vb_util_lib/bag_logger.cpp
115+
src/vb_util_lib/csv_logger.cpp)
115116

116117
target_link_libraries(am_utils ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} jsoncpp)
117118

include/vb_util_lib/csv_logger.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* csv_logger.h
3+
*
4+
* Created on: May 5, 2020
5+
* Author: ubuntu
6+
*/
7+
8+
#ifndef CSV_LOGGER_H_
9+
#define CSV_LOGGER_H_
10+
11+
#include <vector>
12+
#include <utility>
13+
#include <string>
14+
#include <sstream>
15+
#include <iostream>
16+
#include <fstream>
17+
#include <boost/filesystem.hpp>
18+
19+
using namespace std;
20+
21+
namespace am
22+
{
23+
class CSV_LOGGER
24+
{
25+
private:
26+
std::string file_location_;
27+
public:
28+
CSV_LOGGER(const std::string &fl, const vector<std::string> &col_header);
29+
30+
CSV_LOGGER(const std::string &fl, const std::string &header, char deliminator = ',');
31+
32+
CSV_LOGGER(const std::string &fl);
33+
34+
CSV_LOGGER();
35+
36+
virtual ~CSV_LOGGER();
37+
38+
void splitToVector(std::vector<std::string> &result, const std::string &header, char deliminator = ',');
39+
40+
//Insert Functions
41+
void insert(const std::vector<std::string> &content);
42+
43+
void insert(const std::string &content, char deliminator = ',');
44+
45+
46+
template <class T>
47+
void insert(const T &t, char deliminator = ',', bool endOfLine = false)
48+
{
49+
if(!fileExists(file_location_))
50+
{
51+
std::cout << __func__ << ": File does not exist." << std::endl;
52+
return;
53+
}
54+
std::ofstream of;
55+
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);
56+
57+
std::stringstream ss;
58+
ss << t;
59+
60+
if(endOfLine)
61+
{
62+
ss << "\n";
63+
}
64+
else
65+
{
66+
ss << ',';
67+
}
68+
69+
of << ss.str();
70+
of.close();
71+
72+
}
73+
74+
bool fileExists(const std::string &csv_file_name);
75+
76+
bool createFile(const std::string &csv_file_location);
77+
78+
bool createPath(const std::string &path);
79+
80+
};
81+
}
82+
83+
84+
#endif /* CSV_LOGGER_H_ */

src/vb_util_lib/csv_logger.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* csv_logger.cpp
3+
*
4+
* Created on: May 5, 2020
5+
* Author: ubuntu
6+
*/
7+
8+
9+
#include<vb_util_lib/csv_logger.h>
10+
#include<fstream>
11+
#include<iostream>
12+
#include<string>
13+
#include<boost/filesystem.hpp>
14+
15+
namespace am
16+
{
17+
CSV_LOGGER::CSV_LOGGER()
18+
{
19+
}
20+
21+
CSV_LOGGER::CSV_LOGGER(const std::string &fl, const vector<std::string> &col_header)
22+
{
23+
file_location_ = fl;
24+
bool fileStatus = fileExists(file_location_);
25+
std::ofstream of;
26+
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);
27+
if(!fileStatus)
28+
{
29+
stringstream ss;
30+
for(int i =0; i < col_header.size(); i++)
31+
{
32+
ss << col_header[i];
33+
if(i < col_header.size() - 1) ss << ",";
34+
}
35+
ss << "\n";
36+
of << ss.str();
37+
}
38+
39+
40+
of.close();
41+
}
42+
CSV_LOGGER::CSV_LOGGER(const std::string &fl)
43+
{
44+
file_location_ = fl;
45+
if(!fileExists(file_location_))
46+
{
47+
if(!createFile(fl))
48+
{
49+
std::cout << __func__ << ": File Creation Failed." << std::endl;
50+
}
51+
}
52+
}
53+
54+
CSV_LOGGER::CSV_LOGGER(const std::string &fl, const std::string &header, char deliminator)
55+
{
56+
//Parse the header with deliminator
57+
std::vector<std::string> local_header;
58+
splitToVector(local_header, header, deliminator);
59+
60+
file_location_ = fl;
61+
bool fileStatus = fileExists(file_location_);
62+
std::ofstream of;
63+
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);
64+
if(!fileStatus)
65+
{
66+
stringstream ss;
67+
for(int i =0; i < local_header.size(); i++)
68+
{
69+
ss << local_header[i];
70+
if(i < local_header.size() - 1) ss << ",";
71+
}
72+
ss << "\n";
73+
of << ss.str();
74+
}
75+
}
76+
77+
CSV_LOGGER::~CSV_LOGGER()
78+
{
79+
80+
}
81+
82+
void CSV_LOGGER::splitToVector(std::vector<std::string> &result, const std::string &header, char deliminator)
83+
{
84+
std::stringstream ss(header);
85+
std::string token;
86+
while (std::getline(ss, token, deliminator)) {
87+
result.push_back(token);
88+
}
89+
}
90+
91+
bool CSV_LOGGER::fileExists(const std::string &csv_file_location)
92+
{
93+
if (FILE *file = fopen(csv_file_location.c_str(), "r"))
94+
{
95+
fclose(file);
96+
return true;
97+
}
98+
else
99+
{
100+
return false;
101+
}
102+
}
103+
104+
void CSV_LOGGER::insert(const std::string &content, char deliminator)
105+
{
106+
//Parse the header with deliminator
107+
std::vector<std::string> content_vect;
108+
splitToVector(content_vect, content, deliminator);
109+
insert(content_vect);
110+
}
111+
112+
/*
113+
* insert function inserts a row into the given file
114+
*/
115+
void CSV_LOGGER::insert(const std::vector<std::string> &content)
116+
{
117+
if(!fileExists(file_location_))
118+
{
119+
std::cout << __func__ << ": File does not exist." << std::endl;
120+
return;
121+
}
122+
123+
std::ofstream of;
124+
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);
125+
126+
if(content.size() > 0)
127+
{
128+
std::stringstream ss;
129+
for(int i =0; i < content.size(); i++)
130+
{
131+
ss << content[i];
132+
if(i < content.size() - 1)
133+
{
134+
ss << ",";
135+
}
136+
}
137+
ss << "\n";
138+
of << ss.str();
139+
}
140+
141+
of.close();
142+
143+
}
144+
145+
bool CSV_LOGGER::createFile(const std::string &csv_file_location)
146+
{
147+
if(fileExists(csv_file_location))
148+
{
149+
std::cout << __func__ << ": File creation failed: File Exists Already." << std::endl;
150+
return false;
151+
}
152+
153+
std::ofstream of;
154+
of.open(csv_file_location.c_str(), std::ios::out | std::ios::app);
155+
if(!fileExists(csv_file_location))
156+
{
157+
of.close();
158+
std::cout << __func__ << ": File creation failed: Something went wrong : " << csv_file_location << std::endl;
159+
return false;
160+
}
161+
162+
of.close();
163+
return true;
164+
}
165+
166+
167+
bool CSV_LOGGER::createPath(const std::string &path)
168+
{
169+
return boost::filesystem::create_directories(path);
170+
}
171+
172+
}
173+
174+

0 commit comments

Comments
 (0)