Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean stringstream at constructor #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CSVWriter
Simple c++ csv writer class.
# :memo: CSVWriter

<img src="./images/PT%20hero%20image.png"/>

[:computer: Downloading & Release notes](https://github.com/jojo58fr/CSVWriter/releases) | [:pushpin: Documentation](https://github.com/jojo58fr/CSVWriter/wiki) | [:warning: Known issues](#known-issues) | [:bug: Report bugs](https://github.com/jojo58fr/CSVWriter/issues)

> Simple c++ csv writer class.
## How to use ?

just c&p or add the CSVWriter-h file to your project.
Expand Down
Binary file added images/PT hero image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
316 changes: 180 additions & 136 deletions include/CSVWriter.h
Original file line number Diff line number Diff line change
@@ -1,144 +1,188 @@
#ifndef CSVWRITER_H
#define CSVWRITER_H
#include <fstream>
#include <iostream>
#include <sstream>
#include <typeinfo>


/***********************************************************************************************
*
* Classe qui permet de gérer l'écriture sous le format CSV
* Licence : BSD 2-Clause "Simplified" License
* Url : https://github.com/jojo58fr/CSVWriter
*
*********************************************************************************************/

#include <fstream>
#include <iostream>
#include <sstream>
#include <typeinfo>

using namespace std;
class CSVWriter
{
public:
CSVWriter(){
this->firstRow = true;
this->seperator = ";";
this->columnNum = -1;
this->valueCount = 0;
}

CSVWriter(int numberOfColums){
this->firstRow = true;
this->seperator = ";";
this->columnNum = numberOfColums;
this->valueCount = 0;
}

CSVWriter(string seperator){
this->firstRow = true;
this->seperator = seperator;
this->columnNum = -1;
this->valueCount = 0;
}

CSVWriter(string seperator, int numberOfColums){
this->firstRow = true;
this->seperator = seperator;
this->columnNum = numberOfColums;
this->valueCount = 0;
cout << this->seperator << endl;
}

CSVWriter& add(const char *str){
return this->add(string(str));
}

CSVWriter& add(char *str){
return this->add(string(str));
}

CSVWriter& add(string str){
//if " character was found, escape it
size_t position = str.find("\"",0);
bool foundQuotationMarks = position != string::npos;
while(position != string::npos){
str.insert(position,"\"");
position = str.find("\"",position + 2);
}
if(foundQuotationMarks){
str = "\"" + str + "\"";
}else if(str.find(this->seperator) != string::npos){
//if seperator was found and string was not escapted before, surround string with "
str = "\"" + str + "\"";
}
return this->add<string>(str);
}

template<typename T>
CSVWriter& add(T str){
if(this->columnNum > -1){
//if autoNewRow is enabled, check if we need a line break
if(this->valueCount == this->columnNum ){
this->newRow();
}
}
if(valueCount > 0)
this->ss << this->seperator;
this->ss << str;
this->valueCount++;

return *this;
}

template<typename T>
CSVWriter& operator<<(const T& t){
return this->add(t);
}

void operator+=(CSVWriter &csv){
this->ss << endl << csv;
}

string toString(){
return ss.str();
}

friend ostream& operator<<(std::ostream& os, CSVWriter & csv){
return os << csv.toString();
}

CSVWriter& newRow(){
if(!this->firstRow || this->columnNum > -1){
ss << endl;
}else{
//if the row is the first row, do not insert a new line
this->firstRow = false;
}
valueCount = 0;
return *this;
}

bool writeToFile(string filename){
return writeToFile(filename,false);
}

bool writeToFile(string filename, bool append){
ofstream file;
if(append)
file.open(filename.c_str(),ios::out | ios::app);
else
file.open(filename.c_str(),ios::out | ios::trunc);
if(!file.is_open())
return false;
if(append)
file << endl;
file << this->toString();
file.close();
return file.good();
}

void enableAutoNewRow(int numberOfColumns){
this->columnNum = numberOfColumns;
}

void disableAutoNewRow(){
this->columnNum = -1;
}
protected:
bool firstRow;
string seperator;
int columnNum;
int valueCount;
stringstream ss;
public:

/*
* Déclarations des différents constructeurs
*/
CSVWriter(){
this->firstRow = true;
this->seperator = ";";
this->columnNum = -1;
this->valueCount = 0;

//Clear Stream
ss.str(std::string());
}

CSVWriter(int numberOfColums){
this->firstRow = true;
this->seperator = ";";
this->columnNum = numberOfColums;
this->valueCount = 0;

//Clear Stream
ss.str(std::string());
}

CSVWriter(string seperator){
this->firstRow = true;
this->seperator = seperator;
this->columnNum = -1;
this->valueCount = 0;

//Clear Stream
ss.str(std::string());
}

CSVWriter(string seperator, int numberOfColums){
this->firstRow = true;
this->seperator = seperator;
this->columnNum = numberOfColums;
this->valueCount = 0;
cout << this->seperator << endl;

//Clear Stream
ss.str(std::string());
}

/*
* Ajout de texte par l'intermédiaire d'une fonction
*/
CSVWriter& add(const char *str){
return this->add(string(str));
}

CSVWriter& add(char *str){
return this->add(string(str));
}

CSVWriter& add(string str){
//if " character was found, escape it
size_t position = str.find("\"",0);
bool foundQuotationMarks = position != string::npos;
while(position != string::npos){
str.insert(position,"\"");
position = str.find("\"",position + 2);
}
if(foundQuotationMarks){
str = "\"" + str + "\"";
}else if(str.find(this->seperator) != string::npos){
//if seperator was found and string was not escapted before, surround string with "
str = "\"" + str + "\"";
}
return this->add<string>(str);
}

template<typename T>
CSVWriter& add(T str){
if(this->columnNum > -1){
//if autoNewRow is enabled, check if we need a line break
if(this->valueCount == this->columnNum ){
this->newRow();
}
}
if(valueCount > 0)
this->ss << this->seperator;
this->ss << str;
this->valueCount++;

return *this;
}

template<typename T>
CSVWriter& operator<<(const T& t){
return this->add(t);
}

/*
* Ajout de texte par l'intermédiaire d'opérateurs
*/
void operator+=(CSVWriter &csv){
this->ss << endl << csv;
}

friend ostream& operator<<(std::ostream& os, CSVWriter & csv){
return os << csv.toString();
}

/*
* Retour du format CSV sous forme de chaine de caractère
*/
string toString(){
return ss.str();
}

/*
* Préparation d'une nouvelle ligne
*/
CSVWriter& newRow(){
if(!this->firstRow || this->columnNum > -1){
ss << endl;
}else{
//if the row is the first row, do not insert a new line
this->firstRow = false;
}
valueCount = 0;
return *this;
}

/*
* Gestion de l'écriture dans un document
*/
bool writeToFile(string filename){
return writeToFile(filename,false);
}

bool writeToFile(string filename, bool append){
ofstream file;
if(append)
file.open(filename.c_str(),ios::out | ios::app);
else
file.open(filename.c_str(),ios::out | ios::trunc);
if(!file.is_open())
return false;
if(append)
file << endl;
file << this->toString();
file.close();
return file.good();
}

/*
* Gestion d'uns système de colonnes automatique par rapport à un nombre prédéfini
*/
void enableAutoNewRow(int numberOfColumns){
this->columnNum = numberOfColumns;
}

// Désactivation du système de colonnes automatique
void disableAutoNewRow(){
this->columnNum = -1;
}
protected:
bool firstRow; //Si la première ligne
string seperator; //Gestion du séparateur
int columnNum; //Numéro de colomne
int valueCount; //Nombre de valeurs renseigné CAD nombre de colomnes renseigné actuel
stringstream ss; //Flux d'écriture

};

Expand Down