-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetcommand.c
65 lines (65 loc) · 1.73 KB
/
getcommand.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
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include"getcommand.h"
#define max 4096
struct stcommand *createCommand(char *arr,int del){
struct stcommand *s=(struct stcommand*)malloc(sizeof(struct stcommand));
// printf("%ld",sizeof(struct stcommand));
s->query=(char*)malloc(sizeof(char)*max);
strcpy(s->query,arr);
s->type=del;
s->next=NULL;
return s;
}
struct stquerylist *createList(){
struct stquerylist *s=(struct stquerylist*)malloc(sizeof(struct stquerylist));
s->numcommands=0;
s->command=NULL;
return s;
}
struct stcommand *insert(char *command,int del,struct stcommand *temp){
if(temp==NULL){
return createCommand(command,del);
}
struct stcommand *head=temp;
while(head->next!=NULL){
head=head->next;
}
head->next=createCommand(command,del);
return temp;
}
// void freecommand(struct stquerylist *commands){
// struct stcommand *temp;
// struct stcommand *head=commands->command;
// while(head!=NULL){
// temp=head;
// head=head->next;
// free(temp->query);
// free(temp);
// }
// // free(commands->command);
// free(commands);
// }
struct stquerylist *getCommands(char *input){
struct stquerylist *toret=createList();
char temp[max];
strcpy(temp,input);
char *token=strtok(input,"&;");
int count=0;
toret->numcommands++;
while(token!=NULL){
count=count+strlen(token);
if(temp[count]=='&')
{
toret->command=insert(token,1,toret->command);
}
else{
toret->command=insert(token,0,toret->command);
}
token=strtok(NULL,"&;");
count++;
toret->numcommands++;
}
return toret;
}