SunMemstring.cpp:
#include "SunMemstring.h"
string& SunMemString::CopyString(int i,int j)
{
char temp[BUFFERSIZE];
TempBuffer.copy(temp,i,j-i);
temp[j-i]=0;
stemp=temp;
return stemp;
}
void SunMemString::ReadFile()
{
if(fs.eof())
{
IsFileEnd=1;
Locate--;
return;
}
if(Locate>BUFFERSIZE)//????;;???locate?????BUFFERSIZE/2
{
int count=0;
int fl;
fl=Locate-BUFFERSIZE;
char temp[BUFFERSIZE/2];
Locate=BUFFERSIZE/2;
while(!fs.eof()&&(count!=Locate))
{
fs.get(temp[count]);
count++;
FileLocate++;
TrueSize++;
}
TempBuffer+=temp;
SaveFile();
TempBuffer.replace(0,Locate,"");
TrueSize-=Locate;
Locate=TempBuffer.size()-count;
Locate+=fl;
}
else if(Locate<0)//????;;
{
char temp[BUFFERSIZE/2];
int flu;
flu=-Locate;
Locate=BUFFERSIZE/2;
fs.seekg(-TempBuffer.size()-BUFFERSIZE/2,ios::cur);//?????????BUFFERSIZE/2
fs.read(temp,BUFFERSIZE/2);//?BUFFERSIZE/2???
TrueSize+=BUFFERSIZE/2;
FileLocate-=BUFFERSIZE/2;
TempBuffer.insert(0,temp,0,BUFFERSIZE/2);
SaveFile();
fs.seekg(-BUFFERSIZE/2,ios::cur);//????????
TempBuffer.replace(Locate,Locate,"");
TrueSize-=Locate;
Locate-=flu;
}
}
void SunMemString::SaveFile()//??
{
const int EndPoint=FileLocate-TrueSize;//??????????????
double count=0.0;
char temp[1024];
char ch;
fstream TempFile("tempfile.tmp",ios:
ut);
fs.seekg(0,ios::beg);
while((count+1024)<EndPoint)
{
fs.read(temp,1024);
TempFile.write(temp,1024);
count+=1024;
}
while(count<EndPoint)
{
fs.get(ch);
TempFile.put(ch);
count++;
}
TempFile.write(TempBuffer.c_str(),TempBuffer.size());
fs.seekg(FileLocate,ios::beg);
while(!fs.eof())
{
fs.get(ch);
TempFile.put(ch);
}
TempFile.close();
fs.close();
string del="rm -f ";
del+=FileName;
char dele[50];
del.copy(dele,del.size());
dele[del.size()]=0;
system(dele);
string chan="mv tempfile.tmp ";
chan+=FileName;
char chang[50];
chan.copy(chang,chan.size());
chang[chan.size()]=0;
system(chang);
char tempname[50];
FileName.copy(tempname,FileName.size());
tempname[FileName.size()]=0;
FileLocate=EndPoint+TempBuffer.size();
fs.open(tempname,ios::in|ios:
ut);
fs.seekg(EndPoint+TempBuffer.size(),ios::beg);
}
int SunMemString::Find(char* s)
{
int ReturnValue;
int OldLocate;//???????Locate
OldLocate=Locate;
const int Length=strlen(s);
if (Length<1)
{
//cout<<"Command in find error"<<endl;
return -1;
}
else
{
if (s[0]=='^')
{
if (s[strlen(s)-1]=='$')
{
int i;
char *ss=(char*)malloc(strlen(s));
for(i=0;i<(strlen(s)-1);i++)
ss[i]=s[i];
ReturnValue=FindHead(ss);
}
else
{
ReturnValue=FindHead(s);
}
}
if (s[Length-1]=='$')
{
if(s[0]!='^')
{
ReturnValue=FindEnd(s);
}
else
{
int i;
char* ss=(char*)malloc(strlen(s));
for(i=1;i<strlen(s);i++)
ss[i-1]=s[i];
ReturnValue=FindEnd(ss);
}
}
if (s[0]!='^'&&s[Length-1]!='$')
{
ReturnValue=FindNormal(s);
}
}
if (ReturnValue==-1)
Locate=OldLocate;
return ReturnValue;
}
int SunMemString::FindHead(char* s)
{
// int SearchPoint=Locate;
int PointToS;//??s??
int PointTot;//???TempBuffer??
char temp;
int i=0;
int ismatch=0;
while(TempBuffer[Locate]!=s[1])//s[0]?'^',????s[o]
{
Locate++;
ReadFile();
if(IsFileEnd==1)
{
return -1;
}
}
if ((TempBuffer[Locate-1]!='\n')&&(Locate!=0))
{
return -1;
}
else
{
PointToS=1;
PointTot=Locate;
while((TempBuffer[PointTot]!='\n')&&(PointToS!=strlen(s))&&(Locate+PointTot<TempBuffer.size()))
{
temp=TempBuffer[PointTot];
if(s[PointToS]=='[')
{
char *t=(char*)malloc(strlen(s));
for(PointToS=PointToS++;TempBuffer[PointToS]!=']';PointToS++)
{
t[i]=TempBuffer[PointToS];
i++;
}
PointToS++;
int n;
for(n=0;n<i;n++)
{
if(t[n]==temp)
ismatch=1;
}
if(ismatch==0)
{
return -1;
}
}
else if (s[PointToS]=='.')
{
PointToS++;
}
else if (s[PointToS]=='*')
{
if (s[PointToS-1]==temp)
ismatch=1;
else if (s[PointToS+1]==temp)
{
ismatch=1;
PointToS+=2;
}
else if((PointToS+1)==strlen(s))
{
ismatch=1;
PointToS++;
}
if (ismatch==0)
return -1;
}
else if(temp==s[PointToS])
{
PointToS++;
}
else
{
return -1;
}
PointTot++;
ismatch=0;
}
if (PointToS!=strlen(s))
return -1;
while(TempBuffer[PointTot]!='\n')
PointTot++;
return PointTot;
}
}
int SunMemString::FindEnd(char* s)
{
int PointToS;//??s??
int PointTot;//???TempBuffer??
char temp;
int i=0;
int ismatch=0;
while((TempBuffer[Locate]!=s[strlen(s)-2])||(TempBuffer[Locate+1]!='\n'))
{
Locate++;
ReadFile();
if(IsFileEnd==1)
{
return -1;
}
}
if ((TempBuffer[Locate+1]!='\n')&&((Locate+1)!=TempBuffer.size()))
{
return -1;
}
else
{
PointToS=strlen(s)-2;
PointTot=Locate;
while((TempBuffer[PointTot]!='\n')&&(PointToS>0)&&(Locate-PointTot>0))
{
temp=TempBuffer[PointTot];
if(s[PointToS]==']')
{
char *t=(char*)malloc(strlen(s));
for(PointToS=PointToS--;TempBuffer[PointToS]!='[';PointToS--)
{
t[i]=TempBuffer[PointToS];
i--;
}
PointToS--;
int n;
for(n=0;n<i;n++)
{
if(t[n]==temp)
ismatch=1;
}
if(ismatch==0)
{
return -1;
}
}
else if (s[PointToS]=='.')
{
PointToS--;
}
else if (s[PointToS]=='*')
{
if (s[PointToS-1]==temp)
ismatch=1;
else if (s[PointToS-2]==temp)
{
ismatch=1;
PointToS-=3;
}
else if((PointToS-2)<0)
{
ismatch=1;
PointToS-=2;
}
if (ismatch==0)
return -1;
}
else if(temp==s[PointToS])
{
PointToS--;
}
else
{
return -1;
}
PointTot--;
ismatch=0;
}
if (PointToS<0)
return -1;
while(TempBuffer[PointTot]!='\n')
PointTot--;
int ttm;
ttm=PointTot;
PointTot=Locate;
Locate=ttm+1;
return PointTot;
}
}
int SunMemString::FindNormal(char* s)
{
int PointToS;//??s??
int PointTot;//???TempBuffer??
char temp;
int i=0;
int ismatch=0;
while(TempBuffer[Locate]!=s[0])
{
Locate++;
ReadFile();
if(IsFileEnd==1)
{
return -1;
}
}
PointToS=0;
PointTot=Locate;
while((TempBuffer[PointTot]!='\n')&&(PointToS!=strlen(s))&&(Locate+PointTot<TempBuffer.size()))
{
temp=TempBuffer[PointTot];
if(s[PointToS]=='[')
{
char *t=(char*)malloc(strlen(s));
for(PointToS=PointToS++;TempBuffer[PointToS]!=']';PointToS++)
{
t[i]=TempBuffer[PointToS];
i++;
}
PointToS++;
int n;
for(n=0;n<i;n++)
{
if(t[n]==temp)
ismatch=1;
}
if(ismatch==0)
{
return -1;
}
}
else if (s[PointToS]=='.')
{
PointToS++;
}
else if (s[PointToS]=='*')
{
if (s[PointToS-1]==temp)
ismatch=1;
else if (s[PointToS+1]==temp)
{
ismatch=1;
PointToS+=2;
}
if (ismatch==0)
return -1;
}
else if(temp==s[PointToS])
{
PointToS++;
}
else
{
return -1;
}
PointTot++;
ismatch=0;
}
if (PointToS!=strlen(s))
return -1;
return PointTot;
}
int SunMemString::Replace(char* s,char* so)
{
int isfind;
isfind=Find(s);
if (isfind<0)
{
//cout<<"There is no strings named "<<'\"'<<s<<'\"'<<endl;
return -1;
}
else
{
TempBuffer.replace(Locate,(isfind-Locate),so);
return 1;
}
}
int SunMemString::ReplaceAll(char* s,char* so)
{
int is;
is=Replace(s,so);
if(is<0)
{
///cout<<"There is no strings named "<<'\"'<<s<<'\"'<<endl;
return -1;
}
else
{
while ((is=Replace(s,so))>0);
return 1;
}
}