Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "remote_table.h"
- #include "rtable-private.h"
- /*
- * table_client.c
- *
- * Created on: Oct 17, 2011
- * Author: fc37039
- */
- #define NUM_CHAR_MAX_ENTRADA 1999999
- #define MAX_DATA 1000
- char **Split(char *string) {
- char **temp = (char**) malloc(sizeof (char*)*(TamString(strdup(string))+1));
- if (temp == NULL)
- return NULL;
- char *token = NULL;
- char *msg_str_aux = strdup(string);
- token= strtok( msg_str_aux, " ");
- int i = 0;
- temp[i]= strdup(token);
- while( token !=NULL)
- {
- token = strtok(NULL, " ");
- if (token!=NULL){
- i++;
- temp[i]= strdup(token);
- }
- }
- free(msg_str_aux);
- temp[i+1]=NULL;
- return temp;
- }
- int tamanhoArray(char **string){
- int soma=0;
- int i =0;
- for (i=0; string[i]!=NULL; i++){
- soma = soma + 1;
- }
- return soma;
- }
- int tamanhoString(int pos, char **string){
- int soma=0;
- int i =pos;
- for (i=pos; string[i]!=NULL; i++){
- soma = soma + strlen(string[i]);
- }
- return soma;
- }
- int main (int argc, char *argv[]){
- int saida = 0;
- if (argc!=1) {
- struct rtable_t *connection = rtable_connect(argv[1]);
- if (connection != NULL) {
- printf("CONNECTED TO: %s \n", argv[1]);
- while (saida==0){
- printf("Por favor, insira a sua linha de comando v��lida:\n");
- char *linha=(char*)malloc(NUM_CHAR_MAX_ENTRADA);
- //fopen(stdin,"r");
- fgets(linha,NUM_CHAR_MAX_ENTRADA,stdin);//Recebe uma linha v��lida.
- //Separar o primeiro token dos restantes, para poder ser processado.
- //Processar op����es com espa��os.
- char **split = Split(strtok(linha,"\n"));
- if (strcmp("put",split[0])==0){
- //verificar se a linha contem pelo menos 3 argumentos (ex: put <key> <data>)
- if (tamanhoArray(split) >= 3) {
- char **data = (char**) malloc(sizeof (char*)*(tamanhoArray(split)-2));
- char *final;
- //ERRO COMEÇA AQUI
- data[0]=strdup(split[2]);
- final=strdup(data[0]);
- int i=1;
- if (tamanhoArray(split)>3) {
- int j;
- for (j=3;j<tamanhoArray(split);j++){
- data[i]=strdup(split[j]);
- strcat(final, " ");
- strcat(final,data[i]);
- i++;
- }
- }
- //ERRO ACABA AQUI
- int x;
- for(x=0; x<i;x++)
- free(data[x]);
- struct data_t *dataV = data_create(strlen(final)+1);
- // dataV->data=strdup(final);
- memcpy(dataV->data,final,strlen(final)+1);
- int put = rtable_put(connection, split[1], dataV);
- if (put == 0){
- printf(">> PUT <%s> <%s>\n", split[1], (char*)dataV->data);
- }else {
- printf("ERROR!");
- }
- data_destroy(dataV);
- free(data);
- rtable_free_keys(split);
- free(linha);
- free(final);
- } else{
- printf("ERROR! - (e.g: put <key> <data>)\n");
- free(linha);
- list_free_keys(split);
- }
- }
- else if(strcmp("get",split[0])==0){
- if (tamanhoArray(split) == 2) {
- struct data_t *data = rtable_get(connection, split[1]);
- if (data != NULL) {
- printf(">> KEY '%s' SIZE: %d contains DATA: '%s'\n", split[1], (int)strlen((char*)data->data), (char*)data->data);
- data_destroy(data);
- free(linha);
- list_free_keys(split);
- } else{
- printf(">> NOTHING FOUND WITH KEY '%s'\n", split[1]);
- free(linha);
- list_free_keys(split);
- }
- } else{
- printf("ERROR: - (e.g: get <key>)\n");
- free(linha);
- list_free_keys(split);
- }
- }
- else if(strcmp("del",split[0])==0){
- if (tamanhoArray(split) == 2) {
- int resultado = rtable_del(connection, split[1]);
- if (resultado ==0)
- printf(">> KEY '%s' DELETED!\n", split[1]);
- else
- printf(">> KEY '%s' NOT FOUND!\n", split[1]);
- free(linha);
- list_free_keys(split);
- } else{
- printf("ERROR: - (e.g: del <key>)\n");
- free(linha);
- list_free_keys(split);
- }
- }
- else if(strcmp("size",split[0])==0){
- int size = rtable_size(connection);
- printf(">> SIZE: %d\n", size);
- free(linha);
- list_free_keys(split);
- }
- else if(strcmp("getkeys",split[0])==0){
- char **chaves = rtable_get_keys(connection);
- if (chaves==NULL){
- printf("Não foram encontradas chaves na tabela\n");
- free(linha);
- list_free_keys(split);
- }
- else{
- int size = rtable_size(connection);
- //printf(">> SIZE: %d\n", size);
- int i=0;
- for (i=0; (i<size); i++) //FAZER O GET_KEYS para depois saber o tamanho do char **chaves
- printf("ELEMENTO: %s\n",chaves[i]);
- rtable_free_keys(chaves);
- free(linha);
- list_free_keys(split);
- }
- }
- else if(strcmp("quit",split[0])==0){
- printf("Exiting...\n");
- saida=1;
- free(linha);
- list_free_keys(split);
- rtable_disconnect(connection);
- return 0;
- }
- else{
- printf("Opção inválida (Opções válidas:Put,Get,Del,Size,Getkeys)\n");
- free(linha);
- list_free_keys(split);
- }
- }
- }
- } else {
- printf("ERROR: (Usage: ./table-client localhost:1234)");
- return -1;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment