unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Alist Usage
@ 2004-03-30 18:00 Brian S McQueen
  2004-03-30 18:16 ` Paul Jarc
  0 siblings, 1 reply; 2+ messages in thread
From: Brian S McQueen @ 2004-03-30 18:00 UTC (permalink / raw)


I wonder about the best way to use an alist in a C library I have made.
I pass a SCM alist to the library, and it is used in the key-value style
to retrieve the appropriate value from the alist, while providing the C
implementation with total independence from the scheme side. Anyway, the
practical issue is safely getting the C string out of the alist:

gid_scm = scm_cdr(scm_assoc(user_alist_scm, scm_takfrom0str("user_login")));

SCM_STRING_COERCE_0TERMINATION_X(gid_scm);

gid = SCM_STRING_CHARS(gid_scm);

I don't like the SCM_STRING_COERCE_0TERMINATION_X() in there.  It is
missing hadnling for the "not found" (#f) case. I don't like the
scm_takfrom0str("user_login") which provides the key for the key-value
lookup either.

I would like a simple command so I can do something like the following
line from the C library (the SCM alist is "user_alist"). Note the
function get_val():

set_parm(&email, "@email", EMAIL_LEN, get_val(user_alist, "email"));

Defining a macro to accomplish "get_val" seems a bit awkward and
unsafe since I need to drop SCM_STRING_COERCE_0TERMINATION_X() and there
is still no handling of the #f case:

#define get_val(A) ...

I was thinking I could switch to a SCM record, but that introduces a
dependence between the C lib and the scheme code, since the scheme code
would need to provide fields as req'd by the C lib.  With an alist fields
are added or left empty with and the C lib can handle the interface.

Brian McQueen
NAS Division
NASA/Ames


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Alist Usage
  2004-03-30 18:00 Alist Usage Brian S McQueen
@ 2004-03-30 18:16 ` Paul Jarc
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Jarc @ 2004-03-30 18:16 UTC (permalink / raw)
  Cc: guile-user

Brian S McQueen <bqueen@nas.nasa.gov> wrote:
> I would like a simple command so I can do something like the following
> line from the C library (the SCM alist is "user_alist"). Note the
> function get_val():
>
> set_parm(&email, "@email", EMAIL_LEN, get_val(user_alist, "email"));
>
> Defining a macro to accomplish "get_val" seems a bit awkward

Yes, it would be better done as a function:

char const* get_val(SCM alist, char const* key, char const* func_name) {
  SCM handle=scm_assoc(alist, scm_makfrom0str(key));
  SCM data;
  if (SCM_FALSEP(handle)) {
    /* return NULL? throw exception? whatever works for you */
  }
  data=scm_cdr(handle);
  SCM_ASSERT_TYPE(SCM_STRINGP(data), data, 0, func_name, "string");
  SCM_STRING_COERCE_0TERMINATION_X(data);
  return SCM_STRING_CHARS(data);
}


paul


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-03-30 18:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-30 18:00 Alist Usage Brian S McQueen
2004-03-30 18:16 ` Paul Jarc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).