diff options
| author | Patrick | 2023-05-16 17:03:50 +0200 |
|---|---|---|
| committer | Patrick | 2023-05-16 17:03:50 +0200 |
| commit | 7cba6b3cdbd3b82ec9092ce9bda8ec20739096cb (patch) | |
| tree | 3a634529c1bee00d2ea98f7d42d9b8cfca6a1410 | |
| parent | d43e8671acc5709c192e159e0d91626f0677cdf1 (diff) | |
| download | matrix_esp_thesis-7cba6b3cdbd3b82ec9092ce9bda8ec20739096cb.tar.gz matrix_esp_thesis-7cba6b3cdbd3b82ec9092ce9bda8ec20739096cb.zip | |
refactor fixedbuffer, add mjson
| -rw-r--r-- | .gitmodules | 3 | ||||
| m--------- | ext/mjson | 0 | ||||
| -rw-r--r-- | src/fixedbuffer.c | 43 | ||||
| -rw-r--r-- | src/fixedbuffer.h | 19 | ||||
| -rw-r--r-- | src/matrix.c | 11 | ||||
| -rw-r--r-- | src/matrix.h | 13 |
6 files changed, 66 insertions, 23 deletions
diff --git a/.gitmodules b/.gitmodules index 3d6f22a..00137af 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "ext/olm"] path = ext/olm url = https://gitlab.matrix.org/matrix-org/olm/ +[submodule "ext/mjson"] + path = ext/mjson + url = https://github.com/cesanta/mjson diff --git a/ext/mjson b/ext/mjson new file mode 160000 +Subproject b766f343bf68b8ea89889055acbf8aef820e2b7 diff --git a/src/fixedbuffer.c b/src/fixedbuffer.c new file mode 100644 index 0000000..ad99897 --- /dev/null +++ b/src/fixedbuffer.c @@ -0,0 +1,43 @@ +#include "fixedbuffer.h"
+
+#include <string.h>
+
+FixedBuffer
+FixedBuf(const char * str)
+{
+ int len = strlen(str);
+ FixedBuffer result;
+ result.ptr = (char *)str;
+ result.cap = len;
+ result.len = len;
+ return result;
+}
+
+bool
+FixedBufferToInt(FixedBuffer fb, int * outInt)
+{
+ bool valid = false;
+ int result = 0;
+
+ bool negative = false;
+
+ for (int i = 0; i < fb.len; i++)
+ {
+ if (i == 0 && fb.ptr[i] == '-')
+ {
+ negative = true;
+ continue;
+ }
+
+ int val = fb.ptr[i] - '0';
+ if (val < 0 || val > 9)
+ return false;
+
+ result *= 10;
+ result += val;
+ valid = true;
+ }
+
+ *outInt = result;
+ return valid;
+}
\ No newline at end of file diff --git a/src/fixedbuffer.h b/src/fixedbuffer.h new file mode 100644 index 0000000..d8055cb --- /dev/null +++ b/src/fixedbuffer.h @@ -0,0 +1,19 @@ +#ifndef FIXEDBUFFER__H
+#define FIXEDBUFFER__H
+
+#include <stdbool.h>
+
+typedef struct FixedBuffer {
+ char * ptr;
+ int cap;
+ int len;
+} FixedBuffer;
+
+FixedBuffer
+FixedBuf(const char * str);
+
+bool
+FixedBufferToInt(FixedBuffer fb, int * outInt);
+
+
+#endif
\ No newline at end of file diff --git a/src/matrix.c b/src/matrix.c index bc0f1ca..082806a 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,16 +1,5 @@ #include "matrix.h"
-FixedBuffer
-FixedBuf(const char * str)
-{
- int len = strlen(str);
- FixedBuffer result;
- result.ptr = (char *)str;
- result.size = len;
- result.len = len;
- return result;
-}
-
bool
MatrixClientInit(
diff --git a/src/matrix.h b/src/matrix.h index d61c59b..5f53e0e 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -6,18 +6,7 @@ #include <olm/olm.h>
-
-
-
-
-typedef struct FixedBuffer {
- void * ptr;
- int size;
- int len;
-} FixedBuffer;
-
-FixedBuffer
-FixedBuf(const char * str);
+#include "fixedbuffer.h"
|
