summaryrefslogtreecommitdiff
path: root/devices/rinkj/rinkj-config.c
blob: 01df98ff458d1bb3cae5deaf34cd474188b8947e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Support for reading Rinkj config files. */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "rinkj-config.h"

/* A rinkj config string is a series of \n terminated lines, each with
   the format "Key: Value". */

/**
 * rinkj_strdup_size: Duplicate a string with given size.
 * @src: Source string to duplicate.
 * @size: Size of @src.
 *
 * Return value: newly allocated string.
 **/
char *
rinkj_strdup_size (const char *src, int size)
{
  char *result;

  result = malloc (size + 1);
  memcpy (result, src, size);
  result[size] = 0;
  return result;
}

/**
 * rinkj_config_get: Get a config value.
 * @config: A config string.
 * @key: A key.
 *
 * Looks up @key in @config. Returns a newly allocated string, if found.
 *
 * Return value: value of the key, or NULL if not found.
 **/
char *
rinkj_config_get (const char *config, const char *key)
{
  int ix;
  int ix_eol, ix_next;

  if (config == NULL)
    return NULL;

  for (ix = 0; config[ix]; ix = ix_next)
    {
      char *p_nl;
      int key_ix;

      p_nl = strchr (config + ix, '\n');
      if (p_nl == NULL)
        {
          /* last line not \n terminated */
          ix_eol = strlen (config + ix);
          ix_next = ix_eol;
        }
      else
        {
          ix_eol = ix + p_nl - config;
          ix_next = ix_eol + 1;
        }

      for (key_ix = 0; ix + key_ix < ix_eol; key_ix++)
        {
          if (key[key_ix] == 0 && config[ix + key_ix] == ':')
            {
              ix += key_ix + 1;
              while (ix < ix_eol && isspace (config[ix]))
                ix++;
              return rinkj_strdup_size (config + ix, ix_eol - ix);
            }
          else if (key[key_ix] != config[ix + key_ix])
            break;
        }
    }
  return NULL;
}

/**
 * rinkj_config_keyval: Get key, value pair from config string.
 * @config: A config string.
 * @p_val: Where to store newly allocated val.
 * @p_next: Where to store pointer to next config line,
 *
 * Gets a key, value pair from the config string.
 *
 * Return value: Newly allocated key, or NULL if empty config.
 **/
char *
rinkj_config_keyval (const char *config, char **p_val, const char **p_next)
{
  char *key;
  int ix;
  int ix_eol, ix_next;

  if (config == NULL)
    return NULL;

  for (ix = 0; config[ix]; ix = ix_next)
    {
      char *p_nl;
      int key_ix;

      p_nl = strchr (config + ix, '\n');
      if (p_nl == NULL)
        {
          /* last line not \n terminated */
          ix_eol = strlen (config + ix);
          ix_next = ix_eol;
        }
      else
        {
          ix_eol = ix + p_nl - config;
          ix_next = ix_eol + 1;
        }

      for (key_ix = 0; ix + key_ix < ix_eol; key_ix++)
        {
          if (config[ix + key_ix] == ':')
            {
              key = rinkj_strdup_size (config + ix, key_ix);
              ix += key_ix + 1;
              while (ix < ix_eol && isspace (config[ix]))
                ix++;
              if (p_val != NULL)
                *p_val = rinkj_strdup_size (config + ix, ix_eol - ix);
              if (p_next != NULL)
                *p_next = config + ix_next;
              return key;
            }
        }
    }
  return NULL;
}