-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageProcessing.c
More file actions
52 lines (45 loc) · 1.18 KB
/
messageProcessing.c
File metadata and controls
52 lines (45 loc) · 1.18 KB
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
47
48
49
50
51
52
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "datahandler.h"
void stringTokenize(char * msg, char ** inputs) {
bool quoteOpen=false;
int index=0, pos=0;
char * buf = malloc(sizeof(char)*100);
strcpy(buf, "");
for(int i=0; msg[i]!='\n'; i++) {
if(!quoteOpen && (msg[i]==' ' || msg[i]=='\n')) {
inputs[index]=buf;
buf=malloc(sizeof(char)*100);
strcpy(buf, "");
index++;
continue;
}
if(msg[i]=='"') {
quoteOpen=!quoteOpen;
continue;
}
strncat(buf, &msg[i], 1);
pos++;
}
inputs[2]=buf;
}
void performOperation(char ** inputs, char * message) {
if(strcmp(inputs[0], "add")==0) {
datahandler_addData(inputs[1], inputs[2]);
strcpy(message, "data added");
}
else if(strcmp(inputs[0], "get")==0) {
char * val=datahandler_getData(inputs[1]);
if(val!=NULL) {
sprintf(message, "%s", val);
}
else {
strcpy(message, "key not found");
}
}
else {
strcpy(message, "invalid command");
}
}