Skip to content

Commit

Permalink
add new tell function
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-coding committed Sep 11, 2024
1 parent 22867c7 commit 8ae3571
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions code/logic/fossil/io/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions code/logic/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8ae3571

Please sign in to comment.