ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
enums.c File Reference

Option enum value implementation with dynamic enum-to-string mapping. More...

Go to the source code of this file.

Data Structures

struct  enum_map_descriptor_t
 Descriptor for an enum option with its string value mapping. More...
 

Functions

const enum_to_string_entry_t * options_get_enum_entries (const char *option_name, size_t *entry_count)
 Get all enum entries with descriptions for an option.
 
const char ** options_get_enum_values (const char *option_name, size_t *value_count)
 Get all unique string values for an option.
 
bool options_is_enum_option (const char *option_name)
 Check if an option has enum values.
 

Detailed Description

Option enum value implementation with dynamic enum-to-string mapping.

Definition in file enums.c.

Function Documentation

◆ options_get_enum_entries()

const enum_to_string_entry_t * options_get_enum_entries ( const char *  option_name,
size_t *  entry_count 
)

Get all enum entries with descriptions for an option.

Get enum entries with descriptions for an option.

Returns entries containing both string values and descriptions for shell completion. Used by zsh completion to show descriptions alongside values.

Parameters
option_nameOption long name (e.g., "log-level")
entry_countOUTPUT: Number of entries returned
Returns
Array of enum entries, or NULL if not an enum option

Definition at line 134 of file enums.c.

134 {
135 if (!option_name || !entry_count) {
136 return NULL;
137 }
138
139 for (size_t i = 0; enum_registry[i].option_name != NULL; i++) {
140 if (strcmp(enum_registry[i].option_name, option_name) == 0) {
141 /* Count entries up to terminator */
142 size_t count = 0;
143 for (size_t j = 0; enum_registry[i].map[j].enum_value != -1; j++) {
144 count++;
145 }
146 *entry_count = count;
147 return enum_registry[i].map;
148 }
149 }
150
151 *entry_count = 0;
152 return NULL;
153}
const char * option_name
Option long name (e.g., "log-level")
Definition enums.c:107
const enum_to_string_entry_t * map
Mapping array from enum values to strings.
Definition enums.c:108
int enum_value
The numeric enum value.
Definition enums.h:28

References enum_to_string_entry_t::enum_value, enum_map_descriptor_t::map, and enum_map_descriptor_t::option_name.

◆ options_get_enum_values()

const char ** options_get_enum_values ( const char *  option_name,
size_t *  value_count 
)

Get all unique string values for an option.

Get enum values for an option.

Extracts the list of valid string values for an enum option. For options with aliases (like render-mode which has "foreground" and "fg"), both values are included.

Parameters
option_nameOption long name (e.g., "log-level")
value_countOUTPUT: Number of values returned
Returns
Array of valid string values, or NULL if not an enum option

Definition at line 166 of file enums.c.

166 {
167 if (!option_name || !value_count) {
168 return NULL;
169 }
170
171 /* Find the option's mapping in the registry */
172 const enum_map_descriptor_t *descriptor = NULL;
173 for (size_t i = 0; enum_registry[i].option_name != NULL; i++) {
174 if (strcmp(enum_registry[i].option_name, option_name) == 0) {
175 descriptor = &enum_registry[i];
176 break;
177 }
178 }
179
180 if (!descriptor || !descriptor->map) {
181 *value_count = 0;
182 return NULL;
183 }
184
185 /* Count non-terminator entries */
186 size_t count = 0;
187 for (size_t i = 0; descriptor->map[i].enum_value != -1; i++) {
188 count++;
189 }
190
191 /* Build array of unique strings (skip duplicates from aliases) */
192 const char **values = SAFE_MALLOC(count * sizeof(const char *), const char **);
193 size_t unique_count = 0;
194
195 for (size_t i = 0; descriptor->map[i].enum_value != -1; i++) {
196 const char *str = descriptor->map[i].string;
197
198 /* Check if we already have this string (for aliases) */
199 bool already_added = false;
200 for (size_t j = 0; j < unique_count; j++) {
201 if (strcmp(values[j], str) == 0) {
202 already_added = true;
203 break;
204 }
205 }
206
207 if (!already_added) {
208 values[unique_count++] = str;
209 }
210 }
211
212 *value_count = unique_count;
213 return values;
214}
#define SAFE_MALLOC(size, cast)
Definition common.h:264
Descriptor for an enum option with its string value mapping.
Definition enums.c:106
const char * string
The string representation of that enum value.
Definition enums.h:29

References enum_to_string_entry_t::enum_value, enum_map_descriptor_t::map, enum_map_descriptor_t::option_name, SAFE_MALLOC, and enum_to_string_entry_t::string.

Referenced by asciichat_suggest_enum_value().

◆ options_is_enum_option()

bool options_is_enum_option ( const char *  option_name)

Check if an option has enum values.

Parameters
option_nameOption long name
Returns
true if option has enum values, false otherwise

Definition at line 222 of file enums.c.

222 {
223 if (!option_name) {
224 return false;
225 }
226
227 for (size_t i = 0; enum_registry[i].option_name != NULL; i++) {
228 if (strcmp(enum_registry[i].option_name, option_name) == 0) {
229 return true;
230 }
231 }
232
233 return false;
234}

References enum_map_descriptor_t::option_name.