Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<std::string> getNextLineAndSplitIntoTokens(std::istream& str)
- {
- std::vector<std::string> result;
- std::string line;
- std::getline(str,line);
- std::stringstream lineStream(line);
- std::string cell;
- while(std::getline(lineStream,cell,','))
- {
- result.push_back(cell);
- }
- return result;
- }
- #include <iterator>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <string>
- class CSVRow
- {
- public:
- std::string const& operator[](std::size_t index) const
- {
- return m_data[index];
- }
- std::size_t size() const
- {
- return m_data.size();
- }
- void readNextRow(std::istream& str)
- {
- std::string line;
- std::getline(str,line);
- std::stringstream lineStream(line);
- std::string cell;
- m_data.clear();
- while(std::getline(lineStream,cell,','))
- {
- m_data.push_back(cell);
- }
- }
- private:
- std::vector<std::string> m_data;
- };
- std::istream& operator>>(std::istream& str,CSVRow& data)
- {
- data.readNextRow(str);
- return str;
- }
- int main()
- {
- std::ifstream file("plop.csv");
- CSVRow row;
- while(file >> row)
- {
- std::cout << "4th Element(" << row[3] << ")n";
- }
- }
- class CSVIterator
- {
- public:
- typedef std::input_iterator_tag iterator_category;
- typedef CSVRow value_type;
- typedef std::size_t difference_type;
- typedef CSVRow* pointer;
- typedef CSVRow& reference;
- CSVIterator(std::istream& str) :m_str(str.good()?&str:NULL) { ++(*this); }
- CSVIterator() :m_str(NULL) {}
- // Pre Increment
- CSVIterator& operator++() {if (m_str) { (*m_str) >> m_row;m_str = m_str->good()?m_str:NULL;}return *this;}
- // Post increment
- CSVIterator operator++(int) {CSVIterator tmp(*this);++(*this);return tmp;}
- CSVRow const& operator*() const {return m_row;}
- CSVRow const* operator->() const {return &m_row;}
- bool operator==(CSVIterator const& rhs) {return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));}
- bool operator!=(CSVIterator const& rhs) {return !((*this) == rhs);}
- private:
- std::istream* m_str;
- CSVRow m_row;
- };
- int main()
- {
- std::ifstream file("plop.csv");
- for(CSVIterator loop(file);loop != CSVIterator();++loop)
- {
- std::cout << "4th Element(" << (*loop)[3] << ")n";
- }
- }
- std::vector<std::string> vec;
- using namespace boost;
- tokenizer<escaped_list_separator<char> > tk(
- line, escaped_list_separator<char>('\', ',', '"'));
- for (tokenizer<escaped_list_separator<char> >::iterator i(tk.begin());
- i!=tk.end();++i)
- {
- vec.push_back(*i);
- }
- void foo()
- {
- std::string data = "1,2,3,4,5n"
- "0,2,4,6,8n"
- "1,3,5,7,9n";
- strtk::token_grid grid(data,data.size(),",");
- for(std::size_t i = 0; i < grid.row_count(); ++i)
- {
- strtk::token_grid::row_type r = grid.row(i);
- for(std::size_t j = 0; j < r.size(); ++j)
- {
- std::cout << r.get<int>(j) << "t";
- }
- std::cout << std::endl;
- }
- std::cout << std::endl;
- }
- bool r = phrase_parse(first, last,
- // Begin grammar
- (
- double_ % ','
- )
- ,
- // End grammar
- space, v);
- #include <iostream> // cout, endl
- #include <fstream> // fstream
- #include <vector>
- #include <string>
- #include <algorithm> // copy
- #include <iterator> // ostream_operator
- #include <boost/tokenizer.hpp>
- int main()
- {
- using namespace std;
- using namespace boost;
- string data("data.csv");
- ifstream in(data.c_str());
- if (!in.is_open()) return 1;
- typedef tokenizer< escaped_list_separator<char> > Tokenizer;
- vector< string > vec;
- string line;
- while (getline(in,line))
- {
- Tokenizer tok(line);
- vec.assign(tok.begin(),tok.end());
- // vector now contains strings from one row, output to cout here
- copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, "|"));
- cout << "n----------------------" << endl;
- }
- }
- void ParseCSV(const string& csvSource, vector<vector<string> >& lines)
- {
- bool inQuote(false);
- bool newLine(false);
- string field;
- lines.clear();
- vector<string> line;
- string::const_iterator aChar = csvSource.begin();
- while (aChar != csvSource.end())
- {
- switch (*aChar)
- {
- case '"':
- newLine = false;
- inQuote = !inQuote;
- break;
- case ',':
- newLine = false;
- if (inQuote == true)
- {
- field += *aChar;
- }
- else
- {
- line.push_back(field);
- field.clear();
- }
- break;
- case 'n':
- case 'r':
- if (inQuote == true)
- {
- field += *aChar;
- }
- else
- {
- if (newLine == false)
- {
- line.push_back(field);
- lines.push_back(line);
- field.clear();
- line.clear();
- newLine = true;
- }
- }
- break;
- default:
- newLine = false;
- field.push_back(*aChar);
- break;
- }
- aChar++;
- }
- if (field.size())
- line.push_back(field);
- if (line.size())
- lines.push_back(line);
- }
- const char input[] =
- "Year,Make,Model,Description,Pricen"
- "1997,Ford,E350,"ac, abs, moon",3000.00n"
- "1999,Chevy,"Venture ""Extended Edition""","",4900.00n"
- "1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00n"
- "1996,Jeep,Grand Cherokee,"MUST SELL!n
- air, moon roof, loaded",4799.00n"
- ;
- std::istringstream ss(input);
- std::string title[5];
- int year;
- std::string make, model, desc;
- float price;
- csv_istream(ss)
- >> title[0] >> title[1] >> title[2] >> title[3] >> title[4];
- while (csv_istream(ss)
- >> year >> make >> model >> desc >> price) {
- //...do something with the record...
- }
- struct csv_istream {
- std::istream &is_;
- csv_istream (std::istream &is) : is_(is) {}
- void scan_ws () const {
- while (is_.good()) {
- int c = is_.peek();
- if (c != ' ' && c != 't') break;
- is_.get();
- }
- }
- void scan (std::string *s = 0) const {
- std::string ws;
- int c = is_.get();
- if (is_.good()) {
- do {
- if (c == ',' || c == 'n') break;
- if (s) {
- ws += c;
- if (c != ' ' && c != 't') {
- *s += ws;
- ws.clear();
- }
- }
- c = is_.get();
- } while (is_.good());
- if (is_.eof()) is_.clear();
- }
- }
- template <typename T, bool> struct set_value {
- void operator () (std::string in, T &v) const {
- std::istringstream(in) >> v;
- }
- };
- template <typename T> struct set_value<T, true> {
- template <bool SIGNED> void convert (std::string in, T &v) const {
- if (SIGNED) v = ::strtoll(in.c_str(), 0, 0);
- else v = ::strtoull(in.c_str(), 0, 0);
- }
- void operator () (std::string in, T &v) const {
- convert<is_signed_int<T>::val>(in, v);
- }
- };
- template <typename T> const csv_istream & operator >> (T &v) const {
- std::string tmp;
- scan(&tmp);
- set_value<T, is_int<T>::val>()(tmp, v);
- return *this;
- }
- const csv_istream & operator >> (std::string &v) const {
- v.clear();
- scan_ws();
- if (is_.peek() != '"') scan(&v);
- else {
- std::string tmp;
- is_.get();
- std::getline(is_, tmp, '"');
- while (is_.peek() == '"') {
- v += tmp;
- v += is_.get();
- std::getline(is_, tmp, '"');
- }
- v += tmp;
- scan();
- }
- return *this;
- }
- template <typename T>
- const csv_istream & operator >> (T &(*manip)(T &)) const {
- is_ >> manip;
- return *this;
- }
- operator bool () const { return !is_.fail(); }
- };
- template <typename T> struct is_signed_int { enum { val = false }; };
- template <> struct is_signed_int<short> { enum { val = true}; };
- template <> struct is_signed_int<int> { enum { val = true}; };
- template <> struct is_signed_int<long> { enum { val = true}; };
- template <> struct is_signed_int<long long> { enum { val = true}; };
- template <typename T> struct is_unsigned_int { enum { val = false }; };
- template <> struct is_unsigned_int<unsigned short> { enum { val = true}; };
- template <> struct is_unsigned_int<unsigned int> { enum { val = true}; };
- template <> struct is_unsigned_int<unsigned long> { enum { val = true}; };
- template <> struct is_unsigned_int<unsigned long long> { enum { val = true}; };
- template <typename T> struct is_int {
- enum { val = (is_signed_int<T>::val || is_unsigned_int<T>::val) };
- };
- void loadFromCSV( const std::string& filename )
- {
- std::ifstream file( filename.c_str() );
- std::vector< std::vector<std::string> > matrix;
- std::vector<std::string> row;
- std::string line;
- std::string cell;
- while( file )
- {
- std::getline(file,line);
- std::stringstream lineStream(line);
- row.clear();
- while( std::getline( lineStream, cell, ',' ) )
- row.push_back( cell );
- if( !row.empty() )
- matrix.push_back( row );
- }
- for( int i=0; i<int(matrix.size()); i++ )
- {
- for( int j=0; j<int(matrix[i].size()); j++ )
- std::cout << matrix[i][j] << " ";
- std::cout << std::endl;
- }
- }
- /**
- Read line from a CSV file
- @param[in] fp file pointer to open file
- @param[in] vls reference to vector of strings to hold next line
- */
- void readCSV( FILE *fp, std::vector<std::string>& vls )
- {
- vls.clear();
- if( ! fp )
- return;
- char buf[10000];
- if( ! fgets( buf,999,fp) )
- return;
- std::string s = buf;
- int p,q;
- q = -1;
- // loop over columns
- while( 1 ) {
- p = q;
- q = s.find_first_of(",n",p+1);
- if( q == -1 )
- break;
- vls.push_back( s.substr(p+1,q-p-1) );
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- std::vector<std::string> vls;
- FILE * fp = fopen( argv[1], "r" );
- if( ! fp )
- return 1;
- readCSV( fp, vls );
- readCSV( fp, vls );
- readCSV( fp, vls );
- std::cout << "row 3, col 4 is " << vls[3].c_str() << "n";
- return 0;
- }
- #include "csv.h"
- int main(){
- io::CSVReader<3> in("ram.csv");
- in.read_header(io::ignore_extra_column, "vendor", "size", "speed");
- std::string vendor; int size; double speed;
- while(in.read_row(vendor, size, speed)){
- // do stuff with the data
- }
- }
- #include <string>
- #include <sstream>
- #include <fstream>
- void StringReplace(std::string& str, const std::string& oldStr, const std::string& newStr)
- // code by Yves Baumes
- // http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string
- {
- size_t pos = 0;
- while((pos = str.find(oldStr, pos)) != std::string::npos)
- {
- str.replace(pos, oldStr.length(), newStr);
- pos += newStr.length();
- }
- }
- void LoadCSV(std::string &filename) {
- std::ifstream stream(filename);
- std::string in_line;
- std::string Field;
- std::string Chan;
- int ChanType;
- double Scale;
- int Import;
- while (std::getline(stream, in_line)) {
- StringReplace(in_line, ",", " ");
- std::stringstream line(in_line);
- line >> Field >> Chan >> ChanType >> Scale >> Import;
- if (Field.substr(0,2)!="//") {
- // do your stuff
- // this is CBuilder code for demonstration, sorry
- ShowMessage((String)Field.c_str() + "n" + Chan.c_str() + "n" + IntToStr(ChanType) + "n" +FloatToStr(Scale) + "n" +IntToStr(Import));
- }
- }
- }
- using namespace std;
- // trim whitespaces around field or double-quotes, remove double-quotes and replace escaped double-quotes (double double-quotes)
- wstring trimquote(const wstring& str, const wstring& whitespace, const wchar_t quotChar)
- {
- wstring ws;
- wstring::size_type strBegin = str.find_first_not_of(whitespace);
- if (strBegin == wstring::npos)
- return L"";
- wstring::size_type strEnd = str.find_last_not_of(whitespace);
- wstring::size_type strRange = strEnd - strBegin + 1;
- if((str[strBegin] == quotChar) && (str[strEnd] == quotChar))
- {
- ws = str.substr(strBegin+1, strRange-2);
- strBegin = 0;
- while((strEnd = ws.find(quotChar, strBegin)) != wstring::npos)
- {
- ws.erase(strEnd, 1);
- strBegin = strEnd+1;
- }
- }
- else
- ws = str.substr(strBegin, strRange);
- return ws;
- }
- pair<unsigned, unsigned> nextCSVQuotePair(const wstring& line, const wchar_t quotChar, unsigned ofs = 0)
- {
- pair<unsigned, unsigned> r;
- r.first = line.find(quotChar, ofs);
- r.second = wstring::npos;
- if(r.first != wstring::npos)
- {
- r.second = r.first;
- while(((r.second = line.find(quotChar, r.second+1)) != wstring::npos)
- && (line[r.second+1] == quotChar)) // WARNING: assumes null-terminated string such that line[r.second+1] always exist
- r.second++;
- }
- return r;
- }
- unsigned parseLine(vector<wstring>& fields, const wstring& line)
- {
- unsigned ofs, ofs0, np;
- const wchar_t delim = L',';
- const wstring whitespace = L" txa0x3000x2000x2001x2002x2003x2004x2005x2006x2007x2008x2009x200ax202fx205f";
- const wchar_t quotChar = L'"';
- pair<unsigned, unsigned> quot;
- fields.clear();
- ofs = ofs0 = 0;
- quot = nextCSVQuotePair(line, quotChar);
- while((np = line.find(delim, ofs)) != wstring::npos)
- {
- if((np > quot.first) && (np < quot.second))
- { // skip delimiter inside quoted field
- ofs = quot.second+1;
- quot = nextCSVQuotePair(line, quotChar, ofs);
- continue;
- }
- fields.push_back( trimquote(line.substr(ofs0, np-ofs0), whitespace, quotChar) );
- ofs = ofs0 = np+1;
- }
- fields.push_back( trimquote(line.substr(ofs0), whitespace, quotChar) );
- return fields.size();
- }
- #include <stdbool.h>
- #include <wchar.h>
- #include <wctype.h>
- extern const wchar_t *nextCsvField(const wchar_t *p, wchar_t sep, bool *newline);
- // Returns a pointer to the start of the next field,
- // or zero if this is the last field in the CSV
- // p is the start position of the field
- // sep is the separator used, i.e. comma or semicolon
- // newline says whether the field ends with a newline or with a comma
- const wchar_t *nextCsvField(const wchar_t *p, wchar_t sep, bool *newline)
- {
- // Parse quoted sequences
- if ('"' == p[0]) {
- p++;
- while (1) {
- // Find next double-quote
- p = wcschr(p, L'"');
- // If we don't find it or it's the last symbol
- // then this is the last field
- if (!p || !p[1])
- return 0;
- // Check for "", it is an escaped double-quote
- if (p[1] != '"')
- break;
- // Skip the escaped double-quote
- p += 2;
- }
- }
- // Find next newline or comma.
- wchar_t newline_or_sep[4] = L"nr ";
- newline_or_sep[2] = sep;
- p = wcspbrk(p, newline_or_sep);
- // If no newline or separator, this is the last field.
- if (!p)
- return 0;
- // Check if we had newline.
- *newline = (p[0] == 'r' || p[0] == 'n');
- // Handle "rn", otherwise just increment
- if (p[0] == 'r' && p[1] == 'n')
- p += 2;
- else
- p++;
- return p;
- }
- static wchar_t *csvFieldData(const wchar_t *fld_s, const wchar_t *fld_e, wchar_t *buffer, size_t buflen)
- {
- wchar_t *dst = buffer;
- wchar_t *end = buffer + buflen - 1;
- const wchar_t *src = fld_s;
- if (*src == L'"')
- {
- const wchar_t *p = src + 1;
- while (p < fld_e && dst < end)
- {
- if (p[0] == L'"' && p+1 < fld_s && p[1] == L'"')
- {
- *dst++ = p[0];
- p += 2;
- }
- else if (p[0] == L'"')
- {
- p++;
- break;
- }
- else
- *dst++ = *p++;
- }
- src = p;
- }
- while (src < fld_e && dst < end)
- *dst++ = *src++;
- if (dst >= end)
- return 0;
- *dst = L'