OS Abstraction

Summary
OS Abstraction
Debug
dbg_assertBreaks into the debugger based on a test.
dbg_breakBreaks into the debugger.
dbg_msgPrints a debug message.
Memory
mem_allocAllocates memory.
mem_freeFrees a block allocated through mem_alloc.
mem_copyCopies a a memory block.
mem_moveCopies a a memory block
mem_zeroSets a complete memory block to 0
mem_compCompares two blocks of memory
File IO
io_openOpens a file.
io_readReads data into a buffer from a file.
io_skipSkips data in a file.
io_writeWrites data from a buffer to file.
io_seekSeeks to a specified offset in the file.
io_tellGets the current position in the file.
io_lengthGets the total length of the file.
io_closeCloses a file.
io_flushEmpties all buffers and writes all pending data.
io_stdinReturns an <IOHANDLE> to the standard input.
io_stdoutReturns an <IOHANDLE> to the standard output.
io_stderrReturns an <IOHANDLE> to the standard error.
Threads
thread_sleepSuspends the current thread for a given period.
thread_createCreates a new thread.
thread_waitWaits for a thread to be done or destroyed.
thread_destoyDestroys a thread.
thread_yeildYeild the current threads execution slice.
Locks
Timer
time_getFetches a sample from a high resolution timer.
time_freqReturns the frequency of the high resolution timer.
time_timestampRetrives the current time as a UNIX timestamp
Network General
net_initInitiates network functionallity.
net_host_lookupDoes a hostname lookup by name and fills out the passed NETADDR struct with the recieved details.
net_addr_compCompares two network addresses.
net_addr_strTurns a network address into a representive string.
Network UDP
net_udp_createCreates a UDP socket and binds it to a port.
net_udp_sendSends a packet over an UDP socket.
net_udp_recvRecives a packet over an UDP socket.
net_udp_closeCloses an UDP socket.
Network TCP
net_tcp_createCreates a TCP socket.
net_tcp_listenMakes the socket start listening for new connections.
net_tcp_acceptPolls a listning socket for a new connection.
net_tcp_connectConnects one socket to another.
net_tcp_sendSends data to a TCP stream.
net_tcp_recvRecvives data from a TCP stream.
net_tcp_closeCloses a TCP socket.
Strings
str_appendAppends a string to another.
str_copyCopies a string to another.
str_formatPerforms printf formating into a buffer.
str_sanitize_strongReplaces all characters below 32 and above 127 with whitespace.
str_sanitizeReplaces all characters below 32 and above 127 with whitespace with exception to \r, \n and \r.
str_comp_nocaseCompares to strings case insensitive.
str_find_nocaseFinds a string inside another string case insensitive.
str_hexTakes a datablock and generates a hexstring of it.
Filesystem
fs_listdirLists the files in a directory
fs_makedirCreates a directory
fs_storage_pathFetches per user configuration directory.
Undocumented
net_tcp_connect_non_blockingDOCTODO: serp
net_tcp_set_non_blockingDOCTODO: serp
net_tcp_set_non_blockingDOCTODO: serp
net_errnoDOCTODO: serp
net_would_blockDOCTODO: serp

Debug

dbg_assert

void dbg_assert(int test,
const char *msg)

Breaks into the debugger based on a test.

Parameters

testResult of the test.
msgMessage that should be printed if the test fails.

Remarks

Does nothing in release version of the library.

See Also

dbg_break

dbg_break

void dbg_break()

Breaks into the debugger.

Remarks

Does nothing in release version of the library.

See Also

dbg_assert

dbg_msg

void dbg_msg(const char *sys,
const char *fmt,
 ...)

Prints a debug message.

Parameters

sysA string that describes what system the message belongs to
fmtA printf styled format string.

Remarks

Does nothing in relase version of the library.

See Also

dbg_assert

Memory

mem_alloc

void *mem_alloc_debug(const char *filename,
int line,
unsigned size,
unsigned alignment)

Allocates memory.

Parameters

sizeSize of the needed block.
alignmentAlignment for the block.

Returns

Returns a pointer to the newly allocated block.  Returns a null pointer if the memory couldn’t be allocated.

Remarks

  • Passing 0 to size will allocated the smallest amount possible and return a unique pointer.

See Also

mem_free

mem_free

void mem_free(void *block)

Frees a block allocated through mem_alloc.

Remarks

  • In the debug version of the library the function will assert if a non-valid block is passed, like a null pointer or a block that isn’t allocated.

