printers CHANGE modify basic printers' functions to provide number of printed bytes
diff --git a/src/printer.c b/src/printer.c
index 137fe69..4eb6b74 100644
--- a/src/printer.c
+++ b/src/printer.c
@@ -485,14 +485,14 @@
free(out);
}
-API LY_ERR
+API ssize_t
ly_print(struct ly_out *out, const char *format, ...)
{
int count = 0;
char *msg = NULL, *aux;
va_list ap;
- LYOUT_CHECK(out, out->status);
+ LYOUT_CHECK(out, -1 * out->status);
va_start(ap, format);
@@ -504,7 +504,8 @@
#else
/* never should be here since ly_out_fd() is supposed to set type to LY_OUT_FDSTREAM in case vdprintf() is missing */
LOGINT(NULL);
- return LY_EINT;
+ va_end(ap);
+ return -LY_EINT;
#endif
case LY_OUT_FDSTREAM:
case LY_OUT_FILEPATH:
@@ -523,7 +524,7 @@
out->method.mem.size = 0;
LOGMEM(NULL);
va_end(ap);
- return LY_EMEM;
+ return -LY_EMEM;
}
*out->method.mem.buf = aux;
out->method.mem.size = out->method.mem.len + count + 1;
@@ -542,6 +543,8 @@
break;
case LY_OUT_ERROR:
LOGINT(NULL);
+ va_end(ap);
+ return -LY_EINT;
}
va_end(ap);
@@ -549,14 +552,14 @@
if (count < 0) {
LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno));
out->status = LY_ESYS;
- return LY_ESYS;
+ return -LY_ESYS;
} else {
if (out->type == LY_OUT_FDSTREAM) {
/* move the original file descriptor to the end of the output file */
lseek(out->method.fdstream.fd, 0, SEEK_END);
}
out->printed += count;
- return LY_SUCCESS;
+ return count;
}
}
@@ -588,12 +591,12 @@
out->buf_size = out->buf_len = 0;
}
-API LY_ERR
+API ssize_t
ly_write(struct ly_out *out, const char *buf, size_t len)
{
int written = 0;
- LYOUT_CHECK(out, out->status);
+ LYOUT_CHECK(out, -1 * out->status);
if (out->hole_count) {
/* we are buffering data after a hole */
@@ -602,14 +605,15 @@
if (!out->buffered) {
out->buf_len = 0;
out->buf_size = 0;
- LOGMEM_RET(NULL);
+ LOGMEM(NULL);
+ return -LY_EMEM;
}
out->buf_size = out->buf_len + len;
}
memcpy(&out->buffered[out->buf_len], buf, len);
out->buf_len += len;
- return LY_SUCCESS;
+ return len;
}
repeat:
@@ -620,7 +624,8 @@
if (!*out->method.mem.buf) {
out->method.mem.len = 0;
out->method.mem.size = 0;
- LOGMEM_RET(NULL);
+ LOGMEM(NULL);
+ return -LY_EMEM;
}
out->method.mem.size = out->method.mem.len + len + 1;
}
@@ -629,7 +634,7 @@
(*out->method.mem.buf)[out->method.mem.len] = '\0';
out->printed += len;
- return LY_SUCCESS;
+ return len;
case LY_OUT_FD:
written = write(out->method.fd, buf, len);
break;
@@ -643,6 +648,7 @@
break;
case LY_OUT_ERROR:
LOGINT(NULL);
+ return -LY_EINT;
}
if (written < 0) {
@@ -651,25 +657,25 @@
}
LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno));
out->status = LY_ESYS;
- return LY_ESYS;
+ return -LY_ESYS;
} else if ((size_t)written != len) {
LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (unable to write %u from %u data).", __func__, len - (size_t)written, len);
out->status = LY_ESYS;
- return LY_ESYS;
+ return -LY_ESYS;
} else {
if (out->type == LY_OUT_FDSTREAM) {
/* move the original file descriptor to the end of the output file */
lseek(out->method.fdstream.fd, 0, SEEK_END);
}
out->printed += written;
- return LY_SUCCESS;
+ return written;
}
}
-LY_ERR
+ssize_t
ly_write_skip(struct ly_out *out, size_t count, size_t *position)
{
- LYOUT_CHECK(out, out->status);
+ LYOUT_CHECK(out, -1 * out->status);
switch (out->type) {
case LY_OUT_MEMORY:
@@ -678,7 +684,7 @@
if (!(*out->method.mem.buf)) {
out->method.mem.len = 0;
out->method.mem.size = 0;
- out->status = LY_ESYS;
+ out->status = LY_EMEM;
LOGMEM_RET(NULL);
}
out->method.mem.size = out->method.mem.len + count;
@@ -704,8 +710,9 @@
if (!out->buffered) {
out->buf_len = 0;
out->buf_size = 0;
- out->status = LY_ESYS;
- LOGMEM_RET(NULL);
+ out->status = LY_EMEM;
+ LOGMEM(NULL);
+ return -LY_EMEM;
}
out->buf_size = out->buf_len + count;
}
@@ -722,17 +729,18 @@
break;
case LY_OUT_ERROR:
LOGINT(NULL);
+ return -LY_EINT;
}
- return LY_SUCCESS;
+ return count;
}
-LY_ERR
+ssize_t
ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t count)
{
- LY_ERR ret = LY_SUCCESS;
+ ssize_t ret = LY_SUCCESS;
- LYOUT_CHECK(out, out->status);
+ LYOUT_CHECK(out, -1 * out->status);
switch (out->type) {
case LY_OUT_MEMORY:
@@ -745,8 +753,9 @@
case LY_OUT_FILE:
case LY_OUT_CALLBACK:
if (out->buf_len < position + count) {
- out->status = LY_ESYS;
- LOGMEM_RET(NULL);
+ out->status = LY_EMEM;
+ LOGMEM(NULL);
+ return -LY_EMEM;
}
/* write into the hole */
@@ -764,11 +773,12 @@
break;
case LY_OUT_ERROR:
LOGINT(NULL);
+ return -LY_EINT;
}
if (out->type == LY_OUT_FILEPATH) {
/* move the original file descriptor to the end of the output file */
lseek(out->method.fdstream.fd, 0, SEEK_END);
}
- return ret;
+ return ret < 0 ? (-1 * ret) : LY_SUCCESS;
}
diff --git a/src/printer.h b/src/printer.h
index f9ac3af..b0c188b 100644
--- a/src/printer.h
+++ b/src/printer.h
@@ -143,9 +143,10 @@
* @brief Get or change memory where the data are dumped.
*
* @param[in] out Printer handler.
- * @param[in] strp A new string pointer to store the resulting data, same rules as in ly_out_new_memory() are applied.
+ * @param[in] strp Optional new string pointer to store the resulting data, same rules as in ly_out_new_memory() are applied.
* @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data
- * is newly allocated even if @p strp points to a pointer to an existing buffer.
+ * is newly allocated even if @p strp points to a pointer to an existing buffer. In case the @p strp is NULL, this
+ * parameter is ignored.
* @return Previous dumped data. Note that the caller is responsible to free the data in case of changing string pointer @p strp.
*/
char *ly_out_memory(struct ly_out *out, char **strp, size_t size);
@@ -181,8 +182,10 @@
* @param[in] out Output specification.
* @param[in] format format string to be printed.
* @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
+ * @return The number of printed bytes.
+ * @return Negative value in case of error, absolute value of the return code maps to LY_ERR value.
*/
-LY_ERR ly_print(struct ly_out *out, const char *format, ...);
+ssize_t ly_print(struct ly_out *out, const char *format, ...);
/**
* @brief Generic printer of the given string buffer into the specified output.
@@ -195,9 +198,10 @@
* @param[in] out Output specification.
* @param[in] buf Memory buffer with the data to print.
* @param[in] len Length of the data to print in the @p buf.
- * @return LY_ERR value, number of the printed bytes is updated in ly_out::printed.
+ * @return The number of printed bytes.
+ * @return Negative value in case of error, absolute value of the return code maps to LY_ERR value.
*/
-LY_ERR ly_write(struct ly_out *out, const char *buf, size_t len);
+ssize_t ly_write(struct ly_out *out, const char *buf, size_t len);
/**
* @brief Free the printer handler.
diff --git a/src/printer_internal.h b/src/printer_internal.h
index 35102b5..7a000f2 100644
--- a/src/printer_internal.h
+++ b/src/printer_internal.h
@@ -163,10 +163,11 @@
* @param[in] out Output specification.
* @param[in] len Length of the created hole.
* @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call.
- * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
+ * @return The number of bytes prepared for write. The number of the printed bytes is updated in lyout::printed
* only in case the data are really written into the output.
+ * @return Negative value in case of error, absolute value of the return code maps to LY_ERR value.
*/
-LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position);
+ssize_t ly_write_skip(struct ly_out *out, size_t len, size_t *position);
/**
* @brief Write data into the hole at given position.
@@ -176,9 +177,10 @@
* @param[in] buf Memory buffer with the data to print.
* @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
* to the len value specified in the corresponding ly_write_skip() call.
- * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
+ * @return The number of bytes prepared for write. The number of the printed bytes is updated in lyout::printed
* only in case the data are really written into the output.
+ * @return Negative value in case of error, absolute value of the return code maps to LY_ERR value.
*/
-LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
+ssize_t ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
#endif /* LY_PRINTER_INTERNAL_H_ */
diff --git a/src/xml.c b/src/xml.c
index 00b8699..dcc1ca3 100644
--- a/src/xml.c
+++ b/src/xml.c
@@ -1031,7 +1031,7 @@
LY_ERR
lyxml_dump_text(struct ly_out *out, const char *text, int attribute)
{
- LY_ERR ret = LY_SUCCESS;
+ ssize_t ret = LY_SUCCESS;
unsigned int u;
if (!text) {
@@ -1057,11 +1057,11 @@
}
/* falls through */
default:
- ly_write(out, &text[u], 1);
+ ret = ly_write(out, &text[u], 1);
}
}
- return ret;
+ return ret < 0 ? (-1 * ret) : 0;
}
LY_ERR