Changeset 6a08f16


Ignore:
Timestamp:
Mar 11, 2012, 10:48:51 PM (9 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
release-1.8
Children:
5b54595
Parents:
34132f7
git-author:
Anders Kaseorg <andersk@mit.edu> (01/19/12 21:22:29)
git-committer:
David Benjamin <davidben@mit.edu> (03/11/12 22:48:51)
Message:
zephyr: Add an iterator interface to fields

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zephyr.c

    rc855755 r6a08f16  
    520520}
    521521
    522 /* return a pointer to the data in the Jth field, (NULL terminated by
    523  * definition).  Caller must free the return.
    524  */
    525 #ifdef HAVE_LIBZEPHYR
     522#ifdef HAVE_LIBZEPHYR
     523const char *owl_zephyr_first_raw_field(const ZNotice_t *n)
     524{
     525  if (n->z_message_len == 0)
     526    return NULL;
     527  return n->z_message;
     528}
     529
     530const char *owl_zephyr_next_raw_field(const ZNotice_t *n, const char *f)
     531{
     532  const char *end = n->z_message + n->z_message_len;
     533  f = memchr(f, '\0', end - f);
     534  if (f == NULL)
     535    return NULL;
     536  return f + 1;
     537}
     538
     539const char *owl_zephyr_get_raw_field(const ZNotice_t *n, int j)
     540{
     541  int i;
     542  const char *f;
     543  for (i = 1, f = owl_zephyr_first_raw_field(n); i < j && f != NULL;
     544       i++, f = owl_zephyr_next_raw_field(n, f))
     545    ;
     546  return f;
     547}
     548
     549CALLER_OWN char *owl_zephyr_field(const ZNotice_t *n, const char *f)
     550{
     551  if (f == NULL)
     552    return g_strdup("");
     553  return g_strndup(f, n->z_message + n->z_message_len - f);
     554}
     555
     556CALLER_OWN char *owl_zephyr_field_as_utf8(const ZNotice_t *n, const char *f)
     557{
     558  char *tmp = owl_zephyr_field(n, f);
     559  char *out = owl_validate_or_convert(tmp);
     560  g_free(tmp);
     561  return out;
     562}
     563
    526564CALLER_OWN char *owl_zephyr_get_field(const ZNotice_t *n, int j)
    527565{
    528   int i, count, save;
    529 
    530   /* If there's no message here, just run along now */
    531   if (n->z_message_len == 0)
    532     return(g_strdup(""));
    533 
    534   count=save=0;
    535   for (i=0; i<n->z_message_len; i++) {
    536     if (n->z_message[i]=='\0') {
    537       count++;
    538       if (count==j) {
    539         /* just found the end of the field we're looking for */
    540         return(g_strdup(n->z_message+save));
    541       } else {
    542         save=i+1;
    543       }
    544     }
    545   }
    546   /* catch the last field, which might not be null terminated */
    547   if (count==j-1) {
    548     return g_strndup(n->z_message + save, n->z_message_len - save);
    549   }
    550 
    551   return(g_strdup(""));
     566  return owl_zephyr_field(n, owl_zephyr_get_raw_field(n, j));
    552567}
    553568
    554569CALLER_OWN char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j)
    555570{
    556   int i, count, save;
    557 
    558   /* If there's no message here, just run along now */
    559   if (n->z_message_len == 0)
    560     return(g_strdup(""));
    561 
    562   count=save=0;
    563   for (i = 0; i < n->z_message_len; i++) {
    564     if (n->z_message[i]=='\0') {
    565       count++;
    566       if (count == j) {
    567         /* just found the end of the field we're looking for */
    568         return(owl_validate_or_convert(n->z_message + save));
    569       } else {
    570         save = i + 1;
    571       }
    572     }
    573   }
    574   /* catch the last field, which might not be null terminated */
    575   if (count == j - 1) {
    576     char *tmp, *out;
    577     tmp = g_strndup(n->z_message + save, n->z_message_len - save);
    578     out = owl_validate_or_convert(tmp);
    579     g_free(tmp);
    580     return out;
    581   }
    582 
    583   return(g_strdup(""));
    584 }
    585 #else
    586 CALLER_OWN char *owl_zephyr_get_field(void *n, int j)
    587 {
    588   return(g_strdup(""));
    589 }
    590 CALLER_OWN char *owl_zephyr_get_field_as_utf8(void *n, int j)
    591 {
    592   return owl_zephyr_get_field(n, j);
     571  return owl_zephyr_field_as_utf8(n, owl_zephyr_get_raw_field(n, j));
     572}
     573#else
     574const char *owl_zephyr_first_raw_field(const void *n)
     575{
     576  return NULL;
     577}
     578
     579const char *owl_zephyr_next_raw_field(const void *n, const char *f)
     580{
     581  return NULL;
     582}
     583
     584const char *owl_zephyr_get_raw_field(const void *n, int j)
     585{
     586  return NULL;
     587}
     588
     589CALLER_OWN char *owl_zephyr_field(const void *n, const char *f)
     590{
     591  return g_strdup("");
     592}
     593
     594CALLER_OWN char *owl_zephyr_field_as_utf8(const void *n, const char *f)
     595{
     596  return g_strdup("");
     597}
     598
     599CALLER_OWN char *owl_zephyr_get_field(const void *n, int j)
     600{
     601  return g_strdup("");
     602}
     603
     604CALLER_OWN char *owl_zephyr_get_field_as_utf8(const void *n, int j)
     605{
     606  return owl_zephyr_field(n, owl_zephyr_get_raw_field(n, j));
    593607}
    594608#endif
     
    598612int owl_zephyr_get_num_fields(const ZNotice_t *n)
    599613{
    600   int i, fields;
    601 
    602   if(n->z_message_len == 0)
    603     return 0;
    604 
    605   fields=1;
    606   for (i=0; i<n->z_message_len; i++) {
    607     if (n->z_message[i]=='\0') fields++;
    608   }
    609  
    610   return(fields);
     614  int i;
     615  const char *f;
     616  for (i = 0, f = owl_zephyr_first_raw_field(n); f != NULL;
     617       i++, f = owl_zephyr_next_raw_field(n, f))
     618    ;
     619  return i;
    611620}
    612621#else
Note: See TracChangeset for help on using the changeset viewer.