hashmap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * hashmap.c
  3. *
  4. * Created on: 2019 ~4 27
  5. * Author: foluswen
  6. */
  7. #include "Module_OcppBackend.h"
  8. #undef FALSE
  9. #undef TRUE
  10. typedef enum boolean { FALSE, TRUE } BOOL;
  11. static pthread_mutex_t mutexMap = PTHREAD_MUTEX_INITIALIZER;
  12. int MessageSent_add(char *uuid, char *data)
  13. {
  14. int result = PASS;
  15. FILE *outfile;
  16. char rmFileCmd[100]={0};
  17. char tempstring[100]={0};
  18. struct stat stats;
  19. stat("/Storage/OCPP_PH", &stats);
  20. if (S_ISDIR(stats.st_mode) == 1)
  21. {}
  22. else
  23. {
  24. DEBUG_INFO("/Storage/OCPP_PH directory not exist, create dir \n");
  25. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
  26. system(rmFileCmd);
  27. }
  28. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  29. if((access("/Storage/OCPP_PH/MessageSent",F_OK)) != -1)
  30. {}
  31. else
  32. {
  33. DEBUG_INFO("/Storage/OCPP_PH/MessageSent not exist\n");
  34. FILE *log = fopen("/Storage/OCPP_PH/MessageSent", "w+");
  35. if(log == NULL)
  36. {
  37. DEBUG_ERROR("Can't Create File /Storage/OCPP_PH/MessageSent \n");
  38. result = FAIL;
  39. }
  40. else
  41. {
  42. fclose(log);
  43. }
  44. }
  45. // open file for writing
  46. if(result != FAIL)
  47. {
  48. outfile = fopen ("/Storage/OCPP_PH/MessageSent", "a");
  49. sprintf(tempstring,"%s,%s\n", uuid,data);
  50. fputs(tempstring, outfile);
  51. fclose (outfile);
  52. }
  53. return result;
  54. }
  55. int MessageSent_get(char *uuid, char *data)
  56. {
  57. int result = PASS;
  58. FILE *fp;
  59. char str[1200]={0};
  60. char sstr[50]={ 0 }, datastr[30]={0};
  61. int c = 0;
  62. char *loc;
  63. char rmFileCmd[100]={0};
  64. struct stat stats;
  65. stat("/Storage/OCPP_PH", &stats);
  66. // Check for directory existence
  67. if (S_ISDIR(stats.st_mode) == 1)
  68. {}
  69. else
  70. {
  71. DEBUG_INFO("/Storage/OCPP_PH directory not exist, create dir \n");
  72. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
  73. system(rmFileCmd);
  74. }
  75. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  76. if((access("/Storage/OCPP_PH/MessageSent",F_OK)) != -1)
  77. {}
  78. else
  79. {
  80. DEBUG_INFO("/Storage/OCPP_PH/MessageSent not exist\n");
  81. FILE *log = fopen("/Storage/OCPP_PH/MessageSent", "w+");
  82. if(log == NULL)
  83. {
  84. DEBUG_ERROR("Can't Create File /Storage/OCPP_PH/MessageSent \n");
  85. result = FAIL;
  86. }
  87. else
  88. {
  89. fclose(log);
  90. }
  91. }
  92. if(result != FAIL)
  93. {
  94. /* opening file for reading */
  95. fp = fopen("/Storage/OCPP_PH/MessageSent" , "r");
  96. if(fp == NULL)
  97. {
  98. DEBUG_ERROR("Error opening /Storage/OCPP_PH/MessageSent\n");
  99. result = FAIL;
  100. }
  101. c = fgetc(fp);
  102. rewind(fp);
  103. if(c == EOF)
  104. {
  105. DEBUG_INFO("/Storage/OCPP_PH/MessageSent is null\n");
  106. strcpy(data,"");
  107. result = FAIL;
  108. }
  109. else
  110. {
  111. result = FAIL;
  112. while (fgets (str, 1200, fp)!=NULL)
  113. {
  114. str[strlen(str) - 1] = '\0';
  115. /*********************uuid***************/
  116. int d = 0;
  117. while (str[d] != ',')
  118. {
  119. sstr[d] = str[d];
  120. d=d+ 1;
  121. }
  122. sstr[d] = '\0';
  123. if(strcmp(sstr, uuid) == 0)
  124. {
  125. loc = strstr(str, ",");
  126. memset(sstr ,0, sizeof(sstr) );
  127. int e = 0;
  128. while (loc[1+e] != '\0')
  129. {
  130. datastr[e] = loc[1+e];
  131. e++;
  132. }
  133. datastr[e] = '\0';
  134. strcpy(data,datastr);
  135. result = PASS;
  136. break;
  137. }
  138. }
  139. }
  140. fclose(fp);
  141. }
  142. return result;
  143. }
  144. int MessageSent_remove(char *uuid, char *data)
  145. {
  146. int result = PASS;
  147. char tempfile[] = "/Storage/OCPP_PH/temp1.json";
  148. FILE *infile;
  149. FILE *outfile;
  150. int resultRename=0;
  151. char filename[60]={0};
  152. char rmFileCmd[100]={0};
  153. char tempstring[100]={0};
  154. struct stat stats;
  155. stat("/Storage/OCPP_PH", &stats);
  156. // Check for directory existence
  157. if (S_ISDIR(stats.st_mode) == 1)
  158. {}
  159. else
  160. {
  161. DEBUG_INFO("/Storage/OCPP_PH directory not exist, create dir \n");
  162. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
  163. system(rmFileCmd);
  164. }
  165. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  166. if((access("/Storage/OCPP_PH/MessageSent",F_OK)) != -1)
  167. {}
  168. else
  169. {
  170. DEBUG_INFO("/Storage/OCPP_PH/MessageSent not exist\n");
  171. FILE *log = fopen("/Storage/OCPP_PH/MessageSent", "w+");
  172. if(log == NULL)
  173. {
  174. DEBUG_INFO("/Storage/OCPP_PH/MessageSent is NULL\n");
  175. result = FAIL;
  176. }
  177. else
  178. {
  179. fclose(log);
  180. }
  181. }
  182. if(result != FAIL)
  183. {
  184. sprintf(tempstring,"%s,%s", uuid,data);
  185. // open file for writing
  186. strcpy(filename, "/Storage/OCPP_PH/MessageSent");
  187. infile = fopen ("/Storage/OCPP_PH/MessageSent", "r");
  188. outfile = fopen (tempfile, "w");
  189. int c;
  190. c = fgetc(infile);
  191. rewind(infile);
  192. if(c == EOF)
  193. {
  194. DEBUG_INFO("/Storage/OCPP_PH/MessageSent is NULL\n");
  195. fclose(infile);
  196. fclose(outfile);
  197. sprintf(rmFileCmd,"rm -f %s",tempfile);
  198. system(rmFileCmd);
  199. }
  200. else
  201. {
  202. char buf[1200]={0};
  203. while (fgets(buf, sizeof(buf), infile) != NULL)
  204. {
  205. buf[strlen(buf) - 1] = '\0';
  206. if(strcmp(tempstring, buf)== 0)
  207. {}
  208. else
  209. {
  210. fprintf(outfile,"%s\n", buf);
  211. }
  212. }
  213. fclose(infile);
  214. fclose(outfile);
  215. sprintf(rmFileCmd,"rm -f %s",filename);
  216. system(rmFileCmd);
  217. resultRename = rename(tempfile, filename);
  218. if(resultRename == 0)
  219. {}
  220. else
  221. {
  222. DEBUG_ERROR("Error: unable to rename the file");
  223. result = FAIL;
  224. }
  225. }
  226. }
  227. return result;
  228. }
  229. int hashmap_operation(int type, char *uuid, char *data)
  230. {
  231. pthread_mutex_lock(&mutexMap);
  232. int result=0;
  233. if(type == HASH_OP_ADD)
  234. result = MessageSent_add(uuid, data);
  235. else if(type == HASH_OP_GET)
  236. result = MessageSent_get(uuid,data);
  237. else if(type == HASH_OP_REMOVE)
  238. result = MessageSent_remove(uuid, data);
  239. pthread_mutex_unlock(&mutexMap);
  240. return result;
  241. }