main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "json.h"
  4. #include "arraylist.h"
  5. #include "server.h"
  6. #include "error.h"
  7. int parse(){
  8. char str[200];
  9. char* key = NULL;
  10. char* val = NULL;
  11. JsonParser json;
  12. strcpy(str, "{\"name\" : \"Jack\", \"age\": 27, \"test\": true, \"tab\": [1, 2, 3, 4, 5], \"obj\": {\"name\" : \"Jack\", \"age\": 27}, \"nb\": 27.8 }");
  13. //Parse
  14. int a = json_parse(&json, str);
  15. printf("Parse : %d\n", a);
  16. //Affiche toutes les clefs : valeurs
  17. for(int i = 0; i < json.elt; i++){
  18. key = key_index(&json, i);
  19. val = get_index(&json, i);
  20. printf("%s : %s\n", key, val);
  21. }
  22. //Recup un nombre
  23. printf("Double : %f %.2f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
  24. //Recup boolean
  25. printf("Bool : %d %d\n", get_boolean(&json, "test"), get_boolean(&json, "tab"));
  26. //Recup obj
  27. JsonParser* js;
  28. js = get_object(&json, "obj");
  29. if(js != NULL){
  30. int key_i = get_pos(js, "name");
  31. printf("JSON : %s %s, age %d\n", key_index(js, key_i), get_value(js, "name"), get_integer(js, "age"));
  32. } else {
  33. printf("JSON : Error");
  34. }
  35. //Supprime
  36. free(key);
  37. free(val);
  38. clean_json_parser(&json);
  39. return 0;
  40. }
  41. int encode(){
  42. //Encode
  43. JsonEncoder json;
  44. ini_encoder(&json);
  45. add_string(&json, "name", "robert");
  46. add_number(&json, "nb", 25.698, 2);
  47. add_integer(&json, "int", 846);
  48. add_string(&json, "aze", "rty");
  49. add_boolean(&json, "bool", false);
  50. add_value(&json, "\"test\": \"aze\nrty\"");
  51. printf("Json\n");
  52. printf("%s\n", json_encode(&json));
  53. //Encode un JsonEncoder
  54. JsonEncoder json2;
  55. ini_encoder(&json2);
  56. add_integer(&json2, "vie", 42);
  57. add_object(&json2, "obj", json);
  58. printf("\nJson 2\n");
  59. printf("%s\n", json_encode(&json2));
  60. //Decode
  61. JsonParser parser, *parser2;
  62. char* key, *val;
  63. json_parse(&parser, json_encode(&json2));
  64. //Affiche toutes les clefs : valeurs
  65. printf("\nParser\n");
  66. for(int i = 0; i < parser.elt; i++){
  67. key = key_index(&parser, i);
  68. val = get_index(&parser, i);
  69. printf("%s : %s\n", key, val);
  70. }
  71. //Lecture du sous json
  72. parser2 = get_object(&parser, "obj");
  73. //Affiche toutes les clefs : valeurs
  74. printf("\nParser 2\n");
  75. for(int i = 0; i < parser2->elt; i++){
  76. key = key_index(parser2, i);
  77. val = get_index(parser2, i);
  78. printf("%s : %s\n", key, val);
  79. }
  80. //Clean
  81. clean_json_encoder(&json);
  82. clean_json_encoder(&json2);
  83. clean_json_parser(&parser);
  84. return 0;
  85. }
  86. int func(char* arg){
  87. printf("Func : %s\n", arg);
  88. return EXIT_SUCCESS;
  89. }
  90. int tion(char* arg){
  91. printf("Tion : %s\n", arg);
  92. return EXIT_SUCCESS;
  93. }
  94. int array_list(){
  95. arraylist al;
  96. arraylist_ini(&al);
  97. arraylist_add(&al, "func1", func);
  98. arraylist_add(&al, "func2", tion);
  99. printf("%d\n", arraylist_call(&al, "func2", "Pouet"));
  100. printf("%d\n", arraylist_call(&al, "func1", "Loul"));
  101. printf("%d\n", arraylist_call(&al, "func3", "Nop"));
  102. return 0;
  103. }
  104. int serv(){
  105. char str[BUFFER_SIZE];
  106. Server s = server_create_tcp();
  107. s->server_bind(s, 8080);
  108. s->server_accept(s);
  109. s->server_receive(s, str, BUFFER_SIZE);
  110. printf("%s\n", str);
  111. server_close_client(s);
  112. server_close(s);
  113. s = server_create_udp();
  114. s->server_bind(s, 8080);
  115. s->server_receive(s, str, BUFFER_SIZE);
  116. printf("%s\n", str);
  117. server_close(s);
  118. return 0;
  119. }
  120. int serv_udp(){
  121. char str[BUFFER_SIZE];
  122. Server s = server_create_udp();
  123. s->server_bind(s, 18426);
  124. s->server_receive(s, str, BUFFER_SIZE);
  125. s->server_send(s, "i'm a bomberstudent server");
  126. printf("%s\n", str);
  127. server_close(s);
  128. return 0;
  129. }
  130. int serv_tcp(){
  131. char str[BUFFER_SIZE];
  132. Server s = server_create_tcp();
  133. s->server_bind(s, 18426);
  134. s->server_accept(s);
  135. s->server_receive(s, str, BUFFER_SIZE);
  136. printf("%s\n", str);
  137. s->server_send(s, "Hello");
  138. server_close_client(s);
  139. server_close(s);
  140. return 0;
  141. }
  142. int main(){
  143. //return parse();
  144. //return encode();
  145. //return array_list();
  146. //return serv();
  147. //return serv_udp();
  148. return serv_tcp();
  149. }