Cannot find a data structure for this problem

V

Vince

I have a very specific problem to solve but I cannot find a data
structure for it.
I don't know if I am posting on the good newsgroup but I cannot find
a software.design group.
I would like to declare a smart structure initialized via a XML file.
The goal of this structure is to store data from a smart card.

My XML file describes the file structure of my smart card. On a smart
card file are identified by number(sfid) and not by their name like on
PC. And each record are BYTE arrays

<CardMapping>
<CardFile sfid="Ox17">
<CardRecord index="1" size="29">
<CardData
offset="0"
size="20 bytes"
type="string"
name="Last Name1"
/>
<CardData
offset="20"
size="1 bytes"
type="numeric"
name="age1"/>
</CardRecord>
</CardFile>

<CardFile sfid="Ox18">
<CardRecord index="1" size="29">
<CardData
offset="0"
size="20 bytes"
type="string"
name="Last Name2"
/>
<CardData
offset="20"
size="2 bytes"
type="numeric"
name="age2"/>
</CardRecord>
</CardFile>

</CardMapping>

So basically a smart card can have 0 or n files (CardFile) with inside 0
or n records and each records is splitted into chunks of data.
After reading a file like this I should have :

File 0x17
----------------
| |
|Record1 xxxxx |
|Record2 xxxxx |
|... |
|Recordn xxxx |
| |
| |
----------------

File 0x18
----------------
| |
|Record1 xxxxx |
|Record2 xxxxx |
|... |
|Recordn xxxx |
| |
| |
----------------

And finally what I want to do is something like,
CString csName = m_Card.GetData("Last Name1") (it should extract data
from first record and convert it into string.
int nAge = m_Card.GetData("age2").
 
J

Johan

Vince said:
I have a very specific problem to solve but I cannot find a data structure
for it.
I don't know if I am posting on the good newsgroup but I cannot find
a software.design group.
I would like to declare a smart structure initialized via a XML file. The
goal of this structure is to store data from a smart card.

My XML file describes the file structure of my smart card. On a smart card
file are identified by number(sfid) and not by their name like on PC. And
each record are BYTE arrays

<CardMapping>
<CardFile sfid="Ox17">
<CardRecord index="1" size="29">
<CardData
offset="0"
size="20 bytes"
type="string"
name="Last Name1"
/>
<CardData
offset="20"
size="1 bytes"
type="numeric"
name="age1"/>
</CardRecord>
</CardFile>

<CardFile sfid="Ox18">
<CardRecord index="1" size="29">
<CardData
offset="0"
size="20 bytes"
type="string"
name="Last Name2"
/>
<CardData
offset="20"
size="2 bytes"
type="numeric"
name="age2"/>
</CardRecord>
</CardFile>
</CardMapping>

So basically a smart card can have 0 or n files (CardFile) with inside 0
or n records and each records is splitted into chunks of data.
After reading a file like this I should have :

File 0x17 ----------------
| |
|Record1 xxxxx |
|Record2 xxxxx |
|... |
|Recordn xxxx |
| |
| |
----------------

File 0x18
----------------
| |
|Record1 xxxxx |
|Record2 xxxxx |
|... |
|Recordn xxxx |
| |
| |
----------------

And finally what I want to do is something like,
CString csName = m_Card.GetData("Last Name1") (it should extract data from
first record and convert it into string.
int nAge = m_Card.GetData("age2").

Hi,

I think something like

struct CardData
{
int offset;
int size;
string type;
string name;
};

struct CardMapping
{
public :
string CardFile;
int index;
int size;
struct CardData** carddata;
};

int main(int argc, char** argv[])
{
int count = 10;
CardMapping map;
map.carddata = new CardData *[count * sizeof(CardData*)];

for(int i = 0; i < count; i++)
{
map.carddata = new CardData;
}

return 0;
}

Johan
 
R

Richard Herring

Johan <[email protected]> said:
Vince said:
I have a very specific problem to solve but I cannot find a data structure
for it.
I don't know if I am posting on the good newsgroup but I cannot find
a software.design group.
I would like to declare a smart structure initialized via a XML file. The
goal of this structure is to store data from a smart card.

My XML file describes the file structure of my smart card. On a smart card
file are identified by number(sfid) and not by their name like on PC. And
each record are BYTE arrays
[...]
So basically a smart card can have 0 or n files (CardFile) with inside 0
or n records and each records is splitted into chunks of data.
[...]

Hi,

I think something like

I don't. Read on...
struct CardData
{
int offset;
int size;
string type;
string name;
};

struct CardMapping
{
public :
string CardFile;
int index;
int size;
struct CardData** carddata;
};

int main(int argc, char** argv[])
{
int count = 10;
CardMapping map;
map.carddata = new CardData *[count * sizeof(CardData*)];

for(int i = 0; i < count; i++)
{
map.carddata = new CardData;
}

return 0;
}


I think that's *horrible*. Not only are you messing with raw pointers to
arrays of pointers, which is just asking for memory leaks or worse,
you're doing it outside the class, which breaks any pretence at
encapsulation before you even start. Your struct will get a
compiler-defined assignment, copy constructor and destructor, which are
completely inappropriate for something with pointer members and will
cause UB the moment you double-delete something, which is almost
inevitably waiting to happen. In short, it's a nightmare. C++ provides
far better ways of working.

I'd suggest:

Don't write C code in C++.

Use standard containers (std::vector is probably appropriate here.)

Don't use pointers if you don't have to. Copying a few objects in and
out of vectors is probably a small price to pay, compared with managing
pointers to them.

Encapsulate. Use class, not struct, make member data private and provide
appropriate access functions. Make the classes read themselves if
possible, via member or friend functions.

HTH.
 

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

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,310
Latest member
FaustoMont

Latest Threads

Top