LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/langtag/liblangtag - lt-script.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 25 50 50.0 %
Date: 2012-12-17 Functions: 6 10 60.0 %
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-script.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-utils.h"
      22             : #include "lt-script.h"
      23             : #include "lt-script-private.h"
      24             : 
      25             : 
      26             : /**
      27             :  * SECTION: lt-script
      28             :  * @Short_Description: A container class for Script subtag
      29             :  * @Title: Container - Script
      30             :  *
      31             :  * This container class provides a data access to Script subtag entry.
      32             :  */
      33             : struct _lt_script_t {
      34             :         lt_mem_t  parent;
      35             :         char     *tag;
      36             :         char     *description;
      37             : };
      38             : 
      39             : 
      40             : /*< private >*/
      41             : 
      42             : /*< protected >*/
      43             : lt_script_t *
      44         648 : lt_script_create(void)
      45             : {
      46         648 :         lt_script_t *retval = lt_mem_alloc_object(sizeof (lt_script_t));
      47             : 
      48         648 :         return retval;
      49             : }
      50             : 
      51             : void
      52         648 : lt_script_set_name(lt_script_t *script,
      53             :                    const char  *description)
      54             : {
      55         648 :         lt_return_if_fail (script != NULL);
      56         648 :         lt_return_if_fail (description != NULL);
      57             : 
      58         648 :         if (script->description)
      59           0 :                 lt_mem_delete_ref(&script->parent, script->description);
      60         648 :         script->description = strdup(description);
      61         648 :         lt_mem_add_ref(&script->parent, script->description, free);
      62             : }
      63             : 
      64             : void
      65         648 : lt_script_set_tag(lt_script_t *script,
      66             :                   const char  *subtag)
      67             : {
      68         648 :         lt_return_if_fail (script != NULL);
      69         648 :         lt_return_if_fail (subtag != NULL);
      70             : 
      71         648 :         if (script->tag)
      72           0 :                 lt_mem_delete_ref(&script->parent, script->tag);
      73         648 :         script->tag = strdup(subtag);
      74         648 :         lt_mem_add_ref(&script->parent, script->tag, free);
      75             : }
      76             : 
      77             : /*< public >*/
      78             : /**
      79             :  * lt_script_ref:
      80             :  * @script: a #lt_script_t.
      81             :  *
      82             :  * Increases the reference count of @script.
      83             :  *
      84             :  * Returns: (transfer none): the same @script object.
      85             :  */
      86             : lt_script_t *
      87         648 : lt_script_ref(lt_script_t *script)
      88             : {
      89         648 :         lt_return_val_if_fail (script != NULL, NULL);
      90             : 
      91         648 :         return lt_mem_ref(&script->parent);
      92             : }
      93             : 
      94             : /**
      95             :  * lt_script_unref:
      96             :  * @script: a #lt_script_t.
      97             :  *
      98             :  * Decreases the reference count of @script. when its reference count
      99             :  * drops to 0, the object is finalized (i.e. its memory is freed).
     100             :  */
     101             : void
     102        1296 : lt_script_unref(lt_script_t *script)
     103             : {
     104        1296 :         if (script)
     105        1296 :                 lt_mem_unref(&script->parent);
     106        1296 : }
     107             : 
     108             : /**
     109             :  * lt_script_get_name:
     110             :  * @script: a #lt_script_t.
     111             :  *
     112             :  * Obtains the description of the subtag.
     113             :  *
     114             :  * Returns: a description string.
     115             :  */
     116             : const char *
     117           0 : lt_script_get_name(const lt_script_t *script)
     118             : {
     119           0 :         lt_return_val_if_fail (script != NULL, NULL);
     120             : 
     121           0 :         return script->description;
     122             : }
     123             : 
     124             : /**
     125             :  * lt_script_get_tag:
     126             :  * @script: a #lt_script_t.
     127             :  *
     128             :  * Obtains the tag name.
     129             :  *
     130             :  * Returns: a tag string.
     131             :  */
     132             : const char *
     133         662 : lt_script_get_tag(const lt_script_t *script)
     134             : {
     135         662 :         lt_return_val_if_fail (script != NULL, NULL);
     136             : 
     137         662 :         return script->tag;
     138             : }
     139             : 
     140             : /**
     141             :  * lt_script_dump:
     142             :  * @script: a #lt_script_t.
     143             :  *
     144             :  * Dumps the container information to the standard output.
     145             :  */
     146             : void
     147           0 : lt_script_dump(const lt_script_t *script)
     148             : {
     149           0 :         lt_info("Script: %s [%s]",
     150             :                 lt_script_get_tag(script),
     151             :                 lt_script_get_name(script));
     152           0 : }
     153             : 
     154             : /**
     155             :  * lt_script_convert_to_modifier:
     156             :  * @script: a #lt_script_t.
     157             :  *
     158             :  * Convert the script subtag to the locale variant modifier.
     159             :  *
     160             :  * Returns: a modifier string or %NULL.
     161             :  */
     162             : const char *
     163           0 : lt_script_convert_to_modifier(const lt_script_t *script)
     164             : {
     165             :         const char *p;
     166             :         static const struct {
     167             :                 char *modifier;
     168             :                 char *script;
     169             :         } modifiers[] = {
     170             :                 {"abegede", NULL},
     171             :                 {"cyrillic", "Cyrl"},
     172             :                 {"cyrillic", "Cyrs"},
     173             :                 {"devanagari", "Deva"},
     174             :                 {"euro", NULL},
     175             :                 {"iqtelif", NULL},
     176             :                 {"latin", "Latf"},
     177             :                 {"latin", "Latg"},
     178             :                 {"latin", "Latn"},
     179             :                 {"saaho", NULL},
     180             :                 {NULL, NULL}
     181             :         };
     182             :         static size_t len = LT_N_ELEMENTS(modifiers), i;
     183             : 
     184           0 :         lt_return_val_if_fail (script != NULL, NULL);
     185             : 
     186           0 :         p = lt_script_get_tag(script);
     187           0 :         for (i = 0; i < len; i++) {
     188           0 :                 if (modifiers[i].script &&
     189           0 :                     lt_strcasecmp(p, modifiers[i].script) == 0)
     190           0 :                         return modifiers[i].modifier;
     191             :         }
     192             : 
     193           0 :         return NULL;
     194             : }
     195             : 
     196             : /**
     197             :  * lt_script_compare:
     198             :  * @v1: a #lt_script_t.
     199             :  * @v2: a #lt_script_t.
     200             :  *
     201             :  * Compare if @v1 and @v2 is the same object or not.
     202             :  *
     203             :  * Returns: %TRUE if it's the same, otherwise %FALSE.
     204             :  */
     205             : lt_bool_t
     206           0 : lt_script_compare(const lt_script_t *v1,
     207             :                   const lt_script_t *v2)
     208             : {
     209             :         const char *s1, *s2;
     210             : 
     211           0 :         if (v1 == v2)
     212           0 :                 return TRUE;
     213             : 
     214           0 :         s1 = v1 ? lt_script_get_tag(v1) : NULL;
     215           0 :         s2 = v2 ? lt_script_get_tag(v2) : NULL;
     216             : 
     217           0 :         if (lt_strcmp0(s1, "*") == 0 ||
     218           0 :             lt_strcmp0(s2, "*") == 0)
     219           0 :                 return TRUE;
     220             : 
     221           0 :         return lt_strcmp0(s1, s2) == 0;
     222             : }

Generated by: LCOV version 1.10