Skip to content

Commit

Permalink
Fix compatibility with the old interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rsp4jack committed Nov 2, 2024
1 parent 508c152 commit 7969fde
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
53 changes: 52 additions & 1 deletion arm-wt-22k/src/hostmm_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ static const union {
// --- end

typedef struct eas_hw_file_tag {
FILE* handle;
void* handle;
EAS_BOOL own;

// legacy interface for compatibility
int pos;
int (*readAt)(void *handle, void *buf, int offset, int size);
int (*size)(void *handle);
} EAS_HW_FILE;

EAS_RESULT EAS_HWInit(EAS_HW_DATA_HANDLE* pHWInstData)
Expand Down Expand Up @@ -100,6 +105,8 @@ EAS_RESULT EAS_HWOpenFile(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_LOCATOR locato
*pFile = malloc(sizeof(EAS_HW_FILE));
(*pFile)->handle = locator->handle;
(*pFile)->own = EAS_FALSE;
(*pFile)->readAt = locator->readAt;
(*pFile)->size = locator->size;
return EAS_SUCCESS;
}

Expand All @@ -110,6 +117,18 @@ EAS_RESULT EAS_HWReadFile(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, v
return EAS_ERROR_INVALID_HANDLE;
}

if (file->readAt != NULL) {
int count = file->readAt(file->handle, pBuffer, file->pos, n);
file->pos += count;
if (pBytesRead != NULL) {
*pBytesRead = count;
}
if (count < n) {
return EAS_EOF;
}
return EAS_SUCCESS;
}

size_t count = fread(pBuffer, 1, n, file->handle);
if (pBytesRead != NULL) {
*pBytesRead = count;
Expand Down Expand Up @@ -172,6 +191,13 @@ EAS_RESULT EAS_HWFilePos(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EA
return EAS_ERROR_INVALID_HANDLE;
}

if (file->readAt != NULL) {
if (pPosition != NULL) {
*pPosition = file->pos;
}
return EAS_SUCCESS;
}

long pos = ftell(file->handle);
if (pPosition != NULL) {
*pPosition = pos;
Expand All @@ -186,6 +212,11 @@ EAS_RESULT EAS_HWFileSeek(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, E
return EAS_ERROR_INVALID_HANDLE;
}

if (file->readAt != NULL) {
file->pos = position;
return EAS_SUCCESS;
}

/* validate new position */
if (fseek(file->handle, position, SEEK_SET) != 0) {
return EAS_ERROR_FILE_SEEK;
Expand All @@ -201,6 +232,11 @@ EAS_RESULT EAS_HWFileSeekOfs(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file
return EAS_ERROR_INVALID_HANDLE;
}

if (file->readAt != NULL) {
file->pos = position;
return EAS_SUCCESS;
}

if (fseek(file->handle, position, SEEK_CUR) != 0) {
return EAS_ERROR_FILE_SEEK;
}
Expand All @@ -211,6 +247,17 @@ EAS_RESULT EAS_HWFileSeekOfs(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file
EAS_RESULT EAS_HWDupHandle(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_FILE_HANDLE* pDupFile)
{
*pDupFile = NULL;

if (file->readAt != NULL) {
*pDupFile = malloc(sizeof(EAS_HW_FILE));
(*pDupFile)->handle = file->handle;
(*pDupFile)->own = EAS_FALSE;
(*pDupFile)->readAt = file->readAt;
(*pDupFile)->size = file->size;
(*pDupFile)->pos = file->pos;
return EAS_SUCCESS;
}

#if defined(__linux__) // todo: implement for other platforms
char filePath[PATH_MAX];
char linkPath[PATH_MAX];
Expand Down Expand Up @@ -292,6 +339,10 @@ EAS_RESULT EAS_HWCloseFile(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file)
return EAS_ERROR_INVALID_HANDLE;
}

if (file->readAt != NULL) {
return EAS_SUCCESS;
}

if (file->own) {
if (fclose(file->handle) != 0) {
return EAS_ERROR_INVALID_HANDLE;
Expand Down
11 changes: 7 additions & 4 deletions example/sonivoxrender.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ int initializeLibrary(void)

if (dls_path != NULL) {
EAS_FILE mDLSFile;
memset(&mDLSFile, 0, sizeof(EAS_FILE));

mDLSFile.handle = fopen(dls_path, "rb");
if (mDLSFile.handle == NULL) {
Expand All @@ -114,8 +115,8 @@ int initializeLibrary(void)
goto cleanup;
}

mDLSFile.readAt = Read;
mDLSFile.size = Size;
// mDLSFile.readAt = Read;
// mDLSFile.size = Size;

result = EAS_LoadDLSCollection(mEASDataHandle, NULL, &mDLSFile);
fclose(mDLSFile.handle);
Expand Down Expand Up @@ -210,15 +211,17 @@ int renderFile(const char *fileName)

int ok = EXIT_SUCCESS;

memset(&mEasFile, 0, sizeof(EAS_FILE));

mEasFile.handle = fopen(fileName, "rb");
if (mEasFile.handle == NULL) {
fprintf(stderr, "Failed to open %s. error: %s\n", fileName, strerror(errno));
ok = EXIT_FAILURE;
return ok;
}

mEasFile.readAt = Read;
mEasFile.size = Size;
// mEasFile.readAt = Read;
// mEasFile.size = Size;

EAS_RESULT result = EAS_OpenFile(mEASDataHandle, &mEasFile, &mEASStreamHandle);
if (result != EAS_SUCCESS) {
Expand Down

0 comments on commit 7969fde

Please sign in to comment.