• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    C++
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast, header-only, extensively tested, C++11 CSV parser

CSV Parser

Fast, header-only, C++11 CSV parser.

Usage

Configuration

You initialize the parser by passing it any input stream of characters. For example, you can read from a file

std::ifstream f("some_file.csv");
CsvParser parser(f);

or you can read from stdin

CsvParser parser(std::cin);

Moreover, you can configure the parser by chaining configuration methods like

CsvParser parser = CsvParser(std::cin)
  .delimiter(';')    // delimited by ; instead of ,
  .quote('\'')       // quoted fields use ' instead of "
  .terminator('\0'); // terminated by \0 instead of by \r\n, \n, or \r

Parsing

You can read from the CSV using a range based for loop. Each row of the CSV is represented as a std::vector<std::string>.

#include <iostream>
#include "../parser.hpp"

using namespace aria::csv;

int main() {
  std::ifstream f("some_file.csv");
  CsvParser parser(f);

  for (auto& row : parser) {
    for (auto& field : row) {
      std::cout << field << " | ";
    }
    std::cout << std::endl;
  }
}

Behind the scenes, when using the range based for, the parser only ever allocates as much memory as needed to represent a single row of your CSV. If that's too much, you can step down to a lower level, where you read from the CSV a field at a time, which only allocates the amount of memory needed for a single field.

#include <iostream>
#include "./parser.hpp"

using namespace aria::csv;

int main() {
  CsvParser parser(std::cin);

  for (;;) {
    auto field = parser.next_field();
    switch (field.type) {
      case FieldType::DATA:
        std::cout << *field.data << " | ";
        break;
      case FieldType::ROW_END:
        std::cout << std::endl;
        break;
      case FieldType::CSV_END:
        std::cout << std::endl;
        return 0;
    }
  }
}

It is possible to inspect the current cursor position using parser.position(). This will return the position of the last parsed token. This is useful when reporting things like progress through a file. You can use file.seekg(0, std::ios::end); to get a file size.

More Repositories

1

WebpackTutorial

A simple webpack tutorial
JavaScript
2,232
star
2

mobx-store

A data store with declarative querying, observable state, and easy undo/redo.
JavaScript
281
star
3

aria.ai

My Personal Website: Click it --->>>
TypeScript
19
star
4

ppx_str

PPX for template strings
OCaml
14
star
5

leetcode

Answers to interview questions
C++
8
star
6

ocaml-str-concat-benchmark

Determining which way of concatenating strings is fastest
OCaml
8
star
7

crescendo

🎹
Python
5
star
8

formulagrid

Formula Database
JavaScript
4
star
9

weak-memoize

Self-invalidating memoization for async functions
JavaScript
4
star
10

napjs

CLI Alarm Clock
JavaScript
3
star
11

newtab

Chrome extension that lets you embed any website into your new tab page
CSS
2
star
12

twitter-reactions

Chrome extension that added slack-like reactions to twitter
JavaScript
2
star
13

imgur-bot

A reddit bot that would use the reddit API to find Gyazo links and would reupload them to Imgur using their API.
Python
2
star
14

steam-api-example

Quick example using the steam api
JavaScript
2
star
15

eslint-config-aria-react

React eslint configuration
JavaScript
1
star
16

aoc2023

OCaml
1
star
17

babel-preset-node-flow

JavaScript
1
star
18

smalldatamatters

Let small data dream big
HTML
1
star
19

.emacs.d

Emacs Config
Emacs Lisp
1
star
20

bad-snake

why did I use react? also it's broken anyways
JavaScript
1
star
21

s2r

bad
JavaScript
1
star
22

eslint-config-aria

Personal eslint config
JavaScript
1
star
23

aspect

👀
JavaScript
1
star
24

love-snake

Snake written in Lua with LÖVE 2d engine
Lua
1
star
25

babel-preset-aria

My babel preset
JavaScript
1
star
26

pwrap

Add promise capability to any function.
JavaScript
1
star
27

redditcss

A reddit css theme
CSS
1
star
28

blog

Personal Blog
1
star
29

haskell

Learning Haskell 🏫
Haskell
1
star