Files
linguist/samples/C++/CsvStreamer.h
Rob Hunter 4adbbc3fcc Add a few samples of misclassified C++ headers
Several people mentioned in #467 that they were seeing their C++ projects
erroneously showing up as containing Objective C.

I've added a file from each of the problematic repositories:

  - https://github.com/mp13on11/dwarf_mine
  - https://github.com/miguelishawt/anax
  - https://github.com/mholt/cppcsv
  - https://github.com/coder543/libcanister

They all seem to be triggering on different aspects, since adding one sample
wasn't sufficient to correctly classify the others.

The discussion in #467 makes me think that perhaps Linuist might need to take
the rest of the repository into account when classifying ambiguous files.
2013-12-16 14:38:56 -08:00

43 lines
2.1 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <fstream>
#include "util.h"
using namespace std;
#define DEFAULT_DELIMITER ','
class CsvStreamer
{
private:
ofstream file; // File output stream
vector<string> row_buffer; // Buffer which stores a row's data before being flushed/written
int fields; // Number of fields (columns)
long rows; // Number of rows (records) including header row
char delimiter; // Delimiter character; comma by default
string sanitize(string); // Returns a string ready for output into the file
public:
CsvStreamer(); // Empty CSV streamer... be sure to open the file before writing!
CsvStreamer(string, char); // Same as open(string, char)...
CsvStreamer(string); // Opens an output CSV file given a file path/name
~CsvStreamer(); // Ensures the output file is closed and saved
void open(string); // Opens an output CSV file given a file path/name (default delimiter)
void open(string, char); // Opens an output CSV file given a file path/name and a delimiting character (default comma)
void add_field(string); // If still on first line, adds a new field to the header row
void save_fields(); // Call this to save the header row; all new writes should be through append()
void append(string); // Appends the current row with this data for the next field; quoted only if needed (leading/trailing spaces are trimmed)
void append(string, bool); // Like append(string) but can specify whether to trim spaces at either end of the data (false to keep spaces)
void append(float); // Appends the current row with this number
void append(double); // Appends the current row with this number
void append(long); // Appends the current row with this number
void append(int); // Appends the current row with this number
void writeln(); // Flushes what was in the row buffer into the file (writes the row)
void close(); // Saves and closes the file
int field_count(); // Gets the number of fields (columns)
long row_count(); // Gets the number of records (rows) -- NOT including the header row
};