rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 8502b22b8ec0bf65595190a8dfb8d8f8f4afa6e5
parent 09c677e26fede0741f13a4c589a0da887a9ee459
Author: vaplv <vaplv@free.fr>
Date:   Fri, 21 Feb 2020 15:25:35 +0100

Merge branch 'release_0.9'

Diffstat:
MREADME.md | 15++++++++++++---
Mcmake/CMakeLists.txt | 13++++++++-----
Msrc/algorithm.h | 2+-
Msrc/binary_heap.h | 2+-
Msrc/clock_time.c | 2+-
Msrc/clock_time.h | 2+-
Msrc/condition.h | 2+-
Msrc/cstr.c | 2+-
Msrc/cstr.h | 2+-
Msrc/cstr_to_list.h | 2+-
Msrc/double2.h | 2+-
Msrc/double22.h | 2+-
Msrc/double3.h | 2+-
Msrc/double33.h | 2+-
Msrc/double4.h | 2+-
Msrc/double44.h | 2+-
Msrc/dynamic_array.h | 4++--
Msrc/dynamic_array_char.h | 2+-
Msrc/dynamic_array_double.h | 2+-
Msrc/dynamic_array_float.h | 2+-
Msrc/dynamic_array_int.h | 2+-
Msrc/dynamic_array_size_t.h | 2+-
Msrc/dynamic_array_str.h | 2+-
Msrc/dynamic_array_u32.h | 2+-
Msrc/dynamic_array_u64.h | 2+-
Msrc/dynamic_array_uchar.h | 2+-
Msrc/dynamic_array_uint.h | 2+-
Msrc/float2.h | 2+-
Msrc/float22.h | 2+-
Msrc/float3.h | 2+-
Msrc/float33.h | 2+-
Msrc/float4.h | 2+-
Msrc/float44.h | 2+-
Msrc/free_list.h | 2+-
Msrc/hash.h | 2+-
Msrc/hash_table.h | 2+-
Msrc/image.c | 2+-
Msrc/image.h | 2+-
Msrc/io_c99.h | 2+-
Msrc/library.c | 37+++++++------------------------------
Msrc/library.h | 2+-
Msrc/list.h | 2+-
Msrc/logger.c | 2+-
Msrc/logger.h | 2+-
Msrc/math.h | 2+-
Msrc/mem_allocator.c | 2+-
Msrc/mem_allocator.h | 2+-
Msrc/mem_lifo_allocator.c | 2+-
Msrc/mem_proxy_allocator.c | 2+-
Msrc/mutex.h | 2+-
Msrc/pthread/pthread_condition.c | 2+-
Msrc/pthread/pthread_mutex.c | 2+-
Msrc/quaternion.c | 2+-
Msrc/quaternion.h | 2+-
Msrc/real2.h | 2+-
Msrc/real22.h | 2+-
Msrc/real3.h | 2+-
Msrc/real33.h | 2+-
Msrc/real44.h | 2+-
Msrc/realX.h | 2+-
Msrc/realXY.h | 2+-
Msrc/realXY_begin.h | 2+-
Msrc/realXY_end.h | 2+-
Msrc/realX_begin.h | 2+-
Msrc/realX_end.h | 2+-
Msrc/ref_count.h | 2+-
Msrc/rsys.h | 2+-
Msrc/signal.h | 2+-
Msrc/str.c | 51++++++++++++++++++++++++++++-----------------------
Msrc/str.h | 12++++--------
Msrc/stretchy_array.h | 2+-
Msrc/test_algorithm.c | 2+-
Msrc/test_atomic.c | 2+-
Msrc/test_binary_heap.c | 2+-
Msrc/test_condition.c | 2+-
Msrc/test_cstr.c | 2+-
Msrc/test_double2.c | 2+-
Msrc/test_double22.c | 2+-
Msrc/test_double3.c | 2+-
Msrc/test_double33.c | 2+-
Msrc/test_double4.c | 2+-
Msrc/test_double44.c | 2+-
Msrc/test_dynamic_array.c | 2+-
Msrc/test_float2.c | 2+-
Msrc/test_float22.c | 2+-
Msrc/test_float3.c | 2+-
Msrc/test_float33.c | 2+-
Msrc/test_float4.c | 2+-
Msrc/test_float44.c | 2+-
Msrc/test_free_list.c | 2+-
Msrc/test_func_name.c | 2+-
Msrc/test_hash_table.c | 2+-
Msrc/test_image.c | 2+-
Msrc/test_library.c | 2+-
Msrc/test_list.c | 2+-
Msrc/test_logger.c | 2+-
Msrc/test_math.c | 2+-
Msrc/test_mem_allocator.c | 2+-
Msrc/test_misc.c | 2+-
Msrc/test_mutex.c | 2+-
Msrc/test_quaternion.c | 2+-
Msrc/test_real2.h | 2+-
Msrc/test_real22.h | 2+-
Msrc/test_real3.h | 2+-
Msrc/test_real33.h | 2+-
Msrc/test_real4.h | 2+-
Msrc/test_real44.h | 2+-
Msrc/test_ref.c | 2+-
Msrc/test_signal.c | 2+-
Msrc/test_str.c | 8+++++++-
Asrc/test_text_reader.c | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/test_time.c | 2+-
Asrc/text_reader.c | 249+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/text_reader.h | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/win32/win32_condition.c | 2+-
Msrc/win32/win32_mutex.c | 2+-
116 files changed, 640 insertions(+), 178 deletions(-)

