////////////////////////////////////////////////////////////////////////////////////////// // Read data from the file until the terminator and put it into the data buffer. XsensResultValue Cmt1f::readData (const uint32_t maxLength, const char terminator, void* dataV, uint32_t* length) { if (!m_isOpen) return m_lastResult = XRV_NOFILEOPEN; uint32_t len; if (length == NULL) length = &len; char* data = (char*) dataV; int32_t readChar; gotoRead(); *length = 0; readChar = (uint32_t) fgetc(m_handle); while (!feof(m_handle) && !ferror(m_handle)) { data[*length] = (char) readChar; ++(*length); ++m_readPos; if (((char) readChar == terminator) || ((*length) >= maxLength)) return m_lastResult = XRV_OK; } return m_lastResult = XRV_ENDOFFILE; }
/*! \brief Find a string of bytes in the file \details The function searches from the current read position until the given \c needle is found. If the needle is not found, XsResultValue::NOT_FOUND is returned. The function will update the seek position to the first character of the found needle. \param needleV The byte string to find. \param pos The position where \a needleV was found. This will point to the first character of the found \a needleV. \returns XRV_OK if the data was found, XRV_ENDOFFILE if it wasn't found */ XsResultValue IoInterfaceFile::find(const XsByteArray& needleV, XsFilePos& pos) { if (!m_handle) return m_lastResult = XRV_NOFILEOPEN; XsSize needleLength = needleV.size(); pos = 0; if (needleLength == 0) return m_lastResult = XRV_OK; const char* needle = (const char*) needleV.data(); gotoRead(); char buffer[512]; uint32_t bufferPos, needlePos = 0; size_t readBytes; if (m_readPos & 0x1FF) // read a block of data readBytes = fread(buffer, 1, (512-((size_t) m_readPos & 0x1FF)), m_handle); else readBytes = fread(buffer, 1, 512, m_handle); // read a block of data while (readBytes > 0) { m_readPos += readBytes; bufferPos = 0; while (bufferPos < readBytes && needlePos < needleLength) { if (buffer[bufferPos] == needle[needlePos]) { // found a byte ++needlePos; } else { if (needlePos > 0) needlePos = 0; else if (buffer[bufferPos] == needle[0]) { // found a byte needlePos = 1; } } ++bufferPos; } if (needlePos < needleLength) readBytes = fread(buffer, 1, 512, m_handle); // read next block else { m_readPos = m_readPos + bufferPos - readBytes - needleLength; // or without needleLength pos = m_readPos; // - needleLength; FSEEK(m_readPos); return m_lastResult = XRV_OK; } } return m_lastResult = XRV_ENDOFFILE; }
////////////////////////////////////////////////////////////////////////////////////////// // Find a string of bytes in the file XsensResultValue Cmt1f::find( const void* needleV, const uint32_t needleLength, CmtFilePos& pos) { if (!m_isOpen) return m_lastResult = XRV_NOFILEOPEN; const char* needle = (const char*)needleV; gotoRead(); pos = 0; char buffer[512]; uint32_t bufferPos, needlePos = 0; size_t readBytes; if (m_readPos & 0x1FF) // read a block of data readBytes = fread(buffer, 1, (512 - ((size_t)m_readPos & 0x1FF)), m_handle); else readBytes = fread(buffer, 1, 512, m_handle); // read a block of data while (readBytes > 0) { m_readPos += readBytes; bufferPos = 0; while (bufferPos < readBytes && needlePos < needleLength) { if (buffer[bufferPos] == needle[needlePos]) { // found a byte ++needlePos; } else { if (needlePos > 0) needlePos = 0; else if (buffer[bufferPos] == needle[0]) { // found a byte needlePos = 1; } } ++bufferPos; } if (needlePos < needleLength) readBytes = fread(buffer, 1, 512, m_handle); // read next block else { m_readPos = m_readPos + bufferPos - readBytes - needleLength; // or without needleLength pos = m_readPos; // - needleLength; FSEEK(m_readPos); return m_lastResult = XRV_OK; } } return m_lastResult = XRV_ENDOFFILE; }
////////////////////////////////////////////////////////////////////////////////////////// // Read data from the file and put it into the data buffer. XsensResultValue Cmt1f::readData( const uint32_t maxLength, void* data, uint32_t* length) { if (!m_isOpen) return m_lastResult = XRV_NOFILEOPEN; if (maxLength == 0) return m_lastResult = XRV_OK; uint32_t len; if (length == nullptr) length = &len; gotoRead(); length[0] = (uint32_t)fread(data, 1, maxLength, m_handle); if (length[0] == 0) return (m_lastResult = XRV_ENDOFFILE); m_readPos += length[0]; return m_lastResult = XRV_OK; }