Added guac_write_string(), using GUACIO instead of std io functions.

This commit is contained in:
Michael Jumper
2010-09-05 14:21:18 -07:00
parent f8c954c83b
commit be395f197b
3 changed files with 58 additions and 21 deletions

View File

@@ -22,11 +22,37 @@ GUACIO* guac_open(int fd) {
} }
void guac_close(GUACIO* io) { void guac_close(GUACIO* io) {
guac_flush(io);
free(io); free(io);
} }
ssize_t guac_write_string(GUACIO* io, const char* str) {
ssize_t __write_base64_triplet(GUACIO* io, int a, int b, int c) { int fd = io->fd;
char* out_buf = io->out_buf;
int retval;
for (; *str != '\0'; str++) {
out_buf[io->written++] = *str;
/* Flush when necessary, return on error */
if (io->written > 8188 /* sizeof(out_buf) - 4 */) {
retval = write(fd, out_buf, io->written);
if (retval < 0)
return retval;
io->written = 0;
}
}
return 0;
}
ssize_t __guac_write_base64_triplet(GUACIO* io, int a, int b, int c) {
int fd = io->fd; int fd = io->fd;
char* out_buf = io->out_buf; char* out_buf = io->out_buf;
@@ -75,7 +101,7 @@ ssize_t __write_base64_triplet(GUACIO* io, int a, int b, int c) {
} }
ssize_t __write_base64_byte(GUACIO* io, char buf) { ssize_t __guac_write_base64_byte(GUACIO* io, char buf) {
int* ready_buf = io->ready_buf; int* ready_buf = io->ready_buf;
@@ -85,7 +111,7 @@ ssize_t __write_base64_byte(GUACIO* io, char buf) {
/* Flush triplet */ /* Flush triplet */
if (io->ready == 3) { if (io->ready == 3) {
retval = __write_base64_triplet(io, ready_buf[0], ready_buf[1], ready_buf[2]); retval = __guac_write_base64_triplet(io, ready_buf[0], ready_buf[1], ready_buf[2]);
if (retval < 0) if (retval < 0)
return retval; return retval;
@@ -95,7 +121,7 @@ ssize_t __write_base64_byte(GUACIO* io, char buf) {
return 1; return 1;
} }
ssize_t write_base64(GUACIO* io, const void* buf, size_t count) { ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count) {
int retval; int retval;
@@ -104,7 +130,7 @@ ssize_t write_base64(GUACIO* io, const void* buf, size_t count) {
while (char_buf < end) { while (char_buf < end) {
retval = __write_base64_byte(io, *(char_buf++)); retval = __guac_write_base64_byte(io, *(char_buf++));
if (retval < 0) if (retval < 0)
return retval; return retval;
@@ -114,17 +140,10 @@ ssize_t write_base64(GUACIO* io, const void* buf, size_t count) {
} }
ssize_t flush_base64(GUACIO* io) { ssize_t guac_flush(GUACIO* io) {
int retval; int retval;
/* Flush triplet to output buffer */
while (io->ready > 0) {
retval = __write_base64_byte(io, -1);
if (retval < 0)
return retval;
}
/* Flush remaining bytes in buffer */ /* Flush remaining bytes in buffer */
if (io->written > 0) { if (io->written > 0) {
retval = write(io->fd, io->out_buf, io->written); retval = write(io->fd, io->out_buf, io->written);
@@ -138,3 +157,18 @@ ssize_t flush_base64(GUACIO* io) {
} }
ssize_t guac_flush_base64(GUACIO* io) {
int retval;
/* Flush triplet to output buffer */
while (io->ready > 0) {
retval = __guac_write_base64_byte(io, -1);
if (retval < 0)
return retval;
}
return 0;
}

View File

@@ -17,8 +17,10 @@ typedef struct GUACIO {
} GUACIO; } GUACIO;
GUACIO* guac_open(int fd); GUACIO* guac_open(int fd);
ssize_t write_base64(GUACIO* io, const void* buf, size_t count); ssize_t guac_write_string(GUACIO* io, const char* str);
ssize_t flush_base64(GUACIO* io); ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count);
ssize_t guac_flush_base64(GUACIO* io);
ssize_t guac_flush(GUACIO* io);
void guac_close(GUACIO* io); void guac_close(GUACIO* io);
#endif #endif

View File

@@ -8,7 +8,7 @@
void guac_write_png(png_structp png, png_bytep data, png_size_t length) { void guac_write_png(png_structp png, png_bytep data, png_size_t length) {
if (write_base64((GUACIO*) png->io_ptr, data, length) < 0) { if (guac_write_base64((GUACIO*) png->io_ptr, data, length) < 0) {
perror("Error writing PNG"); perror("Error writing PNG");
png_error(png, "Error writing PNG"); png_error(png, "Error writing PNG");
return; return;
@@ -49,7 +49,7 @@ void proxy(int client_fd) {
} }
write(io->fd, "name:hello;size:1024,768;", 25); guac_write_string(io, "name:hello;size:1024,768;");
for (y=0; y<200; y++) { for (y=0; y<200; y++) {
@@ -91,11 +91,11 @@ void proxy(int client_fd) {
PNG_FILTER_TYPE_DEFAULT PNG_FILTER_TYPE_DEFAULT
); );
write(io->fd, "png:0,0,", 8); guac_write_string(io, "png:0,0,");
png_set_rows(png, png_info, png_rows); png_set_rows(png, png_info, png_rows);
png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL); png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL);
if (flush_base64(io) < 0) { if (guac_flush_base64(io) < 0) {
perror("Error flushing PNG"); perror("Error flushing PNG");
png_error(png, "Error flushing PNG"); png_error(png, "Error flushing PNG");
return; return;
@@ -103,15 +103,16 @@ void proxy(int client_fd) {
png_destroy_write_struct(&png, &png_info); png_destroy_write_struct(&png, &png_info);
write(io->fd, ";", 1); guac_write_string(io, ";");
} }
write(io->fd, "error:Test finished.;", 21); guac_write_string(io, "error:Test finished.;");
/* Free PNG data */ /* Free PNG data */
for (y = 0; y<100 /* height */; y++) for (y = 0; y<100 /* height */; y++)
free(png_rows[y]); free(png_rows[y]);
free(png_rows); free(png_rows);
guac_close(io);
} }