/** * raptor_iostream_decimal_write: * @integer: integer to format as decimal * @iostr: raptor iostream * * Write an integer in decimal to the iostream. * * Return value: non-0 on failure **/ int raptor_iostream_decimal_write(int integer, raptor_iostream* iostr) { /* enough for 64 bit signed integer * INT64_MAX is 9223372036854775807 (19 digits) + 1 for sign */ unsigned char buf[20]; unsigned char *p; int i = integer; size_t length = 1; if(integer < 0) { length++; i= -integer; } while(i /= 10) length++; p = buf+length-1; i = integer; if(i < 0) i= -i; do { *p-- ='0'+(i %10); i /= 10; } while(i); if(integer < 0) *p= '-'; return raptor_iostream_write_bytes(buf, 1, length, iostr); }
/** * raptor_iostream_format_hexadecimal: * @iostr: raptor iostream * @integer: unsigned integer to format as hexadecimal * @width: field width * * Write an integer in hexadecimal to the iostream. * * Always 0-fills the entire field and writes in uppercase A-F * * Return value: non-0 on failure **/ int raptor_iostream_format_hexadecimal(raptor_iostream* iostr, unsigned int integer, int width) { unsigned char *buf; unsigned char *p; int rc; if(width <1) return 1; buf=(unsigned char*)RAPTOR_MALLOC(cstring, width); if(!buf) return 1; p=buf+width-1; do { unsigned int digit=(integer & 15); *p-- =(digit < 10) ? '0'+digit : 'A'+(digit-10); integer >>= 4; } while(integer); while(p >= buf) *p-- = '0'; rc=raptor_iostream_write_bytes(iostr, buf, 1, width); RAPTOR_FREE(cstring, buf); return rc; }
/** * raptor_iostream_write_uri: * @iostr: raptor iostream * @uri: URI * * Write a raptor URI to the iostream. * * Return value: non-0 on failure **/ int raptor_iostream_write_uri(raptor_iostream* iostr,raptor_uri* uri) { size_t len; const void *string=raptor_uri_as_counted_string(uri, &len); return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len); }
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 void rasqal_rowsource_write_indent(raptor_iostream *iostr, int indent) { while(indent > 0) { int sp = (indent > SPACES_LENGTH) ? SPACES_LENGTH : indent; raptor_iostream_write_bytes(spaces, sizeof(char), sp, iostr); indent -= sp; } }
static void rasqal_rowsource_write_indent(raptor_iostream *iostr, unsigned int indent) { while(indent > 0) { unsigned int sp = (indent > SPACES_LENGTH) ? SPACES_LENGTH : indent; raptor_iostream_write_bytes(spaces, sizeof(char), RASQAL_GOOD_CAST(size_t, sp), iostr); indent -= sp; } }
/** * raptor_stringbuffer_write: * @sb: #raptor_stringbuffer to write * @iostr: raptor iostream * * Write a stringbuffer to an iostream. * * Return value: non-0 on failure **/ int raptor_stringbuffer_write(raptor_stringbuffer *sb, raptor_iostream* iostr) { size_t length; if(!sb) return 1; length = raptor_stringbuffer_length(sb); if(length) { int count = raptor_iostream_write_bytes(raptor_stringbuffer_as_string(sb), 1, length, iostr); return (RAPTOR_BAD_CAST(size_t, count) != length); } else return 0; }
/** * raptor_iostream_write_stringbuffer: * @iostr: raptor iostream * @sb: #raptor_stringbuffer to write * * Write a stringbuffer to an iostream. * * Return value: non-0 on failure **/ int raptor_iostream_write_stringbuffer(raptor_iostream* iostr, raptor_stringbuffer *sb) { int length; if(!sb) return 1; length=(int)raptor_stringbuffer_length(sb); if(length) { int count=raptor_iostream_write_bytes(iostr, raptor_stringbuffer_as_string(sb), 1, length); return (count != length); } else return 0; }
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; }
/** * raptor_iostream_hexadecimal_write: * @integer: unsigned integer to format as hexadecimal * @width: field width * @iostr: raptor iostream * * Write an integer in hexadecimal to the iostream. * * Always 0-fills the entire field and writes in uppercase A-F * * Return value: non-0 on failure **/ int raptor_iostream_hexadecimal_write(unsigned int integer, int width, raptor_iostream* iostr) { char *buf; int rc; if(width < 1) return 1; buf = RAPTOR_MALLOC(char*, width + 1); if(!buf) return 1; (void)raptor_format_integer(buf, width + 1, integer, /* base */ 16, width, '0'); rc = raptor_iostream_write_bytes(buf, 1, width, iostr); RAPTOR_FREE(char*, buf); 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; }
/** * raptor_iostream_write_counted_string: * @iostr: raptor iostream * @string: string * @len: string length * * Write a counted string to the iostream. * * Return value: non-0 on failure **/ int raptor_iostream_write_counted_string(raptor_iostream *iostr, const void *string, size_t len) { return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len); }
/** * raptor_iostream_write_string: * @iostr: raptor iostream * @string: string * * Write a NULL-terminated string to the iostream. * * Return value: non-0 on failure **/ int raptor_iostream_write_string(raptor_iostream *iostr, const void *string) { size_t len=strlen((const char*)string); return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len); }