See Also

mem_alloc

mem_copy

void mem_copy(void *dest,
const void *source,
unsigned size)

Copies a a memory block.

Parameters

destDestination.
sourceSource to copy.
sizeSize of the block to copy.

Remarks

  • This functions DOES NOT handles cases where source and destination is overlapping.

See Also

mem_move

mem_move

void mem_move(void *dest,
const void *source,
unsigned size)

Copies a a memory block

Parameters

destDestination
sourceSource to copy
sizeSize of the block to copy

Remarks

  • This functions handles cases where source and destination is overlapping

See Also

mem_copy

mem_zero

void mem_zero(void *block,
unsigned size)

Sets a complete memory block to 0

Parameters

blockPointer to the block to zero out
sizeSize of the block

mem_comp

int mem_comp(const void *a,
const void *b,
int size)

Compares two blocks of memory

Parameters

aFirst block of data
bSecond block of data
sizeSize of the data to compare

Returns

<0Block a is lesser then block b
0Block a is equal to block b
0 - Block a is greater then block b

File IO

io_open

IOHANDLE io_open(const char *filename,
int flags)

Opens a file.

Parameters

filenameFile to open.
flagsA set of flags.  IOFLAG_READ, IOFLAG_WRITE, IOFLAG_RANDOM.

Returns

Returns a handle to the file on success and 0 on failure.

io_read

unsigned io_read(IOHANDLE io,
void *buffer,
unsigned size)

Reads data into a buffer from a file.

Parameters

ioHandle to the file to read data from.
bufferPointer to the buffer that will recive the data.
sizeNumber of bytes to read from the file.

Returns

Number of bytes read.

io_skip

unsigned io_skip(IOHANDLE io,
unsigned size)

Skips data in a file.

Parameters

ioHandle to the file.
sizeNumber of bytes to skip.

Returns

Number of bytes skipped.

io_write

unsigned io_write(IOHANDLE io,
const void *buffer,
unsigned size)

Writes data from a buffer to file.

Parameters

ioHandle to the file.
bufferPointer to the data that should be written.
sizeNumber of bytes to write.

Returns

Number of bytes written.

io_seek

int io_seek(IOHANDLE io,
int offset,
int origin)

Seeks to a specified offset in the file.

Parameters

ioHandle to the file.
offsetOffset from pos to stop.
originPosition to start searching from.

Returns

Returns 0 on success.

io_tell

long int io_tell(IOHANDLE io)

Gets the current position in the file.

Parameters

ioHandle to the file.

Returns

Returns the current position.  -1L if an error occured.

io_length

long int io_length(IOHANDLE io)

Gets the total length of the file.  Resetting cursor to the beginning

Parameters

ioHandle to the file.

Returns

Returns the total size.  -1L if an error occured.

io_close

int io_close(IOHANDLE io)

Closes a file.

Parameters

ioHandle to the file.

Returns

Returns 0 on success.

io_flush

int io_flush(IOHANDLE io)

Empties all buffers and writes all pending data.

Parameters

ioHandle to the file.

Returns

Returns 0 on success.

io_stdin

IOHANDLE io_stdin()

Returns an <IOHANDLE> to the standard input.

io_stdout

IOHANDLE io_stdout()

Returns an <IOHANDLE> to the standard output.

io_stderr

IOHANDLE io_stderr()

Returns an <IOHANDLE> to the standard error.

Threads

thread_sleep

void thread_sleep(int milliseconds)

Suspends the current thread for a given period.

Parameters

millisecondsNumber of milliseconds to sleep.

thread_create

void *thread_create(void (*threadfunc)(void *),
void *user)

Creates a new thread.

Parameters

threadfuncEntry point for the new thread.
userPointer to pass to the thread.

thread_wait

void thread_wait(void *thread)

Waits for a thread to be done or destroyed.

Parameters

threadThread to wait for.

thread_destoy

Destroys a thread.

Parameters

threadThread to destroy.

thread_yeild

Yeild the current threads execution slice.

Locks

Timer

time_get

int64 time_get()

Fetches a sample from a high resolution timer.

Returns

Current value of the timer.

Remarks

To know how fast the timer is ticking, see time_freq.

time_freq

int64 time_freq()

Returns the frequency of the high resolution timer.

Returns

Returns the frequency of the high resolution timer.

time_timestamp

unsigned time_timestamp()

Retrives the current time as a UNIX timestamp

Returns

The time as a UNIX timestamp

Network General

net_init

