Commit 116d4cbd authored by Saikumar's avatar Saikumar

update .c with cpp code

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