Source

libyaml / src / api.c

Diff from to

src/api.c

 {
     yaml_parser_t *parser;
 
+    /* Allocate the parser structure. */
+
     parser = yaml_malloc(sizeof(yaml_parser_t));
     if (!parser) return NULL;
 
     memset(parser, 0, sizeof(yaml_parser_t));
 
+    /* Allocate the raw buffer. */
+
+    parser->raw_buffer = yaml_malloc(YAML_RAW_BUFFER_SIZE);
+    if (!parser->raw_buffer) {
+        yaml_free(parser);
+        return NULL;
+    }
+    parser->raw_pointer = parser->raw_buffer;
+    parser->raw_unread = 0;
+
+    /* Allocate the character buffer. */
+
+    parser->buffer = yaml_malloc(YAML_BUFFER_SIZE);
+    if (!parser->buffer) {
+        yaml_free(parser->raw_buffer);
+        yaml_free(parser);
+        return NULL;
+    }
+    parser->buffer_end = parser->buffer;
+    parser->pointer = parser->buffer;
+    parser->unread = 0;
+
     return parser;
 }
 
     assert(parser); /* Non-NULL parser object expected. */
 
     yaml_free(parser->buffer);
-    if (!parser->raw_buffer_foreign)
-        yaml_free(parser->raw_buffer);
+    yaml_free(parser->raw_buffer);
 
     memset(parser, 0, sizeof(yaml_parser_t));
 
 }
 
 /*
- * String read handler (always returns error).
+ * String read handler.
  */
 
 static int
 yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
         size_t *size_read)
 {
-    *size_read = 0;
+    yaml_string_input_t *input = data;
+
+    if (input->current == input->end) {
+        *size_read = 0;
+        return 1;
+    }
+
+    if (size > (input->end - input->current)) {
+        size = input->end - input->current;
+    }
+
+    memcpy(buffer, input->current, size);
+    input->current += size;
+    *size_read = size;
     return 1;
 }
 
 yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
         size_t *size_read)
 {
-    *size_read = fread(buffer, 1, size, (FILE *)ext);
-    return !ferror((FILE *)ext);
+    *size_read = fread(buffer, 1, size, (FILE *)data);
+    return !ferror((FILE *)data);
 }
 
 /*
         unsigned char *input, size_t size)
 {
     assert(parser); /* Non-NULL parser object expected. */
-    assert(!parser->reader); /* You can set the source only once. */
+    assert(!parser->read_handler);  /* You can set the source only once. */
     assert(input);  /* Non-NULL input string expected. */
 
+    parser->string_input.start = input;
+    parser->string_input.current = input;
+    parser->string_input.end = input+size;
+
     parser->read_handler = yaml_string_read_handler;
-    parser->read_handler_data = NULL;
-
-    /* We use the input string as a raw (undecoded) buffer. */
-    parser->raw_buffer = input; 
-    parser->raw_buffer_size = size;
-    parser->raw_buffer_foreign = 1;
+    parser->read_handler_data = &parser->string_input;
 }
 
 /*
 yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
 {
     assert(parser); /* Non-NULL parser object expected. */
-    assert(!parser->reader); /* You can set the source only once. */
+    assert(!parser->read_handler);  /* You can set the source only once. */
     assert(file);   /* Non-NULL file object expected. */
 
     parser->read_handler = yaml_file_read_handler;
         yaml_read_handler_t *handler, void *data)
 {
     assert(parser); /* Non-NULL parser object expected. */
-    assert(!parser->reader); /* You can set the source only once. */
+    assert(!parser->read_handler);  /* You can set the source only once. */
     assert(handler);    /* Non-NULL read handler expected. */
 
     parser->read_handler = handler;
-    parser->read_handler_data = data
+    parser->read_handler_data = data;
 }
 
 /*
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.