int net_init()

Initiates network functionallity.

Returns

Returns 0 on success,

Remarks

You must call this function before using any other network functions.

net_host_lookup

int net_host_lookup(const char *hostname,
NETADDR *addr,
int types)

Does a hostname lookup by name and fills out the passed NETADDR struct with the recieved details.

Returns

0 on success.

net_addr_comp

int net_addr_comp(const NETADDR *a,
const NETADDR *b)

Compares two network addresses.

Parameters

aAddress to compare
bAddress to compare to.

Returns

<0Address a is lesser then address b
0Address a is equal to address b
0 - Address a is greater then address b

net_addr_str

int net_addr_str(const NETADDR *addr,
char *string,
int max_length)

Turns a network address into a representive string.

Parameters

addrAddress to turn into a string.
stringBuffer to fill with the string.
max_lengthMaximum size of the string.

Remarks

  • The string will always be zero terminated

Network UDP

net_udp_create

NETSOCKET net_udp_create(NETADDR bindaddr)

Creates a UDP socket and binds it to a port.

Parameters

bindaddrAddress to bind the socket to.

Returns

On success it returns an handle to the socket.  On failure it returns NETSOCKET_INVALID.

net_udp_send

int net_udp_send(NETSOCKET sock,
const NETADDR *addr,
const void *data,
int size)

Sends a packet over an UDP socket.

Parameters

sockSocket to use.
addrWhere to send the packet.
dataPointer to the packet data to send.
sizeSize of the packet.

Returns

On success it returns the number of bytes sent.  Returns -1 on error.

net_udp_recv

int net_udp_recv(NETSOCKET sock,
NETADDR *addr,
void *data,
int maxsize)

Recives a packet over an UDP socket.

Parameters

sockSocket to use.
addrPointer to an NETADDR that will recive the address.
dataPointer to a buffer that will recive the data.
maxsizeMaximum size to recive.

Returns

On success it returns the number of bytes recived.  Returns -1 on error.

net_udp_close

int net_udp_close(NETSOCKET sock)

Closes an UDP socket.

Parameters

sockSocket to close.

Returns

Returns 0 on success.  -1 on error.

Network TCP

net_tcp_create

NETSOCKET net_tcp_create(const NETADDR *a)

Creates a TCP socket.

Parameters

bindaddrAddress to bind the socket to.

Returns

On success it returns an handle to the socket.  On failure it returns NETSOCKET_INVALID.

net_tcp_listen

int net_tcp_listen(NETSOCKET sock,
int backlog)

Makes the socket start listening for new connections.

Parameters

sockSocket to start listen to.
backlogSize of the queue of incomming connections to keep.

Returns

Returns 0 on success.

net_tcp_accept

int net_tcp_accept(NETSOCKET sock,
NETSOCKET *new_sock,
NETADDR *addr)

Polls a listning socket for a new connection.

Parameters

sockListning socket to poll.
new_sockPointer to a socket to fill in with the new socket.
addrPointer to an address that will be filled in the remote address (optional, can be NULL).

Returns

Returns a non-negative integer on success.  Negative integer on failure.

net_tcp_connect

int net_tcp_connect(NETSOCKET sock,
const NETADDR *addr)

Connects one socket to another.

Parameters

sockSocket to connect.
addrAddress to connect to.

Returns

Returns 0 on success.

net_tcp_send

int net_tcp_send(NETSOCKET sock,
const void *data,
int size)

Sends data to a TCP stream.

Parameters

sockSocket to send data to.
dataPointer to the data to send.
sizeSize of the data to send.

Returns

Number of bytes sent.  Negative value on failure.

net_tcp_recv

int net_tcp_recv(NETSOCKET sock,
void *data,
int maxsize)

Recvives data from a TCP stream.

Parameters

sockSocket to recvive data from.
dataPointer to a buffer to write the data to
max_sizeMaximum of data to write to the buffer.

Returns

Number of bytes recvived.  Negative value on failure.

net_tcp_close

int net_tcp_close(NETSOCKET sock)

Closes a TCP socket.

Parameters

sockSocket to close.

Returns

Returns 0 on success.  Negative value on failure.

Strings

str_append

void str_append(char *dst,
const char *src,
int dst_size)

Appends a string to another.

Parameters

dstPointer to a buffer that contains a string.
srcString to append.
dst_sizeSize of the buffer of the dst string.

Remarks

  • The strings are treated as zero-termineted strings.
  • Garantees that dst string will contain zero-termination.

