LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2004, 04:22 AM   #16
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15

Memstring.h:


#ifndef MEMSTRING_H
#define MEMSTRING_H

#include"All.h"

const unsigned int BUFFERSIZE=1024*1024;

class MemString //??????????
{
string stemp;
char *OneLine;
int LineLen;//the length of OneLine
int LocateInLine;//the location in a line in the NonAutoEndl mode
int StartLocate;//the location where the returning lines start
int StartPage;//the location where one page start
protected:
string TempBuffer;
int Locate;//????
public:
MemString(const char *); //??????,?????????!--!
MemString();
~MemString(){free(OneLine);}
void DelString(const int i ,const int j){TempBuffer.replace(i,j-i+1,"");} //???????????????--!
void ChangeLocate(const int i); //????????--!
void Insert(const string &); //??--!
void Insert(const char);
string& CopyString(int ,int); //?????--!
char *GetLine(const int);
void SetStartLocate(const int i){StartLocate=i;}
int GetSize(){return TempBuffer.size();}
string GetBuffer(){return TempBuffer;}
void Clear();
bool NowLineEnd()const;
bool NowLineStart()const;
int GetLength(){return TempBuffer.size();}
virtual bool IsEndBuf()const;
virtual bool IsStartBuf()const;
static const int TOFRONT=0;
static const int TOBACK=1;
};

/*
inline void MemString::SetStartPage(const int i)
{
if(StartPage+i < 0)
throw CommonError(CommonError::UNDERBUF);
else if(Locate+i >= TempBuffer.size())
throw CommonError(CommonError::OVERBUF);
else
StartPage+=i;
}
*/

inline void MemString::ChangeLocate(const int i)
{
if(Locate+i < 0)
throw CommonError(CommonError::UNDERBUF);
else if(Locate+i >= TempBuffer.size())
throw CommonError(CommonError::OVERBUF);
else
Locate+=i;
}

inline bool MemString::IsStartBuf()const
{
if(Locate == 0)
return true;
else
return false;
}

inline bool MemString::IsEndBuf()const
{
if(Locate == TempBuffer.size())
return true;
else
return false;
}

inline bool MemString::NowLineStart()const
{
if(Locate == 0)
return true;
else if(TempBuffer[Locate-1] == '\n')
return true;
else
return false;
}

inline bool MemString::NowLineEnd()const
{
if(Locate == TempBuffer.size())
return true;
if(TempBuffer[Locate] == '\n')
return true;
else
return false;
}

inline void MemString::Clear()
{
TempBuffer.erase();
StartLocate=0;
Locate=0;
for(int i=0 ; i < LineLen ; i++)
OneLine[i]=0;
}

inline void MemString::Insert(const char ch)
{
char str[2];
str[0]=ch;
str[1]='\0';
TempBuffer.insert(Locate++ , str);
}

inline void MemString::Insert(const string &temp)
{
TempBuffer.insert(Locate,temp,0,temp.length());
Locate+=temp.length();
}

#endif

Last edited by icoming; 05-22-2004 at 04:26 AM.
 
Old 05-22-2004, 04:23 AM   #17
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15
StrView.cpp:

#include<iostream>
using namespace std;
#include "StrView.h"

void StrView::KeyRightNonAuto(MemString &str)
{
if(str.NowLineEnd())
{
if(WinLocat.x < WinSize.ws_row-1)
;
}
}

void StrView::KeyRight(MemString &str)
{
//code for MemString
if(str.IsEndBuf())
{
beep();
return;
}
//some code
else
str.ChangeLocate(1);

//code for screen
getyx(win , WinLocat.x , WinLocat.y);
if(AutoEndl)
KeyRightAuto(str);
else
{
}
}

void StrView::KeyLeft(MemString &str)
{
//code for MemString
if(str.IsStartBuf())
{
beep();
return;
}
//some code
else
str.ChangeLocate(-1);

//code for screen
getyx(win , WinLocat.x , WinLocat.y);
if(AutoEndl)
{
if(str.NowLineStart() && WinLocat.x != 0)
//for some code
;
//move(WinLocat.x-1 , );
else if(WinLocat.y <= WinSize.ws_col-1 && WinLocat.y > 0)
move(WinLocat.x , --(WinLocat.y));
else if(WinLocat.y == 0 && WinLocat.x > 0)
move(WinLocat.x-1 , WinSize.ws_col-1);
else
{
move(0 , WinSize.ws_col-1);
//for();
}
}
else
{
}
}


