summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/cell.c
diff options
context:
space:
mode:
Diffstat (limited to 'libsmartcols/src/cell.c')
-rw-r--r--libsmartcols/src/cell.c95
1 files changed, 82 insertions, 13 deletions
diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c
index 5b83123..df45667 100644
--- a/libsmartcols/src/cell.c
+++ b/libsmartcols/src/cell.c
@@ -56,24 +56,32 @@ int scols_reset_cell(struct libscols_cell *ce)
* @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)
{
- return strdup_to_struct_member(ce, data, data);
+ int rc;
+
+ if (!ce)
+ return -EINVAL;
+
+ ce->is_filled = 1;
+ rc = strdup_to_struct_member(ce, data, data);
+ ce->datasiz = ce->data && *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.
*/
@@ -83,10 +91,52 @@ int scols_cell_refer_data(struct libscols_cell *ce, char *data)
return -EINVAL;
free(ce->data);
ce->data = data;
+ ce->datasiz = ce->data && *ce->data ? strlen(ce->data) + 1: 0;
+ ce->is_filled = 1;
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.
+ *
+ * Since: 2.40
+ */
+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.
+ *
+ * Since: 2.40
+ */
+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
*
@@ -120,7 +170,7 @@ int scols_cell_set_userdata(struct libscols_cell *ce, void *data)
*/
void *scols_cell_get_userdata(struct libscols_cell *ce)
{
- return ce->userdata;
+ return ce ? ce->userdata : NULL;
}
/**
@@ -166,6 +216,9 @@ int scols_cmpstr_cells(struct libscols_cell *a,
*/
int scols_cell_set_color(struct libscols_cell *ce, const char *color)
{
+ if (!ce)
+ return -EINVAL;
+
if (color && !color_is_sequence(color)) {
char *seq = color_get_sequence(color);
if (!seq)
@@ -185,6 +238,9 @@ int scols_cell_set_color(struct libscols_cell *ce, const char *color)
*/
const char *scols_cell_get_color(const struct libscols_cell *ce)
{
+ if (!ce)
+ return NULL;
+
return ce->color;
}
@@ -214,7 +270,7 @@ int scols_cell_set_flags(struct libscols_cell *ce, int flags)
*/
int scols_cell_get_flags(const struct libscols_cell *ce)
{
- return ce->flags;
+ return ce ? ce->flags : 0;
}
/**
@@ -227,9 +283,11 @@ int scols_cell_get_flags(const struct libscols_cell *ce)
*/
int scols_cell_get_alignment(const struct libscols_cell *ce)
{
- if (ce->flags & SCOLS_CELL_FL_RIGHT)
+ int flags = scols_cell_get_flags(ce);
+
+ if (flags & SCOLS_CELL_FL_RIGHT)
return SCOLS_CELL_FL_RIGHT;
- if (ce->flags & SCOLS_CELL_FL_CENTER)
+ if (flags & SCOLS_CELL_FL_CENTER)
return SCOLS_CELL_FL_CENTER;
return SCOLS_CELL_FL_LEFT; /* default */
@@ -240,7 +298,7 @@ int scols_cell_get_alignment(const struct libscols_cell *ce)
* @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.
*/
@@ -248,8 +306,19 @@ int scols_cell_copy_content(struct libscols_cell *dest,
const struct libscols_cell *src)
{
int rc;
+ char *data = NULL;
+
+ if (!dest || !src)
+ return -EINVAL;
+
+ if (src->datasiz) {
+ data = malloc(src->datasiz);
+ if (!data)
+ return -ENOMEM;
+ memcpy(data, src->data, src->datasiz);
+ }
- rc = scols_cell_set_data(dest, scols_cell_get_data(src));
+ rc = scols_cell_refer_memory(dest, data, src->datasiz);
if (!rc)
rc = scols_cell_set_color(dest, scols_cell_get_color(src));
if (!rc)