|
/***** FUNCTION sortCStringList() *****/
void sortCStringList(CStringList *csListSort, BOOL bUp)
{
CStringList csListTemp;
CString csCompare, csTemp;
POSITION mPMin, mP, mPreP, mPTemp;
while(csListSort->GetCount() > 0) {
if(bUp) {
csCompare =
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
} else {
csCompare = "";
}
mP = csListSort->GetHeadPosition();
while(mP!=NULL) {
mPreP = mP;
csTemp = csListSort->GetNext(mP);
if((bUp && csCompare > csTemp) || (!bUp && csCompare < csTemp))
{
mPMin = mPreP;
csCompare = csTemp;
} /* endif */
} /* while mP */
csListTemp.AddTail(csListSort->GetAt(mPMin));
csListSort->RemoveAt(mPMin);
} /* while GetCount() */
mPTemp = csListTemp.GetHeadPosition();
while(mPTemp != NULL) {
csListSort->AddTail(csListTemp.GetNext(mPTemp));
}
} /* end of function sortCStringList() */
/***** FUNCTION sortCStringList() *****/
void sortCStringList(CStringList *csListSort, BOOL bUp)
{
CStringList csListTemp;
CString csCompare, csTemp;
POSITION mPMin, mP, mPreP, mPTemp;
while(csListSort->GetCount() > 0) {
if(bUp) {
csCompare =
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
} else {
csCompare = "";
}
mP = csListSort->GetHeadPosition();
while(mP!=NULL) {
mPreP = mP;
csTemp = csListSort->GetNext(mP);
if((bUp && csCompare > csTemp) || (!bUp && csCompare < csTemp))
{
mPMin = mPreP;
csCompare = csTemp;
} /* endif */
} /* while mP */
csListTemp.AddTail(csListSort->GetAt(mPMin));
csListSort->RemoveAt(mPMin);
} /* while GetCount() */
mPTemp = csListTemp.GetHeadPosition();
while(mPTemp != NULL) {
csListSort->AddTail(csListTemp.GetNext(mPTemp));
}
} /* end of function sortCStringList() */
> struct LessThan
> {
> bool operator()(const std::string &a, const std::string &b)
> {
> int numA = atoi(a);
> int numB = atoi(b);
> if(numA && numB) return numA < numB;
> return a < b;
> }
> };
You made me read up on std::string :微微一笑:
struct LessThan
{
bool operator()(const std::string &a, const std::string &b)
{
int numA = atoi(a.c_str());
int numB = atoi(b.c_str());
if(numA && numB) return numA < numB;
return stricmp(a.c_str(), b.c_str()) < 0;
}
};
> struct LessThan
> {
> bool operator()(const std::string &a, const std::string &b)
> {
> int numA = atoi(a);
> int numB = atoi(b);
> if(numA && numB) return numA < numB;
> return a < b;
> }
> };
You made me read up on std::string :微微一笑:
struct LessThan
{
bool operator()(const std::string &a, const std::string &b)
{
int numA = atoi(a.c_str());
int numB = atoi(b.c_str());
if(numA && numB) return numA < numB;
return stricmp(a.c_str(), b.c_str()) < 0;
}
};
/***** FUNCTION sortCStringList() *****/
void sortCStringList(CStringList *csListSort, BOOL bUp)
{
CStringList csListTemp;
CString csCompare, csTemp;
POSITION mPMin, mP, mPreP, mPTemp;
while(csListSort->GetCount() > 0) {
if(bUp) {
csCompare =
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
} else {
csCompare = "";
}
mP = csListSort->GetHeadPosition();
while(mP!=NULL) {
mPreP = mP;
csTemp = csListSort->GetNext(mP);
if((bUp && csCompare > csTemp) || (!bUp && csCompare < csTemp))
{
mPMin = mPreP;
csCompare = csTemp;
} /* endif */
} /* while mP */
csListTemp.AddTail(csListSort->GetAt(mPMin));
csListSort->RemoveAt(mPMin);
} /* while GetCount() */
mPTemp = csListTemp.GetHeadPosition();
while(mPTemp != NULL) {
csListSort->AddTail(csListTemp.GetNext(mPTemp));
}
} /* end of function sortCStringList() */
struct LessThan
{
bool operator()(const std::string &a, const std::string &b)
{
int numA = atoi(a);
int numB = atoi(b);
if(numA && numB) return numA < numB;
return a < b;
}
};
static addStringToRB (resbuf **work, const char *strVal)
> {
> resbuf *ptrNewbuf, *ptrPos;
>
> ptrNewbuf = ptrPos = NULL;
> ptrPos = *work;
> *work = ptrPos;
>
> if (*work)
> {
> while (ptrPos->rbnext != NULL)
> ptrPos = ptrPos->rbnext;
> }
>
> if ((ptrNewbuf = acutNewRb(RTSTR)) == NULL)
> {
> acutPrintf("\nERROR: Unable to allocate memory for resbuf pointer.");
> return FALSE;
> }
>
> if ((ptrNewbuf->resval.rstring = strdup(strVal)) == NULL)
> {
> acutPrintf("\nERROR: Unable to duplicate definition in memory.");
> acutRelRb(ptrNewbuf);
> return FALSE;
> }
>
> if (*work)
> {
> ptrPos->rbnext = ptrNewbuf;
> ptrPos = ptrNewbuf;
> }
>
> else *work = ptrPos = ptrNewbuf;
>
> return TRUE;
> }
>
> static int ads_getlayers(void)
> {
>
> resbuf *IsList = NULL;
>
> AcDbLayerTable* pLayerTbl = NULL;
>
>
> acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTbl,
> AcDb::kForRead);
>
> AcDbLayerTableIterator* pLayerIterator;
> pLayerTbl->newIterator(pLayerIterator);
>
> AcDbLayerTableRecord* pLayerTblRcd;
> char *pLName;
>
> // start iterator - not done
> for(pLayerIterator->start() ; !pLayerIterator->done() ;
> pLayerIterator->step()) {
>
> //for (; !pLayerIterator->done(); pLayerIterator->step()) {
>
> pLayerIterator->getRecord(pLayerTblRcd, AcDb::kForRead);
> pLayerTblRcd->getName(pLName);
> pLayerTblRcd->close();
> //acutPrintf("\nLayer name: %s",pLName);
>
> // adding the layer name into the list
> addStringToRB(&IsList, pLName);
>
> // deallocates the memory pointed to by pLName and sets pLName to NULL
> acutDelString(pLName);
> }
>
> // delete the layer iterator
> delete pLayerIterator;
> // closing the layer table
> pLayerTbl->close();
>
> // if the list is empty [nil] return nil
> if (IsList == NULL) acedRetNil();
> // if not empty return the result-buffer
> and release the memory of the resbuf
> else {
> acedRetList(IsList);
> acutRelRb(IsList);
> }
>
>
> // return the layer list
> return (RSRSLT) ;
> }
>
> Now what I'm supposed to use in order to have the list of layers [strings]
> sorted? I have read about using the following:
>
> //Make a list.
> std::vector<:STRING> mylist;
>
> // then inside of the for...
> mylist.push_back(pLName);
>
> // Sort the list.
> std::sort(mylist.begin(), mylist.end());
>
> But I cannot figure out how to return "mylist" sorted.....
>
> If anyone could help me, please?
>
> Thanks,
> Luis.
Luis,
You're on the right track. Iterate the sorted vector and call your
addStringToRB() from the vector iterator loop instead of the layer table
iterator loop:
std::vector<:STRING>::iterator layer_iter;
for(layer_iter = mylist.begin(); layer_iter != mylist.end(); layer_iter++)
{
addStringToRB(&IsList, layer_iter->c_str());
}
struct LessThan {
bool operator() (const std::string &a, const std::string &b) {return a < b;}
};
// Sort the list.
std::sort(mylist.begin(), mylist.end(), LessThan()); |
|