From 8ae3571c3807c45d6ef0bfca0c427ae4400b6ff1 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Wed, 11 Sep 2024 10:08:52 -0600 Subject: [PATCH] add new tell function --- code/logic/fossil/io/stream.h | 10 ++++++++++ code/logic/stream.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/code/logic/fossil/io/stream.h b/code/logic/fossil/io/stream.h index 089e636..86827a4 100644 --- a/code/logic/fossil/io/stream.h +++ b/code/logic/fossil/io/stream.h @@ -101,6 +101,16 @@ int32_t fossil_fstream_append(fossil_fstream_t *stream, const void *buffer, size */ int32_t fossil_fstream_seek(fossil_fstream_t *stream, int64_t offset, int32_t origin); +/** + * Get the current position of the file pointer in an open stream. + * + * This function retrieves the current position of the file pointer in an open stream. + * + * @param stream Pointer to the fossil_fstream_t structure to get the position of. + * @return The current position of the file pointer. + */ +int32_t fossil_fstream_tell(fossil_fstream_t *stream); + /** * Save an open stream to a new file. * diff --git a/code/logic/stream.c b/code/logic/stream.c index 868fcc3..49c192a 100644 --- a/code/logic/stream.c +++ b/code/logic/stream.c @@ -125,6 +125,23 @@ int32_t fossil_fstream_seek(fossil_fstream_t *stream, int64_t offset, int32_t or return FOSSIL_ERROR_OK; } +// Get the current position of the file pointer in an open stream +int32_t fossil_fstream_tell(fossil_fstream_t *stream) { + if (stream == NULL || stream->file == NULL) { + fprintf(stderr, "Error: Null pointer\n"); + return FOSSIL_ERROR_NULL_POINTER; + } + + long position = ftell(stream->file); + + if (position == -1L && ferror(stream->file)) { + fprintf(stderr, "Error: IO error from getting file position\n"); + return FOSSIL_ERROR_IO; + } + + return (int32_t)position; +} + // Save an open stream to a new file int32_t fossil_fstream_save(fossil_fstream_t *stream, const char *new_filename) { if (stream == NULL || stream->file == NULL || new_filename == NULL) {