BRL-CAD
bu_structparse Struct Reference

#include <parse.h>

Data Fields

const char sp_fmt [4]
 
size_t sp_count
 
const char * sp_name
 
size_t sp_offset
 
void(* sp_hook )(const struct bu_structparse *, const char *, void *, const char *, void *)
 
const char * sp_desc
 
void * sp_default
 

Detailed Description

The "bu_structparse" struct describes one element of a structure. Collections of these are combined to describe entire structures (or at least those portions for which parse/print/import/export support is desired.

Provides a convenient way of describing a C data structure, and reading and writing it in both human-readable ASCII and efficient binary forms.

For example:

struct data_structure {
char a_char;
char str[32];
short a_short;
int a_int;
fastf_t a_fastf_t;
double a_double;
}
struct data_structure default = { 'c', "the default string", 32767, 1, 1.0, 1.0 };
struct data_structure my_values;
struct bu_structparse data_sp[] ={
{"%c", 1, "a_char", bu_offsetof(data_structure, a_char), BU_STRUCTPARSE_FUNC_NULL, "a single character", (void*)&default.a_char},
{"%s", 32, "str", bu_offsetofarray(data_structure, str), BU_STRUCTPARSE_FUNC_NULL, "This is a full character string", (void*)default.str},
{"%i", 1, "a_short", bu_offsetof(data_structure, a_short), BU_STRUCTPARSE_FUNC_NULL, "A 16bit integer", (void*)&default.a_short},
{"%d", 1, "a_int", bu_offsetof(data_structure, a_int), BU_STRUCTPARSE_FUNC_NULL, "A full integer", (void*)&default.a_int},
{"%f", 1, "a_fastf_t", bu_offsetof(data_structure, a_fastf_t), BU_STRUCTPARSE_FUNC_NULL, "A variable-precision fasf_t floating point value", (void*)&default.a_fastf_t},
{"%g", 1, "a_double", bu_offsetof(data_structure, a_double), BU_STRUCTPARSE_FUNC_NULL, "A double-precision fasf_t floating point value", (void*)&default.a_double},
{ "", 0, (char *)NULL, 0, BU_STRUCTPARSE_FUNC_NULL, (char *)NULL, (void *)NULL}
};

To parse a string, call:

bu_struct_parse(vls_string, data_sp, (char *)my_values)

this will parse the vls string and assign values to the members of the structure my_values

A gross hack: To set global variables (or others for that matter) you can store the actual address of the variable in the sp_offset field and pass a null pointer as the last argument to bu_struct_parse. If you don't understand why this would work, you probably shouldn't use this technique.

Definition at line 137 of file parse.h.

Field Documentation

void(* sp_hook) (const struct bu_structparse *, const char *, void *, const char *, void *)

Optional hooked function, or indir ptr

Definition at line 142 of file parse.h.

Referenced by bu_struct_import(), bu_structparse_argv(), and parse_struct_lookup().

const char* sp_desc

description of element

Definition at line 147 of file parse.h.

void* sp_default

ptr to default value

Definition at line 148 of file parse.h.


The documentation for this struct was generated from the following file: