Skip to content

Commit cbecfb1

Browse files
committed
replace naughty fgetpos/poke/fsetpos with fseeko
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
1 parent ea8cb4e commit cbecfb1

File tree

4 files changed

+28
-36
lines changed

4 files changed

+28
-36
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ check_symbol_exists(strsep "string.h" HAVE_STRSEP)
2626
if(HAVE_STRSEP)
2727
add_definitions(-DHAVE_STRSEP)
2828
endif()
29+
check_symbol_exists(fseeko "stdio.h" HAVE_FSEEKO)
30+
if(HAVE_FSEEKO)
31+
add_definitions(-DHAVE_FSEEKO)
32+
endif()
2933

3034
add_definitions(
3135
-DPACKAGE_VERSION="${PACKAGE_VERSION}"

src/parsley.cpp

+3-20
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ void APar_ScanAtoms(const char *path, bool deepscan_REQ) {
15981598
APar_TestCompatibleBrand(file, 0, dataSize);
15991599
}
16001600

1601-
fseek(file, jump, SEEK_SET);
1601+
fseeko(file, jump, SEEK_SET);
16021602

16031603
while (jump < file_size) {
16041604

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

5721-
// fseek(dest_file, dest_position + file_pos, SEEK_SET);
5722-
#if defined(_WIN32)
5723-
fpos_t file_offset = dest_position + file_pos;
5724-
#elif defined(__GLIBC__)
5725-
fpos_t file_offset = {0};
5726-
file_offset.__pos = dest_position + file_pos;
5727-
#else
5728-
off_t file_offset = dest_position + file_pos;
5729-
#endif
5730-
fsetpos(dest_file, &file_offset);
5721+
fseeko(dest_file, dest_position + file_pos, SEEK_SET);
57315722
fwrite(buffer, max_buffer, 1, dest_file);
57325723
file_pos += max_buffer;
57335724

57345725
} else {
57355726
APar_readX(buffer, src_file, file_pos, src_file_size - file_pos);
57365727
// fprintf(stdout, "buff starts with %s\n", buffer+4);
5737-
#if defined(_WIN32)
5738-
fpos_t file_offset = dest_position + file_pos;
5739-
#elif defined(__GLIBC__)
5740-
fpos_t file_offset = {0};
5741-
file_offset.__pos = dest_position + file_pos;
5742-
#else
5743-
off_t file_offset = dest_position + file_pos;
5744-
#endif
5745-
fsetpos(dest_file, &file_offset);
5728+
fseeko(dest_file, dest_position + file_pos, SEEK_SET);
57465729
fwrite(buffer, src_file_size - file_pos, 1, dest_file);
57475730
file_pos += src_file_size - file_pos;
57485731
break;

src/util.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,29 @@ void TestFileExistence(const char *filePath, bool errorOut) {
155155
}
156156
}
157157

158-
#if defined(_WIN32)
159-
160-
///////////////////////////////////////////////////////////////////////////////////////
161-
// Win32 functions //
162-
///////////////////////////////////////////////////////////////////////////////////////
163-
164158
#ifndef HAVE_FSEEKO
159+
#ifdef _WIN32
160+
int fseeko(FILE *stream, off_t pos, int whence) {
161+
return _fseeki64(stream, pos, whence);
162+
}
165163

166-
int fseeko(FILE *stream, uint64_t pos, int whence) { // only using SEEK_SET here
167-
if (whence == SEEK_SET) {
168-
fpos_t fpos = pos;
169-
return fsetpos(stream, &fpos);
170-
} else {
171-
return -1;
172-
}
173-
return -1;
164+
off_t ftello(FILE *stream) { return _ftelli64(stream); }
165+
166+
#else
167+
int fseeko(FILE *stream, off_t pos, int whence) {
168+
return fseek(stream, pos, whence);
174169
}
175170

171+
off_t ftello(FILE *stream) { return ftell(stream); }
172+
#endif
176173
#endif
177174

175+
#if defined(_WIN32)
176+
177+
///////////////////////////////////////////////////////////////////////////////////////
178+
// Win32 functions //
179+
///////////////////////////////////////////////////////////////////////////////////////
180+
178181
/*----------------------
179182
APar_OpenFileWin32
180183
utf8_filepath - a pointer to a string (possibly utf8) of the full path

src/util.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ FILE *APar_OpenFile(const char *utf8_filepath, const char *file_flags);
4747
FILE *APar_OpenISOBaseMediaFile(const char *file, bool open); // openSomeFile
4848
void TestFileExistence(const char *filePath, bool errorOut);
4949

50-
#if defined(_WIN32)
5150
#ifndef HAVE_FSEEKO
52-
int fseeko(FILE *stream, uint64_t pos, int whence);
51+
int fseeko(FILE *stream, off_t pos, int whence);
52+
off_t ftello(FILE *stream);
5353
#endif
54+
55+
#if defined(_WIN32)
5456
HANDLE APar_OpenFileWin32(const char *utf8_filepath,
5557
DWORD dwDesiredAccess,
5658
DWORD dwShareMode,

0 commit comments

Comments
 (0)