* Underlying socket
*/
int fd;
+
+ /**
+ * FILE* for convenience functions, or NULL
+ */
+ FILE *file;
};
METHOD(stream_t, read_, ssize_t,
}
}
+METHOD(stream_t, vprint, int,
+ private_stream_t *this, char *format, va_list ap)
+{
+ if (!this->file)
+ {
+ this->file = fdopen(this->fd, "w+");
+ if (!this->file)
+ {
+ return -1;
+ }
+ }
+ return vfprintf(this->file, format, ap);
+}
+
+METHOD(stream_t, print, int,
+ private_stream_t *this, char *format, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, format);
+ ret = vprint(this, format, ap);
+ va_end(ap);
+
+ return ret;
+}
+
METHOD(stream_t, destroy, void,
private_stream_t *this)
{
- close(this->fd);
+ if (this->file)
+ {
+ fclose(this->file);
+ }
+ else
+ {
+ close(this->fd);
+ }
free(this);
}
.public = {
.read = _read_,
.write = _write_,
+ .print = _print,
+ .vprint = _vprint,
.destroy = _destroy,
},
.fd = fd,
*/
ssize_t (*write)(stream_t *this, void *buf, size_t len, bool block);
+ /**
+ * printf() convenience function for this stream.
+ *
+ * @param format printf format string
+ * @param ... argument list for format string
+ * @return number of characters written, negative on error
+ */
+ int (*print)(stream_t *this, char *format, ...);
+
+ /**
+ * vprintf() convenience function for this stream.
+ *
+ * @param format printf format string
+ * @param ap argument list for format string
+ * @return number of characters written, negative on error
+ */
+ int (*vprint)(stream_t *this, char *format, va_list ap);
+
/**
* Destroy a stream_t.
*/