void StrView::SigWinch(int signo)
{

}

/*void StrView:isplay(char *str)
{

}*/

struct Location StrView::GetLocat()
{
getyx(win , WinLocat.x , WinLocat.y);
return WinLocat;
}

void StrView::ClearComLine()
{
int y=CommandLines;
if(WinSize.ws_row > y)
for(; y > 0 ; y--)
{
move(WinSize.ws_row-y , 0);
clrtoeol();
}
else
clear();
}

void StrView:isplayCommand(char *s , const bool sign)
{
MemString str(s);
DisplayCommand(str , sign);
}

void StrView:isplayCommand(MemString &str , const bool sign)
{
int y;
int size=str.GetSize();
bool tempAutoEndl=AutoEndl;
AutoEndl=true;
if(size%WinSize.ws_col == 0)
y=size/WinSize.ws_col;
else
y=size/WinSize.ws_col+1;
ClearComLine();
CommandLines=y;
int startline;
if(WinSize.ws_row > y)
{
startline=WinSize.ws_row-CommandLines;
move(WinSize.ws_row-CommandLines , 0);
str.SetStartLocate(0);
}
else
{
startline=0;
move(0 , 0);
int templocate=(y-WinSize.ws_row)*WinSize.ws_col;
str.SetStartLocate(templocate);
}
char *line=NULL;
for(;
{
line=str.GetLine(WinSize.ws_col);
if(line == NULL)
break;
else
addstr(line);
move(++startline , 0);
}
AutoEndl=tempAutoEndl;
}
 
Old 05-22-2004, 04:23 AM   #18
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15
StrView.h:

#ifndef __STRVIEW_H__
#define __STRVIEW_H__

#include"All.h"
#include"Memstring.h"

class StrView
{
int x;
int y;
int CommandLines;
struct Location WinLocat;
struct winsize WinSize;
bool AutoEndl;
WINDOW *win;
int count;//???????????????
int MaxLineLength; //?????80???,????
int MaxLineNumber;//??????20?,????
public:
StrView()
{
AutoEndl=true;
WinSize=::GetWinSize();
CommandLines=1;
MaxLineLength=80;
MaxLineNumber=20;
x=0;
y=0;
win=initscr();
if(win == NULL)
{
cout<<"can't initial"<<endl;
exit(1);
}
cbreak();
nl();
noecho();
intrflush(stdscr,false);
keypad(stdscr,true);
refresh();
}
~StrView()
{
endwin();
}
bool SetMaxLineLength();//??????????
bool SetMaxLineNumber();//???????
void Display(const string &);
void DisplayCommand(MemString & , const bool sign=false);
void DisplayCommand(char * , const bool sign=false);
void ClearComLine();
struct Location GetLocat();
void ChangColor();
void SigWinch(int signo);
struct winsize GetWinSize(){return WinSize;}
bool GetAutoEndl(){return AutoEndl;}

void KeyLeft(MemString &str);
void KeyLeftAuto(const MemString &str);
void KeyRight(MemString &str);
void KeyRightAuto(const MemString &str);
void KeyRightNonAuto(MemString &str);
void KeyBackSpace(MemString &str){}
void KeyEnter(MemString &str){}
void KeyPageUp(MemString &str){}
void KeyPageDown(MemString &str){}
void KeyHome(MemString &str){}
void KeyEnd(MemString &str){}
void KeyDelete(MemString &str){}
void KeyUp(MemString &str){}
void KeyDown(MemString &str){}
void KeyInsert(){}
};

inline void StrView::KeyRightAuto(const MemString &str)
{
if(str.NowLineEnd() && WinLocat.x != WinSize.ws_row-1)
move(WinLocat.x+1 , 0);
else if(WinLocat.y < WinSize.ws_col-1 && WinLocat.y >= 0)
move(WinLocat.x , ++(WinLocat.y));
else if(WinLocat.y == WinSize.ws_col-1 && WinLocat.x < WinSize.ws_row-1)
move(WinLocat.x+1 , 0);
else
{
move(WinSize.ws_row-1 , 0);
//for();
}
}

#endif
 
Old 05-22-2004, 04:24 AM   #19
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15
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;
}
}
 
