static int test_write_to_string(raptor_world *world, const char* test_string, size_t test_string_len, const unsigned int expected_bytes_count) { raptor_iostream *iostr = NULL; unsigned long count; int rc = 0; void *string = NULL; size_t string_len; const char* const label="write iostream to a string"; #if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Testing %s\n", program, label); #endif iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL); if(!iostr) { fprintf(stderr, "%s: Failed to create write iostream to string\n", program); rc = 1; goto tidy; } raptor_iostream_write_bytes(test_string, 1, test_string_len, iostr); raptor_iostream_write_byte('\n', iostr); count = raptor_iostream_tell(iostr); if(count != expected_bytes_count) { fprintf(stderr, "%s: %s wrote %d bytes, expected %d\n", program, label, (int)count, expected_bytes_count); rc = 1; } raptor_free_iostream(iostr); iostr = NULL; if(!string) { fprintf(stderr, "%s: %s failed to create a string\n", program, label); return 1; } if(string_len != count) { fprintf(stderr, "%s: %s created a string length %d, expected %d\n", program, label, (int)string_len, (int)count); return 1; } tidy: if(string) raptor_free_memory(string); if(iostr) raptor_free_iostream(iostr); if(rc) fprintf(stderr, "%s: FAILED Testing %s\n", program, label); return rc; }
static int test_write_to_filename(raptor_world *world, const char* filename, const char* test_string, size_t test_string_len, const unsigned int expected_bytes_count) { raptor_iostream *iostr = NULL; unsigned long count; int rc = 0; const char* const label="write iostream to filename"; #if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Testing %s '%s'\n", program, label, filename); #endif iostr = raptor_new_iostream_to_filename(world, filename); if(!iostr) { fprintf(stderr, "%s: Failed to create %s '%s'\n", program, label, filename); rc = 1; goto tidy; } raptor_iostream_write_bytes(test_string, 1, test_string_len, iostr); raptor_iostream_write_byte('\n', iostr); count = raptor_iostream_tell(iostr); if(count != expected_bytes_count) { fprintf(stderr, "%s: %s wrote %d bytes, expected %d\n", program, label, (int)count, expected_bytes_count); rc = 1; goto tidy; } tidy: if(iostr) raptor_free_iostream(iostr); remove(filename); if(rc) fprintf(stderr, "%s: FAILED Testing %s\n", program, label); return rc; }
static int test_write_to_file_handle(FILE* handle, const char* test_string, size_t test_string_len, const unsigned int expected_bytes_count) { raptor_iostream *iostr=NULL; unsigned long count; int rc=0; const char* const label="write iostream to file handle"; #ifdef RAPTOR_DEBUG fprintf(stderr, "%s: Testing %s\n", program, label); #endif iostr=raptor_new_iostream_to_file_handle(handle); if(!iostr) { fprintf(stderr, "%s: Failed to create %s\n", program, label); rc=1; goto tidy; } raptor_iostream_write_bytes(iostr, test_string, 1, test_string_len); raptor_iostream_write_byte(iostr, '\n'); count=raptor_iostream_tell(iostr); if(count != expected_bytes_count) { fprintf(stderr, "%s: %s wrote %d bytes, expected %d\n", program, label, (int)count, expected_bytes_count); rc=1; } tidy: if(iostr) raptor_free_iostream(iostr); if(rc) fprintf(stderr, "%s: FAILED Testing %s\n", program, label); return rc; }
int main(int argc, char *argv[]) { raptor_world *world; const char *program = raptor_basename(argv[0]); raptor_iostream *iostr; raptor_namespace_stack *nstack; raptor_namespace* foo_ns; raptor_xml_writer* xml_writer; raptor_uri* base_uri; raptor_qname* el_name; raptor_xml_element *element; unsigned long offset; raptor_qname **attrs; raptor_uri* base_uri_copy = NULL; /* for raptor_new_iostream_to_string */ void *string = NULL; size_t string_len = 0; world = raptor_new_world(); if(!world || raptor_world_open(world)) exit(1); iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL); if(!iostr) { fprintf(stderr, "%s: Failed to create iostream to string\n", program); exit(1); } nstack = raptor_new_namespaces(world, 1); xml_writer = raptor_new_xml_writer(world, nstack, iostr); if(!xml_writer) { fprintf(stderr, "%s: Failed to create xml_writer to iostream\n", program); exit(1); } base_uri = raptor_new_uri(world, base_uri_string); foo_ns = raptor_new_namespace(nstack, (const unsigned char*)"foo", (const unsigned char*)"http://example.org/foo-ns#", 0); el_name = raptor_new_qname_from_namespace_local_name(world, foo_ns, (const unsigned char*)"bar", NULL); base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL; element = raptor_new_xml_element(el_name, NULL, /* language */ base_uri_copy); raptor_xml_writer_start_element(xml_writer, element); raptor_xml_writer_cdata_counted(xml_writer, (const unsigned char*)"hello\n", 6); raptor_xml_writer_comment_counted(xml_writer, (const unsigned char*)"comment", 7); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n"); raptor_xml_writer_end_element(xml_writer, element); raptor_free_xml_element(element); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n"); el_name = raptor_new_qname(nstack, (const unsigned char*)"blah", NULL /* no attribute value - element */); base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL; element = raptor_new_xml_element(el_name, NULL, /* language */ base_uri_copy); attrs = (raptor_qname **)RAPTOR_CALLOC(qnamearray, 1, sizeof(raptor_qname*)); attrs[0] = raptor_new_qname(nstack, (const unsigned char*)"a", (const unsigned char*)"b" /* attribute value */); raptor_xml_element_set_attributes(element, attrs, 1); raptor_xml_writer_empty_element(xml_writer, element); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n"); raptor_free_xml_writer(xml_writer); raptor_free_xml_element(element); raptor_free_namespace(foo_ns); raptor_free_namespaces(nstack); raptor_free_uri(base_uri); offset = raptor_iostream_tell(iostr); #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Freeing iostream\n", program); #endif raptor_free_iostream(iostr); if(offset != OUT_BYTES_COUNT) { fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program, (int)offset, (int)OUT_BYTES_COUNT); fputs("[[", stderr); (void)fwrite(string, 1, string_len, stderr); fputs("]]\n", stderr); return 1; } if(!string) { fprintf(stderr, "%s: I/O stream failed to create a string\n", program); return 1; } string_len = strlen((const char*)string); if(string_len != offset) { fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)offset); return 1; } #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Made XML string of %d bytes\n", program, (int)string_len); fputs("[[", stderr); (void)fwrite(string, 1, string_len, stderr); fputs("]]\n", stderr); #endif raptor_free_memory(string); raptor_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { raptor_world *world; const char *program = raptor_basename(argv[0]); raptor_iostream *iostr; raptor_namespace_stack *nstack; raptor_namespace* ex_ns; raptor_turtle_writer* turtle_writer; raptor_uri* base_uri; raptor_qname* el_name; unsigned long count; raptor_uri* datatype; /* for raptor_new_iostream_to_string */ void *string = NULL; size_t string_len = 0; world = raptor_new_world(); if(!world || raptor_world_open(world)) exit(1); iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL); if(!iostr) { fprintf(stderr, "%s: Failed to create iostream to string\n", program); exit(1); } nstack = raptor_new_namespaces(world, 1); base_uri = raptor_new_uri(world, base_uri_string); turtle_writer = raptor_new_turtle_writer(world, base_uri, 1, nstack, iostr); if(!turtle_writer) { fprintf(stderr, "%s: Failed to create turtle_writer to iostream\n", program); exit(1); } raptor_turtle_writer_set_option(turtle_writer, RAPTOR_OPTION_WRITER_AUTO_INDENT, 1); ex_ns = raptor_new_namespace(nstack, (const unsigned char*)"ex", (const unsigned char*)"http://example.org/ns#", 0); raptor_turtle_writer_namespace_prefix(turtle_writer, ex_ns); raptor_turtle_writer_reference(turtle_writer, base_uri); raptor_turtle_writer_increase_indent(turtle_writer); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"ex:foo "); raptor_turtle_writer_quoted_counted_string(turtle_writer, longstr, strlen((const char*)longstr)); raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)" ;", 2); raptor_turtle_writer_newline(turtle_writer); el_name = raptor_new_qname_from_namespace_local_name(world, ex_ns, (const unsigned char*)"bar", NULL); raptor_turtle_writer_qname(turtle_writer, el_name); raptor_free_qname(el_name); raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)" ", 1); datatype = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#decimal"); raptor_turtle_writer_literal(turtle_writer, nstack, (const unsigned char*)"10.0", NULL, datatype); raptor_free_uri(datatype); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_decrease_indent(turtle_writer); raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)".", 1); raptor_turtle_writer_newline(turtle_writer); raptor_free_turtle_writer(turtle_writer); raptor_free_namespace(ex_ns); raptor_free_namespaces(nstack); raptor_free_uri(base_uri); count = raptor_iostream_tell(iostr); #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Freeing iostream\n", program); #endif raptor_free_iostream(iostr); if(count != OUT_BYTES_COUNT) { fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program, (int)count, (int)OUT_BYTES_COUNT); fputs("[[", stderr); (void)fwrite(string, 1, string_len, stderr); fputs("]]\n", stderr); return 1; } if(!string) { fprintf(stderr, "%s: I/O stream failed to create a string\n", program); return 1; } string_len = strlen((const char*)string); if(string_len != count) { fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)count); return 1; } #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Made Turtle string of %d bytes\n", program, (int)string_len); fputs("[[", stderr); (void)fwrite(string, 1, string_len, stderr); fputs("]]\n", stderr); #endif raptor_free_memory(string); raptor_free_world(world); /* keep gcc -Wall happy */ return(0); }