at work
This commit is contained in:
93
clib/cdynarr.c
Normal file
93
clib/cdynarr.c
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <cdynarr.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define INIT_CAPA 64
|
||||||
|
|
||||||
|
/* Integer array */
|
||||||
|
cintarr_t* new_cintarr(void) {
|
||||||
|
cintarr_t* arr = malloc(sizeof(cintarr_t));
|
||||||
|
if (arr == NULL) return NULL;
|
||||||
|
if (cintarr_init(arr) == NULL) {
|
||||||
|
cintarr_free(arr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* cintarr_init(cintarr_t* array) {
|
||||||
|
array->data = malloc(INIT_CAPA * sizeof(int64_t));
|
||||||
|
if (array->data == NULL) return NULL;
|
||||||
|
memset(array->data, 0, INIT_CAPA);
|
||||||
|
array->capa = INIT_CAPA;
|
||||||
|
array->size = 0;
|
||||||
|
return array->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* cintarr_append(cintarr_t* array, int64_t add) {
|
||||||
|
if (array->size + 1 > array->capa) {
|
||||||
|
size_t newcapa = (array->capa * 10) / 6 ;
|
||||||
|
int64_t* newarray = realloc(array->data, newcapa);
|
||||||
|
if (newarray == NULL) return NULL;
|
||||||
|
array->capa = newcapa;
|
||||||
|
array->data = newarray;
|
||||||
|
}
|
||||||
|
array->data[array->size++] = add;
|
||||||
|
return array->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t* cintarr_getref(cintarr_t* array) {
|
||||||
|
return array->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cintarr_destroy(cintarr_t* array) {
|
||||||
|
if (array == NULL) return;
|
||||||
|
free(array->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cintarr_free(cintarr_t* array) {
|
||||||
|
cintarr_destroy(array);
|
||||||
|
free(array->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Float array */
|
||||||
|
void* cfltarr_init(cfltarr_t* array) {
|
||||||
|
array->data = malloc(INIT_CAPA * sizeof(double));
|
||||||
|
if (array->data == NULL) return NULL;
|
||||||
|
memset(array->data, 0, INIT_CAPA);
|
||||||
|
array->capa = INIT_CAPA;
|
||||||
|
array->size = 0;
|
||||||
|
return array->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* cfltarr_append(cfltarr_t* array, double add) {
|
||||||
|
if (array->size + 1 > array->capa) {
|
||||||
|
size_t newcapa = (array->capa * 10) / 6 ;
|
||||||
|
double* newarray = realloc(array->data, newcapa);
|
||||||
|
if (newarray == NULL) return NULL;
|
||||||
|
array->capa = newcapa;
|
||||||
|
array->data = newarray;
|
||||||
|
}
|
||||||
|
array->data[array->size++] = add;
|
||||||
|
return array->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
double* cfltarr_getref(cfltarr_t* array) {
|
||||||
|
return array->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cfltarr_destroy(cfltarr_t* array) {
|
||||||
|
if (array == NULL) return;
|
||||||
|
free(array->data);
|
||||||
|
}
|
||||||
49
clib/cdynarr.h
Normal file
49
clib/cdynarr.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
#ifndef CDYNARR_H_QWERTY
|
||||||
|
#define CDYNARR_H_QWERTY
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int64_t* data;
|
||||||
|
int capa;
|
||||||
|
int size;
|
||||||
|
} cintarr_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool* data;
|
||||||
|
int capa;
|
||||||
|
int size;
|
||||||
|
} cboolarr_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double* data;
|
||||||
|
int capa;
|
||||||
|
int size;
|
||||||
|
} cfltarr_t;
|
||||||
|
|
||||||
|
|
||||||
|
cintarr_t* new_cintarr(void);
|
||||||
|
void* cintarr_init(cintarr_t* array);
|
||||||
|
void* cintarr_append(cintarr_t* array, int64_t add);
|
||||||
|
int64_t* cintarr_getref(cintarr_t* array);
|
||||||
|
void cintarr_destroy(cintarr_t* array);
|
||||||
|
void cintarr_free(cintarr_t* array);
|
||||||
|
|
||||||
|
cfltarr_t* new_cfltarr(void);
|
||||||
|
void* cfltarr_init(cfltarr_t* array);
|
||||||
|
void* cfltarr_append(cfltarr_t* array, double add);
|
||||||
|
double* cfltarr_getref(cfltarr_t* array);
|
||||||
|
void cfltarr_destroy(cfltarr_t* array);
|
||||||
|
void cfltarr_free(cfltarr_t* array);
|
||||||
|
|
||||||
|
cboolarr_t* new_cboolarr(void);
|
||||||
|
void* cboolarr_init(cboolarr_t* array);
|
||||||
|
void* cboolarr_append(cboolarr_t* array, bool add);
|
||||||
|
bool* cboolarr_getref(cboolarr_t* array);
|
||||||
|
void cboolarr_destroy(cboolarr_t* array);
|
||||||
|
void cboolarr_free(cboolarr_t* array);
|
||||||
|
|
||||||
|
#endif
|
||||||
20
clib/cdynarr_test.c
Normal file
20
clib/cdynarr_test.c
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <massert.h>
|
||||||
|
#include <cdynarr.h>
|
||||||
|
|
||||||
|
|
||||||
|
void test01(void) {
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
test01();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
51
clib/cstring.c
Normal file
51
clib/cstring.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <cstring.h>
|
||||||
|
|
||||||
|
#define INIT_CAPA 64
|
||||||
|
|
||||||
|
/* String container */
|
||||||
|
void* cstring_init(cstring_t* str) {
|
||||||
|
str->data = malloc(INIT_CAPA + 1);
|
||||||
|
if (str->data == NULL) return NULL;
|
||||||
|
memset(str->data, '\0', INIT_CAPA + 1);
|
||||||
|
str->capa = INIT_CAPA;
|
||||||
|
str->size = 0;
|
||||||
|
return str->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void* cstring_append(cstring_t* str, char* add) {
|
||||||
|
size_t addsize = strlen(add);
|
||||||
|
size_t newsize = str->size + addsize;
|
||||||
|
if (newsize >= str->capa) {
|
||||||
|
size_t newcapa = newsize + 1;
|
||||||
|
char* newstr = realloc(str->data, newcapa);
|
||||||
|
if (newstr == NULL) return NULL;
|
||||||
|
str->data = newstr;
|
||||||
|
str->capa = newcapa;
|
||||||
|
}
|
||||||
|
strcpy(&(str->data[str->size]), add);
|
||||||
|
str->data[newsize + 1] = '\0';
|
||||||
|
str->size = newsize;
|
||||||
|
return str->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* cstring_getref(cstring_t* str) {
|
||||||
|
return str->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void cstring_destroy(cstring_t* str) {
|
||||||
|
if (str == NULL) return;
|
||||||
|
free(str->data);
|
||||||
|
}
|
||||||
17
clib/cstring.h
Normal file
17
clib/cstring.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
#ifndef CSTRING_H_QWERTY
|
||||||
|
#define CSTRING_H_QWERTY
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* data;
|
||||||
|
int capa;
|
||||||
|
int size;
|
||||||
|
} cstring_t;
|
||||||
|
|
||||||
|
|
||||||
|
void* cstring_init(cstring_t* str);
|
||||||
|
void* cstring_append(cstring_t* str, char* add);
|
||||||
|
char* cstring_getref(cstring_t* str);
|
||||||
|
void cstring_destroy(cstring_t* str);
|
||||||
|
|
||||||
|
#endif
|
||||||
41
clib/cstring_test.c
Normal file
41
clib/cstring_test.c
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <massert.h>
|
||||||
|
#include <cstring.h>
|
||||||
|
|
||||||
|
|
||||||
|
void test01(void) {
|
||||||
|
|
||||||
|
cstring_t str;
|
||||||
|
cstring_init(&str);
|
||||||
|
cstring_append(&str, "qwerty");
|
||||||
|
cstring_append(&str, "123456");
|
||||||
|
|
||||||
|
cstring_append(&str, "qwerty");
|
||||||
|
cstring_append(&str, "123456");
|
||||||
|
|
||||||
|
cstring_append(&str, "qwerty");
|
||||||
|
cstring_append(&str, "123456");
|
||||||
|
|
||||||
|
cstring_append(&str, "qwerty");
|
||||||
|
cstring_append(&str, "123456");
|
||||||
|
|
||||||
|
cstring_append(&str, "qwerty");
|
||||||
|
cstring_append(&str, "123456");
|
||||||
|
|
||||||
|
printf("%s\n", cstring_getref(&str));
|
||||||
|
|
||||||
|
cstring_destroy(&str);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
test01();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user