str_copy

void str_copy(char *dst,
const char *src,
int dst_size)

Copies a string to another.

Parameters

dstPointer to a buffer that shall recive the string.
srcString to be copied.
dst_sizeSize of the buffer dst.

Remarks

  • The strings are treated as zero-termineted strings.
  • Garantees that dst string will contain zero-termination.

str_format

void str_format(char *buffer,
int buffer_size,
const char *format,
 ...)

Performs printf formating into a buffer.

Parameters

bufferPointer to the buffer to recive the formated string.
buffer_sizeSize of the buffer.
formatprintf formating string.
...Parameters for the formating.

Remarks

  • See the C manual for syntax for the printf formating string.
  • The strings are treated as zero-termineted strings.
  • Garantees that dst string will contain zero-termination.

str_sanitize_strong

void str_sanitize_strong(char *str)

Replaces all characters below 32 and above 127 with whitespace.

Parameters

strString to sanitize.

Remarks

  • The strings are treated as zero-termineted strings.

str_sanitize

void str_sanitize(char *str)

Replaces all characters below 32 and above 127 with whitespace with exception to \r, \n and \r.

Parameters

strString to sanitize.

Remarks

  • The strings are treated as zero-termineted strings.

str_comp_nocase

int str_comp_nocase(const char *a,
const char *b)

Compares to strings case insensitive.

Parameters

aString to compare.
bString to compare.

Returns

<0String a is lesser then string b
0String a is equal to string b
0 - String a is greater then string b

Remarks

  • Only garanted to work with a-z/A-Z.
  • The strings are treated as zero-termineted strings.

str_find_nocase

const char *str_find_nocase(const char *haystack,
const char *needle)

Finds a string inside another string case insensitive.

Parameters

haystackString to search in
needleString to search for

Returns

A pointer into haystack where the needle was found.  Returns NULL of needle could not be found.

Remarks

  • Only garanted to work with a-z/A-Z.
  • The strings are treated as zero-termineted strings.

str_hex

void str_hex(char *dst,
int dst_size,
const void *data,
int data_size)

Takes a datablock and generates a hexstring of it.

Parameters

dstBuffer to fill with hex data
dst_sizesize of the buffer
dataData to turn into hex
dataSize of the data

Remarks

  • The desination buffer will be zero-terminated

Filesystem

fs_listdir

typedef void (*fs_listdir_callback)(const char *name, int is_dir, void *user)

Lists the files in a directory

Parameters

dirDirectory to list
cbCallback function to call for each entry
userPointer to give to the callback

Returns

Always returns 0.

fs_makedir

int fs_makedir(const char *path)

Creates a directory

Parameters

pathDirectory to create

Returns

Returns 0 on success.  Negative value on failure.

Remarks

Does not create several directories if needed.  “a/b/c” will result in a failure if b or a does not exist.

fs_storage_path

int fs_storage_path(const char *appname,
char *path,
int max)

Fetches per user configuration directory.

Returns

Returns 0 on success.  Negative value on failure.

Remarks

  • Returns ~/.appname on UNIX based systems
  • Returns ~/Library/Applications Support/appname on Mac OS X
  • Returns %APPDATA%/Appname on Windows based systems

Undocumented

net_tcp_connect_non_blocking

int net_tcp_connect_non_blocking(NETSOCKET sock,
const NETADDR *a)

DOCTODO: serp

net_tcp_set_non_blocking

int net_tcp_set_non_blocking(NETSOCKET sock)

DOCTODO: serp

net_tcp_set_non_blocking

DOCTODO: serp

net_errno

int net_errno()

DOCTODO: serp

net_would_block

int net_would_block()

DOCTODO: serp

void dbg_assert(int test,
const char *msg)
Breaks into the debugger based on a test.
void dbg_break()
Breaks into the debugger.
void dbg_msg(const char *sys,
const char *fmt,
 ...)
