Changeset a03a409 for zephyr.c


Ignore:
Timestamp:
Mar 11, 2012, 10:58:00 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.9
Children:
d199207
Parents:
97cdbaf5
git-author:
Anders Kaseorg <andersk@mit.edu> (01/19/12 21:22:29)
git-committer:
David Benjamin <davidben@mit.edu> (03/11/12 22:58:00)
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

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