123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- /*
- * hashmap.c
- *
- * Created on: 2019 ~4 27
- * Author: foluswen
- */
- #include "Module_OcppBackend.h"
- typedef enum boolean { FALSE, TRUE } BOOL;
- static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
- int MessageSent_add(char *uuid, char *data)
- {
- FILE *outfile;
- char rmFileCmd[100]={0};
- struct stat stats;
- char tempstring[100]={0};
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //printf("\n directory exist \n");
- }
- else
- {
- printf("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(&rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
- //printf("MessageSent exist.\n");
- }
- else
- {
- printf("MessageSent not exist\n");
- FILE *log = fopen("/Storage/OCPP/MessageSent", "w+");
- if(log == NULL)
- {
- printf("Can't Create File MessageSent \n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- // open file for writing
- outfile = fopen ("/Storage/OCPP/MessageSent", "a");
- sprintf(tempstring,"%s,%s\n", uuid,data);
- fputs(tempstring, outfile);
- fclose (outfile);
- //printf("MessageSent add\n");
- return 1;
- }
- int MessageSent_get(char *uuid, char *data)
- {
- FILE *fp;
- int result = FALSE; // 1: TRUE 0:FALSE
- char str[1200]={0};
- char sstr[50]={ 0 }, datastr[30]={0};//sstr[200]={ 0 };
- //int pos = 0, l = 0, c = 0;
- int c = 0;
- char *loc;
- char rmFileCmd[100]={0};
- struct stat stats;
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //printf("\n directory exist \n");
- }
- else
- {
- printf("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(&rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
- //printf("MessageSent exist.\n");
- }
- else
- {
- printf("MessageSent not exist\n");
- FILE *log = fopen("/Storage/OCPP/MessageSent", "w+");
- if(log == NULL)
- {
- printf("Can't Create File MessageSent \n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- /* opening file for reading */
- fp = fopen("/Storage/OCPP/MessageSent" , "r");
- if(fp == NULL) {
- printf("Error opening file");
- return FALSE;
- }
- c = fgetc(fp);
- //printf("c:%d\n",c);
- rewind(fp);
- if(c == EOF)
- {
- printf("MessageSent is null\n");
- //strcpy(uuid,"");
- strcpy(data,"");
- result = FALSE;
- }
- else
- {
- result = FALSE;
- while (fgets (str, 1200, fp)!=NULL)
- {
- //printf("Orignal File get strings \n");
- str[strlen(str) - 1] = '\0'; // eat the newline fgets() stores
- /* writing content to stdout */
- //puts(str);
- //printf("str[0]=%c\n",str[0]);
- /*********************uuid***************/
- int d = 0;
- while (str[d] != ',')
- {
- sstr[d] = str[d];
- d=d+ 1;
- }
- sstr[d] = '\0';
- //printf("sstr=%s\n",sstr);
- //printf("uuid=%s\n",uuid);
- if(strcmp(sstr, uuid) == 0)
- {
- //printf("\n uuid:%s compare all right!!! ", sstr);
- loc = strstr(str, ",");
- memset(sstr ,0, sizeof(sstr) );
- int e = 0;
- while (loc[1+e] != '\0')
- {
- datastr[e] = loc[1+e];
- e++;
- }
- datastr[e] = '\0';
- //printf("\n data:%s", datastr);
- //strcpy(uuid,sstr);
- strcpy(data,datastr);
- result = TRUE;
- break;
- }
- }
- }
- fclose(fp);
- return result;
- }
- int MessageSent_remove(char *uuid, char *data)
- {
- char tempfile[] = "/Storage/OCPP/temp1.json";
- FILE *infile;
- FILE *outfile;
- int resultRename=0;
- char filename[60]={0};
- char rmFileCmd[100]={0};
- char tempstring[100]={0};
- struct stat stats;
- stat("../Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //printf("\n directory exist \n");
- }
- else
- {
- printf("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
- system(rmFileCmd);
- }
- memset(&rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
- //printf("MessageSent exist.\n");
- }
- else
- {
- printf("MessageSent not exist\n");
- FILE *log = fopen("/Storage/OCPP/MessageSent", "w+");
- if(log == NULL)
- {
- printf("log is NULL\n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- sprintf(tempstring,"%s,%s", uuid,data);
- // open file for writing
- strcpy(filename, "/Storage/OCPP/MessageSent");
- infile = fopen ("/Storage/OCPP/MessageSent", "r");
- outfile = fopen (tempfile, "w");
- /*璉�瘚��辣蝏������1嚗�����0��*/
- //DEBUG_INFO("feof(infile) =%d\n",feof(infile));
- int c;
- c = fgetc(infile);
- //printf("file c:%d\n",c);
- rewind(infile);
- if(c == EOF)
- {
- printf("MessageSent is NULL\n");
- fclose(infile);
- fclose(outfile);
- sprintf(rmFileCmd,"rm -f %s",tempfile);
- system(rmFileCmd);
- }
- else
- {
- char buf[1200]={0};
- //int i = 0;
- //printf("Orignal File is not NULL\n");
- while (fgets(buf, sizeof(buf), infile) != NULL)
- {
- //printf("Orignal File get strings \n");
- buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
- //printf("buf=%s\n",buf);
- //printf("tempstring=%s\n",tempstring);
- //if(i != 0)
- if(strcmp(tempstring, buf)== 0)
- {
- //printf("Remove OK ! \n");
- }
- else
- {
- fprintf(outfile,"%s\n", buf);
- }
- //i = i + 1;
- }
- fclose(infile);
- fclose(outfile);
- sprintf(rmFileCmd,"rm -f %s",filename);
- system(rmFileCmd);
- resultRename = rename(tempfile, filename);
- if(resultRename == 0)
- {
- //printf("File renamed successfully");
- }
- else
- {
- printf("Error: unable to rename the file");
- }
- }
- return 0;
- }
- int hashmap_operation(int type, char *uuid, char *data)
- {
- pthread_mutex_lock(&m);
- int result=0;
- if(type == HASH_OP_ADD)
- result = MessageSent_add(uuid, data);
- else if(type == HASH_OP_GET)
- result = MessageSent_get(uuid,data);
- else if(type == HASH_OP_REMOVE)
- result = MessageSent_remove(uuid, data);
- pthread_mutex_unlock(&m);
- return result;
- }
|