Compare commits

...

2 Commits

Author SHA1 Message Date
Ilja Kartašov 0d6fca9b26 Add list module 2019-06-24 07:40:51 +02:00
Ilja Kartašov cf2535c9e5 Correct definitions 2019-06-24 07:40:41 +02:00
3 changed files with 197 additions and 1 deletions

132
list.c Normal file
View File

@ -0,0 +1,132 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file list.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff list module
*
* @see https://lowenware.com/
*/
#include <stdlib.h>
#include <string.h>
#include "list.h"
CStuffRetcode
cstuff_list_init(CStuffList self, int size)
{
if (!(self->items = calloc(size, sizeof (void *))))
return CSTUFF_MALLOC_ERROR;
self->length = 0;
self->size = size;
return CSTUFF_SUCCESS;
}
void
cstuff_list_release(CStuffList self, CStuffListFree item_free)
{
if (self->items) {
if (item_free) {
int i;
for (i = 0; i < self->length; i++) {
void *ptr = self->items[i];
if (ptr) {
item_free(ptr);
}
}
}
free(self->items);
}
}
int
cstuff_list_append(CStuffList self, void *item)
{
void **items;
int size = self->length + 1;
if (size > self->size) {
if (!(items = realloc(self->items, size * sizeof (void*))))
return -1;
self->items = items;
self->size = size;
}
self->items[self->length] = item;
return self->length++;
}
int
cstuff_list_insert(CStuffList self, void *item, int index)
{
int r;
if ((r = cstuff_list_append(self, item)) == -1)
return -1;
if (index >= r || index < 0)
return r;
memmove(&self->items[index + 1], &self->items[index],
(self->length - index - 1) * sizeof (void*));
self->items[index] = item;
return index;
}
void *
cstuff_list_remove(CStuffList self, int index)
{
void *result;
int i;
if (index < self->length) {
result = self->items[index];
self->length--;
for (i = index; i < self->length; i++) {
self->items[i] = self->items[i + 1];
}
} else {
result = NULL;
}
return result;
}
void
cstuff_list_remove_item(CStuffList self, void *item)
{
int i;
for (i = 0; i < self->length; i++) {
if (self->items[i] == item) {
cstuff_list_remove(self, i);
break;
}
}
}
void
cstuff_list_set_item(CStuffList self, int index, void *value)
{
if (self->size > index) {
self->items[index] = value;
if (index >= self->length)
self->length = index + 1;
}
}

64
list.h Normal file
View File

@ -0,0 +1,64 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file list.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff List module header file
*
* @see https://lowenware.com/
*/
#ifndef CSTUFF_LIST_H_372E2D14_8DEE_4A7C_A5AB_8808A87DF436
#define CSTUFF_LIST_H_372E2D14_8DEE_4A7C_A5AB_8808A87DF436
#include "retcode.h"
#define cstuff_list_index(LIST, IDX) (LIST->items[IDX])
struct cstuff_list {
void **items;
int length;
int size;
};
typedef struct cstuff_list * CStuffList;
typedef void
(*CStuffListFree)(void *item);
CStuffRetcode
cstuff_list_init(CStuffList self, int size);
void
cstuff_list_release(CStuffList self, CStuffListFree item_free);
int
cstuff_list_append(CStuffList self, void *item);
int
cstuff_list_insert(CStuffList self, void *item, int index);
void *
cstuff_list_remove(CStuffList self, int index);
void
cstuff_list_remove_item(CStuffList self, void *item);
void
cstuff_list_set_item(CStuffList self, int index, void *value);
#endif /* !LIST_H */

2
log.c
View File

@ -17,7 +17,7 @@
void void
cstuff_log_init(CStuffLog self, uint8_t level) cstuff_log_init(CStuffLog self, int level)
{ {
self->target = stderr; self->target = stderr;
self->level = level; self->level = level;