Commit 116d4cbd authored by Saikumar's avatar Saikumar

update .c with cpp code

parent b10b913e
......@@ -3,9 +3,16 @@
#include <assert.h>
#include <unistd.h>
#include <string.h>
// #include <cstdlib>
#include<fcntl.h>
#include "storage.h"
#include <iostream>
#include<semaphore.h>
using namespace std;
pthread_mutex_t m;
sem_t w;
int readercout=0;
struct n1
{
......@@ -33,6 +40,13 @@ struct n2
struct n1 *h1=NULL;
struct n2 *h2=NULL;
unsigned int nooffiles=0;
void initializeStorageVariables()
{
pthread_mutex_init(&m, NULL);
sem_init(&w, 0,1);
}
struct n2 * check(struct n2 *h2){
while (1)
{
......@@ -271,7 +285,7 @@ struct n1 * insert(struct n1 *r,char * key){
}
struct n1* find=NULL;
struct n1* finding=NULL;
struct n1* pre;
struct n1* fpre(struct n1 *r){
if(r->p2==NULL){
......@@ -366,19 +380,19 @@ struct n1* del(struct n1*r,char *key){
}
if((tt=strcmp(key,r->key))==0){//both are equal
if(r->p1==NULL&&r->p2==NULL){
find=r;
finding=r;
return NULL;
}
if(r->p1==NULL){
find=r;
finding=r;
return r->p2;
}
if(r->p2==NULL){
find=r;
finding=r;
return r->p1;
}
find=r;
finding=r;
fpre(r->p1);
pre->p1=r->p1;
pre->p2=r->p2;
......@@ -554,47 +568,92 @@ struct n1* del(struct n1*r,char *key){
}
char * GET(char * key){
string GETT(char * key){
pthread_mutex_lock(&m);
readercout+=1;
if(readercout==1){
sem_wait(&w);
}
pthread_mutex_unlock(&m);
int tt;
struct n1 *dh=h1;
while(1){
if(dh==NULL){
printf("The key is not present\n");
return NULL;
while(1)
{
if(dh==NULL)
{
string s(1, (char)240);
string result = s + "Get key not found";
printf("The key is not present\n");
// record[0]=240;
// record[1]='\0';
// char *pointer=record;
pthread_mutex_lock(&m);
readercout-=1;
if(readercout==0){
sem_post(&w);
}
pthread_mutex_unlock(&m);
}
else{
printf("%s = %s \n",key ,dh->key);
if((tt=strcmp(key,dh->key))==0){
char filename2[10];
strcpy(filename2,dh->filename);
FILE *f=fopen(strcat(filename2,".txt"),"r");
char record[257];
fseek(f,dh->lineno*257,SEEK_SET);
fgets(record,256,f);
fclose(f);
int sp=0;
while (record[sp]!='#')
return result;
}
else
{
// printf("%s = %s \n",key ,dh->key);
if((tt=strcmp(key,dh->key))==0)
{
sp=sp+1;
char filename2[10];
strcpy(filename2,dh->filename);
int fd1=open(strcat(filename2,".txt"),O_RDWR);
char record[257];
lseek(fd1,dh->lineno*257,SEEK_SET);
// fgets(record,256,f);
read(fd1,record, 256);
// fclose(f);
write(STDOUT_FILENO, record, 256);
write(STDOUT_FILENO, "\n", 1);
close(fd1);
int sp=0;
while (record[sp]!='#')
{
sp=sp+1;
}
record[sp]='\0';
string result=record;
// char *recor=record;
// return recor;
pthread_mutex_lock(&m);
readercout-=1;
if(readercout==0){
sem_post(&w);
}
pthread_mutex_unlock(&m);
return result;
}
else if(tt>0)
{
dh=dh->p2;
}
else{
dh=dh->p1;
}
record[sp]='\0';
char *recor=record;
return recor;
}
else if(tt>0){
dh=dh->p2;
}
else{
dh=dh->p1;
}
}
}
}
void PUT(char *key,char *value){
void PUTT(char *key,char *value){
sem_wait(&w);
// printf("key = %s\nvalue=%s",key, value);
h1=insert(h1,key);
char filename2[10];
strcpy(filename2,add->filename);
......@@ -648,7 +707,9 @@ void PUT(char *key,char *value){
write(n,ts,257);
close(n);
}
// printf("PUTT completed\n");
sem_post(&w);
}
......@@ -676,60 +737,64 @@ struct n2 * checkfile(struct n2 *h2,char *s){
}
void DEL(char *key){
string DELL(char *key){
sem_wait(&w);
h1=del(h1,key);
// write(STDOUT_FILENO,"came out of del \n",17);
if(find==NULL){
// printf("key = %s\n", key);
if(finding==NULL){
printf("could not delete as key does not exist \n");
return;
string s(1, (char)240);
string response = s + "Delete key not found";
sem_post(&w);
return response;
}
// write(STDOUT_FILENO,"the find is not null \n",22);
// write(STDOUT_FILENO,"the finding is not null \n",22);
char filename2[10];
strcpy(filename2,find->filename);
strcpy(filename2,finding->filename);
int n=open(strcat(filename2,".txt"),O_WRONLY);
// write(STDOUT_FILENO,"test \n",6);
lseek(n,find->lineno-1,SEEK_SET);
lseek(n,finding->lineno-1,SEEK_SET);
// write(STDOUT_FILENO,"test \n",6);
write(n,"a",1);
// write(STDOUT_FILENO,"test \n",6);
close(n);
// write(STDOUT_FILENO,"test \n",6);
// write(STDOUT_FILENO,"the file name i shoud be delted",31);
// write(STDOUT_FILENO,find->filename,5);
// write(STDOUT_FILENO,finding->filename,5);
// write(STDOUT_FILENO,"\n",1);
struct n2 *tp=checkfile(h2,find->filename);
struct n2 *tp=checkfile(h2,finding->filename);
// write(STDOUT_FILENO,"the file name i shoud be delted",31);
// write(STDOUT_FILENO,find->filename,1);
// write(STDOUT_FILENO,finding->filename,1);
// write(STDOUT_FILENO,"\n",1);
if(tp==NULL){
printf("the checkfile has given NULL");
return;
string s(1, (char)240);
string response = s + "Delete Failed";
sem_post(&w);
return response;
}
// write(STDOUT_FILENO,"test \n",6);
tp->nooffreeline=tp->nooffreeline-1;
// write(STDOUT_FILENO,"test \n",6);
free(find);
find=NULL;
free(finding);
finding=NULL;
// printf("del function completed\n");
string s(1, (char)200);
string response = s + "Delete Successful";
sem_post(&w);
return response;
}
void pp(struct n1*r){
if(r==NULL){
return;
}
pp(r->p1);
printf("%s\n",r->key);
// printf("%s\n",r->key);
pp(r->p2);
}
......@@ -737,7 +802,7 @@ void pp2(struct n1*r){
if(r==NULL){
return;
}
printf("%s\n",r->key);
// printf("%s\n",r->key);
pp2(r->p1);
......@@ -746,6 +811,11 @@ void pp2(struct n1*r){
// int main()
// {
// printf("Saikumar\n");
// return 0;
// }
......
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