Skip to content

DevWurm/csv-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csv_library

A simple stream or object based csv parsing and creating library for C++. With the library you can stream an CSV File into an object or you add a CSV line as string into the object and get a deque object with the parsed data. The data can get streamed out of the object or yout get a copy of the data from a member function. The same way you can stream or add a deque with data into an object and get a CSV string.

##Library Because the code is based on templates, the library is directory with C++ header files, that is generated by make. Copy the directory into your project and include the libcsv.h file into your project files.

##How to use

#include "./libcsv/libcsv.h" //include the header
#include <iostream>
#include <fstream>
#include <deque>
#include <string>
int main() {
  csv::csv_handler<int>handler; //create a handler object for int
								 //(from namespace csv)
  std::ifstream input("file.csv"); //open input file
  std::ofstream output("file2.csv"); //open output file
  std::deque<int> data; //deque object to store data (is used like vector)
  std::string buffer;

  //stream version
  input >> handler >> data; //read one line from input and store
					       //parsed data in data
  data >> handler >> output; //write the data from data into on one
							 //line of output

  //possible [but not very useful]: stream one line from input through data
  //into output and add a newline
  input >> handler >> data >> handler >> output << std::endl;

  //member function version
  getline(input, buffer); //read one line from input into buffer
  handler.set_csv_line(buffer); //put CSV string into handler (and parse)
  data = handler.get_parsed_line(); //get parsed data and store in data
  handler.set_parsed_line(data); //put parsed data into handler
								//(and create CSV string)
  buffer =  handler.get_csv_line(); //get CSV String and store into buffer
  output << buffer; //write buffer into file

	return 0;
}

You can use the CSV Handler object as an 'tunnel' between a stream and a variable or another stream, or you can set and get data in and from the Handler over public member functions. You can also mix those two ways (conversion is always done at setting data/streaming data into object).
You can also use seperate objects for input/conversion do deque and output/conversion to string (here use ::set_line(DATA) and DATA ::getline():

csv::csv_parser<type> parser;
input >> parser >> data;
getline(input, buffer);
parser.set_line(buffer);
data = parser.get_line();

csv::csv_creator<type> creator;
data >> creator >> output;
parser.set_line(data);
buffer = parser.get_line();
output << buffer;

The data stored in the csv object are deleted from there after get()ting them or streaming them out of the object. The csv objects work with all instances and derivations of ostream and istream. ##License Copyright 2015 DevWurm
'csv_library' is offered under GPL 3 License (Read ./license/gpl-3.0.txt)

##Documentation Documentation will be offered soon. (Email me if you have any questions)

##Setup Building:
To build the library file change into the top directory of the project and use make with

make

on linux or

mingw32-make
,

on windows (only if mingw is installed). Using:
Include the libcsv.h header file into your source file(s)

#include "libcsv.h"

Then you can use the objects and their members.
All the Element functions are showed in the how to use section.

##Authors DevWurm
Email: devwurm@gmx.net
Feel free to contact me, if you have any questions or ideas about the project :)

##Collaborating You can use the GitHub issue tracker for bugs and feature requests or create a pull request to submit changes and forks are welcome, too. If you don't want to use these possibilities, you can also write me an email at devwurm@gmx.net.

About

A simple stream or object based csv parsing and creating library for C++.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors