Change code and naming style, fix several bugs, removed math types.
After a while I felt that putting underscores between words was not the best solution, so I changed the underscores to capital letters. Fixed consistency bug between POSIX/Win32 platform in BH_FileOpen. Removed definitions for math types (vector, matrix, etc.) due to potential aliasing issues.
This commit is contained in:
110
src/algo.c
110
src/algo.c
@@ -2,7 +2,7 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
void bh_swap(void *dest,
|
||||
void BH_Swap(void *dest,
|
||||
void *src,
|
||||
size_t size)
|
||||
{
|
||||
@@ -30,11 +30,11 @@ void bh_swap(void *dest,
|
||||
}
|
||||
|
||||
|
||||
void *bh_partition(void *pivot,
|
||||
void *BH_Partition(void *pivot,
|
||||
void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
char *start, *end, *i, *j;
|
||||
|
||||
@@ -66,7 +66,7 @@ void *bh_partition(void *pivot,
|
||||
pivot = i;
|
||||
|
||||
/* Swap elements and continue */
|
||||
bh_swap(i, j, element);
|
||||
BH_Swap(i, j, element);
|
||||
i += element;
|
||||
j -= element;
|
||||
}
|
||||
@@ -77,10 +77,10 @@ void *bh_partition(void *pivot,
|
||||
|
||||
|
||||
#if 0
|
||||
static void bh_sort_insert(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
static void BH_SortInsert(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
@@ -95,7 +95,7 @@ static void bh_sort_insert(void *array,
|
||||
rhs = (char *)array + (j - 1) * element;
|
||||
|
||||
if (equal(lhs, rhs) < 0)
|
||||
bh_swap(lhs, rhs, element);
|
||||
BH_Swap(lhs, rhs, element);
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -105,10 +105,10 @@ static void bh_sort_insert(void *array,
|
||||
#endif
|
||||
|
||||
|
||||
static void bh_sort_shell(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
static void BH_SortShell(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
static const size_t gaps[10] = {1750, 701, 301, 132, 57, 23, 10, 4, 1, 0};
|
||||
const size_t *gap;
|
||||
@@ -127,7 +127,7 @@ static void bh_sort_shell(void *array,
|
||||
rhs = (char *)array + (j - *gap) * element;
|
||||
|
||||
if (equal(lhs, rhs) < 0)
|
||||
bh_swap(lhs, rhs, element);
|
||||
BH_Swap(lhs, rhs, element);
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -136,24 +136,24 @@ static void bh_sort_shell(void *array,
|
||||
}
|
||||
|
||||
|
||||
static void bh_sort_heap(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
static void BH_SortHeap(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
bh_heap_make(array, size, element, equal);
|
||||
BH_HeapMake(array, size, element, equal);
|
||||
for (i = size; i > 0; i--)
|
||||
bh_heap_remove(array, i, element, equal);
|
||||
BH_HeapRemove(array, i, element, equal);
|
||||
}
|
||||
|
||||
|
||||
static void bh_sort_intro_r(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal,
|
||||
size_t depth)
|
||||
static void BH_SortIntroR(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal,
|
||||
size_t depth)
|
||||
{
|
||||
/* Introsort (with manual tail call optimization) */
|
||||
while (1)
|
||||
@@ -163,13 +163,13 @@ static void bh_sort_intro_r(void *array,
|
||||
if (size < 16)
|
||||
{
|
||||
/* There are less then 16 elements left - use Shell/Insert sort */
|
||||
bh_sort_shell(array, size, element, equal);
|
||||
BH_SortShell(array, size, element, equal);
|
||||
return;
|
||||
}
|
||||
else if (!depth)
|
||||
{
|
||||
/* Max depth reached - use heap sort */
|
||||
bh_sort_heap(array, size, element, equal);
|
||||
BH_SortHeap(array, size, element, equal);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -199,10 +199,10 @@ static void bh_sort_intro_r(void *array,
|
||||
}
|
||||
|
||||
/* Partition the array */
|
||||
middle = bh_partition(pivot, array, size, element, equal);
|
||||
middle = BH_Partition(pivot, array, size, element, equal);
|
||||
|
||||
/* Recursive call into first half */
|
||||
bh_sort_intro_r(array, (middle - start) / element, element, equal, depth - 1);
|
||||
BH_SortIntroR(array, (middle - start) / element, element, equal, depth - 1);
|
||||
|
||||
/* Setup array and size for the second half */
|
||||
array = middle;
|
||||
@@ -212,10 +212,10 @@ static void bh_sort_intro_r(void *array,
|
||||
}
|
||||
|
||||
|
||||
void bh_sort(void *array,
|
||||
void BH_Sort(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
size_t depth, depth_log;
|
||||
|
||||
@@ -229,14 +229,14 @@ void bh_sort(void *array,
|
||||
}
|
||||
|
||||
/* Call main sorting function */
|
||||
bh_sort_intro_r(array, size, element, equal, depth);
|
||||
BH_SortIntroR(array, size, element, equal, depth);
|
||||
}
|
||||
|
||||
|
||||
void bh_heap_make(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
void BH_HeapMake(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
char *start, *end;
|
||||
size_t i;
|
||||
@@ -268,7 +268,7 @@ void bh_heap_make(void *array,
|
||||
if (equal(current, biggest) < 0)
|
||||
{
|
||||
/* Swap content and recalculate children pointers */
|
||||
bh_swap(current, biggest, element);
|
||||
BH_Swap(current, biggest, element);
|
||||
current = biggest;
|
||||
left = start + (current - start) * 2 + element;
|
||||
right = left + element;
|
||||
@@ -280,10 +280,10 @@ void bh_heap_make(void *array,
|
||||
}
|
||||
|
||||
|
||||
void bh_heap_remove(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
void BH_HeapRemove(void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
char *start, *end, *current, *left, *right;
|
||||
|
||||
@@ -298,7 +298,7 @@ void bh_heap_remove(void *array,
|
||||
right = left + element;
|
||||
|
||||
/* Swap first and last element */
|
||||
bh_swap(current, end, element);
|
||||
BH_Swap(current, end, element);
|
||||
|
||||
/* Iterate until we reach the end */
|
||||
while (left < end)
|
||||
@@ -314,7 +314,7 @@ void bh_heap_remove(void *array,
|
||||
if (equal(current, biggest) < 0)
|
||||
{
|
||||
/* Swap content and recalculate children pointers */
|
||||
bh_swap(current, biggest, element);
|
||||
BH_Swap(current, biggest, element);
|
||||
current = biggest;
|
||||
left = start + (current - start) * 2 + element;
|
||||
right = left + element;
|
||||
@@ -325,11 +325,11 @@ void bh_heap_remove(void *array,
|
||||
}
|
||||
|
||||
|
||||
void bh_heap_insert(void *value,
|
||||
void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
void BH_HeapInsert(void *value,
|
||||
void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
char *start, *end, *current;
|
||||
|
||||
@@ -353,7 +353,7 @@ void bh_heap_insert(void *value,
|
||||
if (equal(parent, current) < 0)
|
||||
{
|
||||
/* Swap current and parent */
|
||||
bh_swap(parent, current, element);
|
||||
BH_Swap(parent, current, element);
|
||||
current = parent;
|
||||
}
|
||||
else
|
||||
@@ -362,11 +362,11 @@ void bh_heap_insert(void *value,
|
||||
}
|
||||
|
||||
|
||||
void bh_heap_replace(void *value,
|
||||
void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
bh_equal_cb_t equal)
|
||||
void BH_HeapReplace(void *value,
|
||||
void *array,
|
||||
size_t size,
|
||||
size_t element,
|
||||
BH_EqualCallback equal)
|
||||
{
|
||||
char *start, *end, *current, *left, *right;
|
||||
|
||||
@@ -400,7 +400,7 @@ void bh_heap_replace(void *value,
|
||||
if (equal(current, biggest) < 0)
|
||||
{
|
||||
/* Swap content and recalculate children pointers */
|
||||
bh_swap(current, biggest, element);
|
||||
BH_Swap(current, biggest, element);
|
||||
current = biggest;
|
||||
left = start + (current - start) * 2 + element;
|
||||
right = left + element;
|
||||
|
||||
186
src/dummy/file.c
186
src/dummy/file.c
@@ -1,56 +1,71 @@
|
||||
#include <bh/io.h>
|
||||
|
||||
typedef struct bh_file_s
|
||||
typedef struct BH_File
|
||||
{
|
||||
int implement;
|
||||
int me;
|
||||
} bh_file_t;
|
||||
} BH_File;
|
||||
|
||||
static int file_info(bh_file_t *file,
|
||||
size_t *size,
|
||||
const char **name);
|
||||
|
||||
static int file_init(bh_file_t *file,
|
||||
const char *path);
|
||||
static int BH_FileInfo(BH_File *file,
|
||||
size_t *size,
|
||||
const char **name);
|
||||
|
||||
static int file_destroy(bh_file_t *file);
|
||||
|
||||
static int file_open(bh_file_t *file,
|
||||
int *mode);
|
||||
static int BH_FileInit(BH_File *file,
|
||||
const char *path);
|
||||
|
||||
static int file_close(bh_file_t *file);
|
||||
|
||||
static int file_read(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
static int BH_FileDestroy(BH_File *file);
|
||||
|
||||
static int file_write(bh_file_t *file,
|
||||
const char *data,
|
||||
size_t *size);
|
||||
|
||||
static int file_peek(bh_file_t* file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
static int BH_FileOpen(BH_File *file,
|
||||
int *mode);
|
||||
|
||||
static int file_flush(bh_file_t *file);
|
||||
|
||||
static int file_seek(bh_file_t *file,
|
||||
int64_t *pos,
|
||||
int *dir);
|
||||
static int BH_FileClose(BH_File *file);
|
||||
|
||||
static int file_tell(bh_file_t *file,
|
||||
int64_t *pos);
|
||||
|
||||
static int file_size(bh_file_t *file,
|
||||
int64_t *size);
|
||||
static int BH_FileRead(BH_File *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
|
||||
static int file_flags(bh_file_t *file);
|
||||
|
||||
static int file_clear(bh_file_t *file);
|
||||
static int BH_FileWrite(BH_File *file,
|
||||
const char *data,
|
||||
size_t *size);
|
||||
|
||||
static int file_info(bh_file_t *file,
|
||||
size_t *size,
|
||||
const char **name)
|
||||
|
||||
static int BH_FilePeek(BH_File* file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int BH_FileFlush(BH_File *file);
|
||||
|
||||
|
||||
static int BH_FileSeek(BH_File *file,
|
||||
int64_t *pos,
|
||||
int *dir);
|
||||
|
||||
|
||||
static int BH_FileTell(BH_File *file,
|
||||
int64_t *pos);
|
||||
|
||||
|
||||
static int BH_FileSize(BH_File *file,
|
||||
int64_t *size);
|
||||
|
||||
|
||||
static int BH_FileFlags(BH_File *file);
|
||||
|
||||
|
||||
static int BH_FileClear(BH_File *file);
|
||||
|
||||
|
||||
static int BH_FileInfo(BH_File *file,
|
||||
size_t *size,
|
||||
const char **name)
|
||||
{
|
||||
static const char classname[] = BH_FILE_CLASSNAME;
|
||||
|
||||
@@ -62,109 +77,124 @@ static int file_info(bh_file_t *file,
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_init(bh_file_t *file,
|
||||
const char *path)
|
||||
|
||||
static int BH_FileInit(BH_File *file,
|
||||
const char *path)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_destroy(bh_file_t *file)
|
||||
|
||||
static int BH_FileDestroy(BH_File *file)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_open(bh_file_t *file,
|
||||
int *mode)
|
||||
|
||||
static int BH_FileOpen(BH_File *file,
|
||||
int *mode)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_close(bh_file_t *file)
|
||||
|
||||
static int BH_FileClose(BH_File *file)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_read(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
|
||||
static int BH_FileRead(BH_File *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_write(bh_file_t *file,
|
||||
const char *data,
|
||||
size_t *size)
|
||||
|
||||
static int BH_FileWrite(BH_File *file,
|
||||
const char *data,
|
||||
size_t *size)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_peek(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
|
||||
static int BH_FilePeek(BH_File *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_flush(bh_file_t *file)
|
||||
|
||||
static int BH_FileFlush(BH_File *file)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_seek(bh_file_t *file,
|
||||
int64_t *pos,
|
||||
int *dir)
|
||||
|
||||
static int BH_FileSeek(BH_File *file,
|
||||
int64_t *pos,
|
||||
int *dir)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_tell(bh_file_t *file,
|
||||
int64_t *pos)
|
||||
|
||||
static int BH_FileTell(BH_File *file,
|
||||
int64_t *pos)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_size(bh_file_t *file,
|
||||
int64_t *size)
|
||||
|
||||
static int BH_FileSize(BH_File *file,
|
||||
int64_t *size)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_flags(bh_file_t *file)
|
||||
|
||||
static int BH_FileFlags(BH_File *file)
|
||||
{
|
||||
return BH_IO_FLAG_ERROR;
|
||||
}
|
||||
|
||||
static int file_clear(bh_file_t *file)
|
||||
|
||||
static int BH_FileClear(BH_File *file)
|
||||
{
|
||||
return BH_NOIMPL;
|
||||
}
|
||||
|
||||
static int file_proc(bh_file_t *file,
|
||||
int type,
|
||||
void *arg1,
|
||||
void *arg2)
|
||||
|
||||
static int BH_FileCallback(BH_File *file,
|
||||
int type,
|
||||
void *arg1,
|
||||
void *arg2)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2);
|
||||
case BH_IO_INIT_CB: return file_init(file, (const char *)arg1);
|
||||
case BH_IO_DESTROY_CB: return file_destroy(file);
|
||||
case BH_IO_OPEN_CB: return file_open(file, (int *)arg1);
|
||||
case BH_IO_CLOSE_CB: return file_close(file);
|
||||
case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_FLUSH_CB: return file_flush(file);
|
||||
case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2);
|
||||
case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1);
|
||||
case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1);
|
||||
case BH_IO_FLAGS_CB: return file_flags(file);
|
||||
case BH_IO_CLEAR_CB: return file_clear(file);
|
||||
case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2);
|
||||
case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1);
|
||||
case BH_IO_DESTROY_CB: return BH_FileDestroy(file);
|
||||
case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1);
|
||||
case BH_IO_CLOSE_CB: return BH_FileClose(file);
|
||||
case BH_IO_READ_CB: return BH_FileRead(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_WRITE_CB: return BH_FileWrite(file, (const char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_PEEK_CB: return BH_FilePeek(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_FLUSH_CB: return BH_FileFlush(file);
|
||||
case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2);
|
||||
case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1);
|
||||
case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1);
|
||||
case BH_IO_FLAGS_CB: return BH_FileFlags(file);
|
||||
case BH_IO_CLEAR_CB: return BH_FileClear(file);
|
||||
default: return BH_NOIMPL;
|
||||
}
|
||||
}
|
||||
|
||||
bh_io_t *bh_file_new(const char *path)
|
||||
|
||||
BH_IO *BH_FileNew(const char *path)
|
||||
{
|
||||
return bh_io_new((bh_io_func_t)file_proc, (void *)path);
|
||||
return BH_IONew((BH_IOCallback)BH_FileCallback, (void *)path);
|
||||
}
|
||||
|
||||
138
src/hashmap.c
138
src/hashmap.c
@@ -3,29 +3,29 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
typedef struct bh_hashmap_node_s
|
||||
typedef struct BH_HashmapNode
|
||||
{
|
||||
void *key;
|
||||
void *value;
|
||||
} bh_hashmap_node_t;
|
||||
} BH_HashmapNode;
|
||||
|
||||
|
||||
struct bh_hashmap_s
|
||||
struct BH_Hashmap
|
||||
{
|
||||
bh_hashmap_node_t *data;
|
||||
BH_HashmapNode *data;
|
||||
size_t *psls;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
size_t threshold;
|
||||
bh_equal_cb_t equal;
|
||||
bh_hash_cb_t hash;
|
||||
BH_EqualCallback equal;
|
||||
BH_HashCallback hash;
|
||||
float factor;
|
||||
};
|
||||
|
||||
|
||||
static void bh_hashmap_init(bh_hashmap_t *hashmap,
|
||||
bh_equal_cb_t equal,
|
||||
bh_hash_cb_t hash)
|
||||
static void BH_HashmapInit(BH_Hashmap *hashmap,
|
||||
BH_EqualCallback equal,
|
||||
BH_HashCallback hash)
|
||||
{
|
||||
memset(hashmap, 0, sizeof(*hashmap));
|
||||
hashmap->factor = 0.75f;
|
||||
@@ -34,7 +34,7 @@ static void bh_hashmap_init(bh_hashmap_t *hashmap,
|
||||
}
|
||||
|
||||
|
||||
static void bh_hashmap_destroy(bh_hashmap_t *hashmap)
|
||||
static void BH_HashmapDestroy(BH_Hashmap *hashmap)
|
||||
{
|
||||
if (hashmap->capacity)
|
||||
{
|
||||
@@ -44,10 +44,10 @@ static void bh_hashmap_destroy(bh_hashmap_t *hashmap)
|
||||
}
|
||||
|
||||
|
||||
static int calc_capacity(size_t size,
|
||||
float factor,
|
||||
size_t *capacity,
|
||||
size_t *threshold)
|
||||
static int BH_CalcCapacity(size_t size,
|
||||
float factor,
|
||||
size_t *capacity,
|
||||
size_t *threshold)
|
||||
{
|
||||
/* Check if we need any capacity at all */
|
||||
if (!size)
|
||||
@@ -71,54 +71,54 @@ static int calc_capacity(size_t size,
|
||||
}
|
||||
|
||||
/* Catch malloc overflow */
|
||||
if (*capacity >= ((size_t)-1) / sizeof(bh_hashmap_node_t))
|
||||
if (*capacity >= ((size_t)-1) / sizeof(BH_HashmapNode))
|
||||
return BH_OOM;
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
static void copy_hashmap(bh_hashmap_t *dest,
|
||||
bh_hashmap_t *src)
|
||||
static void BH_CopyHashmap(BH_Hashmap *dest,
|
||||
BH_Hashmap *src)
|
||||
{
|
||||
void *iter;
|
||||
|
||||
/* Iterate and insert data into hashmap */
|
||||
iter = bh_hashmap_iter_next(src, NULL);
|
||||
iter = BH_HashmapIterNext(src, NULL);
|
||||
while (iter)
|
||||
{
|
||||
void *key, *value;
|
||||
|
||||
key = bh_hashmap_iter_key(iter);
|
||||
value = bh_hashmap_iter_value(iter);
|
||||
bh_hashmap_insert(dest, key, value);
|
||||
key = BH_HashmapIterKey(iter);
|
||||
value = BH_HashmapIterValue(iter);
|
||||
BH_HashmapInsert(dest, key, value);
|
||||
|
||||
iter = bh_hashmap_iter_next(src, iter);
|
||||
iter = BH_HashmapIterNext(src, iter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal,
|
||||
bh_hash_cb_t hash)
|
||||
BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
|
||||
BH_HashCallback hash)
|
||||
{
|
||||
bh_hashmap_t *result;
|
||||
BH_Hashmap *result;
|
||||
|
||||
result = malloc(sizeof(*result));
|
||||
if (result)
|
||||
bh_hashmap_init(result, equal, hash);
|
||||
BH_HashmapInit(result, equal, hash);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void bh_hashmap_free(bh_hashmap_t *hashmap)
|
||||
void BH_HashmapFree(BH_Hashmap *hashmap)
|
||||
{
|
||||
bh_hashmap_destroy(hashmap);
|
||||
BH_HashmapDestroy(hashmap);
|
||||
free(hashmap);
|
||||
}
|
||||
|
||||
|
||||
void bh_hashmap_clear(bh_hashmap_t *hashmap)
|
||||
void BH_HashmapClear(BH_Hashmap *hashmap)
|
||||
{
|
||||
if (hashmap->capacity)
|
||||
memset(hashmap->psls, 0, hashmap->capacity * sizeof(size_t));
|
||||
@@ -126,10 +126,10 @@ void bh_hashmap_clear(bh_hashmap_t *hashmap)
|
||||
}
|
||||
|
||||
|
||||
int bh_hashmap_reserve(bh_hashmap_t *hashmap,
|
||||
size_t size)
|
||||
int BH_HashmapReserve(BH_Hashmap *hashmap,
|
||||
size_t size)
|
||||
{
|
||||
bh_hashmap_t other;
|
||||
BH_Hashmap other;
|
||||
size_t capacity, threshold;
|
||||
|
||||
/* New capacity can't be smaller then current hashmap size */
|
||||
@@ -137,7 +137,7 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
|
||||
size = hashmap->size;
|
||||
|
||||
/* Calculate new capacity */
|
||||
if (calc_capacity(size, hashmap->factor, &capacity, &threshold))
|
||||
if (BH_CalcCapacity(size, hashmap->factor, &capacity, &threshold))
|
||||
return BH_OOM;
|
||||
|
||||
/* Prevent same size reallocation */
|
||||
@@ -145,7 +145,7 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
|
||||
return BH_OK;
|
||||
|
||||
/* Initialize new hashmap */
|
||||
bh_hashmap_init(&other, hashmap->equal, hashmap->hash);
|
||||
BH_HashmapInit(&other, hashmap->equal, hashmap->hash);
|
||||
other.factor = hashmap->factor;
|
||||
|
||||
if (capacity)
|
||||
@@ -170,26 +170,26 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
|
||||
memset(other.psls, 0, sizeof(size_t) * other.capacity);
|
||||
|
||||
/* Copy data from old hashmap to the new hashmap */
|
||||
copy_hashmap(&other, hashmap);
|
||||
BH_CopyHashmap(&other, hashmap);
|
||||
}
|
||||
|
||||
/* Swap hashmaps */
|
||||
bh_hashmap_destroy(hashmap);
|
||||
BH_HashmapDestroy(hashmap);
|
||||
*hashmap = other;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
int bh_hashmap_insert(bh_hashmap_t *hashmap,
|
||||
void *key,
|
||||
void *value)
|
||||
int BH_HashmapInsert(BH_Hashmap *hashmap,
|
||||
void *key,
|
||||
void *value)
|
||||
{
|
||||
size_t bucket, psl, tmp_psl;
|
||||
bh_hashmap_node_t item, tmp;
|
||||
BH_HashmapNode item, tmp;
|
||||
|
||||
/* Try to stay below hashmap threshold */
|
||||
if (hashmap->size + 1 > hashmap->threshold)
|
||||
if (bh_hashmap_reserve(hashmap, hashmap->size + 1))
|
||||
if (BH_HashmapReserve(hashmap, hashmap->size + 1))
|
||||
if (hashmap->size >= hashmap->capacity)
|
||||
return BH_OOM;
|
||||
|
||||
@@ -228,60 +228,60 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap,
|
||||
}
|
||||
|
||||
|
||||
void bh_hashmap_remove(bh_hashmap_t *hashmap,
|
||||
void *key)
|
||||
void BH_HashmapRemove(BH_Hashmap *hashmap,
|
||||
void *key)
|
||||
{
|
||||
void *iter;
|
||||
|
||||
iter = bh_hashmap_iter_at(hashmap, key);
|
||||
iter = BH_HashmapIterAt(hashmap, key);
|
||||
if (iter)
|
||||
bh_hashmap_iter_remove(hashmap, iter);
|
||||
BH_HashmapIterRemove(hashmap, iter);
|
||||
}
|
||||
|
||||
|
||||
int bh_hashmap_at(bh_hashmap_t *hashmap,
|
||||
void *key,
|
||||
void **value)
|
||||
int BH_HashmapAt(BH_Hashmap *hashmap,
|
||||
void *key,
|
||||
void **value)
|
||||
{
|
||||
void *iter;
|
||||
iter = bh_hashmap_iter_at(hashmap, key);
|
||||
iter = BH_HashmapIterAt(hashmap, key);
|
||||
|
||||
if (!iter)
|
||||
return BH_NOTFOUND;
|
||||
|
||||
if (value)
|
||||
*value = bh_hashmap_iter_value(iter);
|
||||
*value = BH_HashmapIterValue(iter);
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
int bh_hashmap_empty(bh_hashmap_t *hashmap)
|
||||
int BH_HashmapEmpty(BH_Hashmap *hashmap)
|
||||
{
|
||||
return !hashmap->size;
|
||||
}
|
||||
|
||||
|
||||
size_t bh_hashmap_size(bh_hashmap_t *hashmap)
|
||||
size_t BH_HashmapSize(BH_Hashmap *hashmap)
|
||||
{
|
||||
return hashmap->size;
|
||||
}
|
||||
|
||||
|
||||
size_t bh_hashmap_capacity(bh_hashmap_t *hashmap)
|
||||
size_t BH_HashmapCapacity(BH_Hashmap *hashmap)
|
||||
{
|
||||
return hashmap->capacity;
|
||||
}
|
||||
|
||||
|
||||
float bh_hashmap_factor(bh_hashmap_t *hashmap)
|
||||
float BH_HashmapFactor(BH_Hashmap *hashmap)
|
||||
{
|
||||
return hashmap->factor;
|
||||
}
|
||||
|
||||
|
||||
void bh_hashmap_set_factor(bh_hashmap_t *hashmap,
|
||||
float factor)
|
||||
void BH_HashmapSetFactor(BH_Hashmap *hashmap,
|
||||
float factor)
|
||||
{
|
||||
/* Limit the factor value to [0.15, 1.0] */
|
||||
factor = (factor > 1.0f) ? (1.0f) : (factor);
|
||||
@@ -293,8 +293,8 @@ void bh_hashmap_set_factor(bh_hashmap_t *hashmap,
|
||||
}
|
||||
|
||||
|
||||
void *bh_hashmap_iter_at(bh_hashmap_t *hashmap,
|
||||
void *key)
|
||||
void *BH_HashmapIterAt(BH_Hashmap *hashmap,
|
||||
void *key)
|
||||
{
|
||||
size_t bucket, psl;
|
||||
|
||||
@@ -321,12 +321,12 @@ void *bh_hashmap_iter_at(bh_hashmap_t *hashmap,
|
||||
}
|
||||
|
||||
|
||||
void *bh_hashmap_iter_next(bh_hashmap_t *hashmap,
|
||||
void *iter)
|
||||
void *BH_HashmapIterNext(BH_Hashmap *hashmap,
|
||||
void *iter)
|
||||
{
|
||||
bh_hashmap_node_t *item;
|
||||
BH_HashmapNode *item;
|
||||
|
||||
item = (bh_hashmap_node_t *)iter;
|
||||
item = (BH_HashmapNode *)iter;
|
||||
while (1)
|
||||
{
|
||||
/* Advance or set iterator to the first element */
|
||||
@@ -346,8 +346,8 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap,
|
||||
}
|
||||
|
||||
|
||||
void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
|
||||
void *iter)
|
||||
void BH_HashmapIterRemove(BH_Hashmap *hashmap,
|
||||
void *iter)
|
||||
{
|
||||
size_t bucket, next_bucket;
|
||||
|
||||
@@ -357,7 +357,7 @@ void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
|
||||
|
||||
/* Adjust hashmap size, calculate current and next bucket index */
|
||||
hashmap->size--;
|
||||
bucket = (bh_hashmap_node_t *)iter - hashmap->data;
|
||||
bucket = (BH_HashmapNode *)iter - hashmap->data;
|
||||
next_bucket = (bucket + 1) & (hashmap->capacity - 1);
|
||||
|
||||
/* Shift all elements toward their preffered place */
|
||||
@@ -377,14 +377,14 @@ void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
|
||||
}
|
||||
|
||||
|
||||
void *bh_hashmap_iter_key(void *iter)
|
||||
void *BH_HashmapIterKey(void *iter)
|
||||
{
|
||||
return ((bh_hashmap_node_t *)iter)->key;
|
||||
return ((BH_HashmapNode *)iter)->key;
|
||||
}
|
||||
|
||||
|
||||
void *bh_hashmap_iter_value(void *iter)
|
||||
void *BH_HashmapIterValue(void *iter)
|
||||
{
|
||||
return ((bh_hashmap_node_t *)iter)->value;
|
||||
return ((BH_HashmapNode *)iter)->value;
|
||||
}
|
||||
|
||||
|
||||
125
src/io.c
125
src/io.c
@@ -4,21 +4,20 @@
|
||||
|
||||
#define BUFFER_SIZE (sizeof(char *))
|
||||
|
||||
|
||||
struct bh_io_s
|
||||
struct BH_IO
|
||||
{
|
||||
bh_io_func_t func;
|
||||
BH_IOCallback cb;
|
||||
};
|
||||
|
||||
|
||||
bh_io_t *bh_io_new(bh_io_func_t func,
|
||||
void *data)
|
||||
BH_IO *BH_IONew(BH_IOCallback cb,
|
||||
void *data)
|
||||
{
|
||||
size_t requested;
|
||||
bh_io_t *io;
|
||||
BH_IO *io;
|
||||
|
||||
/* Get information about IO device size */
|
||||
if (func(NULL, BH_IO_INFO_CB, &requested, NULL))
|
||||
if (cb(NULL, BH_IO_INFO_CB, &requested, NULL))
|
||||
return NULL;
|
||||
|
||||
/* Allocate space for the IO device */
|
||||
@@ -27,8 +26,8 @@ bh_io_t *bh_io_new(bh_io_func_t func,
|
||||
return NULL;
|
||||
|
||||
/* Initialize IO device */
|
||||
io->func = func;
|
||||
if (func(io + 1, BH_IO_INIT_CB, data, NULL))
|
||||
io->cb = cb;
|
||||
if (cb(io + 1, BH_IO_INIT_CB, data, NULL))
|
||||
{
|
||||
free(io);
|
||||
return NULL;
|
||||
@@ -38,28 +37,28 @@ bh_io_t *bh_io_new(bh_io_func_t func,
|
||||
}
|
||||
|
||||
|
||||
void bh_io_free(bh_io_t *io)
|
||||
void BH_IOFree(BH_IO *io)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return;
|
||||
|
||||
/* Call the IO device destruction handler */
|
||||
io->func(io + 1, BH_IO_DESTROY_CB, NULL, NULL);
|
||||
io->cb(io + 1, BH_IO_DESTROY_CB, NULL, NULL);
|
||||
|
||||
/* Deallocate object */
|
||||
free(io);
|
||||
}
|
||||
|
||||
|
||||
const char *bh_io_classname(bh_io_t *io)
|
||||
const char *BH_IOClassname(BH_IO *io)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
if (!io)
|
||||
goto error;
|
||||
|
||||
if (io->func(io + 1, BH_IO_INFO_CB, NULL, &name) != BH_OK)
|
||||
if (io->cb(io + 1, BH_IO_INFO_CB, NULL, &name) != BH_OK)
|
||||
goto error;
|
||||
|
||||
return name;
|
||||
@@ -69,33 +68,33 @@ error:
|
||||
}
|
||||
|
||||
|
||||
int bh_io_open(bh_io_t *io,
|
||||
int mode)
|
||||
int BH_IOOpen(BH_IO *io,
|
||||
int mode)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device open handler with specified mode */
|
||||
return io->func(io + 1, BH_IO_OPEN_CB, &mode, NULL);
|
||||
return io->cb(io + 1, BH_IO_OPEN_CB, &mode, NULL);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_close(bh_io_t *io)
|
||||
int BH_IOClose(BH_IO *io)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device close handler */
|
||||
return io->func(io + 1, BH_IO_CLOSE_CB, NULL, NULL);
|
||||
return io->cb(io + 1, BH_IO_CLOSE_CB, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_read(bh_io_t *io,
|
||||
char *buffer,
|
||||
size_t size,
|
||||
size_t *actual)
|
||||
int BH_IORead(BH_IO *io,
|
||||
char *buffer,
|
||||
size_t size,
|
||||
size_t *actual)
|
||||
{
|
||||
int code;
|
||||
|
||||
@@ -104,7 +103,7 @@ int bh_io_read(bh_io_t *io,
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device read handler */
|
||||
code = io->func(io + 1, BH_IO_READ_CB, buffer, &size);
|
||||
code = io->cb(io + 1, BH_IO_READ_CB, buffer, &size);
|
||||
|
||||
/* If caller wants to know actual read size - report it back */
|
||||
if (actual)
|
||||
@@ -114,40 +113,40 @@ int bh_io_read(bh_io_t *io,
|
||||
}
|
||||
|
||||
|
||||
int bh_io_write(bh_io_t *io,
|
||||
const char *buffer,
|
||||
size_t size,
|
||||
size_t *actual)
|
||||
{
|
||||
int code;
|
||||
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device write handler */
|
||||
code = io->func(io + 1, BH_IO_WRITE_CB, (void *)buffer, &size);
|
||||
|
||||
/* If caller wants to know actual written size - report it back */
|
||||
if (actual)
|
||||
*actual = size;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
int bh_io_peek(bh_io_t *io,
|
||||
char *buffer,
|
||||
int BH_IOWrite(BH_IO *io,
|
||||
const char *buffer,
|
||||
size_t size,
|
||||
size_t *actual)
|
||||
{
|
||||
int code;
|
||||
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device write handler */
|
||||
code = io->cb(io + 1, BH_IO_WRITE_CB, (void *)buffer, &size);
|
||||
|
||||
/* If caller wants to know actual written size - report it back */
|
||||
if (actual)
|
||||
*actual = size;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
int BH_IOPeek(BH_IO *io,
|
||||
char *buffer,
|
||||
size_t size,
|
||||
size_t *actual)
|
||||
{
|
||||
int code;
|
||||
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device peek handler */
|
||||
code = io->func(io + 1, BH_IO_PEEK_CB, (void *)buffer, &size);
|
||||
code = io->cb(io + 1, BH_IO_PEEK_CB, (void *)buffer, &size);
|
||||
|
||||
/* If caller wants to know actual written size - report it back */
|
||||
if (actual)
|
||||
@@ -157,71 +156,71 @@ int bh_io_peek(bh_io_t *io,
|
||||
}
|
||||
|
||||
|
||||
int bh_io_tell(bh_io_t *io,
|
||||
int64_t *position)
|
||||
int BH_IOTell(BH_IO *io,
|
||||
int64_t *position)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device tell handler */
|
||||
return io->func(io + 1, BH_IO_TELL_CB, position, NULL);
|
||||
return io->cb(io + 1, BH_IO_TELL_CB, position, NULL);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_seek(bh_io_t *io,
|
||||
int64_t position,
|
||||
int direction)
|
||||
int BH_IOSeek(BH_IO *io,
|
||||
int64_t position,
|
||||
int direction)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device seek handler */
|
||||
return io->func(io + 1, BH_IO_SEEK_CB, &position, &direction);
|
||||
return io->cb(io + 1, BH_IO_SEEK_CB, &position, &direction);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_flush(bh_io_t *io)
|
||||
int BH_IOFlush(BH_IO *io)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device flush handler */
|
||||
return io->func(io + 1, BH_IO_FLUSH_CB, NULL, NULL);
|
||||
return io->cb(io + 1, BH_IO_FLUSH_CB, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_size(bh_io_t *io,
|
||||
int64_t *size)
|
||||
int BH_IOSize(BH_IO *io,
|
||||
int64_t *size)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_ERROR;
|
||||
|
||||
/* Call the IO device size handler */
|
||||
return io->func(io + 1, BH_IO_SIZE_CB, size, NULL);
|
||||
return io->cb(io + 1, BH_IO_SIZE_CB, size, NULL);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_flags(bh_io_t *io)
|
||||
int BH_IOFlags(BH_IO *io)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_IO_FLAG_ERROR;
|
||||
|
||||
/* Call the IO device flags handler */
|
||||
return io->func(io + 1, BH_IO_FLAGS_CB, NULL, NULL);
|
||||
return io->cb(io + 1, BH_IO_FLAGS_CB, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
int bh_io_clear(bh_io_t *io)
|
||||
int BH_IOClear(BH_IO *io)
|
||||
{
|
||||
/* Prevent working with NULL io */
|
||||
if (!io)
|
||||
return BH_OK;
|
||||
|
||||
/* Call the IO device clear error handler */
|
||||
return io->func(io + 1, BH_IO_CLEAR_CB, NULL, NULL);
|
||||
return io->cb(io + 1, BH_IO_CLEAR_CB, NULL, NULL);
|
||||
}
|
||||
|
||||
1914
src/math.c
1914
src/math.c
File diff suppressed because it is too large
Load Diff
166
src/posix/file.c
166
src/posix/file.c
@@ -7,74 +7,74 @@
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
typedef struct bh_file_s
|
||||
typedef struct BH_File
|
||||
{
|
||||
char *path;
|
||||
int mode;
|
||||
int flags;
|
||||
int handle;
|
||||
} bh_file_t;
|
||||
} BH_File;
|
||||
|
||||
|
||||
static int file_info(bh_file_t *file,
|
||||
size_t *size,
|
||||
const char **ident);
|
||||
static int BH_FileInfo(BH_File *file,
|
||||
size_t *size,
|
||||
const char **name);
|
||||
|
||||
|
||||
static int file_init(bh_file_t *file,
|
||||
const char *path);
|
||||
static int BH_FileInit(BH_File *file,
|
||||
const char *path);
|
||||
|
||||
|
||||
static int file_destroy(bh_file_t *file);
|
||||
static int BH_FileDestroy(BH_File *file);
|
||||
|
||||
|
||||
static int file_open(bh_file_t *file,
|
||||
int *mode);
|
||||
static int BH_FileOpen(BH_File *file,
|
||||
int *mode);
|
||||
|
||||
|
||||
static int file_close(bh_file_t *file);
|
||||
static int BH_FileClose(BH_File *file);
|
||||
|
||||
|
||||
static int file_read(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
static int BH_FileRead(BH_File *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int file_write(bh_file_t *file,
|
||||
const char *data,
|
||||
size_t *size);
|
||||
static int BH_FileWrite(BH_File *file,
|
||||
const char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int file_peek(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
static int BH_FilePeek(BH_File* file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int file_flush(bh_file_t *file);
|
||||
static int BH_FileFlush(BH_File *file);
|
||||
|
||||
|
||||
static int file_seek(bh_file_t *file,
|
||||
int64_t *pos,
|
||||
int *dir);
|
||||
static int BH_FileSeek(BH_File *file,
|
||||
int64_t *pos,
|
||||
int *dir);
|
||||
|
||||
|
||||
static int file_tell(bh_file_t *file,
|
||||
int64_t *pos);
|
||||
static int BH_FileTell(BH_File *file,
|
||||
int64_t *pos);
|
||||
|
||||
|
||||
static int file_size(bh_file_t *file,
|
||||
int64_t *size);
|
||||
static int BH_FileSize(BH_File *file,
|
||||
int64_t *size);
|
||||
|
||||
|
||||
static int file_flags(bh_file_t *file);
|
||||
static int BH_FileFlags(BH_File *file);
|
||||
|
||||
|
||||
static int file_clear(bh_file_t *file);
|
||||
static int BH_FileClear(BH_File *file);
|
||||
|
||||
|
||||
static int file_info(bh_file_t *file,
|
||||
size_t *size,
|
||||
const char **name)
|
||||
static int BH_FileInfo(BH_File *file,
|
||||
size_t *size,
|
||||
const char **name)
|
||||
{
|
||||
static const char classname[] = BH_FILE_CLASSNAME;
|
||||
|
||||
@@ -86,8 +86,8 @@ static int file_info(bh_file_t *file,
|
||||
}
|
||||
|
||||
|
||||
static int file_init(bh_file_t *file,
|
||||
const char *path)
|
||||
static int BH_FileInit(BH_File *file,
|
||||
const char *path)
|
||||
{
|
||||
/* Check if path is valid */
|
||||
if (!path)
|
||||
@@ -103,11 +103,11 @@ static int file_init(bh_file_t *file,
|
||||
}
|
||||
|
||||
|
||||
static int file_destroy(bh_file_t *file)
|
||||
static int BH_FileDestroy(BH_File *file)
|
||||
{
|
||||
/* Close the file handle on destruction */
|
||||
if (file->handle != -1)
|
||||
file_close(file);
|
||||
BH_FileClose(file);
|
||||
|
||||
/* Free path string */
|
||||
free(file->path);
|
||||
@@ -116,7 +116,7 @@ static int file_destroy(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_openflags(int mode)
|
||||
static int BH_FileOpenFlags(int mode)
|
||||
{
|
||||
int flags = 0;
|
||||
|
||||
@@ -152,8 +152,8 @@ static int file_openflags(int mode)
|
||||
}
|
||||
|
||||
|
||||
static int file_open(bh_file_t *file,
|
||||
int *mode)
|
||||
static int BH_FileOpen(BH_File *file,
|
||||
int *mode)
|
||||
{
|
||||
static const mode_t open_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||
int flags;
|
||||
@@ -163,7 +163,7 @@ static int file_open(bh_file_t *file,
|
||||
return BH_ERROR;
|
||||
|
||||
/* Determine file open flags */
|
||||
flags = file_openflags(*mode);
|
||||
flags = BH_FileOpenFlags(*mode);
|
||||
if (flags == -1)
|
||||
return BH_ERROR;
|
||||
|
||||
@@ -176,7 +176,7 @@ static int file_open(bh_file_t *file,
|
||||
}
|
||||
|
||||
|
||||
static int file_close(bh_file_t *file)
|
||||
static int BH_FileClose(BH_File *file)
|
||||
{
|
||||
/* If file is closed - report error */
|
||||
if (file->handle == -1)
|
||||
@@ -190,9 +190,9 @@ static int file_close(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_read(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
static int BH_FileRead(BH_File *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
{
|
||||
ssize_t readed;
|
||||
|
||||
@@ -220,9 +220,9 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_write(bh_file_t *file,
|
||||
const char *data,
|
||||
size_t *size)
|
||||
static int BH_FileWrite(BH_File *file,
|
||||
const char *data,
|
||||
size_t *size)
|
||||
{
|
||||
ssize_t written;
|
||||
|
||||
@@ -250,9 +250,9 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_peek(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
static int BH_FilePeek(BH_File *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
{
|
||||
int64_t position;
|
||||
int direction;
|
||||
@@ -262,13 +262,13 @@ static int file_peek(bh_file_t *file,
|
||||
goto error;
|
||||
|
||||
/* Read data from the file */
|
||||
if (file_read(file, data, size))
|
||||
if (BH_FileRead(file, data, size))
|
||||
goto error;
|
||||
|
||||
/* Backtrack by the read amount */
|
||||
position = -((int64_t)*size);
|
||||
direction = BH_IO_SEEK_CUR;
|
||||
if (file_seek(file, &position, &direction))
|
||||
if (BH_FileSeek(file, &position, &direction))
|
||||
goto error;
|
||||
|
||||
return BH_OK;
|
||||
@@ -279,7 +279,7 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_flush(bh_file_t *file)
|
||||
static int BH_FileFlush(BH_File *file)
|
||||
{
|
||||
/* Check if file is open */
|
||||
if (file->handle == -1)
|
||||
@@ -294,9 +294,9 @@ static int file_flush(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_seek(bh_file_t *file,
|
||||
int64_t *pos,
|
||||
int *dir)
|
||||
static int BH_FileSeek(BH_File *file,
|
||||
int64_t *pos,
|
||||
int *dir)
|
||||
{
|
||||
/* Check if file is open */
|
||||
if (file->handle == -1)
|
||||
@@ -314,8 +314,8 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_tell(bh_file_t *file,
|
||||
int64_t *pos)
|
||||
static int BH_FileTell(BH_File *file,
|
||||
int64_t *pos)
|
||||
{
|
||||
/* Check if file is open */
|
||||
if (file->handle == -1)
|
||||
@@ -333,8 +333,8 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_size(bh_file_t *file,
|
||||
int64_t *size)
|
||||
static int BH_FileSize(BH_File *file,
|
||||
int64_t *size)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
@@ -355,7 +355,7 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_flags(bh_file_t *file)
|
||||
static int BH_FileFlags(BH_File *file)
|
||||
{
|
||||
/* If file handle is valid - append IO_OPEN flag */
|
||||
if (file->handle != -1)
|
||||
@@ -365,7 +365,7 @@ static int file_flags(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_clear(bh_file_t *file)
|
||||
static int BH_FileClear(BH_File *file)
|
||||
{
|
||||
/* Clear IO_ERROR flag */
|
||||
file->flags &= ~BH_IO_FLAG_ERROR;
|
||||
@@ -373,33 +373,33 @@ static int file_clear(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_proc(bh_file_t *file,
|
||||
int type,
|
||||
void *arg1,
|
||||
void *arg2)
|
||||
static int BH_FileCallback(BH_File *file,
|
||||
int type,
|
||||
void *arg1,
|
||||
void *arg2)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2);
|
||||
case BH_IO_INIT_CB: return file_init(file, (const char *)arg1);
|
||||
case BH_IO_DESTROY_CB: return file_destroy(file);
|
||||
case BH_IO_OPEN_CB: return file_open(file, (int *)arg1);
|
||||
case BH_IO_CLOSE_CB: return file_close(file);
|
||||
case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_FLUSH_CB: return file_flush(file);
|
||||
case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2);
|
||||
case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1);
|
||||
case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1);
|
||||
case BH_IO_FLAGS_CB: return file_flags(file);
|
||||
case BH_IO_CLEAR_CB: return file_clear(file);
|
||||
case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2);
|
||||
case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1);
|
||||
case BH_IO_DESTROY_CB: return BH_FileDestroy(file);
|
||||
case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1);
|
||||
case BH_IO_CLOSE_CB: return BH_FileClose(file);
|
||||
case BH_IO_READ_CB: return BH_FileRead(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_WRITE_CB: return BH_FileWrite(file, (const char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_PEEK_CB: return BH_FilePeek(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_FLUSH_CB: return BH_FileFlush(file);
|
||||
case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2);
|
||||
case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1);
|
||||
case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1);
|
||||
case BH_IO_FLAGS_CB: return BH_FileFlags(file);
|
||||
case BH_IO_CLEAR_CB: return BH_FileClear(file);
|
||||
default: return BH_NOIMPL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bh_io_t *bh_file_new(const char *path)
|
||||
BH_IO *BH_FileNew(const char *path)
|
||||
{
|
||||
return bh_io_new((bh_io_func_t)file_proc, (void *)path);
|
||||
return BH_IONew((BH_IOCallback)BH_FileCallback, (void *)path);
|
||||
}
|
||||
|
||||
54
src/queue.c
54
src/queue.c
@@ -3,7 +3,7 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
struct bh_queue_s
|
||||
struct BH_Queue
|
||||
{
|
||||
void **data;
|
||||
size_t size;
|
||||
@@ -13,54 +13,54 @@ struct bh_queue_s
|
||||
};
|
||||
|
||||
|
||||
static void bh_queue_init(bh_queue_t *queue)
|
||||
static void BH_QueueInit(BH_Queue *queue)
|
||||
{
|
||||
memset(queue, 0, sizeof(*queue));
|
||||
}
|
||||
|
||||
|
||||
static void bh_queue_destroy(bh_queue_t *queue)
|
||||
static void BH_QueueDestroy(BH_Queue *queue)
|
||||
{
|
||||
if (queue->capacity)
|
||||
free(queue->data);
|
||||
}
|
||||
|
||||
|
||||
static void queue_copy(bh_queue_t *dest,
|
||||
bh_queue_t *src)
|
||||
static void BH_QueueCopy(BH_Queue *dest,
|
||||
BH_Queue *src)
|
||||
{
|
||||
void *iter;
|
||||
|
||||
/* Iterate over old queue and insert data into new queue */
|
||||
iter = bh_queue_iter_next(src, NULL);
|
||||
iter = BH_QueueIterNext(src, NULL);
|
||||
while (iter)
|
||||
{
|
||||
bh_queue_insert(dest, bh_queue_iter_value(iter));
|
||||
iter = bh_queue_iter_next(src, iter);
|
||||
BH_QueueInsert(dest, BH_QueueIterValue(iter));
|
||||
iter = BH_QueueIterNext(src, iter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bh_queue_t *bh_queue_new(void)
|
||||
BH_Queue *BH_QueueNew(void)
|
||||
{
|
||||
bh_queue_t *result;
|
||||
BH_Queue *result;
|
||||
|
||||
result = malloc(sizeof(*result));
|
||||
if (result)
|
||||
bh_queue_init(result);
|
||||
BH_QueueInit(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void bh_queue_free(bh_queue_t *queue)
|
||||
void BH_QueueFree(BH_Queue *queue)
|
||||
{
|
||||
bh_queue_destroy(queue);
|
||||
BH_QueueDestroy(queue);
|
||||
free(queue);
|
||||
}
|
||||
|
||||
|
||||
void bh_queue_clear(bh_queue_t *queue)
|
||||
void BH_QueueClear(BH_Queue *queue)
|
||||
{
|
||||
queue->head = 0;
|
||||
queue->tail = 0;
|
||||
@@ -68,10 +68,10 @@ void bh_queue_clear(bh_queue_t *queue)
|
||||
}
|
||||
|
||||
|
||||
int bh_queue_reserve(bh_queue_t *queue,
|
||||
int BH_QueueReserve(BH_Queue *queue,
|
||||
size_t size)
|
||||
{
|
||||
bh_queue_t other;
|
||||
BH_Queue other;
|
||||
|
||||
/* New capacity should be great or equal to current size */
|
||||
if (size < queue->size)
|
||||
@@ -86,7 +86,7 @@ int bh_queue_reserve(bh_queue_t *queue,
|
||||
return BH_OK;
|
||||
|
||||
/* Prepare new empty queue */
|
||||
bh_queue_init(&other);
|
||||
BH_QueueInit(&other);
|
||||
if (size)
|
||||
{
|
||||
/* Allocate new capacity for the queue */
|
||||
@@ -96,7 +96,7 @@ int bh_queue_reserve(bh_queue_t *queue,
|
||||
return BH_OOM;
|
||||
|
||||
/* Iterate over old queue and insert data into new queue */
|
||||
queue_copy(&other, queue);
|
||||
BH_QueueCopy(&other, queue);
|
||||
}
|
||||
|
||||
/* If old queue had allocated data - free it */
|
||||
@@ -109,7 +109,7 @@ int bh_queue_reserve(bh_queue_t *queue,
|
||||
}
|
||||
|
||||
|
||||
int bh_queue_insert(bh_queue_t *queue,
|
||||
int BH_QueueInsert(BH_Queue *queue,
|
||||
void *value)
|
||||
{
|
||||
/* Check if queue can contain new element */
|
||||
@@ -119,7 +119,7 @@ int bh_queue_insert(bh_queue_t *queue,
|
||||
|
||||
/* Check for capacity overflow and reserve capacity */
|
||||
capacity = (queue->capacity) ? (queue->capacity * 2) : (16);
|
||||
if (capacity < queue->capacity || bh_queue_reserve(queue, capacity))
|
||||
if (capacity < queue->capacity || BH_QueueReserve(queue, capacity))
|
||||
return BH_OOM;
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ int bh_queue_insert(bh_queue_t *queue,
|
||||
}
|
||||
|
||||
|
||||
void bh_queue_remove(bh_queue_t *queue)
|
||||
void BH_QueueRemove(BH_Queue *queue)
|
||||
{
|
||||
/* Do nothing if queue is empty */
|
||||
if (!queue->size)
|
||||
@@ -146,7 +146,7 @@ void bh_queue_remove(bh_queue_t *queue)
|
||||
}
|
||||
|
||||
|
||||
int bh_queue_front(bh_queue_t *queue, void **value)
|
||||
int BH_QueueFront(BH_Queue *queue, void **value)
|
||||
{
|
||||
/* Do nothing if queue is empty */
|
||||
if (!queue->size)
|
||||
@@ -158,25 +158,25 @@ int bh_queue_front(bh_queue_t *queue, void **value)
|
||||
}
|
||||
|
||||
|
||||
int bh_queue_empty(bh_queue_t *queue)
|
||||
int BH_QueueEmpty(BH_Queue *queue)
|
||||
{
|
||||
return !queue->size;
|
||||
}
|
||||
|
||||
|
||||
size_t bh_queue_size(bh_queue_t *queue)
|
||||
size_t BH_QueueSize(BH_Queue *queue)
|
||||
{
|
||||
return queue->size;
|
||||
}
|
||||
|
||||
|
||||
size_t bh_queue_capacity(bh_queue_t *queue)
|
||||
size_t BH_QueueCapacity(BH_Queue *queue)
|
||||
{
|
||||
return queue->capacity;
|
||||
}
|
||||
|
||||
|
||||
void *bh_queue_iter_next(bh_queue_t *queue,
|
||||
void *BH_QueueIterNext(BH_Queue *queue,
|
||||
void *iter)
|
||||
{
|
||||
void **element = (void **)iter;
|
||||
@@ -203,7 +203,7 @@ void *bh_queue_iter_next(bh_queue_t *queue,
|
||||
}
|
||||
|
||||
|
||||
void *bh_queue_iter_value(void *iter)
|
||||
void *BH_QueueIterValue(void *iter)
|
||||
{
|
||||
return *(void **)iter;
|
||||
}
|
||||
|
||||
164
src/win32/file.c
164
src/win32/file.c
@@ -4,74 +4,74 @@
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
typedef struct bh_file_s
|
||||
typedef struct BH_File
|
||||
{
|
||||
char *path;
|
||||
int mode;
|
||||
int flags;
|
||||
HANDLE handle;
|
||||
} bh_file_t;
|
||||
} BH_File;
|
||||
|
||||
|
||||
static int file_info(bh_file_t *file,
|
||||
size_t *size,
|
||||
const char **name);
|
||||
static int BH_FileInfo(BH_File *file,
|
||||
size_t *size,
|
||||
const char **name);
|
||||
|
||||
|
||||
static int file_init(bh_file_t *file,
|
||||
const char *path);
|
||||
static int BH_FileInit(BH_File *file,
|
||||
const char *path);
|
||||
|
||||
|
||||
static int file_destroy(bh_file_t *file);
|
||||
static int BH_FileDestroy(BH_File *file);
|
||||
|
||||
|
||||
static int file_open(bh_file_t *file,
|
||||
int *mode);
|
||||
static int BH_FileOpen(BH_File *file,
|
||||
int *mode);
|
||||
|
||||
|
||||
static int file_close(bh_file_t *file);
|
||||
static int BH_FileClose(BH_File *file);
|
||||
|
||||
|
||||
static int file_read(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
static int BH_FileRead(BH_File *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int file_write(bh_file_t *file,
|
||||
const char *data,
|
||||
size_t *size);
|
||||
static int BH_FileWrite(BH_File *file,
|
||||
const char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int file_peek(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
static int BH_FilePeek(BH_File* file,
|
||||
char *data,
|
||||
size_t *size);
|
||||
|
||||
|
||||
static int file_flush(bh_file_t *file);
|
||||
static int BH_FileFlush(BH_File *file);
|
||||
|
||||
|
||||
static int file_seek(bh_file_t *file,
|
||||
int64_t *pos,
|
||||
int *dir);
|
||||
static int BH_FileSeek(BH_File *file,
|
||||
int64_t *pos,
|
||||
int *dir);
|
||||
|
||||
|
||||
static int file_tell(bh_file_t *file,
|
||||
int64_t *pos);
|
||||
static int BH_FileTell(BH_File *file,
|
||||
int64_t *pos);
|
||||
|
||||
|
||||
static int file_size(bh_file_t *file,
|
||||
int64_t *size);
|
||||
static int BH_FileSize(BH_File *file,
|
||||
int64_t *size);
|
||||
|
||||
|
||||
static int file_flags(bh_file_t *file);
|
||||
static int BH_FileFlags(BH_File *file);
|
||||
|
||||
|
||||
static int file_clear(bh_file_t *file);
|
||||
static int BH_FileClear(BH_File *file);
|
||||
|
||||
|
||||
static int file_info(bh_file_t *file,
|
||||
size_t *size,
|
||||
const char **name)
|
||||
static int BH_FileInfo(BH_File *file,
|
||||
size_t *size,
|
||||
const char **name)
|
||||
{
|
||||
static const char classname[] = BH_FILE_CLASSNAME;
|
||||
|
||||
@@ -84,8 +84,8 @@ static int file_info(bh_file_t *file,
|
||||
}
|
||||
|
||||
|
||||
static int file_init(bh_file_t *file,
|
||||
const char *path)
|
||||
static int BH_FileInit(BH_File *file,
|
||||
const char *path)
|
||||
{
|
||||
/* Check if path is valid */
|
||||
if (!path)
|
||||
@@ -101,11 +101,11 @@ static int file_init(bh_file_t *file,
|
||||
}
|
||||
|
||||
|
||||
static int file_destroy(bh_file_t *file)
|
||||
static int BH_FileDestroy(BH_File *file)
|
||||
{
|
||||
/* Close the file handle on destruction */
|
||||
if (file->handle != INVALID_HANDLE_VALUE)
|
||||
file_close(file);
|
||||
BH_FileClose(file);
|
||||
|
||||
/* Free path string */
|
||||
free(file->path);
|
||||
@@ -114,14 +114,14 @@ static int file_destroy(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_open(bh_file_t *file,
|
||||
int *mode)
|
||||
static int BH_FileOpen(BH_File *file,
|
||||
int *mode)
|
||||
{
|
||||
DWORD access = 0, how = 0;
|
||||
|
||||
/* Check if file is already openned */
|
||||
if (file->handle != INVALID_HANDLE_VALUE)
|
||||
return BH_OK;
|
||||
return BH_ERROR;
|
||||
|
||||
/* Determine read/write access flags */
|
||||
if (*mode & BH_IO_READ)
|
||||
@@ -169,7 +169,7 @@ static int file_open(bh_file_t *file,
|
||||
}
|
||||
|
||||
|
||||
static int file_close(bh_file_t *file)
|
||||
static int BH_FileClose(BH_File *file)
|
||||
{
|
||||
/* If file is opened - close it */
|
||||
if (file->handle == INVALID_HANDLE_VALUE)
|
||||
@@ -183,9 +183,9 @@ static int file_close(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_read(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
static int BH_FileRead(BH_File *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
{
|
||||
DWORD readed;
|
||||
|
||||
@@ -212,9 +212,9 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_write(bh_file_t *file,
|
||||
const char *data,
|
||||
size_t *size)
|
||||
static int BH_FileWrite(BH_File *file,
|
||||
const char *data,
|
||||
size_t *size)
|
||||
{
|
||||
DWORD written;
|
||||
|
||||
@@ -251,28 +251,28 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_peek(bh_file_t *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
static int BH_FilePeek(BH_File *file,
|
||||
char *data,
|
||||
size_t *size)
|
||||
{
|
||||
int64_t position;
|
||||
int direction;
|
||||
|
||||
/* Read data from the file */
|
||||
if (file_read(file, data, size))
|
||||
if (BH_FileRead(file, data, size))
|
||||
return BH_ERROR;
|
||||
|
||||
/* Backtrack by the read amount */
|
||||
position = -((int64_t)*size);
|
||||
direction = BH_IO_SEEK_CUR;
|
||||
if (file_seek(file, &position, &direction))
|
||||
if (BH_FileSeek(file, &position, &direction))
|
||||
return BH_ERROR;
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
static int file_flush(bh_file_t *file)
|
||||
static int BH_FileFlush(BH_File *file)
|
||||
{
|
||||
/* Check if file is opened */
|
||||
if (file->handle == INVALID_HANDLE_VALUE)
|
||||
@@ -288,9 +288,9 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_seek(bh_file_t *file,
|
||||
int64_t *pos,
|
||||
int *dir)
|
||||
static int BH_FileSeek(BH_File *file,
|
||||
int64_t *pos,
|
||||
int *dir)
|
||||
{
|
||||
LARGE_INTEGER position;
|
||||
|
||||
@@ -311,8 +311,8 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_tell(bh_file_t *file,
|
||||
int64_t *pos)
|
||||
static int BH_FileTell(BH_File *file,
|
||||
int64_t *pos)
|
||||
{
|
||||
LARGE_INTEGER dummy, position;
|
||||
|
||||
@@ -334,8 +334,8 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_size(bh_file_t *file,
|
||||
int64_t *size)
|
||||
static int BH_FileSize(BH_File *file,
|
||||
int64_t *size)
|
||||
{
|
||||
LARGE_INTEGER dummy;
|
||||
|
||||
@@ -356,7 +356,7 @@ error:
|
||||
}
|
||||
|
||||
|
||||
static int file_flags(bh_file_t *file)
|
||||
static int BH_FileFlags(BH_File *file)
|
||||
{
|
||||
/* If file handle is valid - append IO_OPEN flag */
|
||||
if (file->handle != INVALID_HANDLE_VALUE)
|
||||
@@ -365,7 +365,7 @@ static int file_flags(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_clear(bh_file_t *file)
|
||||
static int BH_FileClear(BH_File *file)
|
||||
{
|
||||
/* Clear IO_ERROR flag */
|
||||
file->flags &= ~BH_IO_FLAG_ERROR;
|
||||
@@ -373,33 +373,33 @@ static int file_clear(bh_file_t *file)
|
||||
}
|
||||
|
||||
|
||||
static int file_proc(bh_file_t *file,
|
||||
int type,
|
||||
void *arg1,
|
||||
void *arg2)
|
||||
static int BH_FileCallback(BH_File *file,
|
||||
int type,
|
||||
void *arg1,
|
||||
void *arg2)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2);
|
||||
case BH_IO_INIT_CB: return file_init(file, (const char *)arg1);
|
||||
case BH_IO_DESTROY_CB: return file_destroy(file);
|
||||
case BH_IO_OPEN_CB: return file_open(file, (int *)arg1);
|
||||
case BH_IO_CLOSE_CB: return file_close(file);
|
||||
case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_FLUSH_CB: return file_flush(file);
|
||||
case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2);
|
||||
case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1);
|
||||
case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1);
|
||||
case BH_IO_FLAGS_CB: return file_flags(file);
|
||||
case BH_IO_CLEAR_CB: return file_clear(file);
|
||||
case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2);
|
||||
case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1);
|
||||
case BH_IO_DESTROY_CB: return BH_FileDestroy(file);
|
||||
case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1);
|
||||
case BH_IO_CLOSE_CB: return BH_FileClose(file);
|
||||
case BH_IO_READ_CB: return BH_FileRead(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_WRITE_CB: return BH_FileWrite(file, (const char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_PEEK_CB: return BH_FilePeek(file, (char *)arg1, (size_t *)arg2);
|
||||
case BH_IO_FLUSH_CB: return BH_FileFlush(file);
|
||||
case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2);
|
||||
case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1);
|
||||
case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1);
|
||||
case BH_IO_FLAGS_CB: return BH_FileFlags(file);
|
||||
case BH_IO_CLEAR_CB: return BH_FileClear(file);
|
||||
default: return BH_NOIMPL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bh_io_t *bh_file_new(const char *path)
|
||||
BH_IO *BH_FileNew(const char *path)
|
||||
{
|
||||
return bh_io_new((bh_io_func_t)file_proc, (void *)path);
|
||||
return BH_IONew((BH_IOCallback)BH_FileCallback, (void *)path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user