summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: be63d81)
raw | patch | inline | side by side (parent: be63d81)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 Mar 2013 07:02:54 +0000 (00:02 -0700) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 Mar 2013 07:02:54 +0000 (00:02 -0700) |
These functions may be used to remove all consecutive newline characters from
the end of the respective strings.
the end of the respective strings.
src/include/utils/error.h | patch | blob | history | |
src/include/utils/strbuf.h | patch | blob | history | |
src/utils/error.c | patch | blob | history | |
src/utils/strbuf.c | patch | blob | history |
index 487b6e4ffcaa98dffd1202e5667c102fdfcd960e..33731e129e72a2250f7b7d1ecbe25922359fc56d 100644 (file)
int
sdb_error_append(const char *fmt, ...);
+/*
+ * sdb_error_chomp:
+ * Remove all consecutive newline characters at the end of the error message.
+ */
+int
+sdb_error_chomp(void);
+
/*
* sdb_error_log:
* Log the current error message with the specified priority. See sdb_log for
index 7b159edb6971a2509051efd2be2c5d6296ed4df9..302cfdde4bb71839b94d86b47c57fe194b9647f2 100644 (file)
ssize_t
sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...);
+/*
+ * sdb_strbuf_chomp:
+ * Remove all consecutive newline characters from the end of the string buffer
+ * content.
+ *
+ * Returns:
+ * - the number of bytes removed
+ * - a negative value on error
+ */
+ssize_t
+sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
+
/*
* sdb_strbuf_string:
* Returns the content of the string buffer. The caller may not modify the
diff --git a/src/utils/error.c b/src/utils/error.c
index fc360ebaa98b3f0741907e0d85fa79744f59add3..c62b20d5c6c6f22c2230b21320f2fae93a9bd5d8 100644 (file)
--- a/src/utils/error.c
+++ b/src/utils/error.c
return ret;
} /* sdb_error_append */
+int
+sdb_error_chomp(void)
+{
+ sdb_error_ctx_t *ctx;
+
+ ctx = sdb_error_get_ctx();
+ if (! ctx)
+ return -1;
+
+ sdb_strbuf_chomp(ctx->msg);
+ return 0;
+} /* sdb_error_chomp */
+
int
sdb_error_log(int prio)
{
diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c
index a48f7bc5765959d38050bdf2f19c027a9765f789..bc24fef5e54c19781c5cd4bef7c3a1036e897945 100644 (file)
--- a/src/utils/strbuf.c
+++ b/src/utils/strbuf.c
return status;
} /* sdb_strbuf_sprintf */
+ssize_t
+sdb_strbuf_chomp(sdb_strbuf_t *strbuf)
+{
+ ssize_t ret = 0;
+
+ if (! strbuf)
+ return -1;
+
+ while ((strbuf->pos > 0)
+ && (strbuf->string[strbuf->pos - 1] == '\n')) {
+ --strbuf->pos;
+ strbuf->string[strbuf->pos] = '\0';
+ ++ret;
+ }
+
+ return ret;
+} /* sdb_strbuf_chomp */
+
const char *
sdb_strbuf_string(sdb_strbuf_t *strbuf)
{