Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_CSVReader/AP_CSVReader.cpp
Views: 1798
#include "AP_CSVReader.h"12#include <AP_Common/AP_Common.h>34#include <stdio.h>56AP_CSVReader::RetCode AP_CSVReader::handle_unquoted_term(uint8_t c)7{8if (c == separator) {9set_state(State::START_OF_START_OF_TERM);10return RetCode::TERM_DONE;11}12switch (c) {13case '\r':14set_state(State::END_OF_VECTOR_CR);15return RetCode::VECTOR_DONE;16case '\n':17set_state(State::START_OF_START_OF_TERM);18return RetCode::VECTOR_DONE;19default:20if (term_ofs >= term_len-1) { // -1 for null termination21return RetCode::ERROR;22}23term[term_ofs++] = c;24term[term_ofs] = '\0';25return RetCode::OK;26}27}2829AP_CSVReader::RetCode AP_CSVReader::handle_quoted_term(uint8_t c)30{31if (c == '"') {32set_state(State::END_OF_QUOTED_TERM);33return RetCode::OK;34}35if (state == State::END_OF_QUOTED_TERM) {36if (c == separator) {37set_state(State::START_OF_START_OF_TERM);38return RetCode::TERM_DONE;39}4041switch (c) {42case '\r':43set_state(State::END_OF_VECTOR_CR);44return RetCode::VECTOR_DONE;45case '\n':46set_state(State::START_OF_START_OF_TERM);47return RetCode::VECTOR_DONE;48}49return RetCode::ERROR;50}5152// still within the quoted term, append to current value53if (term_ofs >= term_len-1) { // -1 for null termination54return RetCode::ERROR;55}56term[term_ofs++] = c;57term[term_ofs] = '\0';58return RetCode::OK;59}6061AP_CSVReader::RetCode AP_CSVReader::feed(uint8_t c)62{63if (term_len == 0) {64return RetCode::ERROR;65}6667again:68switch (state) {69case State::START_OF_START_OF_TERM:70term_ofs = 0;71term[term_ofs] = '\0';72state = State::START_OF_TERM;73FALLTHROUGH;74case State::START_OF_TERM:75// if (c == '"') {76// set_state(State::START_OF_QUOTED_TERM);77// return RetCode::OK;78// }79if (c == '"') {80set_state(State::IN_QUOTED_TERM);81return RetCode::OK;82} else {83set_state(State::IN_UNQUOTED_TERM);84return handle_unquoted_term(c);85}86case State::END_OF_VECTOR_CR:87if (c == '\n') {88set_state(State::START_OF_START_OF_TERM);89return RetCode::OK;90}91set_state(State::START_OF_START_OF_TERM);92goto again;93case State::IN_UNQUOTED_TERM:94return handle_unquoted_term(c);95case State::IN_QUOTED_TERM:96case State::END_OF_QUOTED_TERM:97return handle_quoted_term(c);98}99100return RetCode::ERROR;101}102103104