From 55ab2437957c757aab93c5c487d18709007a6866 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Mon, 8 Apr 2013 16:34:58 -0400 Subject: [PATCH] Convert to standard types (C99) --- source/buffer/buf.c | 14 +++++++------- source/buffer/buf.h | 12 +++++++----- source/list/sll.c | 20 ++++++++++---------- source/list/sll.h | 18 ++++++++++-------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/source/buffer/buf.c b/source/buffer/buf.c index bf44e00..76b64ec 100644 --- a/source/buffer/buf.c +++ b/source/buffer/buf.c @@ -21,7 +21,7 @@ buf_t* buf_new(size_t size) return buf; } -void buf_free(buf_t* buf, int free_contents) +void buf_free(buf_t* buf, bool free_contents) { buf_clear(buf,free_contents); free( buf->buffer ); @@ -33,19 +33,19 @@ size_t buf_size(buf_t* buf) return (size_t)buf->size; } -int buf_empty(buf_t* buf) +bool buf_empty(buf_t* buf) { return (buf->reads == buf->writes); } -int buf_full(buf_t* buf) +bool buf_full(buf_t* buf) { int full = !buf_empty(buf); full &= ((buf->reads % buf->size) == (buf->writes % buf->size)); return full; } -void buf_clear(buf_t* buf, int free_contents) +void buf_clear(buf_t* buf, bool free_contents) { if (free_contents) { @@ -69,14 +69,14 @@ void* buf_read(buf_t* buf) return data; } -int buf_write(buf_t* buf, void* data) +bool buf_write(buf_t* buf, void* data) { - int success = 0; + bool success = false; if (!buf_full(buf)) { buf->buffer[ buf->writes % buf->size ] = data; buf->writes++; - success = 1; + success = true; } return success; } diff --git a/source/buffer/buf.h b/source/buffer/buf.h index 165795d..773d34b 100644 --- a/source/buffer/buf.h +++ b/source/buffer/buf.h @@ -11,6 +11,8 @@ extern "C" { #endif +#include + /** A structure defining a circular buffer */ typedef struct { void** buffer; /**< Pointer to the buffer */ @@ -34,7 +36,7 @@ buf_t* buf_new(size_t size); * @param buf The buffer to free. * @param free_contents Whether unread contents should also be freed. */ -void buf_free(buf_t* buf, int free_contents); +void buf_free(buf_t* buf, bool free_contents); /** * @brief Returns the size of the provided buffer. @@ -52,7 +54,7 @@ size_t buf_size(buf_t* buf); * * @return 1 if the buffer is empty 0 otherwise. */ -int buf_empty(buf_t* buf); +bool buf_empty(buf_t* buf); /** * @brief Returns whether the buffer is full. @@ -61,7 +63,7 @@ int buf_empty(buf_t* buf); * * @return 1 if the buffer is full 0 otherwise. */ -int buf_full(buf_t* buf); +bool buf_full(buf_t* buf); /** * @brief Clears all unread data from the provided buffer. @@ -69,7 +71,7 @@ int buf_full(buf_t* buf); * @param buf The buffer to clear. * @param free_contents Whether the unread contents should also be freed. */ -void buf_clear(buf_t* buf, int free_contents); +void buf_clear(buf_t* buf, bool free_contents); /** * @brief Reads an item from the provided buffer. @@ -88,7 +90,7 @@ void* buf_read(buf_t* buf); * * @return 1 on successful write 0 otherwise. */ -int buf_write(buf_t* buf, void* data); +bool buf_write(buf_t* buf, void* data); #ifdef __cplusplus } diff --git a/source/list/sll.c b/source/list/sll.c index 550fee2..66ddf4c 100644 --- a/source/list/sll.c +++ b/source/list/sll.c @@ -17,7 +17,7 @@ sll_node_t* sll_new_node(void* contents) return node; } -void sll_free(sll_t* list, int free_contents) +void sll_free(sll_t* list, bool free_contents) { sll_node_t* node = list->head; while( NULL != node ) @@ -29,7 +29,7 @@ void sll_free(sll_t* list, int free_contents) free( list ); } -void sll_free_node(sll_node_t* node, int free_contents) +void sll_free_node(sll_node_t* node, bool free_contents) { if( free_contents ) { @@ -48,9 +48,9 @@ sll_node_t* sll_back( sll_t* list ) return list->tail; } -unsigned int sll_size(sll_t* list) +size_t sll_size(sll_t* list) { - unsigned int length = 0; + size_t length = 0; sll_node_t* node = list->head; while( NULL != node ) { @@ -60,16 +60,16 @@ unsigned int sll_size(sll_t* list) return length; } -int sll_empty(sll_t* list) +bool sll_empty(sll_t* list) { return ((NULL == list->head) && (NULL == list->tail)); } -sll_node_t* sll_at(sll_t* list, unsigned int index) +sll_node_t* sll_at(sll_t* list, size_t index) { sll_node_t* node = NULL; - unsigned int cur_index = 0; + size_t cur_index = 0; sll_node_t* cur_node = list->head; while( NULL != cur_node ) { @@ -151,7 +151,7 @@ sll_node_t* sll_pop_back( sll_t* list ) return node; } -sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents) +sll_node_t* sll_insert( sll_t* list, size_t index, void* contents) { sll_node_t* new_node = NULL; if( 0 == index ) @@ -176,7 +176,7 @@ sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents) return new_node; } -sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents) +sll_node_t* sll_delete( sll_t* list, size_t index, bool free_contents) { sll_node_t* node = NULL; @@ -208,7 +208,7 @@ sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents) return node; } -sll_t* sll_clear(sll_t* list, int free_contents) +sll_t* sll_clear(sll_t* list, bool free_contents) { sll_node_t* node = list->head; while(NULL != node) diff --git a/source/list/sll.h b/source/list/sll.h index 9e0e3fb..b49443c 100644 --- a/source/list/sll.h +++ b/source/list/sll.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + /** A linked list node. */ typedef struct sll_node_t { @@ -48,7 +50,7 @@ sll_node_t* sll_new_node(void* contents); * @param list The list to be freed. * @param free_contents Whether or not to also free the contents of each node. **/ -void sll_free(sll_t* list, int free_contents); +void sll_free(sll_t* list, bool free_contents); /** * @brief Frees all memory used by a node. @@ -59,7 +61,7 @@ void sll_free(sll_t* list, int free_contents); * @param node * @param free_contents */ -void sll_free_node(sll_node_t* node, int free_contents); +void sll_free_node(sll_node_t* node, bool free_contents); /** * @brief Returns pointer to first node in the list @@ -89,7 +91,7 @@ sll_node_t* sll_back( sll_t* list ); * * @return The number of elements in the list. **/ -unsigned int sll_size(sll_t* list); +size_t sll_size(sll_t* list); /** * @brief Returns whether the list is empty or not. @@ -98,7 +100,7 @@ unsigned int sll_size(sll_t* list); * * @return Whether the list is empty, 1 for true and 0 for false. */ -int sll_empty(sll_t* list); +bool sll_empty(sll_t* list); /** * @brief Return the node at the specified index in a linked list. @@ -111,7 +113,7 @@ int sll_empty(sll_t* list); * * @return A pointer to the node at the supplied index, NULL if out of range. **/ -sll_node_t* sll_at(sll_t* list, unsigned int index); +sll_node_t* sll_at(sll_t* list, size_t index); /** * @brief Adds a new node to the front of an existing linked list. @@ -176,7 +178,7 @@ sll_node_t* sll_pop_back( sll_t* list ); * * @return Pointer to the newly inserted node, NULL if index is out of range. **/ -sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents); +sll_node_t* sll_insert( sll_t* list, size_t index, void* contents); /** * @brief Deletes a node from the supplied list. @@ -192,7 +194,7 @@ sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents); * * @return Pointer to the node that is now at the supplied index. **/ -sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents); +sll_node_t* sll_delete( sll_t* list, size_t index, bool free_contents); /** * @brief Deletes all elements in the provided list @@ -202,7 +204,7 @@ sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents); * * @return A pointer to the cleared list. */ -sll_t* sll_clear(sll_t* list, int free_contents); +sll_t* sll_clear(sll_t* list, bool free_contents); #ifdef __cplusplus } -- 2.52.0