96 lines
2.5 KiB
C
96 lines
2.5 KiB
C
/*
|
|
This file is part of Furui.
|
|
|
|
Furui is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Furui 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with Furui. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "core.h"
|
|
|
|
struct furui_uiarray *furui_uiarray_new(void) {
|
|
struct furui_uiarray *uiarray = malloc(sizeof(struct furui_uiarray));
|
|
if (uiarray == NULL) {
|
|
perror("malloc");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
uiarray->max_length = FURUI_UIARRAY_DEFAULT_SIZE;
|
|
uiarray->buffer = malloc(sizeof(unsigned int) * uiarray->max_length);
|
|
if (uiarray->buffer == NULL) {
|
|
perror("malloc");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
uiarray->length = 0;
|
|
|
|
return uiarray;
|
|
}
|
|
|
|
void furui_uiarray_free(struct furui_uiarray *uiarray) {
|
|
if (uiarray == NULL) {
|
|
return;
|
|
}
|
|
|
|
free(uiarray->buffer);
|
|
|
|
free(uiarray);
|
|
}
|
|
|
|
unsigned int furui_uiarray_get_length(struct furui_uiarray *uiarray) {
|
|
if (uiarray == NULL) {
|
|
fprintf(stderr, "Null pointer exception at furui_uiarray_append\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
return uiarray->length;
|
|
}
|
|
|
|
void furui_uiarray_append(struct furui_uiarray *uiarray, unsigned int value) {
|
|
if (uiarray == NULL) {
|
|
fprintf(stderr, "Null pointer exception at furui_uiarray_append\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (uiarray->length >= uiarray->max_length) {
|
|
uiarray->max_length = uiarray->max_length + uiarray->max_length / 2;
|
|
uiarray->buffer = realloc(uiarray->buffer, sizeof(unsigned int) * uiarray->max_length);
|
|
if (uiarray->buffer == NULL) {
|
|
perror("realloc");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
uiarray->buffer[uiarray->length] = value;
|
|
uiarray->length++;
|
|
}
|
|
|
|
unsigned int *furui_uiarray_to_array(const struct furui_uiarray *uiarray) {
|
|
unsigned int *buffer;
|
|
|
|
if (uiarray == NULL) {
|
|
fprintf(stderr, "Null pointer exception at furui_uiarray_append\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
buffer = malloc(sizeof(unsigned int) * uiarray->length);
|
|
if (buffer == NULL) {
|
|
perror("malloc");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
memcpy(buffer, uiarray->buffer, sizeof(unsigned int) * uiarray->length);
|
|
|
|
return buffer;
|
|
}
|