Folks
I have written a bubble sort routine to display AnsiString strings in alphabetical order (yes I know standard sort routines exist in TList etc).
The routine copies the AnsiStrings to char strings, squeezes out the non alphabet letters from the char strings, compares the char strings & reorders the AnsiStrings accordingly. It works fine with one big exception. The chars squeezed from the char strings are also removed from the AnsiStrings !.
Do I have a problem with pointers in that cs1 is pointing to dlldescriptors ?. Under debug the addresses of cs1/2 and *(dlldescriptors+i) are different
Where am I going wrong ?
John
//-------------------------------------------------------------------------
//IDE is CBuilder2007
//This funct. sorts & displays dll descriptors in a StringGrid
int displaydlls(int no_dlls, TObject *Sender)
{
String *dlldescriptors ;
dlldescriptors = new String[no_dlls];//AnsiString array to be bubble sorted alphabetically
//interrogate each dll to populate the dll descriptors String array
for(int i=0;i<no_dlls
{ //ufo->dllpaths is a AnsiString array of directory paths in ufo
dllhandle = LoadLibrary( (*(ufo->dllpaths + i)).c_str() ) ;
pflabel = (text)GetProcAddress(dllhandle, "_flabel") ;
}
//bubble sort descriptors prior to displaying in StringGrid
int depth = no_dlls -1 ;
char *cs1, *cs2 ;
while(depth>=0)
{
for(int i=0;i<depth;i++)
{
cs1 = (*(dlldescriptors+i)).c_str() ; //Is problem here ?
cs2 = (*(dlldescriptors+i+1)).c_str() ;
int j=0;
do //squeeze out non alphabetical chars from string prior to strcmpi-ing
{
if (!(((cs1[j] >= 'A') && (cs1[j] <= 'Z')) || ((cs1[j]>= 'a') && (cs1[j]<= 'z'))))
for(int k= j;cs1[k]!='\0';k++) cs1[k] = cs1[k+1];
else j++ ;
}
while(cs1[j]!='\0') ;
j=0;
do
{
if (!(((cs2[j] >= 'A') && (cs2[j] <= 'Z')) || ((cs2[j]>= 'a') && (cs2[j]<= 'z'))))
for(int k= j;cs2[k]!='\0';k++) cs2[k] = cs2[k+1];
else j++ ;
}
while(cs2[j]!='\0') ;
if(strcmpi(cs2, cs1)<0) //from string.h
{
String stemp = (*(dlldescriptors+i)) ;
(*(dlldescriptors+i)) = (*(dlldescriptors+i+1)) ;
(*(dlldescriptors+i+1)) = stemp ;
}
}
depth-- ;
}
//write out the descriptors to the StringGrids
for(int i=0;i<no_dlls;i++) Form5->StringGrid1->Cells[0] = *(dlldescriptors+i) ;
delete[] dlldescriptors ;
return 0 ;
}
I have written a bubble sort routine to display AnsiString strings in alphabetical order (yes I know standard sort routines exist in TList etc).
The routine copies the AnsiStrings to char strings, squeezes out the non alphabet letters from the char strings, compares the char strings & reorders the AnsiStrings accordingly. It works fine with one big exception. The chars squeezed from the char strings are also removed from the AnsiStrings !.
Do I have a problem with pointers in that cs1 is pointing to dlldescriptors ?. Under debug the addresses of cs1/2 and *(dlldescriptors+i) are different
Where am I going wrong ?
John
//-------------------------------------------------------------------------
//IDE is CBuilder2007
//This funct. sorts & displays dll descriptors in a StringGrid
int displaydlls(int no_dlls, TObject *Sender)
{
String *dlldescriptors ;
dlldescriptors = new String[no_dlls];//AnsiString array to be bubble sorted alphabetically
//interrogate each dll to populate the dll descriptors String array
for(int i=0;i<no_dlls
{ //ufo->dllpaths is a AnsiString array of directory paths in ufo
dllhandle = LoadLibrary( (*(ufo->dllpaths + i)).c_str() ) ;
pflabel = (text)GetProcAddress(dllhandle, "_flabel") ;
}
//bubble sort descriptors prior to displaying in StringGrid
int depth = no_dlls -1 ;
char *cs1, *cs2 ;
while(depth>=0)
{
for(int i=0;i<depth;i++)
{
cs1 = (*(dlldescriptors+i)).c_str() ; //Is problem here ?
cs2 = (*(dlldescriptors+i+1)).c_str() ;
int j=0;
do //squeeze out non alphabetical chars from string prior to strcmpi-ing
{
if (!(((cs1[j] >= 'A') && (cs1[j] <= 'Z')) || ((cs1[j]>= 'a') && (cs1[j]<= 'z'))))
for(int k= j;cs1[k]!='\0';k++) cs1[k] = cs1[k+1];
else j++ ;
}
while(cs1[j]!='\0') ;
j=0;
do
{
if (!(((cs2[j] >= 'A') && (cs2[j] <= 'Z')) || ((cs2[j]>= 'a') && (cs2[j]<= 'z'))))
for(int k= j;cs2[k]!='\0';k++) cs2[k] = cs2[k+1];
else j++ ;
}
while(cs2[j]!='\0') ;
if(strcmpi(cs2, cs1)<0) //from string.h
{
String stemp = (*(dlldescriptors+i)) ;
(*(dlldescriptors+i)) = (*(dlldescriptors+i+1)) ;
(*(dlldescriptors+i+1)) = stemp ;
}
}
depth-- ;
}
//write out the descriptors to the StringGrids
for(int i=0;i<no_dlls;i++) Form5->StringGrid1->Cells[0] = *(dlldescriptors+i) ;
delete[] dlldescriptors ;
return 0 ;
}