CSV APIs

The csv.hpp header provides a convenience include / wrapper around `p-ranav/csv2 <https://github.com/p-pranav/csv2`_. It also exposes csv2’s include folder, so including csv.hpp is completely equivalent to including both csv2/reader.hpp and csv2/writer.hpp. Please see the documentation for csv2 if you have any questions about usage beyond the examples provided here.

API Reference

Header File

Macros

__gnu_linux__

Classes

class __csv_documentation__

Comma Separated Value (CSV) reader/writer convenience wrapper around p-ranav/csv2 which exposes csv2::Reader and csv2::Writer classes for managing efficient (lazy-loaded) parsing and serizaliztion of human-readable CSV-formatted data.

CSV Reader Example

    std::string csv_data =
        "filename, boxart filename, display name\n"
        "mario.nes, boxart/mario.jpg, Mario Bros.\n"
        "super_mario_1.nes, boxart/super_mario_bros_1.jpg, Super Mario Bros.\n"
        "super_mario_3.nes, boxart/super_mario_bros_3.jpg, Super Mario Bros. 3\n"
        "zelda.nes, boxart/zelda1.jpg, The Legend of Zelda\n"
        "zelda_2.nes, boxart/zelda2.jpg, The Legend of Zelda 2: the Adventure of Link\n"
        "mega_man.nes, boxart/megaman1.jpg, MegaMan\n"
        "metroid.nes, boxart/metroid1.jpg, Metroid\n"
        "pokemon_blue.gb, boxart/pokemon_blue.jpg, Pokemon Blue\n"
        "pokemon_red.gb, boxart/pokemon_red.jpg, Pokemon Red\n"
        "pokemon_yellow.gbc, boxart/pokemon_yellow.jpg, Pokemon Yellow\n"
        "links_awakening.gb, boxart/tloz_links_awakening.jpg, The Legend of Zelda: Link's "
        "Awakening\n"
        "links_awakening.gbc, boxart/tloz_links_awakening_dx.jpg, The Legend of Zelda: Link's "
        "Awakening DX";

    // create the csv reader object
    csv2::Reader<csv2::delimiter<','>, csv2::quote_character<'"'>, csv2::first_row_is_header<true>,
                 csv2::trim_policy::trim_whitespace>
        csv;
    if (csv.parse(csv_data)) {
      // print the header
      const auto header = csv.header();
      fmt::print("Header:\n\t");
      for (const auto cell : header) {
        std::string value;
        cell.read_value(value);
        fmt::print("'{}', ", value);
      }
      fmt::print("\n");
      // now print the items
      size_t row_index = 0;
      for (const auto row : csv) {
        fmt::print("Row {} values:\n\t", row_index);
        for (const auto cell : row) {
          std::string value;
          cell.read_value(value);
          fmt::print("'{}', ", value);
        }
        fmt::print("\n");
        row_index++;
      }
    }

Complex CSV Writer Example

    std::ostringstream stream("");
    csv2::Writer<csv2::delimiter<','>, std::ostringstream> writer(stream);

    std::vector<std::vector<std::string>> rows = {
        {"a", "b", "c"}, {"1", "2", "3"}, {"4", "5", "6"}};

    writer.write_rows(rows);
    fmt::print("Wrote:\n'{}'\n", stream.str());

Note

This class does not really exist or do anything, but it’s the only way I could figure out how to get this documentation built into the system :(