You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

204 lines
3.2 KiB

#include "libc99.h"
#include <stdlib.h>
#include <string.h>
C99_C* c99_C_new()
{
C99_C* ret = calloc(1, sizeof(C99_C));
return ret;
}
void c99_C_free(C99_C* c)
{
if (c) {
free(c->pi);
free(c);
}
}
C99_B* c99_B_new()
{
C99_B* result = calloc(1, sizeof(C99_B));
return result;
}
void c99_B_free(C99_B* b)
{
if (b) {
c99_C_free(b->c);
free(b);
}
}
C99_A* c99_A_new()
{
C99_A* result = calloc(1, sizeof(C99_A));
return result;
}
void c99_A_free(C99_A* a)
{
if (a) {
c99_B_free(a->b);
c99_A_free(a);
}
}
// -------------------------------------------------------------------------------------------------
// USE
// ---
// dont do anything
C99_Status c99_use_basic(int basic)
{
return OK;
}
C99_Status c99_use_struct(const C99_A* a)
{
if (!a) {
return ERROR;
}
return OK;
}
C99_Status c99_use_string(const char* c_str)
{
if (!c_str) {
return ERROR;
}
return OK;
}
// SUPPLY
// ------
// modify value
C99_Status c99_supply_basic(int* basic)
{
if (!basic) {
return ERROR;
}
*basic *= 2;
return OK;
}
// modify struct elem
// Replace object in a struct
C99_Status c99_supply_struct(C99_A* a)
{
// validate input
if (!a) {
return ERROR;
}
if (!a->b) {
return ERROR;
}
if (!a->b->c) {
return ERROR;
}
if (!a->b->c->pi) {
return ERROR;
}
// get current value
int curr_val = *(a->b->c->pi);
// calculate new value
int new_val = curr_val * 2;
// replace with new object
c99_B_free(a->b);
a->b = c99_B_new();
a->b->c = c99_C_new();
a->b->c->pi = calloc(1, sizeof(int));
*a->b->c->pi = new_val;
return OK;
}
// prepend pEp
C99_Status c99_supply_string(char** c_str)
{
if (!c_str) {
return ERROR;
}
if (!*c_str) {
return ERROR;
}
char pEp[] = "pEp";
size_t pEp_len = strlen(pEp);
size_t c_str_len = strlen(*c_str);
char* new_str = (char*)malloc(c_str_len + pEp_len);
memcpy(new_str, pEp, pEp_len);
memcpy(new_str + pEp_len, *c_str, c_str_len);
free(*c_str);
c_str = &new_str;
return OK;
}
// PROVIDE
// -------
// dont do anything
C99_Status c99_provide_basic(int basic)
{
return OK;
}
// use and free the struct
C99_Status c99_provide_struct(C99_A* a)
{
if (!a) {
return ERROR;
}
c99_A_free(a);
return OK;
}
// use and free the string
C99_Status c99_provide_string(char* c_str)
{
if (!c_str) {
return ERROR;
}
free(c_str);
return OK;
}
// RETURN
// ------
// create a new instance and return it
C99_Status c99_return_basic(int** basic)
{
if (!basic) {
return ERROR;
}
*basic = NULL;
int* new_basic = calloc(1, sizeof(int));
*basic = new_basic;
return OK;
}
// create a new instance and return it
C99_Status c99_return_struct(C99_A** a)
{
if (!a) {
return ERROR;
}
*a = NULL;
C99_A* new_a = c99_A_new();
*a = new_a;
return OK;
}
// create a new instance and return it
C99_Status c99_return_string(char** c_str)
{
if (!c_str) {
return ERROR;
}
*c_str = NULL;
char* new_str = strdup("pEp");
*c_str = new_str;
return OK;
}