]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - libsmartcols/src/cell.c
libsmartcols: add new functions to API docs
[thirdparty/util-linux.git] / libsmartcols / src / cell.c
index 0717a2d09e22d991da496545ddbfb975d62b44f5..f6ceff21d77f2936aeb5a5527b87f2b02791f2c5 100644 (file)
@@ -56,24 +56,31 @@ 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;
+
+       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.
  */
@@ -83,9 +90,50 @@ int scols_cell_refer_data(struct libscols_cell *ce, char *data)
                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
@@ -120,7 +168,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;
 }
 
 /**
@@ -129,10 +177,10 @@ void *scols_cell_get_userdata(struct libscols_cell *ce)
  * @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,
@@ -152,7 +200,7 @@ int scols_cmpstr_cells(struct libscols_cell *a,
                return -1;
        if (bdata == NULL)
                return 1;
-       return strcmp(adata, bdata);
+       return strcoll(adata, bdata);
 }
 
 /**
@@ -166,10 +214,16 @@ int scols_cmpstr_cells(struct libscols_cell *a,
  */
 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);
 }
@@ -182,6 +236,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;
 }
 
@@ -211,7 +268,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;
 }
 
 /**
@@ -224,9 +281,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;
-       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 */
@@ -237,7 +296,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.
  */
@@ -245,8 +304,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)