Download Litebase User Manual
Transcript
Litebase User manual
We can now rewrite the examples above in a more general way. Let us create a function to print the
contents of a generic record:
void printrec(Rbuf *r)
//
{
int fields = r->fields();
//
int i;
for (i = 0; i < fields; i++) {
//
Field *f = r->field(i);
//
switch (f->type()) {
//
case DB_Text:
printf("%s", f->text());
//
break;
case DB_Short:
printf("%d", f->asint());
//
break;
case DB_Long:
case DB_Serial:
printf("%ld", f->aslong()); //
break;
case DB_Float:
//
case DB_Double:
printf("%f", f->asdouble()); //
break;
case DB_Binary:
//
{
int j = 0;
int flen = f->len();
//
char *s = f->fptr();
//
while (j < flen) {
//
int n = s[j] & 0x0ff;
printf("%02x", n);
j++;
}
}
break;
}
if (i == fields – 1)
//
printf("\n");
//
else
printf("; ");
//
}
}
print generic record
get number of fields in record
for every field
get Field object
switch field type
print field as string
print field as int
print field as long
float can be treated as double
print field as double
binary field: print in hex
get number of bytes in field
pointer to field contents
for each byte
// get byte (mask off sign)
// print it in hexadecimal
last field in record?
print new line
separate fields with "; "
void printhdr(DataFile *f)
// print header row
{
int fields = f->fields();
for (int i = 0; i < fields; i++) {
FieldNode *fld = f->fieldnode(i);
printf("%s/%c%d", fld->name, fld->info.type, fld->info.len);
printf(i == fields – 1 ? "\n" : "; ");
// separator / newline
}
}
void printlist(Rlist *r)
{
DataFile *f = r->datafile();
// print everything
// get Rlist's DataFile
10