Commit 54e0c695 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

some changes in Storages

parent 2619eb7e
......@@ -144,12 +144,65 @@ void testJournal2(void)
}
int main(void)
int main(int args, char **argv)
{
testTable();
printf("args=%d\n",args);
if(args<2)
{
printf("Correct usage: jrntest File_name\n");
return 0;
}
CycleStorage *j = new CycleStorage(argv[1],99,1000,0);
printf("commands:\nadd\ndelete\nview\ndel_all\nenter q or quit to exit\n\n");
char* com=new char[8];
char* str=new char[99];
int num,count;
strcpy(com,"qwerty");
while(strcmp(com,"q")&&strcmp(com,"quit"))
{
scanf("%s",com);
if(!strcmp(com,"add"))
{
printf("string to add: ");
if(scanf("%99s",str)>0)
{
j->AddRow(str);
printf("\n");
}
}
else if(!strcmp(com,"delete"))
{
printf("number of string to delete: ");
if(scanf("%d",&num)>0)
{
j->DelRow(num);
printf("\n");
}
}
else if(!strcmp(com,"view"))
{
printf("start number and count (0 0 for all): ");
if(scanf("%d%d",&num,&count)>1)
{
j->ViewRows(num,count);
printf("\n");
}
}
else if(!strcmp(com,"del_all"))
{
printf("are you sure? (y/n) ");
if(scanf("%s",str)>0)
if(!strcmp(str,"y"))
{
j->DelAllRows();
printf("\n");
}
}
}
/*testTable();
testJournal1();
testJournal2();
testJournal2();*/
return 0;
}
\ No newline at end of file
......@@ -48,7 +48,6 @@ class TableStorage
int seekpos, inf_size;
public:
int size;
TableStorage();
TableStorage(const char* name, int inf_sz, int sz, int seek);
~TableStorage();
int AddRow(char* key, char* val);
......@@ -63,7 +62,6 @@ class CycleStorage
int head,tail;
public:
int size, iter;
CycleStorage();
CycleStorage(const char* name, int inf_sz, int sz, int seek);
~CycleStorage();
int AddRow(char* str);
......
......@@ -30,104 +30,6 @@
#include "Storages.h"
CycleStorage::CycleStorage()
{
inf_size=80;
CycleStorageElem *jrn = (CycleStorageElem*)malloc(sizeof(CycleStorageElem)+inf_size);
file = fopen("jrn", "r+");
size=100;
seekpos=0;
int l=-1,r=size,mid;
iter=0;
if(file==NULL)
{
file = fopen("jrn","w");
for(int i=0;i<100;i++) fwrite(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
fclose(file);
file = fopen("jrn","r+");
head=-1;
tail=-1;
}
else
{
fseek(file,seekpos,0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
if(jrn->status==0)
{
head=-1;
tail=-1;
}
else if((jrn->status==1)||(jrn->status==6))
{
head=0;
while(r - l > 1)
{
mid = (l+r)/2;
fseek(file,seekpos+mid*(sizeof(CycleStorageElem)+inf_size),0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
iter++;
if(jrn->status==0)
r = mid;
else l=mid;
}
if(r<size)
{
fseek(file,seekpos+r*(sizeof(CycleStorageElem)+inf_size),0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
tail=r-1;
}
else tail=size-1;
}
else if((jrn->status==2)||(jrn->status==3))
{
while((jrn->status!=1)&&(jrn->status!=6)&&(r - l > 1))
{
mid = (l+r)/2;
fseek(file,seekpos+mid*(sizeof(CycleStorageElem)+inf_size),0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
iter++;
if((jrn->status==2)||(jrn->status==3))
l = mid;
else if((jrn->status==4)||(jrn->status==5))
r = mid;
else
{
r=mid;
break;
}
}
if(r<size)
head=r;
else head=size-1;
tail=head-1;
if(tail<0) tail=size-1;
}
else
{
while((jrn->status!=1)&&(jrn->status!=6)&&(r - l > 1))
{
mid = (l+r)/2;
fseek(file,seekpos+mid*(sizeof(CycleStorageElem)+inf_size),0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
iter++;
if((jrn->status==2)||(jrn->status==3))
r = mid;
else if((jrn->status==4)||(jrn->status==5))
l = mid;
else
{
r=mid;
break;
}
}
if(r<size)
head=r;
else head=size-1;
tail=head-1;
if(tail<0) tail=size-1;
}
}
}
CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek)
{
......
......@@ -30,21 +30,6 @@
#include "Storages.h"
TableStorage::TableStorage()
{
file = fopen("tbl", "r+");
inf_size=60;
size=100;
seekpos=0;
if(file==NULL)
{
file = fopen("tbl","w");
TableStorageElem *t = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
for(int i=0;i<size;i++) fwrite(t,(sizeof(TableStorageElem)+inf_size),1,file);
fclose(file);
file = fopen("tbl","r+");
}
}
TableStorage::TableStorage(const char* name, int inf_sz, int sz, int seek)
{
......
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