1/*
2 * token.c :  value/string-token functions
3 *
4 * ====================================================================
5 *    Licensed to the Apache Software Foundation (ASF) under one
6 *    or more contributor license agreements.  See the NOTICE file
7 *    distributed with this work for additional information
8 *    regarding copyright ownership.  The ASF licenses this file
9 *    to you under the Apache License, Version 2.0 (the
10 *    "License"); you may not use this file except in compliance
11 *    with the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 *    Unless required by applicable law or agreed to in writing,
16 *    software distributed under the License is distributed on an
17 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 *    KIND, either express or implied.  See the License for the
19 *    specific language governing permissions and limitations
20 *    under the License.
21 * ====================================================================
22 */
23
24#include "svn_types.h"
25#include "svn_error.h"
26
27#include "private/svn_token.h"
28#include "svn_private_config.h"
29
30
31const char *
32svn_token__to_word(const svn_token_map_t *map,
33                   int value)
34{
35  for (; map->str != NULL; ++map)
36    if (map->val == value)
37      return map->str;
38
39  /* Internal, numeric values should always be found.  */
40  SVN_ERR_MALFUNCTION_NO_RETURN();
41}
42
43
44int
45svn_token__from_word_strict(const svn_token_map_t *map,
46                            const char *word)
47{
48  int value = svn_token__from_word(map, word);
49
50  if (value == SVN_TOKEN_UNKNOWN)
51    SVN_ERR_MALFUNCTION_NO_RETURN();
52
53  return value;
54}
55
56
57svn_error_t *
58svn_token__from_word_err(int *value,
59                         const svn_token_map_t *map,
60                         const char *word)
61{
62  *value = svn_token__from_word(map, word);
63
64  if (*value == SVN_TOKEN_UNKNOWN)
65    return svn_error_createf(SVN_ERR_BAD_TOKEN, NULL,
66                             _("Token '%s' is unrecognized"),
67                             word);
68
69  return SVN_NO_ERROR;
70}
71
72
73int
74svn_token__from_word(const svn_token_map_t *map,
75                     const char *word)
76{
77  if (word == NULL)
78    return SVN_TOKEN_UNKNOWN;
79
80  for (; map->str != NULL; ++map)
81    if (strcmp(map->str, word) == 0)
82      return map->val;
83
84  return SVN_TOKEN_UNKNOWN;
85}
86
87
88int
89svn_token__from_mem(const svn_token_map_t *map,
90                    const char *word,
91                    apr_size_t len)
92{
93  for (; map->str != NULL; ++map)
94    if (strncmp(map->str, word, len) == 0 && map->str[len] == '\0')
95      return map->val;
96
97  return SVN_TOKEN_UNKNOWN;
98}
99