Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ add_library(am_utils
src/vb_util_lib/imu_class.cpp
src/vb_util_lib/am_heartbeat.cpp
src/vb_util_lib/vb_main.cpp
src/vb_util_lib/bag_logger.cpp)
src/vb_util_lib/bag_logger.cpp
src/vb_util_lib/csv_logger.cpp)

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

Expand Down
84 changes: 84 additions & 0 deletions include/vb_util_lib/csv_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* csv_logger.h
*
* Created on: May 5, 2020
* Author: ubuntu
*/

#ifndef CSV_LOGGER_H_
#define CSV_LOGGER_H_

#include <vector>
#include <utility>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>

using namespace std;

namespace am
{
class CSV_LOGGER
{
private:
std::string file_location_;
public:
CSV_LOGGER(const std::string &fl, const vector<std::string> &col_header);

CSV_LOGGER(const std::string &fl, const std::string &header, char deliminator = ',');

CSV_LOGGER(const std::string &fl);

CSV_LOGGER();

virtual ~CSV_LOGGER();

void splitToVector(std::vector<std::string> &result, const std::string &header, char deliminator = ',');

//Insert Functions
void insert(const std::vector<std::string> &content);

void insert(const std::string &content, char deliminator = ',');


template <class T>
void insert(const T &t, char deliminator = ',', bool endOfLine = false)
{
if(!fileExists(file_location_))
{
std::cout << __func__ << ": File does not exist." << std::endl;
return;
}
std::ofstream of;
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);

std::stringstream ss;
ss << t;

if(endOfLine)
{
ss << "\n";
}
else
{
ss << ',';
}

of << ss.str();
of.close();

}

bool fileExists(const std::string &csv_file_name);

bool createFile(const std::string &csv_file_location);

bool createPath(const std::string &path);

};
}


#endif /* CSV_LOGGER_H_ */
174 changes: 174 additions & 0 deletions src/vb_util_lib/csv_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* csv_logger.cpp
*
* Created on: May 5, 2020
* Author: ubuntu
*/


#include<vb_util_lib/csv_logger.h>
#include<fstream>
#include<iostream>
#include<string>
#include<boost/filesystem.hpp>

namespace am
{
CSV_LOGGER::CSV_LOGGER()
{
}

CSV_LOGGER::CSV_LOGGER(const std::string &fl, const vector<std::string> &col_header)
{
file_location_ = fl;
bool fileStatus = fileExists(file_location_);
std::ofstream of;
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);
if(!fileStatus)
{
stringstream ss;
for(int i =0; i < col_header.size(); i++)
{
ss << col_header[i];
if(i < col_header.size() - 1) ss << ",";
}
ss << "\n";
of << ss.str();
}


of.close();
}
CSV_LOGGER::CSV_LOGGER(const std::string &fl)
{
file_location_ = fl;
if(!fileExists(file_location_))
{
if(!createFile(fl))
{
std::cout << __func__ << ": File Creation Failed." << std::endl;
}
}
}

CSV_LOGGER::CSV_LOGGER(const std::string &fl, const std::string &header, char deliminator)
{
//Parse the header with deliminator
std::vector<std::string> local_header;
splitToVector(local_header, header, deliminator);

file_location_ = fl;
bool fileStatus = fileExists(file_location_);
std::ofstream of;
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);
if(!fileStatus)
{
stringstream ss;
for(int i =0; i < local_header.size(); i++)
{
ss << local_header[i];
if(i < local_header.size() - 1) ss << ",";
}
ss << "\n";
of << ss.str();
}
}

CSV_LOGGER::~CSV_LOGGER()
{

}

void CSV_LOGGER::splitToVector(std::vector<std::string> &result, const std::string &header, char deliminator)
{
std::stringstream ss(header);
std::string token;
while (std::getline(ss, token, deliminator)) {
result.push_back(token);
}
}

bool CSV_LOGGER::fileExists(const std::string &csv_file_location)
{
if (FILE *file = fopen(csv_file_location.c_str(), "r"))
{
fclose(file);
return true;
}
else
{
return false;
}
}

void CSV_LOGGER::insert(const std::string &content, char deliminator)
{
//Parse the header with deliminator
std::vector<std::string> content_vect;
splitToVector(content_vect, content, deliminator);
insert(content_vect);
}

/*
* insert function inserts a row into the given file
*/
void CSV_LOGGER::insert(const std::vector<std::string> &content)
{
if(!fileExists(file_location_))
{
std::cout << __func__ << ": File does not exist." << std::endl;
return;
}

std::ofstream of;
of.open(file_location_.c_str(), std::ofstream::out | std::ofstream::app);

if(content.size() > 0)
{
std::stringstream ss;
for(int i =0; i < content.size(); i++)
{
ss << content[i];
if(i < content.size() - 1)
{
ss << ",";
}
}
ss << "\n";
of << ss.str();
}

of.close();

}

bool CSV_LOGGER::createFile(const std::string &csv_file_location)
{
if(fileExists(csv_file_location))
{
std::cout << __func__ << ": File creation failed: File Exists Already." << std::endl;
return false;
}

std::ofstream of;
of.open(csv_file_location.c_str(), std::ios::out | std::ios::app);
if(!fileExists(csv_file_location))
{
of.close();
std::cout << __func__ << ": File creation failed: Something went wrong : " << csv_file_location << std::endl;
return false;
}

of.close();
return true;
}


bool CSV_LOGGER::createPath(const std::string &path)
{
return boost::filesystem::create_directories(path);
}

}