forked from mengning/nezha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.c
163 lines (155 loc) · 4.41 KB
/
cmdline.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/********************************************************************/
/* Copyright (C) MC2Lab-USTC, 2012 */
/* */
/* FILE NAME : main.c */
/* PRINCIPAL AUTHOR : Mengning */
/* SUBSYSTEM NAME : DB */
/* MODULE NAME : main */
/* LANGUAGE : C */
/* TARGET ENVIRONMENT : Linux */
/* DATE OF FIRST RELEASE : 2012/11/27 */
/* DESCRIPTION : The cmdline of Nezha KV Database system*/
/********************************************************************/
/*
* Revision log:
* Nezha 0.1.0,it can get/set KV data.Created by Mengning,2012/11/27
* Nezha 0.2.0,it add cmdline interface.Created by Mengning,2012/12/06
*
*/
#include "dbapi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#define CMD_NUM 6
#define MAX_STR_LEN 1024
char cmdbuf[MAX_STR_LEN];
const char *prompt = "Nezha";
char dbname[MAX_STR_LEN] = "\0";
tDatabase db = NULL;
int GetCmd(char * cmdbuf,int size);
int ExecCmd(char * cmdbuf);
int main(int argc, char **argv)
{
/*start cmdline here*/
while(1)
{
printf("%s::%s>>",prompt,dbname);
GetCmd(cmdbuf,MAX_STR_LEN);
ExecCmd(cmdbuf);
}
return;
}
int GetCmd(char * cmdbuf,int size)
{
size;
gets(cmdbuf);
return 0;
}
int CheckCmd(char * cmdbuf,char * pattern)
{
regex_t reg;
regmatch_t pmatch[1];
const size_t nmatch = 1;
regcomp(®,pattern,REG_EXTENDED | REG_ICASE | REG_NOSUB);
if (regexec(®,cmdbuf,nmatch,pmatch,0) == 0)
{
regfree(®);
return 0;
}
}
int ExecCmd(char * cmdbuf)
{
char temp[MAX_STR_LEN] = "\0";
if(CheckCmd(cmdbuf,"help") == 0 )
{
printf("open filename - EX:open nezha.hdb\n");
printf("set key value - EX:set 100 helloworld\n");
printf("get key - EX:get 100\n");
printf("delete key - EX:delete 100\n");
printf("close - leave nezha.hdb\n");
printf("help - list cmds info\n");
}
else if(CheckCmd(cmdbuf,"open (.*)\\.hdb") == 0)
{
if(strlen(dbname) > 0)
{
printf("Please close %s first.\n",dbname);
}
else
{
sscanf(cmdbuf,"%s%s",temp,dbname);
db = DBCreate(dbname);
}
}
else if(db == NULL && CheckCmd(cmdbuf,"set|get|delete") == 0)
{
printf("Please open a database file first.\n");
}
else if(CheckCmd(cmdbuf,"close") == 0)
{
if(db != NULL)
{
DBDelete(db);
db = NULL;
}
dbname[0] = '\0';
}
else if(CheckCmd(cmdbuf,"exit") == 0)
{
if(db != NULL)
{
DBDelete(db);
}
exit(0);
}
else if(CheckCmd(cmdbuf,"set ([0-9]+) (.*)") == 0)
{
tKey key;
tValue value;
char str[MAX_STR_LEN] = "\0";
sscanf(cmdbuf,"%s%d%s",temp,&key,str);
value.str = strstr(cmdbuf,str);
value.len = strlen(value.str);//cmdbuf + MAX_STR_LEN - value.str;
if(DBSetKeyValue(db,key,value) != 0)
{
printf("ERROR:set %d %s\n",(int)key,value.str);
}
//printf("set %d %s\n",(int)key,value.str);
}
else if(CheckCmd(cmdbuf,"get ([0-9]+)") == 0)
{
tKey key = -1;
tValue value;
char str[MAX_STR_LEN] = "\0";
value.str = str;
value.len = MAX_STR_LEN;
sscanf(cmdbuf,"%s%d",temp,&key);
if(DBGetKeyValue(db,key,&value) == 0)
{
printf("%d -> %s\n",key,value.str);
}
else
{
printf("ERROR:get %d Not found!\n",(int)key);
}
}
else if(CheckCmd(cmdbuf,"delete ([0-9]+)") == 0)
{
tKey key = -1;
tValue value;
char str[MAX_STR_LEN] = "\0";
value.str = str;
value.len = MAX_STR_LEN;
sscanf(cmdbuf,"%s%d",temp,&key);
if(DBDelKeyValue(db,key) != 0)
{
printf("ERROR:delete %d\n",(int)key);
}
}
else
{
printf("Unknow Command!\n");
}
return 0;
}