|
@ -2,27 +2,203 @@ |
|
|
#include <stdlib.h> |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
Test_struct1* new_test_struct(int c_int, Test_enum c_enum, const char* c_str) |
|
|
C99_C* c99_C_new() |
|
|
{ |
|
|
{ |
|
|
Test_struct1* result = calloc(1, sizeof(Test_struct1)); |
|
|
C99_C* ret = calloc(1, sizeof(C99_C)); |
|
|
if (result) { |
|
|
return ret; |
|
|
result->c_int = c_int; |
|
|
} |
|
|
result->c_enum = c_enum; |
|
|
|
|
|
if (c_str) { |
|
|
void c99_C_free(C99_C* c) |
|
|
result->c_str = strdup(c_str); |
|
|
{ |
|
|
if (result->c_str == NULL) { |
|
|
if (c) { |
|
|
free_test_struct(result); |
|
|
free(c->pi); |
|
|
return NULL; |
|
|
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; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void free_test_struct(Test_struct1* c_struct) |
|
|
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) |
|
|
{ |
|
|
{ |
|
|
if (c_struct) { |
|
|
// validate input
|
|
|
free(c_struct->c_str); |
|
|
if (!a) { |
|
|
free(c_struct); |
|
|
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; |
|
|
|
|
|
} |