tig-2.1.1
[tig.git] / src / types.c
blob89033cfc3a86991d928036ce34ae4d162860a5a1
1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "tig/tig.h"
15 #include "tig/types.h"
18 * Enumerations
21 int
22 string_enum_compare(const char *str1, const char *str2, int len)
24 size_t i;
26 /* Diff-Header == DIFF_HEADER */
27 for (i = 0; i < len; i++) {
28 if (ascii_toupper(str1[i]) == ascii_toupper(str2[i]))
29 continue;
31 if (string_enum_sep(str1[i]) &&
32 string_enum_sep(str2[i]))
33 continue;
35 return str1[i] - str2[i];
38 return 0;
41 bool
42 enum_name_copy(char buf[], size_t bufsize, const char *name)
44 int bufpos;
46 for (bufpos = 0; name[bufpos] && bufpos < bufsize - 1; bufpos++) {
47 buf[bufpos] = ascii_tolower(name[bufpos]);
48 if (string_enum_sep(buf[bufpos]))
49 buf[bufpos] = '-';
52 buf[bufpos] = 0;
53 return bufpos < bufsize;
56 const char *
57 enum_name(const char *name)
59 static char buf[SIZEOF_STR];
61 return enum_name_copy(buf, sizeof(buf), name) ? buf : name;
64 bool
65 enum_name_prefixed(char buf[], size_t bufsize, const char *prefix, const char *name)
67 char prefixed[SIZEOF_STR];
69 if (*prefix) {
70 if (!string_format(prefixed, "%s-%s", prefix, name))
71 return FALSE;
72 name = prefixed;
75 return enum_name_copy(buf, bufsize, name);
78 const struct enum_map *
79 find_enum_map(const char *type)
81 static struct {
82 const char *type;
83 const struct enum_map *map;
84 } mappings[] = {
85 #define DEFINE_ENUM_MAPPING(name, macro) { #name, name##_map },
86 ENUM_INFO(DEFINE_ENUM_MAPPING)
88 int i;
90 for (i = 0; i < ARRAY_SIZE(mappings); i++)
91 if (!strcmp(type, mappings[i].type))
92 return mappings[i].map;
93 return NULL;
96 bool
97 map_enum_do(const struct enum_map_entry *map, size_t map_size, int *value, const char *name)
99 size_t namelen = strlen(name);
100 int i;
102 for (i = 0; i < map_size; i++)
103 if (enum_equals(map[i], name, namelen)) {
104 *value = map[i].value;
105 return TRUE;
108 return FALSE;
111 #define DEFINE_ENUM_MAPS(name, macro) DEFINE_ENUM_MAP(name, macro);
112 ENUM_INFO(DEFINE_ENUM_MAPS)
114 /* vim: set ts=8 sw=8 noexpandtab: */