int ul_buffer_append_string(struct ul_buffer *buf, const char *str);
int ul_buffer_append_ntimes(struct ul_buffer *buf, size_t n, const char *str);
int ul_buffer_set_data(struct ul_buffer *buf, const char *data, size_t sz);
-char *ul_buffer_get_data(struct ul_buffer *buf);
+char *ul_buffer_get_data(struct ul_buffer *buf, size_t *sz);
+size_t ul_buffer_get_bufsiz(struct ul_buffer *buf);
#endif /* UTIL_LINUX_BUFFER */
return ul_buffer_append_data(buf, data, sz);
}
-char *ul_buffer_get_data(struct ul_buffer *buf)
+char *ul_buffer_get_data(struct ul_buffer *buf, size_t *sz)
{
+ if (sz)
+ *sz = buf->end - buf->begin;
return buf->begin;
}
+/* size of allocated area (!= size of stored data */
+size_t ul_buffer_get_bufsiz(struct ul_buffer *buf)
+{
+ return buf->sz;
+}
+
+
#ifdef TEST_PROGRAM_BUFFER
int main(void)
{
struct ul_buffer buf = UL_INIT_BUFFER;
char *str;
+ size_t sz = 0;
ul_buffer_set_chunksize(&buf, 16);
ul_buffer_append_string(&buf, "=");
ul_buffer_append_string(&buf, "bbb");
- str = ul_buffer_get_data(&buf);
- printf("data '%s'\n", str);
+ str = ul_buffer_get_data(&buf, &sz);
+ printf("data [%zu] '%s'\n", sz, str);
ul_buffer_reset_data(&buf);
ul_buffer_append_string(&buf, "This is really long string to test the buffer function.");
ul_buffer_append_string(&buf, " YES!");
- str = ul_buffer_get_data(&buf);
- printf("data '%s'\n", str);
+ str = ul_buffer_get_data(&buf, &sz);
+ printf("data [%zu] '%s'\n", sz, str);
ul_buffer_free_data(&buf);
str = strdup("foo");
ul_buffer_refer_string(&buf, str);
ul_buffer_append_data(&buf, ",", 1);
ul_buffer_append_string(&buf, "bar");
- str = ul_buffer_get_data(&buf);
- printf("data '%s'\n", str);
+ str = ul_buffer_get_data(&buf, &sz);
+ printf("data [%zu] '%s'\n", sz, str);
ul_buffer_free_data(&buf);
}
rc = __buffer_append_option(&buf, name, nsz, value, vsz);
- *optstr = ul_buffer_get_data(&buf);
+ *optstr = ul_buffer_get_data(&buf, NULL);
return rc;
}
/**
free(*optstr);
}
- *optstr = ul_buffer_get_data(&buf);
+ *optstr = ul_buffer_get_data(&buf, NULL);
return rc;
}
}
if (vfs)
- *vfs = rc ? NULL : ul_buffer_get_data(&xvfs);
+ *vfs = rc ? NULL : ul_buffer_get_data(&xvfs, NULL);
if (fs)
- *fs = rc ? NULL : ul_buffer_get_data(&xfs);
+ *fs = rc ? NULL : ul_buffer_get_data(&xfs, NULL);
if (user)
- *user = rc ? NULL : ul_buffer_get_data(&xuser);
+ *user = rc ? NULL : ul_buffer_get_data(&xuser, NULL);
if (rc) {
ul_buffer_free_data(&xvfs);
ul_buffer_free_data(&xfs);
break;
}
- *subset = rc ? NULL : ul_buffer_get_data(&buf);
+ *subset = rc ? NULL : ul_buffer_get_data(&buf, NULL);
if (rc)
ul_buffer_free_data(&buf);
return rc;
goto err;
}
- *optstr = ul_buffer_get_data(&buf);
+ *optstr = ul_buffer_get_data(&buf, NULL);
}
DBG(CXT, ul_debug("new optstr '%s'", *optstr));
if (i + 1 < n)
ul_buffer_append_data(&buf, "\n", 1);
}
- str = ul_buffer_get_data(&buf);
+ str = ul_buffer_get_data(&buf, NULL);
break;
}
case COL_FSROOTS:
if (i + 1 < n)
ul_buffer_append_data(&buf, "\n", 1);
}
- str = ul_buffer_get_data(&buf);
+ str = ul_buffer_get_data(&buf, NULL);
break;
}
case COL_LABEL: