У меня есть кусок кода, написанный на Builder'e, мне надо переделать его так, чтобы он мог работать без библиотеки vcl (под Линухом). Т.к. там используются только 3 класса: AnsiString, TList и TStringList, я решил написать эти классы, естественно не полность, а только те моменты, которые исп. в коде, но похоже у меня это не очень получилось, потому как все это дело глючит со страшной силой, а при больших объемах данных вообще зависает. Если кто подскажет как решить эту проблему иным путем или довести до ума мой способ, буду признателен.
Непосредственно мой вариант классов:
//-------TStrAnsiClass.h----------------------------------------------------------
#ifndef TStrAnsiClassH
#define TStrAnsiClassH
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
#include "ptypes.h"
//-------AnsiString------AnsiString--------
class AnsiString
{
friend AnsiString operator+( AnsiString &s1, AnsiString &s2);
friend AnsiString operator+( AnsiString &s1,char *s2);
friend AnsiString operator+(char* s1, AnsiString &s2);
private:
int LengthL;
public:
AnsiString();
AnsiString(char * stc);
~AnsiString();
int Length();
AnsiString& SubString(int StartSimbol, int NumSimbols);
char* c_str();
int Pos(AnsiString& str);
int Pos(char* str);
AnsiString& operator = ( char* );
AnsiString & operator = (const AnsiString & );
bool operator == (AnsiString &);
bool operator == (char *);
bool operator != (AnsiString &);
bool operator != (char *);
operator const char*();
char* Data;
};
//--------------------TStringList---------------------------
class TSL
{
private:
AnsiString ** Str;
int maxCount;
public:
TSL();
~TSL();
void Add(AnsiString* str);
void Del(int index);
AnsiString& operator [] (int index);
int Count;
};
class TStringList
{
private:
int maxCount;
public:
TStringList();
~TStringList();
void Clear();
void Add(AnsiString &String);
void Add(char* String);
void AddObject( AnsiString &String,void* Object);
void AddObject( char* String,void* Object);
int Count;
TSL Strings;
void** Objects;
void LoadFromFile(char* FileName);
void SaveToFile(char* FileName);
void Delete(int index);
int IndexOf(AnsiString & str);
int IndexOf(char* str);
void Assign(TStringList* Source);
void Free();
};
typedef TStringList TStrings;
//--------------------TList--------------------------------
class TList
{
private:
int maxCount;
public:
TList();
~TList();
void Clear();
void Add(void * Object);
int Count;
void** Items;
void Delete(int index);
int IndexOf(void * ptr);
void Free();
};
//--------------------TList--------------------------------
extern int AnsiCompareStr(AnsiString& S1, AnsiString& S2);
#endif
//-------TStrAnsiClass.cpp----------------------------------------------------------
//---------------------------------------------------------------------------
#include "TStrAnsiClass.h"
#define min(a, b) (((a) < (b)) ? (a) : (b))
//---------------------------------------------------------------------------
void CopyBuf(char* Po, char* Pi, int L)
{
for (int i=1;i<=L;i++){
*Po=*Pi;
Po++;
Pi++;
}
}
//--------------------------------------------------------------------------
TSL::TSL()
{
Count=0;
maxCount=100;
Str=(AnsiString**)malloc(maxCount*sizeof(AnsiString*));
}
//--------------------------------------------------------------------------
TSL::~TSL()
{
for (int i=0;i<Count;i++)
delete Str[i];
free((void*)Str);
}
//--------------------------------------------------------------------------
void TSL::Add(AnsiString* str)
{
Count++;
if (Count>maxCount){
maxCount+=maxCount*2;
Str=(AnsiString**)realloc((void*)Str,maxCount*sizeof(AnsiString*));
}
Str[Count-1]=str;
}
//--------------------------------------------------------------------------
void TSL::Del(int index)
{
delete Str[index];
int idx=0;
for(int i=0;i<Count;i++)
if (i!=index){
Str[idx]=Str[i];
idx++;
}
Count--;
}
//--------------------------------------------------------------------------
AnsiString& TSL::operator [] (int index)
{
//AnsiString* str=new AnsiString;
//*str=Str[index]->c_str();
//return *str;
AnsiString *str;
str=Str[index];
return *str;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
TStringList::TStringList()
{
Count =0;
maxCount=100;
Objects=(void**)malloc(maxCount*sizeof(void*));
}
//---------------------------------------------------------------------------
TStringList::~TStringList()
{
free(Objects);
}
//---------------------------------------------------------------------------
void TStringList::Clear()
{
int c;
c=Count;
for (int i=0;i<c;i++)
Delete(0);
}
//---------------------------------------------------------------------------
void TStringList::Add(AnsiString &String)
{
Count++;
if (Count>maxCount){
maxCount+=maxCount*2;
Objects=(void**)realloc((void*)Objects,maxCount*sizeof(void*));
}
AnsiString * sd=new AnsiString;
*sd=String.c_str();
Strings.Add(sd);
Objects[Count-1]=NULL;
}
//---------------------------------------------------------------------------
void TStringList::Add(char *String)
{
Count++;
if (Count>maxCount){
maxCount+=maxCount*2;
Objects=(void**)realloc((void*)Objects,maxCount*sizeof(void*));
}
AnsiString * sd=new AnsiString;
*sd=String;
Strings.Add(sd);
Objects[Count-1]=NULL;
}
//---------------------------------------------------------------------------
void TStringList::AddObject( AnsiString &String,void* Object)
{
Count++;
if (Count>maxCount){
maxCount+=maxCount*2;
Objects=(void**)realloc((void*)Objects,maxCount*sizeof(void*));
}
AnsiString * sd=new AnsiString;
*sd=String.c_str();
Strings.Add(sd);
Objects[Count-1]=Object;
}
//---------------------------------------------------------------------------
void TStringList::AddObject( char* String,void* Object)
{
Count++;
if (Count>maxCount){
maxCount+=maxCount*2;
Objects=(void**)realloc((void*)Objects,maxCount*sizeof(void*));
}
AnsiString * sd=new AnsiString;
*sd=String;
Strings.Add(sd);
Objects[Count-1]=Object;
}
//---------------------------------------------------------------------------
void TStringList::LoadFromFile(char* FileName)
{
FILE *F;
F = fopen(FileName, "r");
if (F==NULL)
return;
fseek(F,0,SEEK_END);
int FileSize=ftell(F);
fseek(F,0,SEEK_SET);
char * buf;
buf=(char*)malloc(FileSize);
AnsiString str;
TStringList *t;
t=(TStringList*)this;
t->Clear();
while (!feof(F))
{
fscanf(F, "%s\n", buf);
//fgets(buf,FileSize,F);
str=buf;
t->Add(str);
}
free((void*)buf);
fclose(F);
return;
}
//---------------------------------------------------------------------------
void TStringList::SaveToFile(char* FileName)
{
FILE *F;
F = fopen(FileName, "w+");
AnsiString str;
if (F==NULL)
return;
for (int i=0; i<Count; i++)
{
str=Strings[i];
fprintf(F, "%s\n", str.c_str());
}
fclose(F);
}
//---------------------------------------------------------------------------
void TStringList::Delete(int index)
{
int idx=0;
for (int i=0;i<Count;i++)
if (i!=index){
Objects[idx]=Objects[i];
idx++;
}
Strings.Del(index);
Count--;
}
//---------------------------------------------------------------------------
int TStringList::IndexOf(AnsiString & str)
{
AnsiString s;
for (int i=0;i<Count;i++){
s=Strings[i];
if (s==str)
return i;
}
return -1;
}
//---------------------------------------------------------------------------
int TStringList::IndexOf(char* str)
{
AnsiString s;
for (int i=0;i<Count;i++){
s=Strings[i];
if (s==str)
return i;
}
return -1;
}
//---------------------------------------------------------------------------
void TStringList::Assign(TStringList* Source)
{
AnsiString as;
void * ob;
for (int i=0;i<Source->Count;i++){
as=Source->Strings[i];
ob=Source->Objects[i];
AddObject(as,ob);
}
}
//---------------------------------------------------------------------------
void TStringList::Free()
{
delete this;
}
//---------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//---AnsiString---AnsiString---AnsiString---AnsiString---AnsiString---AnsiString
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
AnsiString::AnsiString()
{
LengthL =0;
}
//---------------------------------------------------------------------------
AnsiString::AnsiString(char * stc)
{
int newLengthL=0;
if (stc!=NULL)
newLengthL =strlen(stc);
if (newLengthL>0)
Data =(char*)malloc(newLengthL+1);
/*else
; if (newLengthL>0)
Data =(char*)realloc(Data,newLengthL+1);
else
free(Data);*/
LengthL =newLengthL;
if (LengthL>0)
strcpy(Data, stc);
else
Data=NULL;
}
//---------------------------------------------------------------------------
AnsiString::~AnsiString()
{
if (LengthL>0)
free(Data);
}
//---------------------------------------------------------------------------
int AnsiString::Length()
{
return LengthL;
}
//---------------------------------------------------------------------------
int AnsiString::Pos(AnsiString &str)
{
char *s=str.Data;
char *d=this->Data;
int ls,ld;
ls=str.Length();
ld=this->LengthL-ls;
if (ls>ld || ld==0 || ls==0) return 0;
bool bl;
for (int i=0;i<=ld;i++){
bl=true;
for (int j=0;(j<ls && bl);j++)
if (d[i+j]!=s[j])
bl=false;
if (bl)
return i+1;
}
return 0;
}
//---------------------------------------------------------------------------
int AnsiString::Pos(char *str)
{
char *s=str;
char *d=this->Data;
int ls,ld;
ls=strlen(s);
ld=this->LengthL-ls;
if (ls>ld || ld==0 || ls==0) return 0;
bool bl;
for (int i=0;i<=ld;i++){
bl=true;
for (int j=0;(j<ls && bl);j++)
if (d[i+j]!=s[j])
bl=false;
if (bl)
return i+1;
}
return 0;
}
//---------------------------------------------------------------------------
AnsiString& AnsiString::SubString(int StartSimbol, int NumSimbols)
{
char * dst;
char * src;
dst=(char*)malloc(NumSimbols+1);
src=Data;
src+=(StartSimbol-1);
CopyBuf(dst,src,NumSimbols);
dst[NumSimbols]=NULL;
AnsiString *sd=new AnsiString;
*sd=dst;
free((void*)dst);
return *sd;
}
//---------------------------------------------------------------------------
char* AnsiString::c_str()
{
return Data;
}
//----------------------------------------------------------------------------
AnsiString& AnsiString::operator = ( char* stc)
{
int newLengthL=0;
if (stc!=NULL)
newLengthL =strlen(stc);
if (LengthL==0 && newLengthL>0)
Data =(char*)malloc(newLengthL+1);
else
if (newLengthL>0)
Data =(char*)realloc(Data,newLengthL+1);
else
if (LengthL>0)
free(Data);
LengthL =newLengthL;
if (LengthL>0)
strcpy(Data, stc);
else
Data=NULL;
return *this;
}
//---------------------------------------------------------------------------
AnsiString& AnsiString::operator = (const AnsiString &sta)
{
char * stc=sta.Data;
int newLengthL=0;
if (stc!=NULL)
newLengthL =strlen(stc);
if (LengthL==0 && newLengthL>0)
Data =(char*)malloc(newLengthL+1);
else
if (newLengthL>0)
Data =(char*)realloc(Data,newLengthL+1);
else
if (LengthL>0)
free(Data);
LengthL =newLengthL;
if (LengthL>0)
strcpy(Data, stc);
else
Data=NULL;
return *this;
}
//---------------------------------------------------------------------------
AnsiString operator+( AnsiString &s1, AnsiString &s2)
{
int l1,l2;
l1=s1.Length();
l2=s2.Length();
AnsiString* sd=new AnsiString;
char *cd;
cd=(char*)malloc(l1+l2+1);
CopyBuf(cd,s1.c_str(),l1);
cd+=l1;
CopyBuf(cd,s2.c_str(),l2);
cd+=l2;
cd[0]=NULL;
cd-=(l1+l2);
sd->LengthL=l1+l2;
sd->Data=cd;
return *sd;
}
//---------------------------------------------------------------------------
AnsiString operator+( AnsiString &s1,char *s2)
{
int l1,l2;
l1=s1.Length();
l2=0;
if (s2!=NULL)
l2=strlen(s2);
AnsiString* sd=new AnsiString;
char *cd;
cd=(char*)malloc(l1+l2+1);
CopyBuf(cd,s1.c_str(),l1);
cd+=l1;
CopyBuf(cd,s2,l2);
cd+=l2;
cd[0]=NULL;
cd-=(l1+l2);
sd->LengthL=l1+l2;
sd->Data=cd;
return *sd;
}
//---------------------------------------------------------------------------
AnsiString operator+(char* s1, AnsiString &s2)
{
int l1,l2;
l2=s2.Length();
l1=0;
if (s1!=NULL)
l1=strlen(s1);
AnsiString* sd=new AnsiString;
char *cd;
cd=(char*)malloc(l1+l2+1);
CopyBuf(cd,s1,l1);
cd+=l1;
CopyBuf(cd,s2.c_str(),l2);
cd+=l2;
cd[0]=NULL;
cd-=(l1+l2);
sd->LengthL=l1+l2;
sd->Data=cd;
return *sd;
}
//---------------------------------------------------------------------------
bool AnsiString::operator == (AnsiString & str)
{
int c;
char *sc;
sc=str.Data;
int l;
l=str.Length();
if (l!=LengthL) return false;
if (LengthL==0) return true;
c=strcmp(sc,Data);
bool bl;
bl=(c==0);
return bl;
}
//---------------------------------------------------------------------------
bool AnsiString::operator == (char * str)
{
int c;
char *sc;
sc=str;
int l;
l=strlen(str);
if (l!=LengthL) return false;
if (LengthL==0) return true;
c=strcmp(sc,Data);
bool bl;
bl=(c==0);
return bl;
}
//---------------------------------------------------------------------------
bool AnsiString::operator != (AnsiString & str)
{
int c;
char *sc;
sc=str.Data;
int l;
l=str.Length();
if (l!=LengthL) return true;
if (LengthL==0) return true;
c=strcmp(sc,Data);
bool bl;
bl=(c==0);
return !bl;
}
//---------------------------------------------------------------------------
bool AnsiString::operator != (char * str)
{
int c;
char *sc;
sc=str;
int l;
l=strlen(str);
if (l!=LengthL) return true;
if (LengthL==0) return tr