aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-15 15:06:13 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-15 15:06:13 +0300
commite6d5f9552e66ee2db3056e810b58b2bf82551356 (patch)
tree28aba181f716f5ab9df442531d8c72c3c8b4d197 /src/utils.c
parentca08ed93e67a99868f01d21f0d2e34d6a6757c75 (diff)
downloadmagi-e6d5f9552e66ee2db3056e810b58b2bf82551356.tar
magi-e6d5f9552e66ee2db3056e810b58b2bf82551356.tar.xz
magi-e6d5f9552e66ee2db3056e810b58b2bf82551356.zip
Merge remote-tracking branch 'croco/master' into test
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..4212561
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,57 @@
+#include "utils.h"
+
+#include <ctype.h>
+#include <stdlib.h>
+
+
+void magi_str_lowercase(char * str)
+{
+ if (str) {
+ while (*str) {
+ *str = tolower(*str);
+ ++str;
+ }
+ }
+}
+
+char * magi_str_create_copy(const char * begin, const char * end)
+{
+ 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;
+}
+
+char * magi_str_create(int len)
+{
+ char * str = malloc(len + 1);
+ if (str) {
+ str[len] = 0;
+ } else {
+ magi_log("Cannot allocate string.");
+ }
+ return str;
+}
+
+int magi_str_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;
+}