RPM Community Forums

Mailing List Message of <popt-devel>

Named bit fields ala ancient BSD %b format

From: Jeff Johnson <n3npq@mac.com>
Date: Thu 01 May 2008 - 19:43:17 CEST
Message-Id: <09951966-FE5C-4940-A939-A838BEA37758@mac.com>
While I'm thinking about popt features, here is another:

The BSD kernel (iirc) devised a means to map bit files to names
using a %b format.

A string was used to map bit# <-> name. E.g. here's an example
from some rpmdb code that I use for output purposes

static const char * dbtFlags =
         "\20\1APPMALLOC\2ISSET\3MALLOC\4PARTIAL\5REALLOC\6USERMEM 
\7DUPOK";

The same technique (of nerdy embedded octal in a string) can be used  
to assign names to bits in a set,
permitting option parsing (using the format string above):

     --option PARTIAL,DUPOK

to save the value 0x48 (i.e. (1 << 3) | (1 << 6)) into the target  
storage.

OTOH, generating the %b format string can be tedious, and perhaps
a more general table like (this from LZMA):

     typedef struct {
         const char *name;
         uint64_t id;
    } name_id_map;

with name = NULL as table end sentinel should be done.

Opinions?

73 De Jeff
Received on Thu May 1 19:43:55 2008
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.