Skip to content

Add ability to parse headers easier #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,12 @@ namespace io{
void parse_header_line(
char*line,
std::vector<int>&col_order,
std::vector<std::string>&col_names,
const std::string*col_name,
ignore_column ignore_policy
){
col_order.clear();
col_names.clear();

bool found[column_count];
std::fill(found, found + column_count, false);
Expand All @@ -903,6 +905,7 @@ namespace io{

trim_policy::trim(col_begin, col_end);
quote_policy::unescape(col_begin, col_end);
col_names.push_back(std::string(col_begin));

for(unsigned i=0; i<column_count; ++i)
if(col_begin == col_name[i]){
Expand Down Expand Up @@ -1120,6 +1123,7 @@ namespace io{
std::string column_names[column_count];

std::vector<int>col_order;
std::vector<std::string>col_names;

template<class ...ColNames>
void set_column_names(std::string s, ColNames...cols){
Expand Down Expand Up @@ -1165,7 +1169,7 @@ namespace io{

detail::parse_header_line
<column_count, trim_policy, quote_policy>
(line, col_order, column_names, ignore_policy);
(line, col_order, col_names, column_names, ignore_policy);
}catch(error::with_file_name&err){
err.set_file_name(in.get_truncated_file_name());
throw;
Expand All @@ -1185,6 +1189,23 @@ namespace io{
col_order[i] = i;
}

void set_header_from_self(){
const size_t size = col_names.size();
size_t i = 0;
for (; i < size; i++) {
column_names[i] = col_names[i];
col_order[i] = i;
}

size_t order_size = col_order.size();
for (; i < order_size; i++) {
//column_names[i] = col_names[i];
col_order[i] = -1;
}
}

const std::vector<std::string> &get_col_names() const { return col_names; }

bool has_column(const std::string&name) const {
return col_order.end() != std::find(
col_order.begin(), col_order.end(),
Expand Down