LCOV - code coverage report
Current view: top level - json - i18n.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 62 0.0 %
Date: 2022-08-25 06:15:09 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020, 2021 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file json/i18n.c
      18             :  * @brief helper functions for i18n in JSON processing
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include "taler_util.h"
      24             : #include "taler_json_lib.h"
      25             : 
      26             : 
      27             : const json_t *
      28           0 : TALER_JSON_extract_i18n (const json_t *object,
      29             :                          const char *language_pattern,
      30             :                          const char *field)
      31             : {
      32             :   const json_t *ret;
      33             :   json_t *i18n;
      34           0 :   double quality = -1;
      35             : 
      36           0 :   ret = json_object_get (object,
      37             :                          field);
      38           0 :   if (NULL == ret)
      39           0 :     return NULL; /* field MUST exist in object */
      40             :   {
      41             :     char *name;
      42             : 
      43           0 :     GNUNET_asprintf (&name,
      44             :                      "%s_i18n",
      45             :                      field);
      46           0 :     i18n = json_object_get (object,
      47             :                             name);
      48           0 :     GNUNET_free (name);
      49             :   }
      50           0 :   if (NULL == i18n)
      51           0 :     return ret;
      52             :   {
      53             :     const char *key;
      54             :     json_t *value;
      55             : 
      56           0 :     json_object_foreach (i18n, key, value) {
      57           0 :       double q = TALER_language_matches (language_pattern,
      58             :                                          key);
      59           0 :       if (q > quality)
      60             :       {
      61           0 :         quality = q;
      62           0 :         ret = value;
      63             :       }
      64             :     }
      65             :   }
      66           0 :   return ret;
      67             : }
      68             : 
      69             : 
      70             : bool
      71           0 : TALER_JSON_check_i18n (const json_t *i18n)
      72             : {
      73             :   const char *field;
      74             :   json_t *member;
      75             : 
      76           0 :   if (! json_is_object (i18n))
      77           0 :     return false;
      78           0 :   json_object_foreach ((json_t *) i18n, field, member)
      79             :   {
      80           0 :     if (! json_is_string (member))
      81           0 :       return false;
      82             :     /* Field name must be either of format "en_UK"
      83             :        or just "en"; we do not care about capitalization;
      84             :        for syntax, see GNU Gettext manual, including
      85             :        appendix A for rare language codes. */
      86           0 :     switch (strlen (field))
      87             :     {
      88           0 :     case 0:
      89             :     case 1:
      90           0 :       return false;
      91           0 :     case 2:
      92           0 :       if (! isalpha (field[0]))
      93           0 :         return false;
      94           0 :       if (! isalpha (field[1]))
      95           0 :         return false;
      96           0 :       break;
      97           0 :     case 3:
      98             :     case 4:
      99           0 :       return false;
     100           0 :     case 5:
     101           0 :       if (! isalpha (field[0]))
     102           0 :         return false;
     103           0 :       if (! isalpha (field[1]))
     104           0 :         return false;
     105           0 :       if ('_' != field[2])
     106           0 :         return false;
     107           0 :       if (! isalpha (field[3]))
     108           0 :         return false;
     109           0 :       if (! isalpha (field[4]))
     110           0 :         return false;
     111           0 :       break;
     112           0 :     case 6:
     113           0 :       if (! isalpha (field[0]))
     114           0 :         return false;
     115           0 :       if (! isalpha (field[1]))
     116           0 :         return false;
     117           0 :       if ('_' != field[2])
     118           0 :         return false;
     119           0 :       if (! isalpha (field[3]))
     120           0 :         return false;
     121           0 :       if (! isalpha (field[4]))
     122           0 :         return false;
     123           0 :       if (! isalpha (field[5]))
     124           0 :         return false;
     125           0 :       break;
     126           0 :     default:
     127           0 :       return false;
     128             :     }
     129             :   }
     130           0 :   return true;
     131             : }
     132             : 
     133             : 
     134             : /* end of i18n.c */

Generated by: LCOV version 1.14