(root)/
util-linux-2.39/
libsmartcols/
src/
cell.c
       1  /*
       2   * cell.c - functions for table handling at the cell level
       3   *
       4   * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
       5   * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
       6   *
       7   * This file may be redistributed under the terms of the
       8   * GNU Lesser General Public License.
       9   */
      10  
      11  /**
      12   * SECTION: cell
      13   * @title: Cell
      14   * @short_description: container for your data
      15   *
      16   * An API to access and modify per-cell data and information. Note that cell is
      17   * always part of the line. If you destroy (un-reference) a line than it
      18   * destroys all line cells too.
      19   */
      20  
      21  
      22  #include <stdlib.h>
      23  #include <unistd.h>
      24  #include <string.h>
      25  #include <ctype.h>
      26  
      27  #include "smartcolsP.h"
      28  
      29  /*
      30   * The cell has no ref-counting, free() and new() functions. All is
      31   * handled by libscols_line.
      32   */
      33  
      34  /**
      35   * scols_reset_cell:
      36   * @ce: pointer to a struct libscols_cell instance
      37   *
      38   * Frees the cell's internal data and resets its status.
      39   *
      40   * Returns: 0, a negative value in case of an error.
      41   */
      42  int scols_reset_cell(struct libscols_cell *ce)
      43  {
      44  	if (!ce)
      45  		return -EINVAL;
      46  
      47  	/*DBG(CELL, ul_debugobj(ce, "reset"));*/
      48  	free(ce->data);
      49  	free(ce->color);
      50  	memset(ce, 0, sizeof(*ce));
      51  	return 0;
      52  }
      53  
      54  /**
      55   * scols_cell_set_data:
      56   * @ce: a pointer to a struct libscols_cell instance
      57   * @data: data (used for scols_print_table())
      58   *
      59   * Stores a copy of the @str in @ce, the old data are deallocated by free().
      60   *
      61   * Returns: 0, a negative value in case of an error.
      62   */
      63  int scols_cell_set_data(struct libscols_cell *ce, const char *data)
      64  {
      65  	return strdup_to_struct_member(ce, data, data);
      66  }
      67  
      68  /**
      69   * scols_cell_refer_data:
      70   * @ce: a pointer to a struct libscols_cell instance
      71   * @data: data (used for scols_print_table())
      72   *
      73   * Adds a reference to @str to @ce. The pointer is deallocated by
      74   * scols_reset_cell() or scols_unref_line(). This function is mostly designed
      75   * for situations when the data for the cell are already composed in allocated
      76   * memory (e.g. asprintf()) to avoid extra unnecessary strdup().
      77   *
      78   * Returns: 0, a negative value in case of an error.
      79   */
      80  int scols_cell_refer_data(struct libscols_cell *ce, char *data)
      81  {
      82  	if (!ce)
      83  		return -EINVAL;
      84  	free(ce->data);
      85  	ce->data = data;
      86  	return 0;
      87  }
      88  
      89  /**
      90   * scols_cell_get_data:
      91   * @ce: a pointer to a struct libscols_cell instance
      92   *
      93   * Returns: data in @ce or NULL.
      94   */
      95  const char *scols_cell_get_data(const struct libscols_cell *ce)
      96  {
      97  	return ce ? ce->data : NULL;
      98  }
      99  
     100  /**
     101   * scols_cell_set_userdata:
     102   * @ce: a pointer to a struct libscols_cell instance
     103   * @data: private user data
     104   *
     105   * Returns: 0, a negative value in case of an error.
     106   */
     107  int scols_cell_set_userdata(struct libscols_cell *ce, void *data)
     108  {
     109  	if (!ce)
     110  		return -EINVAL;
     111  	ce->userdata = data;
     112  	return 0;
     113  }
     114  
     115  /**
     116   * scols_cell_get_userdata
     117   * @ce: a pointer to a struct libscols_cell instance
     118   *
     119   * Returns: user data
     120   */
     121  void *scols_cell_get_userdata(struct libscols_cell *ce)
     122  {
     123  	return ce->userdata;
     124  }
     125  
     126  /**
     127   * scols_cmpstr_cells:
     128   * @a: pointer to cell
     129   * @b: pointer to cell
     130   * @data: unused pointer to private data (defined by API)
     131   *
     132   * Compares cells data by strcoll(). The function is designed for
     133   * scols_column_set_cmpfunc() and scols_sort_table().
     134   *
     135   * Returns: follows strcoll() return values.
     136   */
     137  int scols_cmpstr_cells(struct libscols_cell *a,
     138  		       struct libscols_cell *b,
     139  		       __attribute__((__unused__)) void *data)
     140  {
     141  	const char *adata, *bdata;
     142  
     143  	if (a == b)
     144  		return 0;
     145  
     146  	adata = scols_cell_get_data(a);
     147  	bdata = scols_cell_get_data(b);
     148  
     149  	if (adata == NULL && bdata == NULL)
     150  		return 0;
     151  	if (adata == NULL)
     152  		return -1;
     153  	if (bdata == NULL)
     154  		return 1;
     155  	return strcoll(adata, bdata);
     156  }
     157  
     158  /**
     159   * scols_cell_set_color:
     160   * @ce: a pointer to a struct libscols_cell instance
     161   * @color: color name or ESC sequence
     162   *
     163   * Set the color of @ce to @color.
     164   *
     165   * Returns: 0, a negative value in case of an error.
     166   */
     167  int scols_cell_set_color(struct libscols_cell *ce, const char *color)
     168  {
     169  	if (color && !color_is_sequence(color)) {
     170  		char *seq = color_get_sequence(color);
     171  		if (!seq)
     172  			return -EINVAL;
     173  		free(ce->color);
     174  		ce->color = seq;
     175  		return 0;
     176  	}
     177  	return strdup_to_struct_member(ce, color, color);
     178  }
     179  
     180  /**
     181   * scols_cell_get_color:
     182   * @ce: a pointer to a struct libscols_cell instance
     183   *
     184   * Returns: the current color of @ce.
     185   */
     186  const char *scols_cell_get_color(const struct libscols_cell *ce)
     187  {
     188  	return ce->color;
     189  }
     190  
     191  /**
     192   * scols_cell_set_flags:
     193   * @ce: a pointer to a struct libscols_cell instance
     194   * @flags: SCOLS_CELL_FL_* flags
     195   *
     196   * Note that cells in the table are always aligned by column flags. The cell
     197   * flags are used for table title only (now).
     198   *
     199   * Returns: 0, a negative value in case of an error.
     200   */
     201  int scols_cell_set_flags(struct libscols_cell *ce, int flags)
     202  {
     203  	if (!ce)
     204  		return -EINVAL;
     205  	ce->flags = flags;
     206  	return 0;
     207  }
     208  
     209  /**
     210   * scols_cell_get_flags:
     211   * @ce: a pointer to a struct libscols_cell instance
     212   *
     213   * Returns: the current flags
     214   */
     215  int scols_cell_get_flags(const struct libscols_cell *ce)
     216  {
     217  	return ce->flags;
     218  }
     219  
     220  /**
     221   * scols_cell_get_alignment:
     222   * @ce: a pointer to a struct libscols_cell instance
     223   *
     224   * Since: 2.30
     225   *
     226   * Returns: SCOLS_CELL_FL_{RIGHT,CELNTER,LEFT}
     227   */
     228  int scols_cell_get_alignment(const struct libscols_cell *ce)
     229  {
     230  	if (ce->flags & SCOLS_CELL_FL_RIGHT)
     231  		return SCOLS_CELL_FL_RIGHT;
     232  	if (ce->flags & SCOLS_CELL_FL_CENTER)
     233  		return SCOLS_CELL_FL_CENTER;
     234  
     235  	return SCOLS_CELL_FL_LEFT;	/* default */
     236  }
     237  
     238  /**
     239   * scols_cell_copy_content:
     240   * @dest: a pointer to a struct libscols_cell instance
     241   * @src: a pointer to an immutable struct libscols_cell instance
     242   *
     243   * Copy the contents of @src into @dest.
     244   *
     245   * Returns: 0, a negative value in case of an error.
     246   */
     247  int scols_cell_copy_content(struct libscols_cell *dest,
     248  			    const struct libscols_cell *src)
     249  {
     250  	int rc;
     251  
     252  	rc = scols_cell_set_data(dest, scols_cell_get_data(src));
     253  	if (!rc)
     254  		rc = scols_cell_set_color(dest, scols_cell_get_color(src));
     255  	if (!rc)
     256  		dest->userdata = src->userdata;
     257  
     258  	DBG(CELL, ul_debugobj(src, "copy"));
     259  	return rc;
     260  }