Commit 0809995e authored by Chris Müller's avatar Chris Müller

move header into its folder. Small refactorings in naming conventions

parent d6691d7d
......@@ -8,7 +8,7 @@ set(VERSION "0.1")
include(${PROJECT_SOURCE_DIR}/macros/standard.cmake)
include_directories(src)
include_directories(include)
add_subdirectory(src)
add_subdirectory(test)
......
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_STRUCTURES_ARRAY_H
#define CRYSTAL_STRUCTURES_ARRAY_H
#pragma once
/**
* @file structures/array.h
......@@ -52,10 +50,10 @@ size_t cry_array_ensure(struct CryArray* array, size_t min_capa
size_t cry_array_trim(struct CryArray* array);
void cry_array_append(struct CryArray* array, const_pointer data, size_t size);
return_code cry_array_insert(struct CryArray* array, size_t index, const_pointer data, size_t size);
return_code cry_array_remove(struct CryArray* array, size_t index, size_t size);
return_code cry_array_replace(struct CryArray* array, size_t index, const_pointer data, size_t size);
void cry_array_clear(struct CryArray* array);
int cry_array_insert(struct CryArray* array, size_t index, const_pointer data, size_t size);
int cry_array_remove(struct CryArray* array, size_t index, size_t size);
int cry_array_replace(struct CryArray* array, size_t index, const_pointer data, size_t size);
void cry_array_clear(struct CryArray* array);
pointer cry_array_clone(struct CryArray* array);
pointer cry_array_get(struct CryArray* array, size_t index);
......@@ -73,14 +71,13 @@ size_t cry_ptr_array_trim(struct CryPtrArray* array);
void cry_ptr_array_append(struct CryPtrArray* array, pointer ptr);
return_code cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr);
int cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr);
pointer cry_ptr_array_remove(struct CryPtrArray* array, size_t index);
pointer cry_ptr_array_replace(struct CryPtrArray* array, size_t index, pointer ptr);
void cry_ptr_array_clear(struct CryPtrArray* array, cry_free_funptr fun);
pointer cry_ptr_array_replace(struct CryPtrArray* array, size_t index, pointer ptr);
void cry_ptr_array_clear(struct CryPtrArray* array, cry_free_funptr fun);
pointer cry_ptr_array_get(struct CryPtrArray* array, size_t index);
void cry_ptr_array_each(struct CryPtrArray* array, cry_foreach_funptr fun, pointer data);
void cry_ptr_array_each(struct CryPtrArray* array, cry_foreach_funptr fun, pointer data);
#endif // CRYSTAL_STRUCTURES_ARRAY_H
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_STRUCTURES_HEAP_H
#define CRYSTAL_STRUCTURES_HEAP_H
#pragma once
#include "structures.h"
......@@ -37,4 +35,3 @@ pointer cry_heap_pop(struct CryHeap* heap);
#endif // CRYSTAL_STRUCTURES_HEAP_H
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_STRUCTURES_LIST_H
#define CRYSTAL_STRUCTURES_LIST_H
#pragma once
#include "structures.h"
......@@ -28,8 +26,8 @@ struct CryList* cry_list_new(void);
void cry_list_free(struct CryList* list, cry_free_funptr handler);
size_t cry_list_size(struct CryList* list);
pointer cry_list_head(struct CryList* list);
pointer cry_list_tail(struct CryList* list);
pointer cry_list_head(struct CryList* list);
pointer cry_list_tail(struct CryList* list);
void cry_list_append(struct CryList* list, pointer data);
void cry_list_prepend(struct CryList* list, pointer data);
......@@ -49,4 +47,3 @@ pointer cry_list_remove(struct CryList* list, struct CryIndex* p
struct CryList* cry_list_concat(struct CryList* first, struct CryList* second);
#endif // CRYSTAL_STRUCTURES_LIST_H
......@@ -16,9 +16,7 @@
*
*/
#ifndef CRYSTAL_STRUCTURES_MAP_H
#define CRYSTAL_STRUCTURES_MAP_H
#pragma once
#include "structures.h"
......@@ -36,8 +34,6 @@ size_t cry_map_size(struct CryMap* map);
pointer cry_map_lookup(struct CryMap* map, const_pointer key);
int cry_map_insert(struct CryMap* map, const_pointer key, const_pointer value);
struct CryMapPair cry_map_remove(struct CryMap* map, const_pointer key);
void cry_map_clear(struct CryMap* map, cry_free_funptr fun_key, cry_free_funptr fun_data);
void cry_map_clear(struct CryMap* map, cry_free_funptr fun_key, cry_free_funptr fun_data);
#endif // CRYSTAL_STRUCTURES_MAP_H
......@@ -16,8 +16,7 @@
*
*/
#ifndef CRYSTAL_STRUCTURES_QUEUE_H
#define CRYSTAL_STRUCTURES_QUEUE_H
#pragma once
#include "structures.h"
......@@ -34,5 +33,3 @@ pointer cry_queue_nth(struct CryQueue* queue, size_t index);
pointer cry_queue_dequeue(struct CryQueue* queue);
void cry_queue_enqueue(struct CryQueue* queue, pointer data);
#endif // CRYSTAL_STRUCTURES_QUEUE_H
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_STRUCTURES_REDBLACKTREE_H
#define CRYSTAL_STRUCTURES_REDBLACKTREE_H
#pragma once
#include "structures.h"
......@@ -45,7 +43,7 @@ struct CryRbTree {
struct CryRbTree* cry_rbtree_new(cry_ordering_funptr compare);
void cry_rbtree_clear(struct CryRbTree* tree, cry_free_funptr fun_entry, cry_free_funptr fun_data);
void cry_rbtree_clear(struct CryRbTree* tree, cry_free_funptr fun_entry, cry_free_funptr fun_data);
struct CryRbNode* cry_rbtree_lookup(struct CryRbTree* tree, const_pointer entry);
struct CryRbNode* cry_rbtree_insert(struct CryRbTree* tree, const_pointer entry, const_pointer data);
......@@ -58,10 +56,5 @@ struct CryRbNode* cry_rbnode_next(struct CryRbNode* node);
struct CryRbNode* cry_rbnode_prev(struct CryRbNode* node);
struct CryRbNode* cry_rbnode_new(enum CryRbNodeColor color, struct CryRbNode* parent, const_pointer entry, const_pointer data);
void cry_rbnode_free(struct CryRbNode* node, cry_free_funptr fun_entry, cry_free_funptr fun_data);
void cry_rbnode_free(struct CryRbNode* node, cry_free_funptr fun_entry, cry_free_funptr fun_data);
#endif // CRYSTAL_STRUCTURES_REDBLACKTREE_H
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_STRUCTURES_STACK_H
#define CRYSTAL_STRUCTURES_STACK_H
#pragma once
#include "structures.h"
......@@ -33,4 +31,3 @@ pointer cry_stack_pop(struct CryStack* stack);
void cry_stack_push(struct CryStack* stack, pointer data);
#endif // CRYSTAL_STRUCTURES_STACK_H
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_STANDARD_H
#define CRYSTAL_STANDARD_H
#pragma once
/**
* @file standard.h
......@@ -27,46 +25,23 @@
#include <stdlib.h>
#include <stdint.h>
/**
* Convenient Macro for generating tuple types, returning error codes
* and data in a synchronous manner (without using return arguments via pointers)
* @param type Sets the type for the data field of this tuple
* @param T Sets the name for this tuple struct
*/
#define crydefine__tuple(type, T) \
struct T { \
return_code code; \
type data; \
}
/** General error code indicating all is fine */
#define CRY_OKAY 0
/** General error code indicating a failure occured */
#define CRY_FAIL -1
/** typedef to hide void* pointers for clearer interfaces */
typedef void* pointer;
/** typedef to hide void* const_pointers for clearer interfaces */
typedef const void* const_pointer;
/** General typedef for setting an error code identifier */
typedef int return_code;
crydefine__tuple(pointer, tuple_pointer);
crydefine__tuple(const char*, tuple_string);
crydefine__tuple(int, tuple_int);
crydefine__tuple(short, tuple_short);
crydefine__tuple(long, tuple_long);
crydefine__tuple(double, tuple_double);
crydefine__tuple(float, tuple_float);
/** Smaller identifier for uint8_t */
typedef uint8_t byte;
typedef uint8_t byte_t;
/** Two bytes are a word */
typedef uint16_t word_t;
/** Unicode character type */
typedef uint32_t unicode;
typedef uint32_t unicode_t;
#define FALSE 0
#define TRUE 1
/**
* Crystal's malloc macro for allocating memory based on a type
......@@ -102,4 +77,3 @@ static void (*cry_free)(pointer data) = free;
*/
#define cry_cast(Type, Var) ((Type)Var)
#endif // CRYSTAL_STANDARD_H
......@@ -16,16 +16,11 @@
*
*/
#ifndef CRYSTAL_STRUCTURES_H
#define CRYSTAL_STRUCTURES_H
#pragma once
#include "../standard.h"
#include "standard.h"
#define CRY_OUT_OF_INDEX_RANGE -1
#define CRY_ENTRY_NOT_FOUND -2
#define CRY_ENTRY_FULL -3
typedef int (*cry_ordering_funptr)(const_pointer a, const_pointer b);
typedef int (*cry_ordering_funptr)(const_pointer a, const_pointer b);
typedef void (*cry_foreach_funptr)(pointer element, pointer data);
typedef void (*cry_free_funptr)(pointer data);
......@@ -33,4 +28,3 @@ int cry_int_compare(const_pointer a, const_pointer b);
int cry_double_compare(const_pointer a, const_pointer b);
#endif // CRYSTAL_STRUCTURES_H
......@@ -15,26 +15,35 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_UTF8_H
#define CRYSTAL_UTF8_H
#include "standard.h"
int cry_utf8_validate(const byte* str);
int cry_utf8_compare(const byte* str1, const byte* str2);
byte* cry_utf8_chr(const byte* str, unicode character);
byte* cry_utf8_rchr(const byte* str, unicode character);
byte* cry_utf8_str(const byte* str1, const byte* str2);
#pragma once
size_t cry_utf8_size(const byte* str);
size_t cry_utf8_len(const byte* str);
unicode cry_utf8_get(const byte* str);
size_t cry_utf8_codepoints(const byte* str);
byte* cry_utf8_next(const byte* str);
byte* cry_utf8_prev(const byte* str);
#endif // CRYSTAL_UTF8_H
#include "standard.h"
int cry_unicode_isblank(unicode_t ch);
int cry_unicode_isspace(unicode_t ch);
int cry_unicode_isalpha(unicode_t ch);
int cry_unicode_isalnum(unicode_t ch);
int cry_unicode_isdigit(unicode_t ch);
int cry_unicode_ishex(unicode_t ch);
int cry_unicode_isoct(unicode_t ch);
int cry_unicode_isprint(unicode_t ch);
int cry_unicode_isgraph(unicode_t ch);
int cry_unicode_ispunct(unicode_t ch);
int cry_unicode_iscntrl(unicode_t ch);
int cry_utf8_validate(const byte_t* str);
int cry_utf8_compare(const byte_t* str1, const byte_t* str2);
byte_t* cry_utf8_chr(const byte_t* str, unicode_t character);
byte_t* cry_utf8_rchr(const byte_t* str, unicode_t character);
byte_t* cry_utf8_str(const byte_t* str1, const byte_t* str2);
size_t cry_utf8_size(const byte_t* str);
size_t cry_utf8_len(const byte_t* str);
unicode_t cry_utf8_get(const byte_t* str);
size_t cry_utf8_codepoints(const byte_t* str);
byte_t* cry_utf8_next(const byte_t* str);
byte_t* cry_utf8_prev(const byte_t* str);
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_UNITTEST_H
#define CRYSTAL_UNITTEST_H
#pragma once
#include "standard.h"
#include <assert.h>
......@@ -30,4 +28,3 @@ void cry_unittest_initialize(int argc, char** argv);
void cry_unittest_finalize(void);
#endif // CRYSTAL_UNITTEST_H
......@@ -15,9 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_UNIXCOLORS_H
#define CRYSTAL_UNIXCOLORS_H
#pragma once
// Colors without bold attribute
#define CRY_NORMAL "\x1B[0m"
......@@ -60,4 +58,3 @@
#define CRY_BG_DARKCYAN "\x1B[1;46m"
#define CRY_BG_WHITE "\x1B[1;47m"
#endif // CRYSTAL_UNIXCOLORS_H
set(SOURCES
unittest.c
utf8.c
unicode.c
structures/single_linked_list.c
structures/list.c
......@@ -15,7 +14,6 @@ set(HEADER
standard.h
unix_colors.h
unittest.h
utf8.h
unicode.h
structures/structures.h
structures/array.h
......@@ -30,6 +28,6 @@ set(HEADER
add_library(${TARGET_NAME} SHARED ${SOURCES})
install(DIRECTORY . DESTINATION include/${TARGET_NAME} FILES_MATCHING PATTERN "*.h")
#install(FILES ${HEADER} DESTINATION include/${TARGET_NAME})
install(FILES ${HEADER} DESTINATION include/${TARGET_NAME})
install(TARGETS ${TARGET_NAME} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib)
This diff is collapsed.
......@@ -17,7 +17,7 @@
*/
#include "array.h"
#include "crystal/array.h"
#include <assert.h>
#include <string.h>
......@@ -116,13 +116,13 @@ cry_array_append(struct CryArray* array, const_pointer data, size_t size)
}
return_code
int
cry_array_insert(struct CryArray* array, size_t index, const_pointer data, size_t size)
{
assert(array != 0);
if(index >= array->length)
return CRY_OUT_OF_INDEX_RANGE;
return FALSE;
cry_array_ensure(array, array->length + size);
......@@ -131,36 +131,36 @@ cry_array_insert(struct CryArray* array, size_t index, const_pointer data, size_
array->length += size;
return CRY_OKAY;
return TRUE;
}
return_code
int
cry_array_replace(struct CryArray* array, size_t index, const_pointer data, size_t size)
{
assert(array != 0);
if(index >= array->length && index + size > array->length)
return CRY_OUT_OF_INDEX_RANGE;
return FALSE;
cry_array_ensure(array, array->length + size);
memcpy(array->data + index, data, size);
}
return_code
int
cry_array_remove(struct CryArray* array, size_t index, size_t size)
{
assert(array != 0);
if(index >= array->length)
return CRY_OUT_OF_INDEX_RANGE;
return FALSE;
memmove(array->data + index, array->data + index + size, array->length - index - size);
array->length -= size;
return CRY_OKAY;
return TRUE;
}
......@@ -293,13 +293,13 @@ cry_ptr_array_append(struct CryPtrArray* array, pointer ptr)
}
return_code
int
cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr)
{
assert(array != 0);
if(index >= array->length)
return CRY_OUT_OF_INDEX_RANGE;
return FALSE;
cry_ptr_array_ensure(array, array->length + 1);
......@@ -309,7 +309,7 @@ cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr)
array->length += 1;
return CRY_OKAY;
return TRUE;
}
......@@ -334,7 +334,7 @@ cry_ptr_array_remove(struct CryPtrArray* array, size_t index)
assert(array != 0);
if(index >= array->length)
return 0;
return FALSE;
pointer data = *(array->data + index);
......
......@@ -17,8 +17,8 @@
*/
#include <assert.h>
#include "heap.h"
#include "array.h"
#include "crystal/heap.h"
#include "crystal/array.h"
struct CryHeap {
struct CryPtrArray* tree;
......
......@@ -16,8 +16,8 @@
*
*/
#include "list.h"
#include "../standard.h"
#include "crystal/list.h"
#include "crystal/standard.h"
#include <assert.h>
struct Node {
......
......@@ -16,8 +16,8 @@
*
*/
#include "map.h"
#include "red_black_tree.h"
#include "crystal/map.h"
#include "crystal/red_black_tree.h"
#include <assert.h>
......
......@@ -19,7 +19,7 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "red_black_tree.h"
#include "crystal/red_black_tree.h"
struct CryRbTree*
......
......@@ -16,8 +16,8 @@
*
*/
#include "queue.h"
#include "stack.h"
#include "crystal/queue.h"
#include "crystal/stack.h"
#include <assert.h>
......
......@@ -16,7 +16,7 @@
*
*/
#include "structures.h"
#include "crystal/structures.h"
int
cry_int_compare(const_pointer a, const_pointer b)
......
......@@ -17,81 +17,433 @@
*/
#include <ctype.h>
#include "unicode.h"
#include <assert.h>
#include "crystal/unicode.h"
int
cry_unicode_isblank(unicode ch)
cry_unicode_isblank(unicode_t ch)
{
return isblank(ch);
}
int
cry_unicode_isspace(unicode ch)
cry_unicode_isspace(unicode_t ch)
{
return isspace(ch);
}
int
cry_unicode_isalpha(unicode ch)
cry_unicode_isalpha(unicode_t ch)
{
return isalpha(ch);
}
int
cry_unicode_isalnum(unicode ch)
cry_unicode_isalnum(unicode_t ch)
{
return isalnum(ch);
}
int
cry_unicode_isdigit(unicode ch)
cry_unicode_isdigit(unicode_t ch)
{
return isdigit(ch);
}
int
cry_unicode_ishex(unicode ch)
cry_unicode_ishex(unicode_t ch)
{
return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f') || ('a' <= ch && ch <= 'f');
}
int
cry_unicode_iscntrl(unicode ch)
cry_unicode_iscntrl(unicode_t ch)
{
return iscntrl(ch);
}
int
cry_unicode_isprint(unicode ch)
cry_unicode_isprint(unicode_t ch)
{
return isprint(ch);
}
int
cry_unicode_isgraph(unicode ch)
cry_unicode_isgraph(unicode_t ch)
{
return isgraph(ch);
}
int
cry_unicode_ispunct(unicode ch)
cry_unicode_ispunct(unicode_t ch)
{
return ispunct(ch);
}
int
cry_unicode_isctrl(unicode ch)
cry_unicode_isctrl(unicode_t ch)
{
return 0;
}
/**
* Counts the trail byte_ts for a UTF-8 lead byte_t of a valid UTF-8 sequence.
* The maximum supported lead byte_t is 0xf4 corresponding to U+10FFFF.
* leadByte might be evaluated multiple times.
*
* This is internal since it is not meant to be called directly by external clients;
* however it is called by public macros in this file and thus must remain stable.
*
* @param leadByte The first byte_t of a UTF-8 sequence. Must be 0..0xff.
* @internal
*/
#define UTF8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
(((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
/**
* Does this code unit (byte_t) encode a code point by itself (US-ASCII 0..0x7f)?
* @param c 8-bit code unit (byte_t)
* @return TRUE or FALSE
* @internal
*/
#define UTF8_IS_SINGLE(c) (((c)&0x80)==0)
/**
* Is this code unit (byte_t) a UTF-8 lead byte_t?
* @param c 8-bit code unit (byte_t)
* @return TRUE or FALSE
* @internal
*/
#define UTF8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
/**
* Is this code unit (byte_t) a UTF-8 trail byte_t?
* @param c 8-bit code unit (byte_t)
* @return TRUE or FALSE
* @internal
*/
#define UTF8_IS_TRAIL(c) (((c)&0xc0)==0x80)
/**
* Advance the string pointer from one code point boundary to the next.
* (Post-incrementing iteration.)
* "Unsafe" macro, assumes well-formed UTF-8.
*
* @param s const uint8_t * string
* @internal
*/
#define UTF8_FWD_1_UNSAFE(s) { \
(s) += 1 + UTF8_COUNT_TRAIL_BYTES_UNSAFE(*s); \
}
/**
* Move the string pointer from one code point boundary to the previous one.
* (Pre-decrementing backward iteration.)
* The input offset may be the same as the string length.
* "Unsafe" macro, assumes well-formed UTF-8.
*
* @param s const uint8_t * string
* @internal
*/