//****************************************************************************
// Filename: Csv.h
// Copyright 1999 Daniel X. Pape. All rights reserved.
//
// Description: A parser for a string of comma separated values.
//
// This is based on a similar class described in "The Practice of
// Programming" by Brian W. Kernighan and Rob Pike.
//
// Sample input: "foo",1,1.485,"la la lala","poit"
//
//****************************************************************************
// Revision History:
// Sunday, July 11, 1999 - Original
//****************************************************************************
#ifndef _CSV_H_
#define _CSV_H_
#ifdef _MSC_VER
// Disable stupid MSVC warning about identifiers > 255 chars long
#pragma warning (disable: 4786)
#endif
#include <string>
#include <vector>
class Csv
{
public:
// typedefs
typedef std::vector<std::string> cont_t;
typedef cont_t::value_type value_type;
#ifndef _MSC_VER
typedef cont_t::pointer pointer;
typedef cont_t::const_pointer const_pointer;
#endif
typedef cont_t::iterator iterator;
typedef cont_t::const_iterator const_iterator;
typedef cont_t::reference reference;
typedef cont_t::const_reference const_reference;
typedef cont_t::size_type size_type;
typedef cont_t::difference_type difference_type;
Csv(std::string line, std::string sep = ",");
std::string GetField(size_type n);
size_type GetNField(void) const { return nField_; }
iterator begin() { return fields_.begin(); }
const_iterator begin() const { return fields_.begin(); }
iterator end() { return fields_.end(); }
const_iterator end() const { return fields_.end(); }
size_type size() const { return fields_.size(); }
bool empty() const { return fields_.empty(); }
reference operator[](size_type n) { return fields_[n]; }
const_reference operator[](size_type n) const { return fields_[n]; }
private:
Csv(const Csv &);
Csv & operator=(const Csv &);
cont_t fields_;
size_type nField_;
std::string line_;
std::string fieldSep_;
int split(void);
int advPlain(const std::string& line, std::string& field, size_type);
int advQuoted(const std::string& line, std::string& field, size_type);
};
#endif