aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-13 19:16:21 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-13 19:16:21 +0300
commit23f3f8592c21ee58b0ac040736b5b766d52de193 (patch)
tree43e41480fab64d68274340f8fd934bd20c5f27b2 /src/utils.h
parentb11b1c52280f172ebfa42c3da906ea9aa3ea8799 (diff)
downloadmagi-23f3f8592c21ee58b0ac040736b5b766d52de193.tar
magi-23f3f8592c21ee58b0ac040736b5b766d52de193.tar.xz
magi-23f3f8592c21ee58b0ac040736b5b766d52de193.zip
Cosmetical changes.
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h62
1 files changed, 57 insertions, 5 deletions
diff --git a/src/utils.h b/src/utils.h
index a6c0aff..f16ea3f 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -2,7 +2,7 @@
#define MAGI_INCLUDED_UTILS
-static void lowercase(char *str)
+static void lowercase(char * str)
{
if (str) {
while (*str) {
@@ -12,20 +12,22 @@ static void lowercase(char *str)
}
}
-static char *create_str(const char *begin, const char *end)
+static char * create_str(const char * begin, const char * end)
{
- char *res;
+ char * res;
res = malloc(end - begin + 1);
if (res) {
memcpy(res, begin, end - begin);
res[end - begin] = 0;
+ } else {
+ magi_log("Cannot allocate string.");
}
return res;
}
-static char *str_alloc(int len)
+static char * str_alloc(int len)
{
- char *str = malloc(len + 1);
+ char * str = malloc(len + 1);
if (str) {
str[len] = 0;
} else {
@@ -34,5 +36,55 @@ static char *str_alloc(int len)
return str;
}
+/* Shouldn't be called with 'c' as not hex digit. */
+static char from_hex(char c)
+{
+ char num;
+ if (isdigit(c)) {
+ num = c - '0';
+ } else {
+ num = toupper(c) - 'A' + 10;
+ }
+ return num;
+}
+
+static int is_hex(char c)
+{
+ return isdigit(c) || strchr("ABCDEF", toupper(c));
+}
+
+static int add(char ** dest, int * len, int * size, char c)
+{
+ int ok = 1;
+ if (*len + 1 == *size) {
+ *size *= 2;
+ *dest = realloc(*dest, *size);
+ }
+ if (*dest == 0) {
+ ok = 0;
+ magi_log("Cannot allocate string.");
+ } else {
+ (*dest)[*len] = c;
+ ++*len;
+ (*dest)[*len] = 0;
+ }
+ return ok;
+}
+
+static int is_token(char c)
+{
+ return 32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={} \t", c);
+}
+
+static int is_str_token(char * str)
+{
+ int is = str && *str; /* Empty string is not valid. */
+ while (is && *str) {
+ is = is_token(*str);
+ ++str;
+ }
+ return is;
+}
+
#endif