Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "remote_table.h"
  6. #include "rtable-private.h"
  7.  
  8.  
  9. /*
  10. * table_client.c
  11. *
  12. * Created on: Oct 17, 2011
  13. * Author: fc37039
  14. */
  15. #define NUM_CHAR_MAX_ENTRADA 1999999
  16. #define MAX_DATA 1000
  17.  
  18.  
  19. char **Split(char *string) {
  20. char **temp = (char**) malloc(sizeof (char*)*(TamString(strdup(string))+1));
  21. if (temp == NULL)
  22. return NULL;
  23.  
  24. char *token = NULL;
  25. char *msg_str_aux = strdup(string);
  26. token= strtok( msg_str_aux, " ");
  27. int i = 0;
  28. temp[i]= strdup(token);
  29. while( token !=NULL)
  30. {
  31. token = strtok(NULL, " ");
  32. if (token!=NULL){
  33. i++;
  34. temp[i]= strdup(token);
  35. }
  36. }
  37. free(msg_str_aux);
  38. temp[i+1]=NULL;
  39. return temp;
  40. }
  41.  
  42. int tamanhoArray(char **string){
  43. int soma=0;
  44. int i =0;
  45. for (i=0; string[i]!=NULL; i++){
  46. soma = soma + 1;
  47. }
  48. return soma;
  49. }
  50.  
  51. int tamanhoString(int pos, char **string){
  52.  
  53. int soma=0;
  54. int i =pos;
  55. for (i=pos; string[i]!=NULL; i++){
  56. soma = soma + strlen(string[i]);
  57. }
  58. return soma;
  59. }
  60.  
  61. int main (int argc, char *argv[]){
  62. int saida = 0;
  63. if (argc!=1) {
  64.  
  65. struct rtable_t *connection = rtable_connect(argv[1]);
  66.  
  67. if (connection != NULL) {
  68. printf("CONNECTED TO: %s \n", argv[1]);
  69.  
  70. while (saida==0){
  71.  
  72. printf("Por favor, insira a sua linha de comando v��lida:\n");
  73. char *linha=(char*)malloc(NUM_CHAR_MAX_ENTRADA);
  74. //fopen(stdin,"r");
  75. fgets(linha,NUM_CHAR_MAX_ENTRADA,stdin);//Recebe uma linha v��lida.
  76. //Separar o primeiro token dos restantes, para poder ser processado.
  77. //Processar op����es com espa��os.
  78.  
  79. char **split = Split(strtok(linha,"\n"));
  80.  
  81. if (strcmp("put",split[0])==0){
  82. //verificar se a linha contem pelo menos 3 argumentos (ex: put <key> <data>)
  83. if (tamanhoArray(split) >= 3) {
  84.  
  85. char **data = (char**) malloc(sizeof (char*)*(tamanhoArray(split)-2));
  86. char *final;
  87. //ERRO COMEÇA AQUI
  88. data[0]=strdup(split[2]);
  89. final=strdup(data[0]);
  90. int i=1;
  91. if (tamanhoArray(split)>3) {
  92. int j;
  93. for (j=3;j<tamanhoArray(split);j++){
  94. data[i]=strdup(split[j]);
  95. strcat(final, " ");
  96. strcat(final,data[i]);
  97. i++;
  98. }
  99.  
  100. }
  101. //ERRO ACABA AQUI
  102. int x;
  103. for(x=0; x<i;x++)
  104. free(data[x]);
  105.  
  106. struct data_t *dataV = data_create(strlen(final)+1);
  107. // dataV->data=strdup(final);
  108. memcpy(dataV->data,final,strlen(final)+1);
  109.  
  110. int put = rtable_put(connection, split[1], dataV);
  111. if (put == 0){
  112. printf(">> PUT <%s> <%s>\n", split[1], (char*)dataV->data);
  113.  
  114. }else {
  115. printf("ERROR!");
  116. }
  117. data_destroy(dataV);
  118. free(data);
  119. rtable_free_keys(split);
  120. free(linha);
  121. free(final);
  122.  
  123. } else{
  124. printf("ERROR! - (e.g: put <key> <data>)\n");
  125. free(linha);
  126. list_free_keys(split);
  127. }
  128.  
  129. }
  130. else if(strcmp("get",split[0])==0){
  131. if (tamanhoArray(split) == 2) {
  132.  
  133. struct data_t *data = rtable_get(connection, split[1]);
  134. if (data != NULL) {
  135. printf(">> KEY '%s' SIZE: %d contains DATA: '%s'\n", split[1], (int)strlen((char*)data->data), (char*)data->data);
  136. data_destroy(data);
  137. free(linha);
  138. list_free_keys(split);
  139. } else{
  140. printf(">> NOTHING FOUND WITH KEY '%s'\n", split[1]);
  141. free(linha);
  142. list_free_keys(split);
  143. }
  144. } else{
  145. printf("ERROR: - (e.g: get <key>)\n");
  146. free(linha);
  147. list_free_keys(split);
  148. }
  149.  
  150.  
  151. }
  152. else if(strcmp("del",split[0])==0){
  153.  
  154. if (tamanhoArray(split) == 2) {
  155.  
  156. int resultado = rtable_del(connection, split[1]);
  157. if (resultado ==0)
  158. printf(">> KEY '%s' DELETED!\n", split[1]);
  159. else
  160. printf(">> KEY '%s' NOT FOUND!\n", split[1]);
  161. free(linha);
  162. list_free_keys(split);
  163. } else{
  164. printf("ERROR: - (e.g: del <key>)\n");
  165. free(linha);
  166. list_free_keys(split);
  167. }
  168.  
  169. }
  170. else if(strcmp("size",split[0])==0){
  171. int size = rtable_size(connection);
  172. printf(">> SIZE: %d\n", size);
  173. free(linha);
  174. list_free_keys(split);
  175.  
  176. }
  177. else if(strcmp("getkeys",split[0])==0){
  178. char **chaves = rtable_get_keys(connection);
  179. if (chaves==NULL){
  180. printf("Não foram encontradas chaves na tabela\n");
  181. free(linha);
  182. list_free_keys(split);
  183. }
  184. else{
  185. int size = rtable_size(connection);
  186. //printf(">> SIZE: %d\n", size);
  187. int i=0;
  188. for (i=0; (i<size); i++) //FAZER O GET_KEYS para depois saber o tamanho do char **chaves
  189. printf("ELEMENTO: %s\n",chaves[i]);
  190.  
  191. rtable_free_keys(chaves);
  192. free(linha);
  193. list_free_keys(split);
  194. }
  195. }
  196. else if(strcmp("quit",split[0])==0){
  197. printf("Exiting...\n");
  198. saida=1;
  199. free(linha);
  200. list_free_keys(split);
  201. rtable_disconnect(connection);
  202. return 0;
  203. }
  204. else{
  205. printf("Opção inválida (Opções válidas:Put,Get,Del,Size,Getkeys)\n");
  206. free(linha);
  207. list_free_keys(split);
  208. }
  209. }
  210. }
  211.  
  212. } else {
  213. printf("ERROR: (Usage: ./table-client localhost:1234)");
  214. return -1;
  215. }
  216. return 0;
  217. }
Add Comment
Please, Sign In to add comment