* @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;
+
+ 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.
+ *
+ * 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
*/
void *scols_cell_get_userdata(struct libscols_cell *ce)
{
- return ce->userdata;
+ return ce ? ce->userdata : NULL;
}
/**
* @b: pointer to cell
* @data: unused pointer to private data (defined by API)
*
- * Compares cells data by strcmp(). The function is designed for
+ * Compares cells data by strcoll(). The function is designed for
* scols_column_set_cmpfunc() and scols_sort_table().
*
- * Returns: follows strcmp() return values.
+ * Returns: follows strcoll() return values.
*/
int scols_cmpstr_cells(struct libscols_cell *a,
struct libscols_cell *b,
return -1;
if (bdata == NULL)
return 1;
- return strcmp(adata, bdata);
+ return strcoll(adata, bdata);
}
/**
*/
int scols_cell_set_color(struct libscols_cell *ce, const char *color)
{
- if (color && isalpha(*color)) {
- color = color_sequence_from_colorname(color);
- if (!color)
+ if (!ce)
+ return -EINVAL;
+
+ if (color && !color_is_sequence(color)) {
+ char *seq = color_get_sequence(color);
+ if (!seq)
return -EINVAL;
+ free(ce->color);
+ ce->color = seq;
+ return 0;
}
return strdup_to_struct_member(ce, color, color);
}
*/
const char *scols_cell_get_color(const struct libscols_cell *ce)
{
+ if (!ce)
+ return NULL;
+
return ce->color;
}
*/
int scols_cell_get_flags(const struct libscols_cell *ce)
{
- return ce->flags;
+ return ce ? ce->flags : 0;
}
/**
*/
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;
- else if (ce->flags & SCOLS_CELL_FL_CENTER)
+ if (flags & SCOLS_CELL_FL_CENTER)
return SCOLS_CELL_FL_CENTER;
return SCOLS_CELL_FL_LEFT; /* default */
* @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;
+
+ 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)