Managing Dynamic Data Structures (with dbd) -
08-01-2007
, 09:51 PM
The C-code below demos how an app can manage dynamic data structures
with dbd. During the first run, it creates two persons with gender
attributes. During the second run, it adds a third person and age
attributes. The second run also performs a query to find all persons
who are male. During the third run, it adds a fourth person with body-
build attribute that can have multiple values. Code near end of the
function prints all persons and their attributes and values during
each run.
#include "stdafx.h"
#include "dbd.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
Db_init();
int errCode = Db_FileSpec_set_r(_T("Db1.dbd"));
if (!Db_File_exists_b()){
// * This code is executed during 1st run *
// Create db file
errCode = Db_File_create_r(kDbSzDef_g);
// Open db file
errCode = Db_File_open_r();
// Create gender
Xp_process_r(_T("(new 'gender)"));
// Create a person named john and set his gender to male
Xp_process_r(_T("(new 'john 'person)"));
Xp_process_r(_T("(set+ john gender 'male)"));
// Create a person named mary and set her gender to female
Xp_process_r(_T("(new 'mary 'person)"));
Xp_process_r(_T("(set+ mary gender 'female)"));
}
else{
// Open db file
errCode = Db_File_open_r();
// If bob is missing in db
int* pBob = AStr_getDefN(_T("bob"));
if (!pBob){
// * This is code is executed during 2nd run *
// Create age,
Xp_process_r(_T("(new 'age)"));
// Create a person named bob
// Set his gender to male and age to 35
Xp_process_r(_T("(new 'bob 'person)"));
Xp_process_r(_T("(set+ bob gender 'male)"));
Xp_process_r(_T("(set+ bob age '35)"));
// Set john's age to 30
Xp_process_r(_T("(set+ john age '30)"));
// Get all person that are male
// Following prints john and bob
int* pQry = Xp_compile(
_T("(and (get person instance *) (get * gender male))"));
while (int* pPersonX=Xp_execute(pQry)){
TCHAR sName[kStrSz_g+1] = _T("");
N_Name_get(pPersonX, sName, kStrSz_g);
wprintf(_T("%s\n"), sName);
}
}
else{
int* pSize = AStr_getDefN(_T("size"));
if (!pSize){
// * This is code is executed during 3nd run *
// Create body build
Xp_process_r(_T("(new 'build)"));
// Set bob's build to tall
Xp_process_r(_T("(set+ bob build 'tall)"));
// Set mary's build to thin and petite
Xp_process_r(_T("(set+ mary build 'thin)"));
Xp_process_r(_T("(set+ mary build 'petite)"));
}
}
}
// * This is code is executed during all runs *
// Print each person's attributes and values
// During 1st run, prints:
// john gender male
// mary gender female
// During 2nd run, prints:
// john gender male
// john age 30
// mary gender female
// bob gender male
// bob age 35
// During 3rd run, prints:
// john gender male
// john age 30
// mary gender female
// mary build thin
// mary build petite
// bob gender male
// bob age 35
// bob build tall
int* pQry = Xp_compile(_T("(get person instance *)"));
while (int* pPtX=Xp_execute(pQry)){
int* pA[256]; int* p = (int*)pA;
int* eS = Xp_Node(pPtX, p);
int* eSVO = Xp_S_getSVO(eS, p);
while (int* pSVO=Xp_execute(eSVO)){
int* pSub = N_SVO_getElem(pSVO, kSeqSub_g);
int* pVb = N_SVO_getElem(pSVO, kSeqVb_g);
int* pObj = N_SVO_getElem(pSVO, kSeqObj_g);
if (pVb != pName_g){
// Print "person attribute value"
TCHAR sPoint[kStrSz_g+1] = _T("");
N_Name_get(pSub, sPoint, kStrSz_g);
TCHAR sProp[kStrSz_g+1] = _T("");
N_Name_get(pVb, sProp, kStrSz_g);
TCHAR sVal[kStrSz_g+1] = _T("");
N_Name_get(pObj, sVal, kStrSz_g);
wprintf(_T("%s %s %s\n"), sPoint, sProp, sVal);
}
}
}
Db_File_save();
Db_File_close_r();
return 0;
}
For more info, see www.dbfordummies.com/Dev/C/InstallTestDLL.asp |