M
manya
Ok, it's been a while since I've done the whole memcpy stuff with C++
and I'm having a hard time remembering everything.
I hope, however, that you can help me with my problem.
I memcpy a struct into a buffer to put it into a database (BerkeleyDB,
to be specific) - what do I do to memcpy the thing back?
Code Example:
//struct I want to work with
struct dbHeader {
MyToken commandToken;
MyId id;
MyDate date;
unsigned long length;
};
where MyToken is an enum and MyId as well as MyDate are classes with a
private member unsigned char value[14]; and just set/get methods ( eg
inline void set( const unsigned char *s ) { strncpy( (char *)value,
(char *)s, 14); }; ) this should be correct so far.
now I want to memcpy the memory into a buffer to store into the
database.
MyToken cCommand = record.getMyToken();
MyId cId = record.getMyId();
MyDate cDate = record.getMyDate();
long cLength = record.getLength();
size_t len;
u_int8_t *p, data_buffer[1024];
p = &data_buffer[0];
len = sizeof(cCommand);
memcpy(p, &len, sizeof(len));
p += sizeof(len);
memcpy(p, &cCommand, len);
p += len;
memcpy(p, cId.get(), sizeof(cId.get()));
p += sizeof(cId.get());
memcpy(p, cDate.get(), sizeof(cDate.get()));
p += sizeof(cDate.get());
memcpy(p, &cLength, sizeof(cLength));
p += sizeof(cLength);
(cId.get() returns a char*)
Ok, p has the struct now, has it?
Now I put it into the db and when I want it back, how do I read it back
to my struct?
I hope you can help me.
and I'm having a hard time remembering everything.
I hope, however, that you can help me with my problem.
I memcpy a struct into a buffer to put it into a database (BerkeleyDB,
to be specific) - what do I do to memcpy the thing back?
Code Example:
//struct I want to work with
struct dbHeader {
MyToken commandToken;
MyId id;
MyDate date;
unsigned long length;
};
where MyToken is an enum and MyId as well as MyDate are classes with a
private member unsigned char value[14]; and just set/get methods ( eg
inline void set( const unsigned char *s ) { strncpy( (char *)value,
(char *)s, 14); }; ) this should be correct so far.
now I want to memcpy the memory into a buffer to store into the
database.
MyToken cCommand = record.getMyToken();
MyId cId = record.getMyId();
MyDate cDate = record.getMyDate();
long cLength = record.getLength();
size_t len;
u_int8_t *p, data_buffer[1024];
p = &data_buffer[0];
len = sizeof(cCommand);
memcpy(p, &len, sizeof(len));
p += sizeof(len);
memcpy(p, &cCommand, len);
p += len;
memcpy(p, cId.get(), sizeof(cId.get()));
p += sizeof(cId.get());
memcpy(p, cDate.get(), sizeof(cDate.get()));
p += sizeof(cDate.get());
memcpy(p, &cLength, sizeof(cLength));
p += sizeof(cLength);
(cId.get() returns a char*)
Ok, p has the struct now, has it?
Now I put it into the db and when I want it back, how do I read it back
to my struct?
I hope you can help me.