diff --git a/README.md b/README.md @@ -17,6 +17,15 @@ project can be now edited, built, tested and installed as any CMake project. ## Release notes +### Version 0.9 + +- Add the text reader API. A text reader reads lines from a text stream. In + contrast to the `getline` POSIX function, a text reader skips the empty lines + as well as comments that are text directly following a user defined comment + character. +- Make silent the library API: the library functions do not print anymore any + message on the standard error. + ### Version 0.8.1 - Fix the allocation policy of the dynamic array that exhibited strong @@ -91,7 +100,7 @@ project can be now edited, built, tested and installed as any CMake project. ## License -RSys is Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr). It is a free -software released under LGPL v3+ license. You are welcome to redistribute it -under certain conditions; refer to the COPYING files for details. +Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr). RSys is a free software +released under LGPL v3+ license. You are welcome to redistribute it under +certain conditions; refer to the COPYING files for details. diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +# Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) # # This CMake script is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -38,8 +38,8 @@ include(rcmake) # Configure and define targets ################################################################################ set(VERSION_MAJOR 0) -set(VERSION_MINOR 8) -set(VERSION_PATCH 1) +set(VERSION_MINOR 9) +set(VERSION_PATCH 0) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(RSYS_FILES_SRC @@ -52,7 +52,8 @@ set(RSYS_FILES_SRC mem_lifo_allocator.c mem_proxy_allocator.c quaternion.c - str.c) + str.c + text_reader.c) if(CMAKE_USE_PTHREADS_INIT) set(RSYS_FILES_SRC_THREAD @@ -121,7 +122,8 @@ set(RSYS_FILES_INC_API rsys.h signal.h str.h - stretchy_array.h) + stretchy_array.h + text_reader.h) set(RSYS_FILES_DOC COPYING COPYING.LESSER README.md) @@ -229,6 +231,7 @@ if(NOT NO_TEST) new_test(test_signal rsys) new_test(test_str rsys) new_test(test_stretchy_array rsys) + new_test(test_text_reader rsys) new_test(test_time rsys) add_library(test_lib SHARED ${RSYS_SOURCE_DIR}/test_library.c) diff --git a/src/algorithm.h b/src/algorithm.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/binary_heap.h b/src/binary_heap.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/clock_time.c b/src/clock_time.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/clock_time.h b/src/clock_time.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/condition.h b/src/condition.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/cstr.c b/src/cstr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/cstr.h b/src/cstr.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/cstr_to_list.h b/src/cstr_to_list.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/double2.h b/src/double2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/double22.h b/src/double22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/double3.h b/src/double3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/double33.h b/src/double33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/double4.h b/src/double4.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/double44.h b/src/double44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -198,7 +198,7 @@ DARRAY_FUNC__(resize)(struct DARRAY_TYPE__* darray, const size_t sz) if(sz < darray->capacity) { sz_adjusted = sz; - } else if (sz < darray->size*2) { + } else if(sz < darray->size*2) { sz_adjusted = darray->size*2; } else { sz_adjusted = sz; diff --git a/src/dynamic_array_char.h b/src/dynamic_array_char.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_double.h b/src/dynamic_array_double.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_float.h b/src/dynamic_array_float.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_int.h b/src/dynamic_array_int.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_size_t.h b/src/dynamic_array_size_t.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_str.h b/src/dynamic_array_str.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_u32.h b/src/dynamic_array_u32.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_u64.h b/src/dynamic_array_u64.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_uchar.h b/src/dynamic_array_uchar.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/dynamic_array_uint.h b/src/dynamic_array_uint.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/float2.h b/src/float2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/float22.h b/src/float22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/float3.h b/src/float3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/float33.h b/src/float33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/float4.h b/src/float4.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/float44.h b/src/float44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/free_list.h b/src/free_list.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/hash.h b/src/hash.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/hash_table.h b/src/hash_table.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/image.c b/src/image.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/image.h b/src/image.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/io_c99.h b/src/io_c99.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/library.c b/src/library.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -55,48 +55,25 @@ library_get_symbol(void* lib, const char* sym) void* library_open(const char* filename) { - void* handle = NULL; - if(!filename) - return NULL; - - handle = dlopen(filename, RTLD_NOW|RTLD_GLOBAL); - if(!handle) { - fprintf(stderr, "%s\n", dlerror()); - } - return handle; + if(!filename) return NULL; + return dlopen(filename, RTLD_NOW|RTLD_GLOBAL); } void* library_get_symbol(void* lib, const char* sym) { - void* tmp_sym = NULL; - char* err = NULL; - - if(!lib || !sym) - return NULL; - - tmp_sym = dlsym(lib, sym); - err = dlerror(); - if(err == NULL) { - return tmp_sym; - } else { - fprintf(stderr, "%s\n", err); - return NULL; - } + if(!lib || !sym) return NULL; + return dlsym(lib, sym); } res_T library_close(void* handle) { int err = 0; - if(!handle) - return RES_BAD_ARG; + if(!handle) return RES_BAD_ARG; err = dlclose(handle); - if(err) { - fprintf(stderr, "%s\n", dlerror()); - return RES_UNKNOWN_ERR; - } + if(err) return RES_UNKNOWN_ERR; return RES_OK; } diff --git a/src/library.h b/src/library.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/list.h b/src/list.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/logger.c b/src/logger.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/logger.h b/src/logger.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/math.h b/src/math.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/mem_allocator.c b/src/mem_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/mem_allocator.h b/src/mem_allocator.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/mem_lifo_allocator.c b/src/mem_lifo_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/mem_proxy_allocator.c b/src/mem_proxy_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/mutex.h b/src/mutex.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/pthread/pthread_condition.c b/src/pthread/pthread_condition.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/pthread/pthread_mutex.c b/src/pthread/pthread_mutex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/quaternion.c b/src/quaternion.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/quaternion.h b/src/quaternion.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/real2.h b/src/real2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/real22.h b/src/real22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/real3.h b/src/real3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/real33.h b/src/real33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/real44.h b/src/real44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/realX.h b/src/realX.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/realXY.h b/src/realXY.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/realXY_begin.h b/src/realXY_begin.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/realXY_end.h b/src/realXY_end.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/realX_begin.h b/src/realX_begin.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/realX_end.h b/src/realX_end.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/ref_count.h b/src/ref_count.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/rsys.h b/src/rsys.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/signal.h b/src/signal.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/str.c b/src/str.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -66,36 +66,37 @@ str_set(struct str* str, const char* cstr) res = ensure_allocated(str, cstr_len + 1, 0); if(res != RES_OK) return res; strncpy(str->cstr, cstr, cstr_len + 1); - str->len = cstr_len; return RES_OK; } res_T str_insert(struct str* str, const size_t i, const char* cstr) { + size_t len = 0; size_t cstr_len = 0; ASSERT(str); - if(i > str->len) + len = str_len(str); + + if(i > len) return RES_BAD_ARG; cstr_len = strlen(cstr); ASSERT(!MEM_AREA_OVERLAP (str->cstr, str->allocated, cstr, (cstr_len + 1) * sizeof(char))); - if(i == str->len) { + if(i == len) { return str_append(str, cstr); } else { - const res_T res = ensure_allocated(str, cstr_len + str->len + 1, 1); + const res_T res = ensure_allocated(str, cstr_len + len + 1, 1); if(res != RES_OK) return res; memmove (str->cstr + i + cstr_len, str->cstr + i, - (str->len - i) * sizeof(char)); + (len - i) * sizeof(char)); memcpy(str->cstr + i, cstr, cstr_len * sizeof(char)); - str->len = str->len + cstr_len; - str->cstr[str->len] = '\0'; + str->cstr[len + cstr_len] = '\0'; } return RES_OK; } @@ -103,25 +104,27 @@ str_insert(struct str* str, const size_t i, const char* cstr) res_T str_insert_char(struct str* str, const size_t i, const char ch) { - if(i > str->len) + size_t len = 0; + ASSERT(str); + + len = str_len(str); + if(i > len) return RES_BAD_ARG; - if(i == str->len) { + if(i == len) { return str_append_char(str, ch); } else if(ch == '\0') { str->cstr[i] = ch; - str->len = i; } else { - const res_T res = ensure_allocated(str, str->len + 2, 1); + const res_T res = ensure_allocated(str, len + 2, 1); if(res != RES_OK) return res; memmove (str->cstr + i + 1, str->cstr + i, - (str->len - i) * sizeof(char)); + (len - i) * sizeof(char)); str->cstr[i] = ch; - ++str->len; - str->cstr[str->len] = '\0'; + str->cstr[len+1] = '\0'; } return RES_OK; } @@ -129,6 +132,7 @@ str_insert_char(struct str* str, const size_t i, const char ch) res_T str_append(struct str* str, const char* cstr) { + size_t len = 0; size_t cstr_len = 0; res_T res = RES_OK; ASSERT(str && cstr); @@ -137,31 +141,32 @@ str_append(struct str* str, const char* cstr) ASSERT(!MEM_AREA_OVERLAP (str->cstr, str->allocated, cstr, (cstr_len + 1) * sizeof(char))); - res = ensure_allocated(str, cstr_len + str->len + 1, 1); + len = str_len(str); + res = ensure_allocated(str, cstr_len + len + 1, 1); if(res != RES_OK) return res; - memcpy(str->cstr + str->len, cstr, cstr_len * sizeof(char)); - str->len += cstr_len; - str->cstr[str->len] = '\0'; + memcpy(str->cstr + len, cstr, cstr_len * sizeof(char)); + str->cstr[len + cstr_len] = '\0'; return RES_OK; } res_T str_append_char(struct str* str, const char ch) { + size_t len = 0; res_T res = RES_OK; ASSERT( str ); if(ch == '\0') return RES_OK; - res = ensure_allocated(str, str->len + 2, 1); + len = str_len(str); + res = ensure_allocated(str, len + 2, 1); if(res != RES_OK) return res; - str->cstr[str->len] = ch; - ++str->len; - str->cstr[str->len] = '\0'; + str->cstr[len+0] = ch; + str->cstr[len+1] = '\0'; return RES_OK; } diff --git a/src/str.h b/src/str.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -25,7 +25,6 @@ struct str { /* Internal data. Should not be publicly accessed */ struct mem_allocator* allocator; size_t allocated; /* <=> string capacity */ - size_t len; char* cstr; char buffer[16]; /* static buffer. Avoid allocation on small string */ }; @@ -36,7 +35,6 @@ str_init(struct mem_allocator* allocator, struct str* str) ASSERT(str); str->allocator = allocator ? allocator : &mem_default_allocator; str->allocated = sizeof(str->buffer); - str->len = 0; str->cstr = str->buffer; str->buffer[0] = '\0'; } @@ -54,7 +52,7 @@ static INLINE size_t str_len(const struct str* str) { ASSERT(str); - return str->len; + return strlen(str->cstr); } static INLINE char @@ -68,7 +66,6 @@ static INLINE void str_clear(struct str* str) { ASSERT(str); - str->len = 0; str->cstr[0] = '\0'; } @@ -135,7 +132,6 @@ str_copy_and_clear(struct str* dst, struct str* src) MEM_RM(dst->allocator, dst->cstr); dst->cstr = src->cstr; dst->allocated = src->allocated; - dst->len = src->len; /* Reset the src to its initial state */ str_init(src->allocator, src); } else { @@ -168,9 +164,9 @@ str_hash(const struct str* str) ASSERT(str); c_str = str_cget(str); #ifdef ARCH_32BITS - return (size_t)hash_fnv32(c_str, str->len); + return (size_t)hash_fnv32(c_str, str_len(str)); #elif defined ARCH_64BITS - return (size_t)hash_fnv64(c_str, str->len); + return (size_t)hash_fnv64(c_str, str_len(str)); #else #error "Unexpected architecture" #endif diff --git a/src/stretchy_array.h b/src/stretchy_array.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_algorithm.c b/src/test_algorithm.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_atomic.c b/src/test_atomic.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_binary_heap.c b/src/test_binary_heap.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_condition.c b/src/test_condition.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_cstr.c b/src/test_cstr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_double2.c b/src/test_double2.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_double22.c b/src/test_double22.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_double3.c b/src/test_double3.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_double33.c b/src/test_double33.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_double4.c b/src/test_double4.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_double44.c b/src/test_double44.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_float2.c b/src/test_float2.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_float22.c b/src/test_float22.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_float3.c b/src/test_float3.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_float33.c b/src/test_float33.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_float4.c b/src/test_float4.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_float44.c b/src/test_float44.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_free_list.c b/src/test_free_list.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_func_name.c b/src/test_func_name.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_hash_table.c b/src/test_hash_table.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_image.c b/src/test_image.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_library.c b/src/test_library.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_list.c b/src/test_list.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_logger.c b/src/test_logger.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_math.c b/src/test_math.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_mem_allocator.c b/src/test_mem_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_misc.c b/src/test_misc.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_mutex.c b/src/test_mutex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_quaternion.c b/src/test_quaternion.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_real2.h b/src/test_real2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_real22.h b/src/test_real22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_real3.h b/src/test_real3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_real33.h b/src/test_real33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_real4.h b/src/test_real4.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_real44.h b/src/test_real44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_ref.c b/src/test_ref.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_signal.c b/src/test_signal.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/test_str.c b/src/test_str.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -138,6 +138,12 @@ main(int argc, char** argv) str_release(&str); + str_init(&allocator_proxy, &str); + CHK(str_set(&str, "abcd") == RES_OK); + CHK(str_len(&str) == 4); + str_get(&str)[3] = '\0'; + CHK(str_len(&str) == 3); + check_memory_allocator(&allocator_proxy); mem_shutdown_proxy_allocator(&allocator_proxy); CHK(mem_allocated_size() == 0); diff --git a/src/test_text_reader.c b/src/test_text_reader.c @@ -0,0 +1,127 @@ +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) + * + * The RSys library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The RSys library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ + +#include "mem_allocator.h" +#include "test_utils.h" +#include "text_reader.h" + +#include <string.h> + +static const char* text[] = { + "Znxr rnpu cebtenz qb bar guvat jryy. Gb qb n arj wbo, ohvyq nserfu engure" + "guna pbzcyvpngr byq cebtenzf ol nqqvat arj \"srngherf\"\n", + " \t\t # rzcgl yvar\n", + "Rkcrpg gur bhgchg bs rirel cebtenz gb orpbzr gur vachg gb nabgure, nf lrg" + "haxabja, cebtenz. Qba'g pyhggre bhgchg jvgu rkgenarbhf vasbezngvba. Nibvq" + "fgevatragyl pbyhzane be ovanel vachg sbezngf. Qba'g vafvfg ba vagrenpgvir" + "vachg.\n", + "# pbzzrag\n", + "Qrfvta naq ohvyq fbsgjner, rira bcrengvat flfgrzf, gb or gevrq rneyl, vqrnyyl" + "jvguva jrrxf. Qba'g urfvgngr gb guebj njnl gur pyhzfl cnegf naq erohvyq gurz.\n", + "\n", + "Hfr gbbyf va cersrerapr gb hafxvyyrq uryc gb yvtugra n cebtenzzvat gnfx, rira" + "vs lbh unir gb qrgbhe gb ohvyq gur gbbyf naq rkcrpg gb guebj fbzr bs gurz bhg" + "nsgre lbh'ir svavfurq hfvat gurz.\n" +}; +static const size_t nlines = sizeof(text)/sizeof(const char*); + +static void +check_text_reader(struct txtrdr* txtrdr) +{ + size_t iline = 0; + + CHK(txtrdr_read_line(txtrdr) == RES_OK); + while(txtrdr_get_line(txtrdr)) { + + /* Discard empty line */ + while(strcspn(text[iline], "#\n") == strspn(text[iline], " \t")) ++iline; + + CHK(!strncmp(txtrdr_get_line(txtrdr), text[iline], strcspn(text[iline], "#\n"))); + CHK(txtrdr_get_line_num(txtrdr) == iline+1); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + ++iline; + } + CHK(txtrdr_get_line_num(txtrdr) == nlines); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + CHK(txtrdr_get_line(txtrdr) == NULL); + CHK(txtrdr_get_line_num(txtrdr) == nlines); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + CHK(txtrdr_get_line(txtrdr) == NULL); + txtrdr_set_line_num(txtrdr, nlines/2); + CHK(txtrdr_get_line_num(txtrdr) == nlines/2); +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct txtrdr* txtrdr = NULL; + size_t i; + FILE* stream; + const char* filename = "test_text_reader.txt"; + const char* stream_name = "my_stream"; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + /* Write the text into a stream */ + CHK(stream = fopen(filename, "w+")); + FOR_EACH(i, 0, nlines) { + const size_t len = strlen(text[i]); + CHK(fwrite(text[i], 1, len, stream) == len); + } + + rewind(stream); + CHK(txtrdr_stream(NULL, stream, NULL, '#', &txtrdr) == RES_OK); + check_text_reader(txtrdr); + txtrdr_ref_get(txtrdr); + txtrdr_ref_put(txtrdr); + txtrdr_ref_put(txtrdr); + + rewind(stream); + CHK(txtrdr_stream(&allocator, stream, stream_name, '#', &txtrdr) == RES_OK); + CHK(!strcmp(txtrdr_get_name(txtrdr), stream_name)); + CHK(txtrdr_get_stream(txtrdr) == stream); + check_text_reader(txtrdr); + txtrdr_ref_put(txtrdr); + + CHK(txtrdr_file(&allocator, filename, '#', &txtrdr) == RES_OK); + CHK(!strcmp(txtrdr_get_name(txtrdr), filename)); + check_text_reader(txtrdr); + txtrdr_ref_put(txtrdr); + + CHK(stream = freopen(filename, "w+", stream)); + fprintf(stream, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + fprintf(stream, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); + rewind(stream); + CHK(txtrdr_stream(&allocator, stream, filename, -1, &txtrdr) == RES_OK); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + txtrdr_ref_put(txtrdr); + + CHK(stream = freopen(filename, "w+", stream)); + fprintf(stream, "\r\n"); + rewind(stream); + CHK(txtrdr_stream(&allocator, stream, filename, '#', &txtrdr) == RES_OK); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + CHK(txtrdr_get_line(txtrdr) == NULL); + txtrdr_ref_put(txtrdr); + + fclose(stream); + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} + diff --git a/src/test_time.c b/src/test_time.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/text_reader.c b/src/text_reader.c @@ -0,0 +1,249 @@ +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) + * + * The RSys library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The RSys library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ + +#include "dynamic_array_char.h" +#include "mem_allocator.h" +#include "ref_count.h" +#include "str.h" +#include "text_reader.h" + +/* #chars to add to the line buffer when there is no sufficient space to store + * the text */ +#define CHUNK 32 + +struct txtrdr { + FILE* stream; /* Stream of the text to read */ + struct str name; /* Stream name */ + size_t nlines; /* # read lines */ + struct darray_char line; /* Buffer storing the read line */ + + /* String of chars from which the remaining line chars are skipped */ + char reject[4]; + + /* Boolean defining if the stream is internally managed or not, i.e. if it + * has to be closed on text_reader release or not */ + int manage_stream; + + struct mem_allocator* allocator; + ref_T ref; +}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +release_txtrdr(ref_T* ref) +{ + struct txtrdr* txtrdr = NULL; + ASSERT(ref); + txtrdr = CONTAINER_OF(ref, struct txtrdr, ref); + str_release(&txtrdr->name); + darray_char_release(&txtrdr->line); + if(txtrdr->stream && txtrdr->manage_stream) fclose(txtrdr->stream); + MEM_RM(txtrdr->allocator, txtrdr); +} + +/******************************************************************************* + * Text reader API + ******************************************************************************/ +res_T +txtrdr_stream + (struct mem_allocator* mem_allocator, + FILE* stream, + const char* name, + const char comment, + struct txtrdr** out_txtrdr) +{ + struct mem_allocator* allocator = NULL; + struct txtrdr* txtrdr = NULL; + res_T res = RES_OK; + ASSERT(stream && out_txtrdr); + + allocator = mem_allocator ? mem_allocator : &mem_default_allocator; + txtrdr = MEM_CALLOC(allocator, 1, sizeof(*txtrdr)); + if(!txtrdr) { + res = RES_MEM_ERR; + goto error; + } + ref_init(&txtrdr->ref); + str_init(allocator, &txtrdr->name); + darray_char_init(allocator, &txtrdr->line); + txtrdr->allocator = allocator; + txtrdr->stream = stream; + txtrdr->nlines = 0; + txtrdr->reject[0] = comment; + txtrdr->reject[1] = '\n'; + txtrdr->reject[2] = '\r'; + txtrdr->reject[3] = '\0'; /* Finalize the string */ + + res = str_set(&txtrdr->name, name ? name : "<null>"); + if(res != RES_OK) goto error; + + res = darray_char_resize(&txtrdr->line, CHUNK); + if(res != RES_OK) goto error; + +exit: + *out_txtrdr = txtrdr; + return res; +error: + if(txtrdr) { + txtrdr_ref_put(txtrdr); + txtrdr = NULL; + } + goto exit; +} + +res_T +txtrdr_file + (struct mem_allocator* mem_allocator, + const char* filename, + const char comment, + struct txtrdr** out_txtrdr) +{ + FILE* fp = NULL; + res_T res = RES_OK; + ASSERT(filename); + + fp = fopen(filename, "r"); + if(!fp) { res = RES_IO_ERR; goto error; } + + res = txtrdr_stream(mem_allocator, fp, filename, comment, out_txtrdr); + if(res != RES_OK) goto error; + + (*out_txtrdr)->manage_stream = 1; + +exit: + return res; +error: + if(fp) fclose(fp); + goto exit; +} + +void +txtrdr_ref_get(struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + ref_get(&txtrdr->ref); +} + +void +txtrdr_ref_put(struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + ref_put(&txtrdr->ref, release_txtrdr); +} + +res_T +txtrdr_read_line(struct txtrdr* txtrdr) +{ + char* str = NULL; + res_T res = RES_OK; + + if(!txtrdr) { + res = RES_BAD_ARG; + goto error; + } + + do { + /* Read the line */ + str = fgets + (darray_char_data_get(&txtrdr->line), + (int)darray_char_size_get(&txtrdr->line), + txtrdr->stream); + if(!str) { + if(ferror(txtrdr->stream)) { + res = RES_IO_ERR; + goto error; + } else { + darray_char_clear(&txtrdr->line); + break; + } + } + + /* Ensure tht the whole line is read */ + while(!strrchr(DARRAY_BUF(&txtrdr->line), '\n') && !feof(txtrdr->stream)) { + const size_t sz = darray_char_size_get(&txtrdr->line); + char* more = NULL; + + /* Resize the line buffer */ + res = darray_char_resize(&txtrdr->line, sz+CHUNK); + if(res != RES_OK) goto error; + + /* Read the remaing chars */ + more = darray_char_data_get(&txtrdr->line) + sz-1/*null char*/; + str = fgets(more, CHUNK+1/*previous null char*/, txtrdr->stream); + if(!str && ferror(txtrdr->stream)) { + res = RES_IO_ERR; + goto error; + } + } + + /* Remove new line & comments */ + str = darray_char_data_get(&txtrdr->line); + str[strcspn(str, txtrdr->reject)] = '\0'; + + ++txtrdr->nlines; /* Increment the number of read lines */ + + } while(strspn(str, " \t") == strlen(str));/*Keep going if the line is Empty*/ + +exit: + return res; +error: + goto exit; +} + +char* +txtrdr_get_line(struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + return darray_char_size_get(&txtrdr->line) == 0 + ? NULL : darray_char_data_get(&txtrdr->line); +} + +const char* +txtrdr_get_cline(const struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + return darray_char_size_get(&txtrdr->line) == 0 + ? NULL : darray_char_cdata_get(&txtrdr->line); +} + +size_t +txtrdr_get_line_num(const struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + return txtrdr->nlines; +} + +void +txtrdr_set_line_num(struct txtrdr* txtrdr, const size_t nlines) +{ + ASSERT(txtrdr); + txtrdr->nlines = nlines; +} + +const char* +txtrdr_get_name(const struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + return str_cget(&txtrdr->name); +} + +FILE* +txtrdr_get_stream(const struct txtrdr* txtrdr) +{ + ASSERT(txtrdr); + return txtrdr->stream; +} diff --git a/src/text_reader.h b/src/text_reader.h @@ -0,0 +1,90 @@ +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) + * + * The RSys library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The RSys library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef TEXT_READER_H +#define TEXT_READER_H + +#include "rsys.h" + +struct txtrdr; +struct mem_allocator; + +BEGIN_DECLS + +RSYS_API res_T +txtrdr_file + (struct mem_allocator* allocator, /* May be NULL <=> default allocator */ + const char* filename, + const char comment, /* Char preceeding a comment */ + struct txtrdr** txtrdr); + +RSYS_API res_T +txtrdr_stream + (struct mem_allocator* allocator, /* May be NULL <=> default allocator */ + FILE* stream, + const char* name, /* Stream name. May be NULL */ + const char comment, /* Char preceeding a comment */ + struct txtrdr** txtrdr); + +RSYS_API void +txtrdr_ref_get + (struct txtrdr* txtrdr); + +RSYS_API void +txtrdr_ref_put + (struct txtrdr* txtrdr); + +/* Read a non empty line, i.e. line with text that is not a comment. The new + * line character as well as the comments are skipped from the reading. A + * returned value different of RES_OK means than an error occurs while reading + * the stream. Note that reaching the end of file is not an error and thus that + * no error is returned if the end of file is reached, even though nothing was + * read. In this case the returned line is simply NULL. */ +RSYS_API res_T +txtrdr_read_line + (struct txtrdr* txtrdr); + +RSYS_API char* +txtrdr_get_line + (struct txtrdr* txtrdr); + +RSYS_API const char* +txtrdr_get_cline + (const struct txtrdr* txtrdr); + +/* Return the number of read lines */ +RSYS_API size_t +txtrdr_get_line_num + (const struct txtrdr* txtrdr); + +/* Overwrite the internal line number */ +RSYS_API void +txtrdr_set_line_num + (struct txtrdr* txtrdr, + const size_t nlines); + +RSYS_API const char* +txtrdr_get_name + (const struct txtrdr* txtrdr); + +/* Return the stream currently read. Note that any modification of the returned + * stream will affect the text reader */ +RSYS_API FILE* +txtrdr_get_stream + (const struct txtrdr* txtrdr); + +END_DECLS + +#endif /* TEXT_READER_H */ diff --git a/src/win32/win32_condition.c b/src/win32/win32_condition.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published diff --git a/src/win32/win32_mutex.c b/src/win32/win32_mutex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published