Browse Source

parsebuffer: type and macros

Max Bruckner 8 years ago
parent
commit
0f98214e71
1 changed files with 16 additions and 0 deletions
  1. 16 0
      cJSON.c

+ 16 - 0
cJSON.c

@@ -185,6 +185,22 @@ static unsigned char get_decimal_point(void)
     return (unsigned char) lconv->decimal_point[0];
 }
 
+typedef struct
+{
+    const unsigned char *content;
+    size_t length;
+    size_t offset;
+} parse_buffer;
+
+/* check if the given size is left to read in a given parse buffer (starting with 1) */
+#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
+#define cannot_read(buffer, size) (!can_read(buffer, size))
+/* check if the buffer can be accessed at the given index (starting with 0) */
+#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
+#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
+/* get a pointer to the buffer at the position */
+#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
+
 /* Parse the input text to generate a number, and populate the result into item. */
 static const unsigned char *parse_number(cJSON * const item, const unsigned char * const input)
 {