Fix diff context restoring for diff opened directly via 'tig show'
[tig.git] / src / types.c
blobc1927099cb6d4ba20e6a386c945f061208c46240
1 /* Copyright (c) 2006-2014 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 #define string_enum_sep(x) ((x) == '-' || (x) == '_')
28 /* Diff-Header == DIFF_HEADER */
29 for (i = 0; i < len; i++) {
30 if (ascii_toupper(str1[i]) == ascii_toupper(str2[i]))
31 continue;
33 if (string_enum_sep(str1[i]) &&
34 string_enum_sep(str2[i]))
35 continue;
37 return str1[i] - str2[i];
40 return 0;
43 bool
44 enum_name_copy(char *buf, size_t bufsize, const char *name)
46 int bufpos;
48 for (bufpos = 0; name[bufpos] && bufpos < bufsize - 1; bufpos++) {
49 buf[bufpos] = ascii_tolower(name[bufpos]);
50 if (buf[bufpos] == '_')
51 buf[bufpos] = '-';
54 buf[bufpos] = 0;
55 return bufpos < bufsize;
58 const char *
59 enum_name(const char *name)
61 static char buf[SIZEOF_STR];
63 return enum_name_copy(buf, sizeof(buf), name) ? buf : name;
66 bool
67 enum_name_prefixed(char buf[], size_t bufsize, const char *prefix, const char *name)
69 char prefixed[SIZEOF_STR];
71 if (*prefix) {
72 if (!string_format(prefixed, "%s-%s", prefix, name))
73 return FALSE;
74 name = prefixed;
77 return enum_name_copy(buf, bufsize, name);
80 const struct enum_map *
81 find_enum_map(const char *type)
83 static struct {
84 const char *type;
85 const struct enum_map *map;
86 } mappings[] = {
87 #define DEFINE_ENUM_MAPPING(name, macro) { #name, name##_map },
88 ENUM_INFO(DEFINE_ENUM_MAPPING)
90 int i;
92 for (i = 0; i < ARRAY_SIZE(mappings); i++)
93 if (!strcmp(type, mappings[i].type))
94 return mappings[i].map;
95 return NULL;
98 bool
99 map_enum_do(const struct enum_map_entry *map, size_t map_size, int *value, const char *name)
101 size_t namelen = strlen(name);
102 int i;
104 for (i = 0; i < map_size; i++)
105 if (enum_equals(map[i], name, namelen)) {
106 *value = map[i].value;
107 return TRUE;
110 return FALSE;
113 #define DEFINE_ENUM_MAPS(name, macro) DEFINE_ENUM_MAP(name, macro);
114 ENUM_INFO(DEFINE_ENUM_MAPS);
116 /* vim: set ts=8 sw=8 noexpandtab: */