summaryrefslogtreecommitdiff
path: root/src/libtipidee/tipidee_method.c
blob: c5e71958b34c4aa436ac1408f533e95d35873028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* ISC license. */

#include <string.h>
#include <stdlib.h>

#include <tipidee/method.h>

struct blah_s
{
  tipidee_method num ;
  char const *str ;
} ;

static struct blah_s const table[] =
{
  { .num = TIPIDEE_METHOD_CONNECT, .str = "CONNECT" },
  { .num = TIPIDEE_METHOD_DELETE, .str = "DELETE" },
  { .num = TIPIDEE_METHOD_GET, .str = "GET" },
  { .num = TIPIDEE_METHOD_HEAD, .str = "HEAD" },
  { .num = TIPIDEE_METHOD_OPTIONS, .str = "OPTIONS" },
  { .num = TIPIDEE_METHOD_POST, .str = "POST" },
  { .num = TIPIDEE_METHOD_PRI, .str = "PRI" },
  { .num = TIPIDEE_METHOD_PUT, .str = "PUT" },
  { .num = TIPIDEE_METHOD_TRACE, .str = "TRACE" },
} ;

static int blah_cmp (void const *key, void const *el)
{
  return strcmp((char const *)key, ((struct blah_s const *)el)->str) ;
}

tipidee_method tipidee_method_tonum (char const *s)
{
  struct blah_s const *p = bsearch(
    s,
    table,
    sizeof(table) / sizeof(struct blah_s),
    sizeof(struct blah_s),
    &blah_cmp) ;
  return p ? p->num : TIPIDEE_METHOD_UNKNOWN ;
}

char const *tipidee_method_tostr (tipidee_method m)
{
  return m < TIPIDEE_METHOD_UNKNOWN ? table[m].str : 0 ;
}