diff options
| author | Leandro A. F. Pereira <leandro@hardinfo.org> | 2019-08-10 16:48:51 -0700 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-10 16:48:51 -0700 | 
| commit | b9157b3f6520f46e788157fbfe9753b209efe9b7 (patch) | |
| tree | 80d72e25837f3b569734ba476df84d3462617e29 /hardinfo/gg_key_file_parse_string_as_value.c | |
| parent | 7db082583f72598e2118553faf1ba3206d7c3ee7 (diff) | |
| parent | 4827b88fa34807425640d2a9f07bbf4ac4119ab3 (diff) | |
Merge branch 'master' into usb3
Diffstat (limited to 'hardinfo/gg_key_file_parse_string_as_value.c')
| -rw-r--r-- | hardinfo/gg_key_file_parse_string_as_value.c | 109 | 
1 files changed, 109 insertions, 0 deletions
| diff --git a/hardinfo/gg_key_file_parse_string_as_value.c b/hardinfo/gg_key_file_parse_string_as_value.c new file mode 100644 index 00000000..496b1d35 --- /dev/null +++ b/hardinfo/gg_key_file_parse_string_as_value.c @@ -0,0 +1,109 @@ +/* From: gkeyfile.c - key file parser + * + *  Copyright 2004  Red Hat, Inc. + *  Copyright 2009-2010  Collabora Ltd. + *  Copyright 2009  Nokia Corporation + * + * Written by Ray Strode <rstrode@redhat.com> + *            Matthias Clasen <mclasen@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, see <http://www.gnu.org/licenses/>. + */ + +static gchar * +gg_key_file_parse_string_as_value (const gchar *string, const gchar list_separator) +{ +  gchar *value, *p, *q; +  gsize length; +  gboolean parsing_leading_space; + +  length = strlen (string) + 1; + +  /* Worst case would be that every character needs to be escaped. +   * In other words every character turns to two characters. */ +  value = g_new (gchar, 2 * length); + +  p = (gchar *) string; +  q = value; +  parsing_leading_space = TRUE; +  while (p < (string + length - 1)) +    { +      gchar escaped_character[3] = { '\\', 0, 0 }; + +      switch (*p) +        { +        case ' ': +          if (parsing_leading_space) +            { +              escaped_character[1] = 's'; +              strcpy (q, escaped_character); +              q += 2; +            } +          else +            { +	      *q = *p; +	      q++; +            } +          break; +        case '\t': +          if (parsing_leading_space) +            { +              escaped_character[1] = 't'; +              strcpy (q, escaped_character); +              q += 2; +            } +          else +            { +	      *q = *p; +	      q++; +            } +          break; +        case '\n': +          escaped_character[1] = 'n'; +          strcpy (q, escaped_character); +          q += 2; +          break; +        case '\r': +          escaped_character[1] = 'r'; +          strcpy (q, escaped_character); +          q += 2; +          break; +        case '\\': +          escaped_character[1] = '\\'; +          strcpy (q, escaped_character); +          q += 2; +          parsing_leading_space = FALSE; +          break; +        default: +	  if (list_separator && *p == list_separator) +	    { +	      escaped_character[1] = list_separator; +	      strcpy (q, escaped_character); +	      q += 2; +              parsing_leading_space = TRUE; +	    } +	  else +	    { +	      *q = *p; +	      q++; +              parsing_leading_space = FALSE; +	    } +          break; +        } +      p++; +    } +  *q = '\0'; + +  return value; +} | 
