struct containing vector?

C

cerr

Hi There,

I have a little issue going here:
I have a xml configuration file that looks kinda like this:
<PID>
<DESCRIPTION>Steveston Hwy</DESCRIPTION>
<BCAST>192.168.101.255</BCAST>
<DESTINATION>
<DESTNO>1531</DESTNO>
<DESTNO>301</DESTNO>
<DESTNO>12555</DESTNO>
</DESTINATION>
</PID>
Now there could be various <PID> tags as well as each <PID> would
likely have multiple <DESTNO> tags.
How do I store this best in my app?
I thoughtr I'd declare a struct like this:
struct structPID
{
string strBcast;
string strDescription;
vector<int> Dest;
};
and just extend the array when there's more PIDs in that file.
But when I read out the file i'm using my xml parser and i thought i'd
do something like this:
//i_PIDlist is the pointer to a variable decalred private in class
PIDClient.
bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
*i_PIDlist)
{
RSXMLParser xmlPIDData(Configfile,
RSXMLParser::FILE);

if (!xmlPIDData.IsDataGood()) {
OUTPUT(std::cerr << "Document " << conf->
intersectionDataFile << " is not parsed
successfully.\n";
)
return false;
}

string strPID = xmlPIDData.GetNodeData("PID"); //PID
while (strPID.size() > 0) {
RSXMLParser xmlPID(strPID); //PID details
structPID tmpPID;

tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPTION");

string strDest = xmlPIDData.GetNodeData("DESTINATION");
while(strDest.size() > 0) {
RSXMLParser xmlPIDDest(strDest);
xmlPIDDest.GetNodeData("DESTNO");
tmpPID.Dest->push_back();
}
i_PIDlist->push_back(tmpPID);
//delete tmpPID;
//tmpPID=NULL;

strPID = xmlPIDData.GetNext();
}

return true;
}
But there's two problems i'm seeing just right out of the bat:
If the outer while loop goes twice, how would tmpPID be re-declared?
I thought about declaring it on the heap and then calling delete in
the end of the funtion before it loops around but even then,
I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
pionter that gets ereased soon after...

Thanks for a little help and guidance here.

Ron
 
C

cerr

Hi There,

I have a little issue going here:
I have a xml configuration file that looks kinda like this:
<PID>
  <DESCRIPTION>Steveston Hwy</DESCRIPTION>
  <BCAST>192.168.101.255</BCAST>
  <DESTINATION>
    <DESTNO>1531</DESTNO>
    <DESTNO>301</DESTNO>
    <DESTNO>12555</DESTNO>
  </DESTINATION>
</PID>
Now there could be various <PID> tags as well as each <PID> would
likely have multiple <DESTNO> tags.
How do I store this best in my app?
I thoughtr I'd declare a struct like this:
struct structPID
{
        string strBcast;
        string strDescription;
        vector<int> Dest;};

and just extend the array when there's more PIDs in that file.
But when I read out the file i'm using my xml parser and i thought i'd
do something like this:
//i_PIDlist is the pointer to a variable decalred private in class
PIDClient.
bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
*i_PIDlist)
{
    RSXMLParser xmlPIDData(Configfile,
                             RSXMLParser::FILE);

    if (!xmlPIDData.IsDataGood()) {
        OUTPUT(std::cerr << "Document " << conf->
                         intersectionDataFile << " is not parsed
successfully.\n";
              )
        return false;
    }

    string strPID = xmlPIDData.GetNodeData("PID");      //PID
    while (strPID.size() > 0) {
        RSXMLParser xmlPID(strPID);                     //PID details
        structPID tmpPID;

        tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
        tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPTION");

        string strDest = xmlPIDData.GetNodeData("DESTINATION");
        while(strDest.size() > 0) {
          RSXMLParser xmlPIDDest(strDest);
          xmlPIDDest.GetNodeData("DESTNO");
          tmpPID.Dest->push_back();
        }
        i_PIDlist->push_back(tmpPID);
        //delete tmpPID;
        //tmpPID=NULL;

        strPID = xmlPIDData.GetNext();
    }

    return true;}

But there's two problems i'm seeing just right out of the bat:
If the outer while loop goes twice, how would tmpPID be re-declared?
I thought about declaring it on the heap and then calling delete in
the end of the funtion before it loops around but even then,
I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
pionter that gets ereased soon after...

Thanks for a little help and guidance here.

Ron

Ah,

I think I've figured out how I can do it:
How about this?

while (strPID.size() > 0) {
RSXMLParser xmlPID(strPID); //PID details
structPID *tmpPID = new structPID;

tmpPID->strBcast=xmlPID.GetNodeData("BCAST");
tmpPID->strDescription=xmlPID.GetNodeData("DESCRIPTION");

string strDest = xmlPIDData.GetNodeData("DESTINATION");
while(strDest.size() > 0) {
RSXMLParser xmlPIDDest(strDest);
tmpPID->Dest.push_back(atoi(xmlPIDDest.GetNodeData("DESTNO").c_str
()));
}
i_PIDlist->push_back((*tmpPID));
delete tmpPID;
tmpPID=NULL;

strPID = xmlPIDData.GetNext();
}
 
B

Bart van Ingen Schenau

Hi There,

I have a little issue going here:
I have a xml configuration file that looks kinda like this:
    string strPID = xmlPIDData.GetNodeData("PID");      //PID
    while (strPID.size() > 0) {
        RSXMLParser xmlPID(strPID);                     //PID details
        structPID tmpPID;
        i_PIDlist->push_back(tmpPID);
        //delete tmpPID;
        //tmpPID=NULL;

        strPID = xmlPIDData.GetNext();
    }

    return true;}

But there's two problems i'm seeing just right out of the bat:
If the outer while loop goes twice, how would tmpPID be re-declared?

These is no need to do anything special.
If the outer loop iterates multiple times, the brace-enclosed body of
the loop is entered and exited multiple times. At each entry, the
local variables are created anew by the compiler, and at each exit of
the loop body, they are destroyed.

So, if your loop iterates twice, then you will have two distinct
objects named tmpPID, with one being created after the other.
I thought about declaring it on the heap and then calling delete in
the end of the funtion before it loops around but even then,
I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
pionter that gets ereased soon after...

Thanks for a little help and guidance here.

Ron

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top