Skip to content

Commit

Permalink
replace naughty fgetpos/poke/fsetpos with fseeko
Browse files Browse the repository at this point in the history
fpos_t is intended to be an opaque type so reaching inside it
is not right.

Switch to fseeko/ftello which are the off_t equivalents of
fseek/ftell.

closes: #13
  • Loading branch information
wez committed Dec 31, 2020
1 parent ea8cb4e commit cbecfb1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ check_symbol_exists(strsep "string.h" HAVE_STRSEP)
if(HAVE_STRSEP)
add_definitions(-DHAVE_STRSEP)
endif()
check_symbol_exists(fseeko "stdio.h" HAVE_FSEEKO)
if(HAVE_FSEEKO)
add_definitions(-DHAVE_FSEEKO)
endif()

add_definitions(
-DPACKAGE_VERSION="${PACKAGE_VERSION}"
Expand Down
23 changes: 3 additions & 20 deletions src/parsley.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ void APar_ScanAtoms(const char *path, bool deepscan_REQ) {
APar_TestCompatibleBrand(file, 0, dataSize);
}

fseek(file, jump, SEEK_SET);
fseeko(file, jump, SEEK_SET);

while (jump < file_size) {

Expand Down Expand Up @@ -5718,31 +5718,14 @@ void APar_MergeTempFile(FILE *dest_file,
if (file_pos + max_buffer <= src_file_size) {
APar_readX(buffer, src_file, file_pos, max_buffer);

// fseek(dest_file, dest_position + file_pos, SEEK_SET);
#if defined(_WIN32)
fpos_t file_offset = dest_position + file_pos;
#elif defined(__GLIBC__)
fpos_t file_offset = {0};
file_offset.__pos = dest_position + file_pos;
#else
off_t file_offset = dest_position + file_pos;
#endif
fsetpos(dest_file, &file_offset);
fseeko(dest_file, dest_position + file_pos, SEEK_SET);
fwrite(buffer, max_buffer, 1, dest_file);
file_pos += max_buffer;

} else {
APar_readX(buffer, src_file, file_pos, src_file_size - file_pos);
// fprintf(stdout, "buff starts with %s\n", buffer+4);
#if defined(_WIN32)
fpos_t file_offset = dest_position + file_pos;
#elif defined(__GLIBC__)
fpos_t file_offset = {0};
file_offset.__pos = dest_position + file_pos;
#else
off_t file_offset = dest_position + file_pos;
#endif
fsetpos(dest_file, &file_offset);
fseeko(dest_file, dest_position + file_pos, SEEK_SET);
fwrite(buffer, src_file_size - file_pos, 1, dest_file);
file_pos += src_file_size - file_pos;
break;
Expand Down
31 changes: 17 additions & 14 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,29 @@ void TestFileExistence(const char *filePath, bool errorOut) {
}
}

#if defined(_WIN32)

///////////////////////////////////////////////////////////////////////////////////////
// Win32 functions //
///////////////////////////////////////////////////////////////////////////////////////

#ifndef HAVE_FSEEKO
#ifdef _WIN32
int fseeko(FILE *stream, off_t pos, int whence) {
return _fseeki64(stream, pos, whence);
}

int fseeko(FILE *stream, uint64_t pos, int whence) { // only using SEEK_SET here
if (whence == SEEK_SET) {
fpos_t fpos = pos;
return fsetpos(stream, &fpos);
} else {
return -1;
}
return -1;
off_t ftello(FILE *stream) { return _ftelli64(stream); }

#else
int fseeko(FILE *stream, off_t pos, int whence) {
return fseek(stream, pos, whence);
}

off_t ftello(FILE *stream) { return ftell(stream); }
#endif
#endif

#if defined(_WIN32)

///////////////////////////////////////////////////////////////////////////////////////
// Win32 functions //
///////////////////////////////////////////////////////////////////////////////////////

/*----------------------
APar_OpenFileWin32
utf8_filepath - a pointer to a string (possibly utf8) of the full path
Expand Down
6 changes: 4 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ FILE *APar_OpenFile(const char *utf8_filepath, const char *file_flags);
FILE *APar_OpenISOBaseMediaFile(const char *file, bool open); // openSomeFile
void TestFileExistence(const char *filePath, bool errorOut);

#if defined(_WIN32)
#ifndef HAVE_FSEEKO
int fseeko(FILE *stream, uint64_t pos, int whence);
int fseeko(FILE *stream, off_t pos, int whence);
off_t ftello(FILE *stream);
#endif

#if defined(_WIN32)
HANDLE APar_OpenFileWin32(const char *utf8_filepath,
DWORD dwDesiredAccess,
DWORD dwShareMode,
Expand Down

1 comment on commit cbecfb1

@Soldering-Stan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool solution wez - much better than my hack :-)

Bit more work though so apologies for that :-)

All the best for 2021.

Regards,

S

Please sign in to comment.