Add custom fseek and fread Add test framework gtest and added my custom fread and fseek tests + functions
45 lines
1.3 KiB
C++
45 lines
1.3 KiB
C++
#include "tftp.hpp"
|
|
|
|
/**
|
|
* @brief tftp custom file functions to set the offset and read the data
|
|
* @param[in,out] handle Custom file handles
|
|
* @param[in] offset The offset to set
|
|
* @param[in] whence The origin of the offset
|
|
*/
|
|
void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
|
|
switch (whence) {
|
|
case SEEK_SET:
|
|
handle->ofset = offset;
|
|
break;
|
|
case SEEK_CUR:
|
|
handle->ofset += offset;
|
|
break;
|
|
case SEEK_END:
|
|
break;
|
|
}
|
|
if (handle->ofset > handle->len) {
|
|
handle->ofset = handle->len;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief tftp custom file functions to read the data
|
|
* auto rolling over the offset
|
|
* if the bytes to read is bigger than the remaining bytes
|
|
* it will read the remaining bytes and set the bytes to 0
|
|
* @param[out] buf The buffer to write the data to
|
|
* @param[in] bytes The number of bytes to read
|
|
* @param[in,out] handle Custom file handles
|
|
*/
|
|
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
|
if (handle->ofset + bytes > handle->len) {
|
|
bytes = handle->len - handle->ofset;
|
|
}
|
|
memcpy(buf, handle->data + handle->ofset, bytes);
|
|
handle->ofset += bytes;
|
|
((char*)buf)[bytes] = '\0';
|
|
if (handle->ofset > handle->len) {
|
|
bytes = 0;
|
|
}
|
|
return bytes;
|
|
} |