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)
int index=modulus(key,256,setSize);
sem_wait(&mutex[index]);
FILE * fp;
char * line = NULL;
char * line = malloc(sizeof(char)*514);
size_t len = 0;
int read;
fp = fdopen(fds[index], "a+");
fp = fdopen(fds[index], "r+b");
if (fp == NULL)
printf("\n[Invalid file]\n");
int position=0;
while ((read = getline(&line, &len, fp)) != -1)
int position=ftell(fp);
while (fscanf(fp,"%s",line) != EOF )
{
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;
}
else
{
position+=len;
}
position=ftell(fp);
printf("\n%d ", position);
}
//fclose(fp);
if (line)
free(line);
sem_wait(&readerLocks[index]);
......@@ -112,41 +108,16 @@ void file_put(char *key,char *value)
sem_wait(&mutex[index]);
printf("\n[Write to File: %d]\n",index);
char line[514];
int i=0;
for(i=0;i<256;i++)
{
if(key[i]!='\0')
{
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';
memcpy(line,key,256);
line[strlen(key)]=':';
memcpy(line+strlen(line),value,256);
line[strlen(line)]='\n';
if(write(fds[index],line,strlen(line))<0)
{
printf("\n[Unable to Write to File]\n");
}
sem_post(&mutex[index]);
}
int main()
{
/*
......@@ -155,11 +126,12 @@ int main()
PUT,DEL would use write lock
GET would use read lock
each write should return the line number
*/
int n2=0;
char key[256]="24";
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);
readCounters=(int *)malloc(sizeof(int)*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