hashmap.c 5.4 KB

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