]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/fixedbuffer.c
refactor fixedbuffer, add mjson
[matrix_esp_thesis] / src / fixedbuffer.c
diff --git a/src/fixedbuffer.c b/src/fixedbuffer.c
new file mode 100644 (file)
index 0000000..ad99897
--- /dev/null
@@ -0,0 +1,43 @@
+#include "fixedbuffer.h"\r
+\r
+#include <string.h>\r
+\r
+FixedBuffer\r
+FixedBuf(const char * str)\r
+{\r
+    int len = strlen(str);\r
+    FixedBuffer result;\r
+    result.ptr = (char *)str;\r
+    result.cap = len;\r
+    result.len = len;\r
+    return result;\r
+}\r
+\r
+bool\r
+FixedBufferToInt(FixedBuffer fb, int * outInt)\r
+{\r
+    bool valid = false;\r
+    int result = 0;\r
+\r
+    bool negative = false;\r
+\r
+    for (int i = 0; i < fb.len; i++)\r
+    {\r
+        if (i == 0 && fb.ptr[i] == '-')\r
+        {\r
+            negative = true;\r
+            continue;\r
+        }\r
+\r
+        int val = fb.ptr[i] - '0';\r
+        if (val < 0 || val > 9)\r
+            return false;\r
+\r
+        result *= 10;\r
+        result += val;\r
+        valid = true;\r
+    }\r
+\r
+    *outInt = result;\r
+    return valid;\r
+}
\ No newline at end of file