Old 05-22-2004, 04:25 AM   #20
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15
SunMemstring.h:


#ifndef SUNMEMSTRING_H
#define SUNMEMSTRING_H

#include"Memstring.h"
#include"All.h"

class SunMemStringublic MemString //??????????
{
int TrueSize;//?????????????
string stemp;
int FileLocate;//????????
string FileName;//??? !--
fstream fs;
int IsFileEnd;
int Locate;
public:
SunMemString(char *); //??????,?????????!--!
~SunMemString(){}
void ReadFile(); // !--!
void SaveFile(); //??
string& CopyString(int ,int); //?????--!
int Find(char *); //??,???????,?????????????
int Replace(char *,char *);
int ReplaceAll(char *,char *);
int FindHead(char *);
int FindEnd(char *);
int FindNormal(char *);
const string &GetFileName()const{return FileName;}
int GetLocate();
bool IsEndBuf()const;
bool IsStartBuf()const;
};

bool SunMemString::IsStartBuf()const
{
if(FileLocate == 0)
return true;
else
return false;
}

bool SunMemString::IsEndBuf()const
{
}

inline SunMemString::SunMemString(char* s):fs(s,ios::in|ios:ut)
{
if(fs.eof())
throw -1;
char temp[BUFFERSIZE];
FileName=s;
Locate=0;
FileLocate=0;
TrueSize=0;
fs.read(temp,BUFFERSIZE);
TempBuffer=temp;
FileLocate+=BUFFERSIZE;
TrueSize=BUFFERSIZE;
IsFileEnd=0;
}

#endif //SUNMEMSTRING_H
 
Old 05-22-2004, 04:26 AM   #21
icoming
Member
 
Registered: Feb 2004
Posts: 96

Original Poster
Rep: Reputation: 15
Makefile:

all:StrView.o main.o Control.o Memstring.o SunMemstring.o FileList.o
g++ -ggdb3 -o myvi StrView.o main.o Control.o Memstring.o FileList.o -lcurses
StrView.o:StrView.cpp StrView.h
g++ -ggdb3 -c StrView.cpp
main.o:main.cpp
g++ -ggdb3 -c main.cpp
Control.o:Control.cpp Control.h
g++ -ggdb3 -c Control.cpp
Memstring.o:Memstring.cpp Memstring.h
g++ -ggdb3 -c Memstring.cpp
SunMemstring.o:SunMemstring.cpp SunMemstring.h
g++ -ggdb3 -c SunMemstring.cpp
FileList.o:FileList.cpp FileList.h
g++ -ggdb3 -c FileList.cpp
 
Old 06-08-2004, 02:13 AM   #22
bluie
LQ Newbie
 
Registered: May 2004
Location: Europe
Distribution: Mutable
Posts: 10

Rep: Reputation: 0
Hi icoming!
Yeah! The stuff compiles. 'myvi' exists.
The essential step was providing a copy-constructor for SunMemString
like the following
Code:
SunMemString(const SunMemString& rhs) 
   {
     TrueSize = rhs.TrueSize;
     stemp = rhs.stemp;
     FileLocate = rhs.FileLocate;
     FileName = rhs.FileName;
     IsFileEnd = rhs.IsFileEnd;
     Locate = rhs.Locate;
   }
You---or better the stl---need this ctor to be able to copy the items
into the list. Maybe you shoud better use a list of pointers. I
remember that Scott Meyer wrote something about this.

Could not reproduce the "in charge" thing. Also googling gave me no
real insight.

Ciao, bluie

PS: Hey! Why not wrapping code in these cute code-tags?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me, how to debug this? lmmix Programming 1 03-03-2005 07:21 AM
How to debug ?NFS Steve Cronje Linux - Networking 4 01-23-2005 12:57 PM
Help with Python debug rootyard Programming 1 06-26-2004 09:16 PM
Plz debug this... balanagireddy Linux - Newbie 3 02-03-2004 04:17 PM
Plz debug this balanagireddy Programming 5 02-03-2004 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration