void IFCParser::parse() { wstring line = getline(); if (line != FILE_TYPE) THROW_BAD_FORMAT(FILE_TYPE, lineno); parseheader(); parsedata(); line = getline(); if (line != END_FILE_TYPE) THROW_BAD_FORMAT(END_FILE_TYPE, lineno); }
/* * The serial data is assembled on the fly, without using any redundant buffers. * When a sentence is complete (one that starts with $, ending in EOL), all processing is done on * this temporary buffer that we've built: checksum computation, extracting sentence "words" (the CSV values), * and so on. * When a new sentence is fully assembled using the fusedata function, the code calls parsedata. * This function in turn, splits the sentences and interprets the data. Here is part of the parser function, * handling both the $GPRMC NMEA sentence: */ int NMEA::fusedata(char c) { if (c == '$') { m_bFlagRead = true; // init parser vars m_bFlagComputedCks = false; m_nChecksum = 0; // after getting * we start cuttings the received m_nChecksum m_bFlagReceivedCks = false; index_received_checksum = 0; // word cutting variables m_nWordIdx = 0; m_nPrevIdx = 0; m_nNowIdx = 0; } if (m_bFlagRead) { // check ending if (c == '\r' || c== '\n') { // catch last ending item too tmp_words[m_nWordIdx][m_nNowIdx - m_nPrevIdx] = 0; m_nWordIdx++; // cut received m_nChecksum tmp_szChecksum[index_received_checksum] = 0; // sentence complete, read done m_bFlagRead = false; // parse parsedata(); } else { // computed m_nChecksum logic: count all chars between $ and * exclusively if (m_bFlagComputedCks && c == '*') m_bFlagComputedCks = false; if (m_bFlagComputedCks) m_nChecksum ^= c; if (c == '$') m_bFlagComputedCks = true; // received m_nChecksum if (m_bFlagReceivedCks) { tmp_szChecksum[index_received_checksum] = c; index_received_checksum++; } if (c == '*') m_bFlagReceivedCks = true; // build a word tmp_words[m_nWordIdx][m_nNowIdx - m_nPrevIdx] = c; if (c == ',') { tmp_words[m_nWordIdx][m_nNowIdx - m_nPrevIdx] = 0; m_nWordIdx++; m_nPrevIdx = m_nNowIdx; } else m_nNowIdx++; } } return m_nWordIdx; }
bool SpikeData::parse(const char* filename) { std::ifstream in; in.open(filename, std::ios::binary); if (in.good()) { // Calculate the filesize in.seekg(0, std::ios_base::beg); std::ifstream::pos_type begin_pos = in.tellg(); in.seekg(0, std::ios_base::end); int fsize = static_cast<int>(in.tellg()-begin_pos); in.seekg(0, std::ios_base::beg); // Read in the header ID char cID[12]; in.read(reinterpret_cast<char*>(&cID), sizeof(cID)); int VERSION = headerversion(cID); if (VERSION < HEADER_50) { std::cerr << "ERROR: Spike data file is too old (" << filename << ")" << std::endl; return false; } // Read in the header in.seekg(0, std::ios_base::beg); int hsize; if (VERSION == HEADER_50) { in.read(reinterpret_cast<char*>(&m_head50), sizeof(m_head50)); hsize = sizeof(m_head50); upgradeheader(VERSION); } if (VERSION == HEADER_60 || VERSION == HEADER_61 || VERSION == HEADER_62) { in.read(reinterpret_cast<char*>(&m_head), sizeof(m_head)); hsize = sizeof(m_head); upgradeheader(VERSION); } int nbytes = fsize-hsize; if (nbytes < 0) { // std::cerr << "ERROR: Invalid spike data file." << std::endl; return false; } int size = nbytes / sizeof(DWORD); m_dwDataArray.reserve(size); int i = 0; for (i = 0; i < size; ++i) { DWORD dw; in.read(reinterpret_cast<char*>(&dw), sizeof(dw)); m_dwDataArray.push_back(dw); } if (!parsedata()) { // std::cerr << "ERROR: Failed to parse data file." << std::endl; in.close(); return false; } } else { // std::cerr << "ERROR: Unable to open " << filename << std::endl; return false; } in.close(); return true; }