📄 blockfil1.h
字号:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
struct Block
{ char area[512];
char *rear;
int charcount;
int linecount;
};
class BlockFile
{
private:
Block *bpointer[512];
char *lpointer[512];
int lcount;
int bcount;
long lbn[512];
void Format(Block *bp);
public:
BlockFile(void);
~BlockFile(void);
int InsertLine(char* input,int lnum);
int Insert(char* input,int lnum);
int DeleteLine(int lum); //return if the line is deleted correctly
int Optimize(int whichblock); //Optimize the block refered to
char* GetLine(int lnum); // get the line refered
int Find(char* input,int lnum); //find the string in the line concerned
long GetLcount(void); //to get the count of the line
long GetLcnt(void);
};
BlockFile::BlockFile(void)
{
lcount=0;
bcount=1;
for(int posi=0;posi<=511;posi++)
{
lpointer[posi]=NULL;
lbn[posi]=-1;
}
bpointer[0]=new Block;
if(bpointer[0]==NULL)
{
cerr<<"Memory allocation error"<<endl;
exit(1);
}
Format(bpointer[0]);
}
BlockFile::~BlockFile(void)
{
int posi;
for( posi=0;posi<bcount;posi++)
{
delete bpointer[posi];
}
for( posi=0;posi<=511;posi++)
{
lpointer[posi]=NULL;
lbn[posi]=-1;
}
bcount=0;
lcount=0;
}
void BlockFile::Format(Block *bp)
{
bp->rear=bp->area;
for(int posi=0;posi<=511;posi++)
{
bp->area[posi]='\0';
}
bp->linecount=0;
bp->charcount=0;
}
//The function insert//////////////////////////
int BlockFile::InsertLine(char* input,int lnum)
{
int length=strlen(input);
int posi=0;
int n=0,temp;
if(lnum<0||lnum>lcount)
{ //return if the line required does not exist
return (-1);
}
if(length==0||((lcount+(length-1)/128+1)>512))
{ //return if the line number is larger than 512
return (-2);
}
if(lnum>=0&&(lcount+(length-1)/128+1)<=512)
{ //if the line can be inserted
//insert it
if( (length-1)/128==0){ //if the line to be inserted is less than 128 letters
for(posi=lcount-1;posi>=lnum;posi--)
{ //move the pointers forward
lpointer[posi+1]=lpointer[posi];
}
for(posi=0;posi<bcount;posi++)
{ //get the block which the line can be inserted
if((512-bpointer[posi]->charcount)>length+1)
{
break;
}
}
if(posi==bcount)
{ //if no block is available , create a new one
bpointer[bcount-1]->rear-=1;
strcpy(bpointer[bcount-1]->rear,"\n");
bpointer[bcount-1]->rear+=1;
bpointer[bcount]=new Block;
if(bpointer[bcount]==NULL)
{
cerr<<"memory allocation error"<<endl;
exit(1);
}
Format(bpointer[bcount]);
bcount++;
}
strcpy(bpointer[posi]->rear,input); //insert the line into the block
lpointer[lnum]=bpointer[posi]->rear;//connect the content with the line pointer concerned
strcat(lpointer[lnum],"\n");//add the sign of the line
bpointer[posi]->rear+=length+1;//get the new rear of the block
while (lbn[n+1]/1000<=posi&&lbn[n+1]*lbn[n]!=1){ //find the place where the line is inserted
n++;
}
for(temp=0;temp<lcount;temp++){ //add the number of the line attribute if the line is behind the line concerned
if(lbn[temp]%1000>=lnum){ //today
lbn[temp]+=1;
}
}
for(temp=lcount-1;temp>n;temp--){ //insert the line into the attribute list
lbn[temp+1]=lbn[temp];
}
lbn[n]=posi*1000+lnum; //calculate the number of the line
}
lcount++;
bpointer[posi]->charcount+=length+1;
}
return 0;
}
int BlockFile::Insert(char *input,int lnum)
{
int length=strlen(input);
char *temp;
if(length<=128)
{ // if the input is less than 128, input it directly
InsertLine(input,lnum);
return 0;
}
if(length>128)
{ // if the input is longer than 128
while(strlen(input)>128)
{
strcat(input,"\x0");
temp=(char *)malloc(128);
strncpy(temp,input,128);
temp[128]='\x0';
InsertLine(temp,lnum);
lnum++;
input+=128;
free(temp);
}
strcat(input,"\x0");
InsertLine(input,lnum);
}
return 0;
}
//The function delete/////////////////
int BlockFile::DeleteLine(int lnum)
{
int posi=0;
int temp=0;
int length;
if(lnum<=0||lnum>lcount)
{
return -1;
}
while(lbn[posi]%1000!=lnum-1)
{
posi++;
}
//////////////////////
if(lbn[posi]/1000==lbn[posi+1]/1000&&lbn[posi+1]>=0){ //if the line is not at the rear of the block
length=strlen(lpointer[lbn[posi]%1000])-strlen(lpointer[lbn[posi+1]%1000]);
bpointer[lbn[posi]/1000]->charcount-=length;
strcpy(lpointer[lbn[posi]%1000],lpointer[lbn[posi+1]%1000]);
for(temp=posi+1;lbn[temp]/1000==lbn[posi]/1000&&lbn[temp]>=0;temp++)
{
lpointer[lbn[temp]%1000]-=length;
}
for( temp=lnum-1;temp<lcount;temp++)
{
lpointer[temp]=lpointer[temp+1];
}
bpointer[lbn[posi]/1000]->rear-=length;
strcat(bpointer[lbn[posi]/1000]->rear,"\x0");
//bpointer[lbn[posi]/1000]->rear[0]='\x0';
for(temp=0;temp<lcount;temp++)
{
if(lbn[temp]%1000>=lnum)
{
lbn[temp]--;
}
}
for(temp=posi;temp<lcount;temp++)
{
lbn[temp]=lbn[temp+1];
}
lcount--;
return 0;
}
//////////////////////
if(lbn[posi]/1000!=lbn[posi+1]/1000||lbn[posi+1]<0){ //if the line is at the rear of the block
if(posi!=0&&lbn[posi]/1000==lbn[posi-1]/1000){ // sub if the line is not the only one in the line
length=strlen(lpointer[lbn[posi]%1000]);
bpointer[lbn[posi]/1000]->rear-=length;
strcpy( bpointer[lbn[posi]/1000]->rear,"\x0");
for(temp=lnum-1;temp<lcount;temp++){
lpointer[temp]=lpointer[temp+1];
}
bpointer[lbn[posi]/1000]->charcount-=length;//
for(temp=0;temp<lcount;temp++){
if(lbn[temp]%1000>lnum-1){
lbn[temp]--;
}
}
for(temp=posi;temp<lcount;temp++){
lbn[temp]=lbn[temp+1];
}
lcount--;
return 0;
}
//////////////
if(posi==0||lbn[posi]/1000>lbn[posi-1]/1000){ //sub if the line is the only line in the block
Format(bpointer[lbn[posi]/1000]);
//bpointer[bcount]=new Block;
for(temp=lbn[posi]/1000;temp<bcount-1;temp++){
bpointer[temp]=bpointer[temp+1];
}
if(temp!=0){
bpointer[temp+2]=new Block;
bpointer[temp+1]=bpointer[temp+2];
delete bpointer[temp+1];
}
for(temp=lnum-1;temp<lcount;temp++){
lpointer[temp]=lpointer[temp+1];
}
for(temp=0;temp<lcount;temp++){
if(lbn[temp]%1000>lnum-1){
lbn[temp]--;
}
}
for(temp=posi;temp<lcount;temp++){
if(lbn[temp]/1000>=lbn[posi]/1000){
lbn[temp]-=1000;
}
}
for(temp=posi;temp<lcount;temp++){
lbn[temp]=lbn[temp+1];
}
bcount--;
lcount--;
return 0;
}
}
return -2;
}
////The function optimize////////////////////////
int BlockFile::Optimize(int whichblock){
int posi=0;
char chararray[512];
char *temp;
int n;
if(whichblock>=bcount||whichblock<0){
return -2;
}
if(bcount==1){
return -1;
}
strcpy(chararray,bpointer[whichblock]->area);
for(n=0;n<=511;n++){
if(chararray[n]=='\n'){
chararray[n]='\x0';
}
}
temp=chararray;
while(lbn[posi]/1000!=whichblock){
posi++;
}
while(lbn[posi]/1000==whichblock){
bpointer[lbn[posi]/1000]->charcount=511;
DeleteLine(lbn[posi]%1000+1);
Insert(temp,lbn[posi]%1000);
temp+=strlen(temp)+1;
posi++;
}
return 0;
}
/////////////////////////////////////////////////
//The function getline
char* BlockFile::GetLine(int lnum){
char *temparray="";
int temp;
temparray=(char *)malloc(129);
if (lnum>lcount){
return NULL;
}
strncpy(temparray,lpointer[lnum-1],128);
temparray[128]='\x0';
for(temp=0;temp<=128;temp++){
if(temparray[temp]=='\n'||temparray[temp]=='\x0'){
temparray[temp]='\x0';
break;
}
}
return temparray;
}
//the function///////////
int BlockFile::Find(char* input,int lnum){
int length=strlen(input);
char* temp;
temp=lpointer[lnum-1];
while(strncmp(temp,input,length)!=0&&*temp!='\n'){
temp++;
}
if(*temp=='\n') {
return 0;
}
if(strncmp(temp,input,length)==0){
return lnum;
}
return 0;
}
long BlockFile::GetLcount(void){
return lcount;
}
long BlockFile::GetLcnt(void)
{
return lcount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -