Commit 2a343ede authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

changed file_put to use memcpy

parent a5d82517
...@@ -34,32 +34,28 @@ void file_del(char *key) ...@@ -34,32 +34,28 @@ void file_del(char *key)
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
FILE * fp; FILE * fp;
char * line = NULL; char * line = malloc(sizeof(char)*514);
size_t len = 0; size_t len = 0;
int read; int read;
fp = fdopen(fds[index], "a+"); fp = fdopen(fds[index], "r+b");
if (fp == NULL) if (fp == NULL)
printf("\n[Invalid file]\n"); printf("\n[Invalid file]\n");
int position=0; int position=ftell(fp);
while ((read = getline(&line, &len, fp)) != -1) while (fscanf(fp,"%s",line) != EOF )
{ {
if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL) if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
{ {
//printf("%s\n",line);
fseek(fp,position,SEEK_SET); printf("%d", fseek(fp, position, SEEK_SET));
fprintf(fp, "%s", "\0");
break; break;
} }
else position=ftell(fp);
{ printf("\n%d ", position);
position+=len;
}
} }
//fclose(fp);
if (line) if (line)
free(line); free(line);
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
...@@ -112,41 +108,16 @@ void file_put(char *key,char *value) ...@@ -112,41 +108,16 @@ void file_put(char *key,char *value)
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
printf("\n[Write to File: %d]\n",index); printf("\n[Write to File: %d]\n",index);
char line[514]; char line[514];
int i=0; memcpy(line,key,256);
for(i=0;i<256;i++) line[strlen(key)]=':';
{ memcpy(line+strlen(line),value,256);
if(key[i]!='\0') line[strlen(line)]='\n';
{
line[i]=key[i];
}
else
{
break;
}
}
int k=0;
line[i++]=':';
for(;i<512;i++)
{
if(value[k]!='\0')
{
line[i]=value[k++];
}
else
{
break;
}
}
line[i]='\n';
if(write(fds[index],line,strlen(line))<0) if(write(fds[index],line,strlen(line))<0)
{ {
printf("\n[Unable to Write to File]\n"); printf("\n[Unable to Write to File]\n");
} }
sem_post(&mutex[index]); sem_post(&mutex[index]);
} }
int main() int main()
{ {
/* /*
...@@ -155,11 +126,12 @@ int main() ...@@ -155,11 +126,12 @@ int main()
PUT,DEL would use write lock PUT,DEL would use write lock
GET would use read lock GET would use read lock
each write should return the line number each write should return the line number
*/ */
int n2=0; int n2=0;
char key[256]="24"; char key[256]="24";
char value[256]="value"; char value[256]="value";
bzero(key+strlen(key),sizeof(key)-strlen(key));
bzero(value+strlen(value),sizeof(value)-strlen(value));
fds=(int *)malloc(sizeof(int)*setSize); fds=(int *)malloc(sizeof(int)*setSize);
readCounters=(int *)malloc(sizeof(int)*setSize); readCounters=(int *)malloc(sizeof(int)*setSize);
readerLocks=(sem_t *)malloc(sizeof(sem_t)*setSize); readerLocks=(sem_t *)malloc(sizeof(sem_t)*setSize);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment