(root)/
util-linux-2.39/
libsmartcols/
src/
symbols.c
       1  /*
       2   * symbols.c - routines for symbol handling
       3   *
       4   * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
       5   * Copyright (C) 2016 Igor Gnatenko <i.gnatenko.brain@gmail.com>
       6   *
       7   * This file may be redistributed under the terms of the
       8   * GNU Lesser General Public License.
       9   */
      10  
      11  /**
      12   * SECTION: symbols
      13   * @title: Symbols
      14   * @short_description: can be used to overwrite default output chars (for ascii art)
      15   *
      16   * An API to access and modify data and information per symbol/symbol group.
      17   */
      18  
      19  
      20  #include <stdlib.h>
      21  #include <unistd.h>
      22  #include <string.h>
      23  
      24  #include "smartcolsP.h"
      25  
      26  /**
      27   * scols_new_symbols:
      28   *
      29   * Returns: a pointer to a newly allocated struct libscols_symbols instance.
      30   */
      31  struct libscols_symbols *scols_new_symbols(void)
      32  {
      33  	struct libscols_symbols *sy = calloc(1, sizeof(struct libscols_symbols));
      34  
      35  	if (!sy)
      36  		return NULL;
      37  	sy->refcount = 1;
      38  	return sy;
      39  }
      40  
      41  /**
      42   * scols_ref_symbols:
      43   * @sy: a pointer to a struct libscols_symbols instance
      44   *
      45   * Increases the refcount of @sy.
      46   */
      47  void scols_ref_symbols(struct libscols_symbols *sy)
      48  {
      49  	if (sy)
      50  		sy->refcount++;
      51  }
      52  
      53  /**
      54   * scols_unref_symbols:
      55   * @sy: a pointer to a struct libscols_symbols instance
      56   *
      57   * Decreases the refcount of @sy.
      58   */
      59  void scols_unref_symbols(struct libscols_symbols *sy)
      60  {
      61  	if (sy && --sy->refcount <= 0) {
      62  		free(sy->tree_branch);
      63  		free(sy->tree_vert);
      64  		free(sy->tree_right);
      65  		free(sy->group_last_member);
      66  		free(sy->group_middle_member);
      67  		free(sy->group_first_member);
      68  		free(sy->group_vert);
      69  		free(sy->group_horz);
      70  		free(sy->group_last_child);
      71  		free(sy->group_middle_child);
      72  		free(sy->title_padding);
      73  		free(sy->cell_padding);
      74  		free(sy);
      75  	}
      76  }
      77  
      78  /**
      79   * scols_symbols_set_branch:
      80   * @sy: a pointer to a struct libscols_symbols instance
      81   * @str: a string which will represent the branch part of a tree output
      82   *
      83   * Returns: 0, a negative value in case of an error.
      84   */
      85  int scols_symbols_set_branch(struct libscols_symbols *sy, const char *str)
      86  {
      87  	return strdup_to_struct_member(sy, tree_branch, str);
      88  }
      89  
      90  /**
      91   * scols_symbols_set_vertical:
      92   * @sy: a pointer to a struct libscols_symbols instance
      93   * @str: a string which will represent the vertical part of a tree output
      94   *
      95   * Returns: 0, a negative value in case of an error.
      96   */
      97  int scols_symbols_set_vertical(struct libscols_symbols *sy, const char *str)
      98  {
      99  	return strdup_to_struct_member(sy, tree_vert, str);
     100  }
     101  
     102  /**
     103   * scols_symbols_set_right:
     104   * @sy: a pointer to a struct libscols_symbols instance
     105   * @str: a string which will represent the right part of a tree output
     106   *
     107   * Returns: 0, a negative value in case of an error.
     108   */
     109  int scols_symbols_set_right(struct libscols_symbols *sy, const char *str)
     110  {
     111  	return strdup_to_struct_member(sy, tree_right, str);
     112  }
     113  
     114  /**
     115   * scols_symbols_set_title_padding:
     116   * @sy: a pointer to a struct libscols_symbols instance
     117   * @str: a string which will represent the symbols which fill title output
     118   *
     119   * The current implementation uses only the first byte from the padding string.
     120   * A multibyte chars are not supported yet.
     121   *
     122   * Returns: 0, a negative value in case of an error.
     123   *
     124   * Since: 2.28
     125   */
     126  int scols_symbols_set_title_padding(struct libscols_symbols *sy, const char *str)
     127  {
     128  	return strdup_to_struct_member(sy, title_padding, str);
     129  }
     130  
     131  /**
     132   * scols_symbols_set_cell_padding:
     133   * @sy: a pointer to a struct libscols_symbols instance
     134   * @str: a string which will represent the symbols which fill cells
     135   *
     136   * The padding char has to take up just one cell on the terminal.
     137   *
     138   * Returns: 0, a negative value in case of an error.
     139   *
     140   * Since: 2.29
     141   */
     142  int scols_symbols_set_cell_padding(struct libscols_symbols *sy, const char *str)
     143  {
     144  	return strdup_to_struct_member(sy, cell_padding, str);
     145  }
     146  
     147  
     148  /**
     149   * scols_symbols_set_group_vertical:
     150   * @sy: a pointer to a struct libscols_symbols instance
     151   * @str: a string which will represent the vertival line
     152   *
     153   * Returns: 0, a negative value in case of an error.
     154   *
     155   * Since: 2.34
     156   */
     157  int scols_symbols_set_group_vertical(struct libscols_symbols *sy, const char *str)
     158  {
     159  	return strdup_to_struct_member(sy, group_vert, str);
     160  }
     161  
     162  /**
     163   * scols_symbols_set_group_horizontal:
     164   * @sy: a pointer to a struct libscols_symbols instance
     165   * @str: a string which will represent the horizontal line
     166   *
     167   * Returns: 0, a negative value in case of an error.
     168   *
     169   * Since: 2.34
     170   */
     171  int scols_symbols_set_group_horizontal(struct libscols_symbols *sy, const char *str)
     172  {
     173  	return strdup_to_struct_member(sy, group_horz, str);
     174  }
     175  
     176  /**
     177   * scols_symbols_set_group_first_member:
     178   * @sy: a pointer to a struct libscols_symbols instance
     179   * @str: a string which will represent first member
     180   *
     181   * Returns: 0, a negative value in case of an error.
     182   *
     183   * Since: 2.34
     184   */
     185  int scols_symbols_set_group_first_member(struct libscols_symbols *sy, const char *str)
     186  {
     187  	return strdup_to_struct_member(sy, group_first_member, str);
     188  }
     189  
     190  /**
     191   * scols_symbols_set_group_last_member:
     192   * @sy: a pointer to a struct libscols_symbols instance
     193   * @str: a string which will represent last member
     194   *
     195   * Returns: 0, a negative value in case of an error.
     196   *
     197   * Since: 2.34
     198   */
     199  int scols_symbols_set_group_last_member(struct libscols_symbols *sy, const char *str)
     200  {
     201  	return strdup_to_struct_member(sy, group_last_member, str);
     202  }
     203  
     204  /**
     205   * scols_symbols_set_group_middle:
     206   * @sy: a pointer to a struct libscols_symbols instance
     207   * @str: a string which will represent middle member
     208   *
     209   * Returns: 0, a negative value in case of an error.
     210   *
     211   * Since: 2.34
     212   */
     213  int scols_symbols_set_group_middle_member(struct libscols_symbols *sy, const char *str)
     214  {
     215  	return strdup_to_struct_member(sy, group_middle_member, str);
     216  }
     217  
     218  /**
     219   * scols_symbols_set_group_last_child:
     220   * @sy: a pointer to a struct libscols_symbols instance
     221   * @str: a string which will represent last child
     222   *
     223   * Returns: 0, a negative value in case of an error.
     224   *
     225   * Since: 2.34
     226   */
     227  int scols_symbols_set_group_last_child(struct libscols_symbols *sy, const char *str)
     228  {
     229  	return strdup_to_struct_member(sy, group_last_child, str);
     230  }
     231  
     232  /**
     233   * scols_symbols_set_group_middle_child:
     234   * @sy: a pointer to a struct libscols_symbols instance
     235   * @str: a string which will represent last child
     236   *
     237   * Returns: 0, a negative value in case of an error.
     238   *
     239   * Since: 2.34
     240   */
     241  int scols_symbols_set_group_middle_child(struct libscols_symbols *sy, const char *str)
     242  {
     243  	return strdup_to_struct_member(sy, group_middle_child, str);
     244  }
     245  
     246  /**
     247   * scols_copy_symbols:
     248   * @sy: a pointer to a struct libscols_symbols instance
     249   *
     250   * Returns: a newly allocated copy of the @sy symbol group or NULL in case of an error.
     251   */
     252  struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sy)
     253  {
     254  	struct libscols_symbols *ret;
     255  	int rc;
     256  
     257  	assert(sy);
     258  	if (!sy)
     259  		return NULL;
     260  
     261  	ret = scols_new_symbols();
     262  	if (!ret)
     263  		return NULL;
     264  
     265  	rc = scols_symbols_set_branch(ret, sy->tree_branch);
     266  	if (!rc)
     267  		rc = scols_symbols_set_vertical(ret, sy->tree_vert);
     268  	if (!rc)
     269  		rc = scols_symbols_set_right(ret, sy->tree_right);
     270  	if (!rc)
     271  		rc = scols_symbols_set_group_vertical(ret, sy->group_vert);
     272  	if (!rc)
     273  		rc = scols_symbols_set_group_horizontal(ret, sy->group_horz);
     274  	if (!rc)
     275  		rc = scols_symbols_set_group_first_member(ret, sy->group_first_member);
     276  	if (!rc)
     277  		rc = scols_symbols_set_group_last_member(ret, sy->group_last_member);
     278  	if (!rc)
     279  		rc = scols_symbols_set_group_middle_member(ret, sy->group_middle_member);
     280  	if (!rc)
     281  		rc = scols_symbols_set_group_middle_child(ret, sy->group_middle_child);
     282  	if (!rc)
     283  		rc = scols_symbols_set_group_last_child(ret, sy->group_last_child);
     284  	if (!rc)
     285  		rc = scols_symbols_set_title_padding(ret, sy->title_padding);
     286  	if (!rc)
     287  		rc = scols_symbols_set_cell_padding(ret, sy->cell_padding);
     288  	if (!rc)
     289  		return ret;
     290  
     291  	scols_unref_symbols(ret);
     292  	return NULL;
     293  }