[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: librec examples
From: |
wim |
Subject: |
Re: librec examples |
Date: |
Wed, 5 Apr 2023 22:16:15 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 |
Hi ,
after some digging I put this basic example together, maybe it can help
others in the future:
Just an example of selecting some records and fields through the
C-library from a recutils database , i used
this chem.rec file
https://gist.githubusercontent.com/Gavinok/c3510aea2386e755bcc17d4d36fa5dec/raw/15ff07952004ff3bdb51d7ef60270ed74521fff1/chem.rec
It's as bare bones of an example I could make, no error checking or
nothing. Just the bare minimum to get started.
Check out the utils/recsel.c if you want to the whole package.
Example:
#include <rec.h>
#include <stdio.h>
// constants used for the query
char *recutl_sex_str = "Symbol = \"Mg\"";
rec_sex_t recutl_sex = NULL;
char *recutl_quick_str = NULL;
char *recsel_fex_str = NULL;
rec_fex_t recsel_fex = NULL;
char *recutl_type = NULL;
bool recsel_collapse = false;
bool recsel_count = false;
bool recutl_insensitive = false;
bool recsel_descriptors = false;
rec_fex_t recutl_sort_by_fields = NULL;
rec_fex_t recsel_group_by_fields = NULL;
rec_writer_mode_t recsel_write_mode = REC_WRITER_NORMAL;
char *recsel_password = NULL;
bool recsel_uniq = false;
size_t recutl_random = 0;
char *recsel_join = NULL;
int main (int argc, char *argv[])
{
// firste open our rec database file
FILE *fp;
fp = fopen("chem.rec","r");
// Create our database in memory
rec_db_t db;
db = rec_db_new();
// Declare our recordset variable
rec_rset_t rset;
// Declare the file parser and load the recordset into memory.
rec_parser_t parser;
parser = rec_parser_new(fp,"chem.rec");
while (rec_parse_rset (parser, &rset))
{
rec_db_insert_rset(db,rset,rec_db_size(db));
}
//Create the Search Expression
char search[] = "Symbol~\"M\"";
rec_sex_t sex = rec_sex_new(recutl_insensitive);
//Compile the Search Expression (sex)
rec_sex_compile(sex,search);
//What fields will we show:
//Create a field expression
// rec_fex_t fex = rec_fex_new("Name",REC_FEX_CSV);
rec_fex_t fex = NULL;
//Queries our in memory database
rset =
rec_db_query(db,recutl_type,recsel_join,0,sex,recutl_quick_str,recutl_random,fex,recsel_password,recsel_group_by_fields,recutl_sort_by_fields,0);
//Create an iterator to walk through the recordsets
rec_mset_iterator_t iter = rec_mset_iterator(rec_rset_mset(rset));
void *data;
rec_mset_elem_t elem;
//loop over the recordset and print to stdout
while (rec_mset_iterator_next (&iter, MSET_ANY, (const void **)
&data, &elem))
{
rec_mset_iterator_t iter;
char *dat;
size_t num_field, num_elem, num_fields, num_elems;
rec_record_t record = (rec_record_t) data;
num_elems = rec_record_num_elems(record);
iter = rec_mset_iterator(rec_record_mset(record));
while (rec_mset_iterator_next (&iter, MSET_ANY, (const void **)
&data, &elem))
{
rec_field_t field = (rec_field_t) data;
const char *fvalue;
const char *fname;
fvalue = rec_field_value(field);
fname = rec_field_name(field);
printf("\n %s = %s",fname,fvalue);
}
}
}
Have fun,
Kind Regards,
Wim Stockman
Op 04-04-2023 om 07:22 schreef Jose E. Marchesi:
Hi would it be possible to give a basic sample of the librec c library
how to insert a record ?
how to select a record ?
Let's say for a basic adresse book example
The recutils themselves (recsel, recins, etc) are just simple little C
programs that use librec.
You can find them under src/ in the recutils source distribution.