*/
int fd;
- /**
- * FILE* for convenience functions, or NULL
- */
- FILE *file;
-
/**
* Callback if data is ready to read
*/
add_watcher(this);
}
-METHOD(stream_t, vprint, int,
- private_stream_t *this, char *format, va_list ap)
+METHOD(stream_t, get_file, FILE*,
+ private_stream_t *this)
{
- if (!this->file)
+ FILE *file;
+ int fd;
+
+ /* fclose() closes the FD passed to fdopen(), so dup() it */
+ fd = dup(this->fd);
+ if (fd == -1)
{
- this->file = fdopen(this->fd, "w+");
- if (!this->file)
- {
- return -1;
- }
+ return NULL;
}
- 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;
+ file = fdopen(fd, "w+");
+ if (!file)
+ {
+ close(fd);
+ }
+ return file;
}
METHOD(stream_t, destroy, void,
private_stream_t *this)
{
remove_watcher(this);
- if (this->file)
- {
- fclose(this->file);
- }
- else
- {
- close(this->fd);
- }
+ close(this->fd);
free(this);
}
.on_read = _on_read,
.write = _write_,
.on_write = _on_write,
- .print = _print,
- .vprint = _vprint,
+ .get_file = _get_file,
.destroy = _destroy,
},
.fd = fd,
void (*on_write)(stream_t *this, stream_cb_t cb, void *data);
/**
- * printf() convenience function for this stream.
+ * Get a FILE reference for this stream.
*
- * @param format printf format string
- * @param ... argument list for format string
- * @return number of characters written, negative on error
+ * @return FILE*, must be fclose()d, NULL 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);
+ FILE* (*get_file)(stream_t *this);
/**
* Destroy a stream_t.