librfn
An ad-hoc utility library
enum.c
Go to the documentation of this file.
1 /*
2  * enum.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <string.h>
15 
16 #include "librfn.h"
17 
18 const char *rf_enum2string(rf_enumtable_t *t, int e)
19 {
20  for (; t->s; t++) {
21  if (t->e == e)
22  return t->s;
23  }
24 
25  return NULL;
26 }
27 
28 int rf_string2enum(rf_enumtable_t *t, const char *s)
29 {
30  for (; t->s; t++) {
31  if (0 == strcmp(t->s, s))
32  return t->e;
33  }
34 
35  return RF_ENUM_OUT_OF_RANGE;
36 }
const char * rf_enum2string(rf_enumtable_t *t, int e)
Definition: enum.c:18
int rf_string2enum(rf_enumtable_t *t, const char *s)
Definition: enum.c:28
Lookup table used for the conversions.
Definition: enum.h:43
const char * s
Definition: enum.h:44
#define RF_ENUM_OUT_OF_RANGE
Definition: enum.h:56