Browse Source

c code rewritten to c99 without gnu extensions

pull/1/head
Ivan Polyakov 2 years ago
parent
commit
5751e17f98
  1. 2
      .clangd
  2. 5
      c/keyval.c
  3. 8
      c/query.c
  4. 10
      c/url.c
  5. 29
      c/utils.c
  6. 4
      c/utils.h
  7. 2
      config.mk

2
.clangd

@ -13,7 +13,7 @@ CompileFlags:
If: If:
PathMatch: [.*\.c, .*\.h] PathMatch: [.*\.c, .*\.h]
CompileFlags: CompileFlags:
Add: [-std=gnu99] Add: [-std=c99]
--- ---

5
c/keyval.c

@ -2,6 +2,7 @@
/* Copyright 2022 Ivan Polyakov */ /* Copyright 2022 Ivan Polyakov */
#include "keyval.h" #include "keyval.h"
#include "utils.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -57,8 +58,8 @@ int rpd_keyval_insert(rpd_keyval *keyval, const char *key, const char *value)
keyval->size++; keyval->size++;
} }
item->key = strdup(key); item->key = rpd_strdup(key);
item->val = strdup(value); item->val = rpd_strdup(value);
return item->key && item->val ? 0 : 2; return item->key && item->val ? 0 : 2;
} }

8
c/query.c

@ -17,7 +17,7 @@ int rpd_query_parse(rpd_keyval *dest, const char *src)
return 0; return 0;
} }
char *query = strdup(src); char *query = rpd_strdup(src);
if (!query) if (!query)
return 1; return 1;
@ -28,7 +28,7 @@ int rpd_query_parse(rpd_keyval *dest, const char *src)
char *tokens = query, *p = query; char *tokens = query, *p = query;
int i = 0; int i = 0;
while ((p = strsep(&tokens, "&\n")) && i < len) { while ((p = rpd_strsep(&tokens, "&\n")) && i < len) {
char *param, *val; char *param, *val;
if (*p == '=') if (*p == '=')
@ -37,8 +37,8 @@ int rpd_query_parse(rpd_keyval *dest, const char *src)
val = strtok(NULL, "="); val = strtok(NULL, "=");
if (param) { if (param) {
dest->items[i].key = strdup(param); dest->items[i].key = rpd_strdup(param);
dest->items[i].val = val ? strdup(val) : NULL; dest->items[i].val = val ? rpd_strdup(val) : NULL;
i++; i++;
} }
} }

10
c/url.c

@ -23,11 +23,11 @@ int rpd_url_parse(rpd_url *dest, const char *src)
i = 0; i = 0;
char *tmp, *token; char *tmp, *token;
tmp = strdup(src); tmp = rpd_strdup(src);
while ((token = strsep(&tmp, "/"))) { while ((token = rpd_strsep(&tmp, "/"))) {
if (!strlen(token)) if (!strlen(token))
continue; continue;
dest->parts[i] = strdup(token); dest->parts[i] = rpd_strdup(token);
i++; i++;
} }
free(tmp); free(tmp);
@ -65,7 +65,7 @@ int rpd_url_params_parse_keys(rpd_keyval *dest, const rpd_url *tpl)
for (int i = 0; i < tpl->parts_len; i++) { for (int i = 0; i < tpl->parts_len; i++) {
if (*tpl->parts[i] == ':') { if (*tpl->parts[i] == ':') {
dest->items[dest->size].key = strdup(tpl->parts[i] + 1); dest->items[dest->size].key = rpd_strdup(tpl->parts[i] + 1);
dest->size++; dest->size++;
} }
} }
@ -78,7 +78,7 @@ int rpd_url_params_parse_vals(rpd_keyval *dest, const rpd_url *url, const rpd_ur
int i = 0, j = 0; int i = 0, j = 0;
while (i < tpl->parts_len) { while (i < tpl->parts_len) {
if (*tpl->parts[i] == ':') { if (*tpl->parts[i] == ':') {
dest->items[j++].val = strdup(url->parts[i]); dest->items[j++].val = rpd_strdup(url->parts[i]);
} }
i++; i++;
} }

29
c/utils.c

@ -2,6 +2,8 @@
/* Copyright 2022 Ivan Polyakov */ /* Copyright 2022 Ivan Polyakov */
#include "utils.h" #include "utils.h"
#include <stdlib.h>
#include <string.h>
int count_char_entries(const char *str, char ch) int count_char_entries(const char *str, char ch)
{ {
@ -14,3 +16,30 @@ int count_char_entries(const char *str, char ch)
} }
return cnt; return cnt;
} }
char *rpd_strdup(const char *src)
{
size_t size = strlen(src) + 1;
char *dest = (char *) malloc(sizeof(char) * size);
if (!dest) {
return NULL;
}
return memcpy(dest, src, size);
}
char *rpd_strsep(char **str, const char *sep)
{
char *s = *str, *end;
if (!s) {
return NULL;
}
end = s + strcspn(s, sep);
if (*end) {
*end++ = 0;
} else {
end = 0;
}
*str = end;
return s;
}

4
c/utils.h

@ -6,4 +6,8 @@
int count_char_entries(const char *str, char ch); int count_char_entries(const char *str, char ch);
char *rpd_strdup(const char *src);
char *rpd_strsep(char **str, const char *sep);
#endif /* RAPIDA_UTILS_H_ENTRY */ #endif /* RAPIDA_UTILS_H_ENTRY */

2
config.mk

@ -3,7 +3,7 @@ VERSION=0.2.2
PREFIX=/usr/local PREFIX=/usr/local
CC=gcc CC=gcc
CFLAGS=-std=gnu99 -pedantic -Iinclude CFLAGS=-std=c99 -pedantic -Iinclude
CXX=c++ CXX=c++
CXXFLAGS=-pedantic -Iinclude CXXFLAGS=-pedantic -Iinclude
CXXSTD=-ansi CXXSTD=-ansi

Loading…
Cancel
Save