hashmap.c 6.1 KB

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