Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Enum.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdio.h>
26#include <string.h>
27
28#include "Enum.h"
29#include "MutableString.h"
30
31String *nameof(const EnumName *names, int value) {
32
33 for (const EnumName *name = names; name->name; name++) {
34 if (name->value == value) {
35 return $$(String, stringWithCharacters, name->name);
36 }
37 }
38
39 MutableString *string = NULL;
40
41 for (const EnumName *name = names; name->name; name++) {
42 if (name->value) {
43 if ((name->value & value) == name->value) {
44 if (string == NULL) {
45 string = $$(MutableString, string);
46 } else {
47 $(string, appendCharacters, " | ");
48 }
49 $(string, appendCharacters, name->name);
50 }
51 }
52 }
53
54 return (String *) string;
55}
56
57int valueof(const EnumName *names, const char *chars) {
58
59 int value = 0;
60
61 const char *c = chars;
62 while (*c) {
63 const size_t size = strcspn(c, " |");
64 if (size) {
65 const EnumName *en;
66 for (en = names; en->name; en++) {
67 if (strncmp(en->name, c, size) == 0) {
68 value |= en->value;
69 break;
70 }
71 if (en->alias && strncmp(en->alias, c, size) == 0) {
72 value |= en->value;
73 break;
74 }
75 }
76 if (en->name == NULL) {
77 fprintf(stderr, "%s: Unknown character sequence: %s\n", __func__, c);
78 }
79 }
80 c += size;
81 c += strspn(c, " |");
82 }
83
84 return value;
85}
String * nameof(const EnumName *names, int value)
Marshal value, returning a String of OR'ed EnumNames for the given enum value.
Definition: Enum.c:31
Named enumerations.
Mutable UTF-8 strings.
Immutable UTF-8 strings.
Boole * valueof(_Bool value)
Definition: Boole.c:93
The EnumName type.
Definition: Enum.h:37
const char * name
Definition: Enum.h:38
int value
Definition: Enum.h:40
const char * alias
Definition: Enum.h:39
Mutable UTF-8 strings.
Definition: MutableString.h:40
MutableString * string(void)
Returns a new MutableString.
void appendCharacters(MutableString *self, const char *chars)
Appends the specified UTF-8 encoded C string.
Definition: MutableString.c:54
Immutable UTF-8 strings.
Definition: String.h:69
String * stringWithCharacters(const char *chars)
Returns a new String by copying chars.
Definition: String.c:487