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 0 : TALER_language_matches (const char *language_pattern, 38 : const char *lang) 39 : { 40 0 : char *p = GNUNET_strdup (language_pattern); 41 : char *sptr; 42 0 : double r = 0.0; 43 : 44 0 : for (char *tok = strtok_r (p, ",", &sptr); 45 : NULL != tok; 46 0 : tok = strtok_r (NULL, ",", &sptr)) 47 : { 48 : char *sptr2; 49 0 : char *lp = strtok_r (tok, ";", &sptr2); 50 0 : char *qp = strtok_r (NULL, ";", &sptr2); 51 0 : double q = 1.0; 52 : 53 0 : if (NULL == lp) 54 0 : continue; /* should be impossible, but makes static analysis happy */ 55 0 : while (isspace ((int) *lp)) 56 0 : lp++; 57 0 : if (NULL != qp) 58 0 : while (isspace ((int) *qp)) 59 0 : qp++; 60 0 : GNUNET_break_op ( (NULL == qp) || 61 : (1 == sscanf (qp, 62 : "q=%lf", 63 : &q)) ); 64 0 : if (0 == strcasecmp (lang, 65 : lp)) 66 0 : r = GNUNET_MAX (r, q); 67 : } 68 0 : GNUNET_free (p); 69 0 : return r; 70 : } 71 : 72 : 73 : /* end of lang.c */