summaryrefslogtreecommitdiff
path: root/src/cmd.c
blob: 266f17bb0891234f262e1c7ba1943c49812a7f42 (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
#include <string.h>
#include <stdlib.h>

#include "cmd.h"

char *getselector(char *str);

struct cmd eval(char *str)
{
  struct cmd ret;
  ret.params = NULL;
  ret.selector = NULL;
  if (!strncmp(str, "GET", 3)) {
    ret.type = GET;
  } else if (!strncmp(str, "SET", 3)) {
    ret.type = SET;
  } else if (!strncmp(str, "DEL", 3)) {
    ret.type = DEL;
  } else {
    ret.type = ERR;
    return ret;
  }

  ret.selector = getselector(str);
  return ret;
}

char *getselector(char *str)
{
  char *selector = calloc(1, sizeof(char));
  int pos = 4, i;
  for (i = 0; str[pos] != '/' && pos < strlen(str); ++pos) {
    if (i >= 1) {
      selector = realloc(selector, (i + 1) * sizeof(char));
    }
    selector[i++] = str[pos];
  }
  selector = realloc(selector, (i + 1) * sizeof(char));
  selector[i] = '\0';
  return selector;
}