2

I am changing from a file system I made into DBD.

And I had no choice to convert structures that have character point members to insert these structures into the DBD file

for examble, If there is a structure as below

typedef struct
{
    int   IndexKey;
    int   groupID;
    char* name;
    char* pNum;
    char* pAddr;
    char* pMemo;
} TsomeRec;

I made a structure to convert as below

typedef struct
{
    int   IndexKey;
    int   groupID;
    char  name[MAX_NAME_LEN];
    char  pNum[MAX_NUM_LEN];
    char  pAddr[MAX_ADDR_LEN];
    char  pMemo[MAX_MEMO_LEN];
} TsomeRec2;

But, there are too many structures to convert.

So, I am seeking for the most efficient way to insert these structures into DBD files, considering Performance.

Frankly speaking, I'am not proficient. please describe as specific as possible.

Thank you~

3 Answers3

2

if you want to insert the following structure:

struct S
{
char* string1;
char* string2;
}



char* p=malloc( (sizeof(size_t) + strlen(s.string1)+ sizeof(size_t) + strlen(s.string2));
size_t i=0;
size_t len1=strlen(s.string1);
memcpy(&len1,&p[i],sizeof(size_t)); i+=sizeof(size_t);
memcpy(&s.string1,&p[i],strlen(s.string1)); i+=strlen(s.string1);
size_t len2=strlen(s.string2);
memcpy(&len2,&p[i],sizeof(size_t)); i+=sizeof(size_t);
memcpy(&s.string2,&p[i],strlen(s.string1)); i+=strlen(s.string2);    

then, use 'p' to store your key/data into BDB and free(p)

to read, the data back, read the size of the string, malloc the required memory and read the string itself

Pierre
  • 34,472
  • 31
  • 113
  • 192
0

I think you can try setting default values for you structure

struct a
{
    a() : i(X),j(Y){}

    int i;
    int j;
};

or making enumeration into your structure

struct X {
enum Colour { red, blue } colour;
};

int main(void)
{
struct X x;

x.colour = blue;

return 0;
}

hopefully this helps

user97560
  • 1
  • 4
0

Your data needs to made serializable. You have done that in one case in your Question. To do this in a general way for many types of records is a task for somebody proficient in C++ and BDB (I have done it before). You seemed to indicate you were not proficient. So if you want to do it yourself you will have to convert each structure which sounds monotonous (you said there were too many), but at least it requires less skill.

The solution you are looking for is too complex to produce and too long to post up here.

ScrollerBlaster
  • 1,578
  • 2
  • 17
  • 21