LCOV - code coverage report
Current view: top level - util - lang.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 17 20 85.0 %
Date: 2021-08-30 06:43:37 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 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 lang.c
      18             :  * @brief Utility functions for parsing and matching RFC 7231 language strings.
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_util.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Check if @a lang matches the @a language_pattern, and if so with
      27             :  * which preference.
      28             :  * See also: https://tools.ietf.org/html/rfc7231#section-5.3.1
      29             :  *
      30             :  * @param language_pattern a language preferences string
      31             :  *        like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1"
      32             :  * @param lang the 2-digit language to match
      33             :  * @return q-weight given for @a lang in @a language_pattern, 1.0 if no weights are given;
      34             :  *         0 if @a lang is not in @a language_pattern
      35             :  */
      36             : double
      37          40 : TALER_language_matches (const char *language_pattern,
      38             :                         const char *lang)
      39             : {
      40          40 :   char *p = GNUNET_strdup (language_pattern);
      41             :   char *sptr;
      42          40 :   double r = 0.0;
      43             : 
      44         112 :   for (char *tok = strtok_r (p, ",", &sptr);
      45             :        NULL != tok;
      46          72 :        tok = strtok_r (NULL, ",", &sptr))
      47             :   {
      48             :     char *sptr2;
      49          72 :     char *lp = strtok_r (tok, ";", &sptr2);
      50          72 :     char *qp = strtok_r (NULL, ";", &sptr2);
      51          72 :     double q = 1.0;
      52             : 
      53          72 :     if (NULL == lp)
      54           0 :       continue; /* should be impossible, but makes static analysis happy */
      55          72 :     while (isspace ((int) *lp))
      56           0 :       lp++;
      57          72 :     if (NULL != qp)
      58          16 :       while (isspace ((int) *qp))
      59           0 :         qp++;
      60          72 :     GNUNET_break_op ( (NULL == qp) ||
      61             :                       (1 == sscanf (qp,
      62             :                                     "q=%lf",
      63             :                                     &q)) );
      64          72 :     if (0 == strcasecmp (lang,
      65             :                          lp))
      66          40 :       r = GNUNET_MAX (r, q);
      67             :   }
      68          40 :   GNUNET_free (p);
      69          40 :   return r;
      70             : }
      71             : 
      72             : 
      73             : /* end of lang.c */

Generated by: LCOV version 1.14