* @ce: a pointer to a struct libscols_cell instance
* @data: data (used for scols_print_table())
*
- * Stores a copy of the @str in @ce, the old data are deallocated by free().
+ * Stores a copy of the @data in @ce, the old data are deallocated by free().
*
* Returns: 0, a negative value in case of an error.
*/
int scols_cell_set_data(struct libscols_cell *ce, const char *data)
{
+ int rc;
+
if (!ce)
return -EINVAL;
- return strdup_to_struct_member(ce, data, data);
+ rc = strdup_to_struct_member(ce, data, data);
+ ce->datasiz = ce->data ? strlen(ce->data) + 1: 0;
+ return rc;
}
/**
* scols_cell_refer_data:
* @ce: a pointer to a struct libscols_cell instance
- * @data: data (used for scols_print_table())
+ * @data: string (used for scols_print_table())
*
- * Adds a reference to @str to @ce. The pointer is deallocated by
- * scols_reset_cell() or scols_unref_line(). This function is mostly designed
- * for situations when the data for the cell are already composed in allocated
- * memory (e.g. asprintf()) to avoid extra unnecessary strdup().
+ * Adds a reference to @data to @ce. The pointer is deallocated by
+ * scols_reset_cell() or scols_unref_line() by free(). This function is mostly
+ * designed for situations when the data for the cell are already composed in
+ * allocated memory (e.g. asprintf()) to avoid extra unnecessary strdup().
*
* Returns: 0, a negative value in case of an error.
*/
return -EINVAL;
free(ce->data);
ce->data = data;
+ ce->datasiz = ce->data ? strlen(ce->data) + 1: 0;
+ return 0;
+}
+
+/**
+ * scols_cell_refer_memory:
+ * @ce: a pointer to a struct libscols_cell instance
+ * @data: data
+ * @datasiz: size of the data
+ *
+ * Same like scols_cell_refer_data, but @data does not have to be zero terminated.
+ * The pointer is deallocated by scols_reset_cell() or scols_unref_line() by free().
+ *
+ * The column (for the cell) has to define wrap function which converts the
+ * data to zero terminated string, otherwise library will work with the data as
+ * with string!
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_cell_refer_memory(struct libscols_cell *ce, char *data, size_t datasiz)
+{
+ if (!ce)
+ return -EINVAL;
+ free(ce->data);
+ ce->data = data;
+ ce->datasiz = datasiz;
return 0;
}
+/**
+ * scols_cell_get_datasiz:
+ * @ce: a pointer to a struct libscols_cell instance
+ *
+ * Returns: the current set data size.
+ */
+size_t scols_cell_get_datasiz(struct libscols_cell *ce)
+{
+ return ce ? ce->datasiz : 0;
+}
+
/**
* scols_cell_get_data:
* @ce: a pointer to a struct libscols_cell instance
* @dest: a pointer to a struct libscols_cell instance
* @src: a pointer to an immutable struct libscols_cell instance
*
- * Copy the contents of @src into @dest.
+ * Copy the contents (data, usewrdata, colors) of @src into @dest.
*
* Returns: 0, a negative value in case of an error.
*/
const struct libscols_cell *src)
{
int rc;
+ char *data = NULL;
if (!dest || !src)
return -EINVAL;
- rc = scols_cell_set_data(dest, scols_cell_get_data(src));
+ if (src->datasiz) {
+ data = malloc(src->datasiz);
+ if (!data)
+ return -ENOMEM;
+ memcpy(data, src->data, src->datasiz);
+ }
+
+ rc = scols_cell_refer_memory(dest, data, src->datasiz);
if (!rc)
rc = scols_cell_set_color(dest, scols_cell_get_color(src));
if (!rc)
const struct libscols_cell *src);
extern int scols_cell_set_data(struct libscols_cell *ce, const char *data);
extern int scols_cell_refer_data(struct libscols_cell *ce, char *data);
+extern int scols_cell_refer_memory(struct libscols_cell *ce, char *data, size_t datasiz);
+
extern const char *scols_cell_get_data(const struct libscols_cell *ce);
+extern size_t scols_cell_get_datasiz(struct libscols_cell *ce);
+
extern int scols_cell_set_color(struct libscols_cell *ce, const char *color);
extern const char *scols_cell_get_color(const struct libscols_cell *ce);
extern int scols_cmpstr_cells(struct libscols_cell *a,
struct libscols_cell *b, void *data);
+
/* column.c */
extern int scols_column_is_tree(const struct libscols_column *cl);
extern int scols_column_is_trunc(const struct libscols_column *cl);
int cal)
{
const char *data = NULL;
+ size_t datasiz = 0;
struct libscols_cell *ce;
struct libscols_line *ln;
struct libscols_column *cl;
if (rc < 0)
return rc;
data = x;
+ if (data && *data)
+ datasiz = strlen(data);
rc = 0;
- } else
+ } else {
data = scols_cell_get_data(ce);
+ datasiz = scols_cell_get_datasiz(ce);
+ }
}
if (!scols_column_is_tree(cl))
if (!rc && (ln->parent || cl->is_groups) && !scols_table_is_json(tb))
ul_buffer_save_pointer(buf, SCOLS_BUFPTR_TREEEND);
notree:
- if (!rc && data)
- rc = ul_buffer_append_string(buf, data);
+ if (!rc && data && datasiz)
+ rc = ul_buffer_append_data(buf, data, datasiz);
+
return rc;
}