Browse Source

list_shift and queue_get now take a char**

master
Brett Langdon 13 years ago
parent
commit
11306f2bd5
5 changed files with 24 additions and 16 deletions
  1. +17
    -10
      src/handlers.c
  2. +1
    -1
      src/queue.c
  3. +1
    -1
      src/queue.h
  4. +4
    -3
      src/util.c
  5. +1
    -1
      src/util.h

+ 17
- 10
src/handlers.c View File

@ -56,18 +56,24 @@ void handle_flush(KCLIST* tokens, FILE* client){
void handle_delete(KCLIST* tokens, FILE* client){ void handle_delete(KCLIST* tokens, FILE* client){
if(kclistcount(tokens)){ if(kclistcount(tokens)){
char key[128];
list_shift(tokens, key);
if(kcdbremove(db, key, strlen(key))){
fputs("DELETED\r\n", client);
char* key;
list_shift(tokens, &key);
if(key != NULL){
if(kcdbremove(db, key, strlen(key))){
fputs("DELETED\r\n", client);
}
free(key);
} }
} }
} }
void handle_get(KCLIST* tokens, FILE* client){ void handle_get(KCLIST* tokens, FILE* client){
if(kclistcount(tokens)){ if(kclistcount(tokens)){
char key[128];
list_shift(tokens, key);
char* key;
list_shift(tokens, &key);
if(key == NULL){
return;
}
char out[1024]; char out[1024];
char* result_buffer; char* result_buffer;
size_t result_size; size_t result_size;
@ -88,20 +94,20 @@ void handle_get(KCLIST* tokens, FILE* client){
queue_add(&requests, key); queue_add(&requests, key);
} }
fputs(out, client); fputs(out, client);
free(key);
} else{ } else{
fputs("INVALID GET COMMAND: GET <KEY>\r\n", client); fputs("INVALID GET COMMAND: GET <KEY>\r\n", client);
return;
} }
} }
int handle_command(char* buffer, FILE* client){ int handle_command(char* buffer, FILE* client){
int status = 0; int status = 0;
char command[1024];
KCLIST* tokens = kclistnew(); KCLIST* tokens = kclistnew();
tokenize(tokens, buffer, " "); tokenize(tokens, buffer, " ");
list_shift(tokens, command);
char* command;
list_shift(tokens, &command);
if(command != NULL){ if(command != NULL){
if(strcmp(command, "get") == 0){
if(strncmp(command, "get", 3) == 0){
handle_get(tokens, client); handle_get(tokens, client);
} else if(strcmp(command, "stats") == 0){ } else if(strcmp(command, "stats") == 0){
handle_stats(tokens, client); handle_stats(tokens, client);
@ -118,6 +124,7 @@ int handle_command(char* buffer, FILE* client){
sprintf(out, "UNKNOWN COMMAND: %s\r\n", command); sprintf(out, "UNKNOWN COMMAND: %s\r\n", command);
fputs(out, client); fputs(out, client);
} }
free(command);
} }
kclistdel(tokens); kclistdel(tokens);


+ 1
- 1
src/queue.c View File

@ -21,7 +21,7 @@ void queue_add(PTR_QUEUE q, char* value){
pthread_cond_signal(&q->cond); pthread_cond_signal(&q->cond);
} }
void queue_get(PTR_QUEUE q, char* value){
void queue_get(PTR_QUEUE q, char** value){
pthread_mutex_lock(&q->mutex); pthread_mutex_lock(&q->mutex);
while(*(&q->size) == 0){ while(*(&q->size) == 0){
pthread_cond_wait(&q->cond, &q->mutex); pthread_cond_wait(&q->cond, &q->mutex);


+ 1
- 1
src/queue.h View File

@ -17,7 +17,7 @@ typedef QUEUE* PTR_QUEUE;
void queue_del(PTR_QUEUE q); void queue_del(PTR_QUEUE q);
void queue_init(PTR_QUEUE q); void queue_init(PTR_QUEUE q);
void queue_add(PTR_QUEUE q, char* value); void queue_add(PTR_QUEUE q, char* value);
void queue_get(PTR_QUEUE q, char* value);
void queue_get(PTR_QUEUE q, char** value);
int queue_size(PTR_QUEUE q); int queue_size(PTR_QUEUE q);
#endif #endif

+ 4
- 3
src/util.c View File

@ -1,10 +1,11 @@
#include "util.h" #include "util.h"
void list_shift(KCLIST* list, char* next){
void list_shift(KCLIST* list, char** next){
size_t size; size_t size;
const char* results = kclistget(list, 0, &size); const char* results = kclistget(list, 0, &size);
strcpy(next, results);
next[size] = 0;
*next = malloc((size * sizeof(char)) + 1);
strcpy(*next, results);
(*next)[size] = 0;
kclistshift(list); kclistshift(list);
} }


+ 1
- 1
src/util.h View File

@ -3,7 +3,7 @@
#include <kclangc.h> #include <kclangc.h>
void list_shift(KCLIST* list, char* next);
void list_shift(KCLIST* list, char** next);
void lower(char* word); void lower(char* word);
void tokenize(KCLIST* tokens, char* base, char* delimiter); void tokenize(KCLIST* tokens, char* base, char* delimiter);
#endif #endif

Loading…
Cancel
Save