Skip to content

Commit

Permalink
improves init_string, fixes linter warnings, make internal functions …
Browse files Browse the repository at this point in the history
…static
  • Loading branch information
auyer committed Oct 17, 2023
1 parent a1725d1 commit c8a97ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
18 changes: 3 additions & 15 deletions sdks/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
void print_memkv_result(memkv_result *response) {
if (!response) {
fprintf(stderr, "Error: response is NULL\n");
return;
}
if (response->success) {

} else if (response->success) {
printf("Success! Result: %s\n", response->result);
} else {
fprintf(stderr, "Error: %s\n", response->error);
}
free(response);
}

int main(void) {
Expand All @@ -37,71 +37,59 @@ int main(void) {
fprintf(stdout, "\nPut Key '%s'\n", key);
response = memkv_put_key(client, key, put_body);
print_memkv_result(response);
free(response);

// list key
fprintf(stdout, "\nList Keys\n");
response = memkv_list_keys(client);
print_memkv_result(response);
free(response);

// get key
fprintf(stdout, "\nGet Key '%s'\n", key);
response = memkv_get_key(client, key);
print_memkv_result(response);
free(response);

fprintf(stdout, "\nPut Key '%s'\n", key2);
response = memkv_put_key(client, key2, put_body);
print_memkv_result(response);
free(response);

fprintf(stdout, "\nPut Key '%s'\n", key3);
response = memkv_put_key(client, key3, put_body);
print_memkv_result(response);
free(response);

fprintf(stdout, "\nPut Key '%s' (again)\n", key3);
response = memkv_put_key(client, key3, put_body);
print_memkv_result(response);
free(response);

char prefix[] = "c";

fprintf(stdout, "\nList Keys With Prefix '%s'\n", prefix);
response = memkv_list_keys_with_prefix(client, prefix);
print_memkv_result(response);
free(response);

prefix[0] = 'a';
fprintf(stdout, "\nList Keys With Prefix '%s'\n", prefix);
response = memkv_list_keys_with_prefix(client, prefix);
print_memkv_result(response);
free(response);

// delete key
prefix[0] = 'c';
fprintf(stdout, "\nMaking a delete Key Request with Prefix '%s'\n", prefix);
response = memkv_delete_keys_with_prefix(client, prefix);
print_memkv_result(response);
free(response);

// delete all keys
fprintf(stdout, "\nDelete all Keys\n");
response = memkv_delete_all_keys(client);
print_memkv_result(response);
free(response);

fprintf(stdout, "\nMaking a delete Key '%s'\n", key3);
response = memkv_delete_key(client, key3);
print_memkv_result(response);
free(response);

// list key
fprintf(stdout, "\nList Keys\n");
response = memkv_list_keys(client);
print_memkv_result(response);
free(response);

free(client);
}
53 changes: 29 additions & 24 deletions sdks/c/libMemoryKV/libMemoryKV.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,37 @@ typedef struct string_carrier {
} string_carrier;

// init_string_carrier initializes the string_carrier struct to store the body results from curl
void init_string_carrier(string_carrier *s) {
if (s == NULL) {
printf("Error in init_string_carrier");
return;
}
memset(s, 0, sizeof *s);
static void init_string_carrier(string_carrier *s) {
s->len = 0;
s->error_flag = false;
s->ptr = malloc(s->len + 1);
memset(s->ptr, 0, sizeof *s->ptr);

if (s->ptr == NULL) {
fprintf(stderr, "memkv client: Error in INIT, malloc() failed\n");
if (s->ptr != NULL) {
// set ptr as null terminator
s->ptr[0] = '\0';
} else {
// set error flag to true if malloc fails
s->error_flag = true;
}
}
fprintf(stderr, "memkv client: Error in INIT, malloc() failed\n");
}
}

string_carrier *new_string_carrier() {
static string_carrier *new_string_carrier() {
string_carrier *s = malloc(sizeof *s);
if (!s) {
free(s);
return NULL;
}
init_string_carrier(s);
if (s == NULL || s->error_flag || s->ptr == NULL) {
if (s->error_flag || s->ptr == NULL) {
free(s);
printf("Error in new_string_carrier\n");
return NULL;
}
return s;
}

// string_carrier_writefunc is the callback function to read the body from libcurl into a string_carrier
size_t string_carrier_writefunc(void *ptr, size_t size, size_t nmemb, struct string_carrier *s) {
static size_t string_carrier_writefunc(void *ptr, size_t size, size_t nmemb, struct string_carrier *s) {
// new length to realocate response chunks from libcurl
size_t new_len = s->len + (size * nmemb);
void *const tmp = realloc(s->ptr, new_len + 1);
Expand All @@ -48,15 +52,16 @@ size_t string_carrier_writefunc(void *ptr, size_t size, size_t nmemb, struct str
} else {
/* Now s->ptr points to the new chunk of memory. */
s->ptr = tmp;
// we already know the length of the string, so we can just copy it with memcpy instead of strlcpy
memcpy(s->ptr + s->len, ptr, new_len +1);
// we already know the length of the string, so we can just copy it with memcpy instead of strlcpy
memcpy(s->ptr + s->len, ptr, new_len + 1);
s->len = new_len;
}

return size * nmemb;
}

// memkv_client
// memkv_client stores the configurations used to connect to the MemoryKV server
// use `memkv_client_new` to create one, or `memkv_init_client` to initialize an existing instance
typedef struct memkv_client {
char *host;
} memkv_client;
Expand All @@ -76,7 +81,7 @@ memkv_client *memkv_client_new(char *host) {
return client;
}

char *build_url(const char *host, const char *params) {
static char *build_url(const char *host, const char *params) {
// snprintf returns the number of characters that would have been written if called with NULL
unsigned long size_needed = snprintf(NULL, 0, "%s/%s", host, params);
// use that number to allocate a buffer of the right size
Expand All @@ -97,7 +102,7 @@ typedef struct {
static const char base_curl_error[] = "Error from curl";
static const char unknown_error_msg[] = "unknown error";

void make_curl_error(memkv_result *r, const char *err) {
static void make_curl_error(memkv_result *r, const char *err) {
r->success = false;

if (strlen(err) == 0) {
Expand Down Expand Up @@ -131,7 +136,7 @@ memkv_result *init_memkv_result() {
return r;
}

memkv_result *memkv_execute_request(
static memkv_result *memkv_execute_request(
const char *url,
const char *custom_req,
const char *content_type,
Expand Down Expand Up @@ -207,9 +212,9 @@ memkv_result *memkv_execute_request(
}

r->success = true;
// updates the result pointer to point to the string_carrier->ptr
r-> result = s->ptr;
free(s);
// updates the result pointer to point to the string_carrier->ptr
r->result = s->ptr;
free(s);

return r;
}
Expand Down

0 comments on commit c8a97ae

Please sign in to comment.