LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/langtag/liblangtag - lt-redundant.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 34 63 54.0 %
Date: 2012-12-17 Functions: 8 12 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
       2             : /* 
       3             :  * lt-redundant.c
       4             :  * Copyright (C) 2011-2012 Akira TAGOH
       5             :  * 
       6             :  * Authors:
       7             :  *   Akira TAGOH  <akira@tagoh.org>
       8             :  * 
       9             :  * You may distribute under the terms of either the GNU
      10             :  * Lesser General Public License or the Mozilla Public
      11             :  * License, as specified in the README file.
      12             :  */
      13             : #ifdef HAVE_CONFIG_H
      14             : #include "config.h"
      15             : #endif
      16             : 
      17             : #include <stdlib.h>
      18             : #include <string.h>
      19             : #include "lt-mem.h"
      20             : #include "lt-messages.h"
      21             : #include "lt-string.h"
      22             : #include "lt-utils.h"
      23             : #include "lt-redundant.h"
      24             : #include "lt-redundant-private.h"
      25             : 
      26             : 
      27             : /**
      28             :  * SECTION: lt-redundant
      29             :  * @Short_Description: A container class for Redundant subtag
      30             :  * @Title: Container - Redundant
      31             :  *
      32             :  * This container class provides a data access to Redundant subtag entry.
      33             :  */
      34             : struct _lt_redundant_t {
      35             :         lt_mem_t  parent;
      36             :         char     *tag;
      37             :         char     *description;
      38             :         char     *preferred_tag;
      39             : };
      40             : 
      41             : /*< private >*/
      42             : 
      43             : /*< protected >*/
      44             : lt_redundant_t *
      45         264 : lt_redundant_create(void)
      46             : {
      47             :         lt_redundant_t *retval;
      48             : 
      49         264 :         retval = lt_mem_alloc_object(sizeof (lt_redundant_t));
      50             : 
      51         264 :         return retval;
      52             : }
      53             : 
      54             : void
      55         264 : lt_redundant_set_tag(lt_redundant_t *redundant,
      56             :                      const char     *tag)
      57             : {
      58         264 :         lt_return_if_fail (redundant != NULL);
      59         264 :         lt_return_if_fail (tag != NULL);
      60             : 
      61         264 :         if (redundant->tag)
      62           0 :                 lt_mem_delete_ref(&redundant->parent, redundant->tag);
      63         264 :         redundant->tag = strdup(tag);
      64         264 :         lt_mem_add_ref(&redundant->parent, redundant->tag, free);
      65             : }
      66             : 
      67             : void
      68         264 : lt_redundant_set_name(lt_redundant_t *redundant,
      69             :                       const char     *description)
      70             : {
      71         264 :         lt_return_if_fail (redundant != NULL);
      72         264 :         lt_return_if_fail (description != NULL);
      73             : 
      74         264 :         if (redundant->description)
      75           0 :                 lt_mem_delete_ref(&redundant->parent, redundant->description);
      76         264 :         redundant->description = strdup(description);
      77         264 :         lt_mem_add_ref(&redundant->parent, redundant->description, free);
      78             : }
      79             : 
      80             : void
      81          96 : lt_redundant_set_preferred_tag(lt_redundant_t *redundant,
      82             :                                const char     *subtag)
      83             : {
      84          96 :         lt_return_if_fail (redundant != NULL);
      85          96 :         lt_return_if_fail (subtag != NULL);
      86             : 
      87          96 :         if (redundant->preferred_tag)
      88           0 :                 lt_mem_delete_ref(&redundant->parent, redundant->preferred_tag);
      89          96 :         redundant->preferred_tag = strdup(subtag);
      90          96 :         lt_mem_add_ref(&redundant->parent, redundant->preferred_tag, free);
      91             : }
      92             : 
      93             : /*< public >*/
      94             : /**
      95             :  * lt_redundant_ref:
      96             :  * @redundant: a #lt_redundant_t.
      97             :  *
      98             :  * Increases the reference count of @redundant.
      99             :  *
     100             :  * Returns: (transfer none): the same @redundant object.
     101             :  */
     102             : lt_redundant_t *
     103         266 : lt_redundant_ref(lt_redundant_t *redundant)
     104             : {
     105         266 :         lt_return_val_if_fail (redundant != NULL, NULL);
     106             : 
     107         266 :         return lt_mem_ref(&redundant->parent);
     108             : }
     109             : 
     110             : /**
     111             :  * lt_redundant_unref:
     112             :  * @redundant: a #lt_redundant_t.
     113             :  *
     114             :  * Decreases the reference count of @redundant. when its reference count
     115             :  * drops to 0, the object is finalized (i.e. its memory is freed).
     116             :  */
     117             : void
     118         530 : lt_redundant_unref(lt_redundant_t *redundant)
     119             : {
     120         530 :         if (redundant)
     121         530 :                 lt_mem_unref(&redundant->parent);
     122         530 : }
     123             : 
     124             : /**
     125             :  * lt_redundant_get_better_tag:
     126             :  * @redundant: a #lt_redundant_t.
     127             :  *
     128             :  * Obtains the better tag for use. this is a convenient function to get
     129             :  * the preferred-value if available.
     130             :  *
     131             :  * Returns: a tag string.
     132             :  */
     133             : const char *
     134           0 : lt_redundant_get_better_tag(const lt_redundant_t *redundant)
     135             : {
     136           0 :         const char *retval = lt_redundant_get_preferred_tag(redundant);
     137             : 
     138           0 :         if (!retval)
     139           0 :                 retval = lt_redundant_get_tag(redundant);
     140             : 
     141           0 :         return retval;
     142             : }
     143             : 
     144             : /**
     145             :  * lt_redundant_get_tag:
     146             :  * @redundant: a #lt_redundant_t.
     147             :  *
     148             :  * Obtains the tag name.
     149             :  *
     150             :  * Returns: a tag string.
     151             :  */
     152             : const char *
     153         264 : lt_redundant_get_tag(const lt_redundant_t *redundant)
     154             : {
     155         264 :         lt_return_val_if_fail (redundant != NULL, NULL);
     156             : 
     157         264 :         return redundant->tag;
     158             : }
     159             : 
     160             : /**
     161             :  * lt_redundant_get_name:
     162             :  * @redundant: a #lt_redundant_t.
     163             :  *
     164             :  * Obtains the description of the tag.
     165             :  *
     166             :  * Returns: a description string.
     167             :  */
     168             : const char *
     169           0 : lt_redundant_get_name(const lt_redundant_t *redundant)
     170             : {
     171           0 :         lt_return_val_if_fail (redundant != NULL, NULL);
     172             : 
     173           0 :         return redundant->description;
     174             : }
     175             : 
     176             : /**
     177             :  * lt_redundant_get_preferred_tag:
     178             :  * @redundant: a #lt_redundant_t.
     179             :  *
     180             :  * Obtains the preferred-value. this is available only when the tag is
     181             :  * marked as deprecated.
     182             :  *
     183             :  * Returns: a preferred-value for the tag or %NULL.
     184             :  */
     185             : const char *
     186           2 : lt_redundant_get_preferred_tag(const lt_redundant_t *redundant)
     187             : {
     188           2 :         lt_return_val_if_fail (redundant != NULL, NULL);
     189             : 
     190           2 :         return redundant->preferred_tag;
     191             : }
     192             : 
     193             : /**
     194             :  * lt_redundant_dump:
     195             :  * @redundant: a #lt_redundant_t.
     196             :  *
     197             :  * Dumps the container information to the standard output.
     198             :  */
     199             : void
     200           0 : lt_redundant_dump(const lt_redundant_t *redundant)
     201             : {
     202           0 :         const char *preferred = lt_redundant_get_preferred_tag(redundant);
     203           0 :         lt_string_t *string = lt_string_new(NULL);
     204             : 
     205           0 :         if (preferred) {
     206           0 :                 if (lt_string_length(string) == 0)
     207           0 :                         lt_string_append(string, " (");
     208           0 :                 lt_string_append_printf(string, "preferred-value: %s",
     209             :                                         preferred);
     210             :         }
     211           0 :         if (lt_string_length(string) > 0)
     212           0 :                 lt_string_append(string, ")");
     213             : 
     214           0 :         lt_info("Redundant: %s [%s]%s",
     215             :                 lt_redundant_get_tag(redundant),
     216             :                 lt_redundant_get_name(redundant),
     217             :                 lt_string_value(string));
     218             : 
     219           0 :         lt_string_unref(string);
     220           0 : }
     221             : 
     222             : /**
     223             :  * lt_redundant_compare:
     224             :  * @v1: a #lt_redundant_t.
     225             :  * @v2: a #lt_redundant_t.
     226             :  *
     227             :  * Compare if @v1 and @v2 is the same object or not.
     228             :  *
     229             :  * Returns: %TRUE if it's the same. otherwise %FALSE.
     230             :  */
     231             : lt_bool_t
     232           0 : lt_redundant_compare(const lt_redundant_t *v1,
     233             :                      const lt_redundant_t *v2)
     234             : {
     235           0 :         lt_return_val_if_fail (v1 != NULL, FALSE);
     236           0 :         lt_return_val_if_fail (v2 != NULL, FALSE);
     237             : 
     238           0 :         if (v1 == v2)
     239           0 :                 return TRUE;
     240             : 
     241           0 :         return lt_strcmp0(lt_redundant_get_tag(v1), lt_redundant_get_tag(v2)) == 0;
     242             : }

Generated by: LCOV version 1.10