Prints a debug message.
void *mem_alloc_debug(const char *filename,
int line,
unsigned size,
unsigned alignment)
Allocates memory.
void mem_free(void *block)
Frees a block allocated through mem_alloc.
void mem_copy(void *dest,
const void *source,
unsigned size)
Copies a a memory block.
void mem_move(void *dest,
const void *source,
unsigned size)
Copies a a memory block
void mem_zero(void *block,
unsigned size)
Sets a complete memory block to 0
int mem_comp(const void *a,
const void *b,
int size)
Compares two blocks of memory
IOHANDLE io_open(const char *filename,
int flags)
Opens a file.
unsigned io_read(IOHANDLE io,
void *buffer,
unsigned size)
Reads data into a buffer from a file.
unsigned io_skip(IOHANDLE io,
unsigned size)
Skips data in a file.
unsigned io_write(IOHANDLE io,
const void *buffer,
unsigned size)
Writes data from a buffer to file.
int io_seek(IOHANDLE io,
int offset,
int origin)
Seeks to a specified offset in the file.
long int io_tell(IOHANDLE io)
Gets the current position in the file.
long int io_length(IOHANDLE io)
Gets the total length of the file.
int io_close(IOHANDLE io)
Closes a file.
int io_flush(IOHANDLE io)
Empties all buffers and writes all pending data.
IOHANDLE io_stdin()
Returns an IOHANDLE to the standard input.
IOHANDLE io_stdout()
Returns an IOHANDLE to the standard output.
IOHANDLE io_stderr()
Returns an IOHANDLE to the standard error.
void thread_sleep(int milliseconds)
Suspends the current thread for a given period.
void *thread_create(void (*threadfunc)(void *),
void *user)
Creates a new thread.
void thread_wait(void *thread)
Waits for a thread to be done or destroyed.
int64 time_get()
Fetches a sample from a high resolution timer.
int64 time_freq()
Returns the frequency of the high resolution timer.
unsigned time_timestamp()
Retrives the current time as a UNIX timestamp
int net_init()
Initiates network functionallity.
int net_host_lookup(const char *hostname,
NETADDR *addr,
int types)
Does a hostname lookup by name and fills out the passed NETADDR struct with the recieved details.
int net_addr_comp(const NETADDR *a,
const NETADDR *b)
Compares two network addresses.
int net_addr_str(const NETADDR *addr,
char *string,
int max_length)
Turns a network address into a representive string.
NETSOCKET net_udp_create(NETADDR bindaddr)
Creates a UDP socket and binds it to a port.
int net_udp_send(NETSOCKET sock,
const NETADDR *addr,
const void *data,
int size)
Sends a packet over an UDP socket.
int net_udp_recv(NETSOCKET sock,
NETADDR *addr,
void *data,
int maxsize)
Recives a packet over an UDP socket.
int net_udp_close(NETSOCKET sock)
Closes an UDP socket.
NETSOCKET net_tcp_create(const NETADDR *a)
Creates a TCP socket.
int net_tcp_listen(NETSOCKET sock,
int backlog)
Makes the socket start listening for new connections.
int net_tcp_accept(NETSOCKET sock,
NETSOCKET *new_sock,
NETADDR *addr)
Polls a listning socket for a new connection.
int net_tcp_connect(NETSOCKET sock,
const NETADDR *addr)
Connects one socket to another.
int net_tcp_send(NETSOCKET sock,
const void *data,
int size)
Sends data to a TCP stream.
int net_tcp_recv(NETSOCKET sock,
void *data,
int maxsize)
Recvives data from a TCP stream.
int net_tcp_close(NETSOCKET sock)
Closes a TCP socket.
void str_append(char *dst,
const char *src,
int dst_size)
Appends a string to another.
void str_copy(char *dst,
const char *src,
int dst_size)
Copies a string to another.
void str_format(char *buffer,
int buffer_size,
const char *format,
 ...)
Performs printf formating into a buffer.
void str_sanitize_strong(char *str)
Replaces all characters below 32 and above 127 with whitespace.
void str_sanitize(char *str)
Replaces all characters below 32 and above 127 with whitespace with exception to \r, \n and \r.
int str_comp_nocase(const char *a,
const char *b)
Compares to strings case insensitive.
const char *str_find_nocase(const char *haystack,
const char *needle)
Finds a string inside another string case insensitive.
void str_hex(char *dst,
int dst_size,
const void *data,
int data_size)
Takes a datablock and generates a hexstring of it.
typedef void (*fs_listdir_callback)(const char *name, int is_dir, void *user)
Lists the files in a directory
int fs_makedir(const char *path)
Creates a directory
int fs_storage_path(const char *appname,
char *path,
int max)
Fetches per user configuration directory.
int net_tcp_connect_non_blocking(NETSOCKET sock,
const NETADDR *a)
DOCTODO: serp
int net_tcp_set_non_blocking(NETSOCKET sock)
DOCTODO: serp
int net_errno()
DOCTODO: serp
int net_would_block()
DOCTODO: serp
Close