By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

Cannot find a data structure for this problem

P: n/a
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").


Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Vince" <vs**@caramail.com> schreef in bericht
news:41**********************@news.free.fr...
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[i] = new CardData;
}

return 0;
}

Johan


Jul 22 '05 #2

P: n/a
In message <10*************@corp.supernews.com>, Johan <me@knoware.nl>
writes

"Vince" <vs**@caramail.com> schreef in bericht
news:41**********************@news.free.fr...
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[i] = 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.
--
Richard Herring
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.