Changeset 8f95fc4


Ignore:
Timestamp:
Dec 25, 2013, 2:45:30 PM (8 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Children:
58777e9
Parents:
7dcef03
git-author:
Anders Kaseorg <andersk@mit.edu> (12/25/13 12:17:55)
git-committer:
Anders Kaseorg <andersk@mit.edu> (12/25/13 14:45:30)
Message:
Use GData for message attributes

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • message.c

    r7dcef03 r8f95fc4  
    3939
    4040  owl_message_set_hostname(m, "");
    41   m->attributes = g_ptr_array_new();
     41  g_datalist_init(&m->attributes);
    4242 
    4343  /* save the time */
     
    5454void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue)
    5555{
    56   int i;
    57   owl_pair *p = NULL, *pair = NULL;
    58 
    59   attrname = g_intern_string(attrname);
    60 
    61   /* look for an existing pair with this key, */
    62   for (i = 0; i < m->attributes->len; i++) {
    63     p = m->attributes->pdata[i];
    64     if (owl_pair_get_key(p) == attrname) {
    65       g_free(owl_pair_get_value(p));
    66       pair = p;
    67       break;
    68     }
    69   }
    70 
    71   if(pair ==  NULL) {
    72     pair = g_slice_new(owl_pair);
    73     owl_pair_create(pair, attrname, NULL);
    74     g_ptr_array_add(m->attributes, pair);
    75   }
    76   owl_pair_set_value(pair, owl_validate_or_convert(attrvalue));
     56  g_datalist_set_data_full(&m->attributes, attrname,
     57                           owl_validate_or_convert(attrvalue), g_free);
    7758}
    7859
     
    8263const char *owl_message_get_attribute_value(const owl_message *m, const char *attrname)
    8364{
    84   int i;
    85   owl_pair *p;
    86   GQuark quark;
    87 
    88   quark = g_quark_try_string(attrname);
    89   if (quark == 0)
    90     /* don't bother inserting into string table */
    91     return NULL;
    92   attrname = g_quark_to_string(quark);
    93 
    94   for (i = 0; i < m->attributes->len; i++) {
    95     p = m->attributes->pdata[i];
    96     if (owl_pair_get_key(p) == attrname) {
    97       return(owl_pair_get_value(p));
    98     }
    99   }
    100 
    101   /*
    102   owl_function_debugmsg("No attribute %s found for message %i",
    103                         attrname,
    104                         owl_message_get_id(m));
    105   */
    106   return(NULL);
     65  return g_datalist_get_data(&((owl_message *)m)->attributes, attrname);
    10766}
    10867
     
    11170 * function to indent fmtext.
    11271 */
    113 void owl_message_attributes_tofmtext(const owl_message *m, owl_fmtext *fm) {
    114   int i;
    115   owl_pair *p;
     72static void owl_message_attribute_tofmtext(GQuark key_id, gpointer data, gpointer fm)
     73{
    11674  char *buff, *tmpbuff;
    11775
     76  tmpbuff = owl_text_indent(data, 19, false);
     77  buff = g_strdup_printf("  %-15.15s: %s\n", g_quark_to_string(key_id), tmpbuff);
     78  g_free(tmpbuff);
     79  owl_fmtext_append_normal(fm, buff);
     80  g_free(buff);
     81}
     82
     83void owl_message_attributes_tofmtext(const owl_message *m, owl_fmtext *fm)
     84{
    11885  owl_fmtext_init_null(fm);
    119 
    120   for (i = 0; i < m->attributes->len; i++) {
    121     p = m->attributes->pdata[i];
    122 
    123     buff = g_strdup(owl_pair_get_value(p));
    124     if (buff) {
    125       tmpbuff = owl_text_indent(buff, 19, false);
    126       g_free(buff);
    127       buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), tmpbuff);
    128       g_free(tmpbuff);
    129     }
    130 
    131     if(buff == NULL) {
    132       buff = g_strdup_printf("  %-15.15s: %s\n", owl_pair_get_key(p), "<error>");
    133       if(buff == NULL)
    134         buff=g_strdup("   <error>\n");
    135     }
    136     owl_fmtext_append_normal(fm, buff);
    137     g_free(buff);
    138   }
     86  g_datalist_foreach(&((owl_message *)m)->attributes, owl_message_attribute_tofmtext, fm);
    13987}
    14088
     
    1006954void owl_message_cleanup(owl_message *m)
    1007955{
    1008   int i;
    1009   owl_pair *p;
    1010956#ifdef HAVE_LIBZEPHYR   
    1011957  if (m->has_notice) {
     
    1016962
    1017963  /* free all the attributes */
    1018   for (i = 0; i < m->attributes->len; i++) {
    1019     p = m->attributes->pdata[i];
    1020     g_free(owl_pair_get_value(p));
    1021     g_slice_free(owl_pair, p);
    1022   }
    1023 
    1024   g_ptr_array_free(m->attributes, true);
     964  g_datalist_clear(&m->attributes);
    1025965 
    1026966  owl_message_invalidate_format(m);
  • owl.h

    rca1fb26a r8f95fc4  
    356356  int delete;
    357357  const char *hostname;
    358   GPtrArray *attributes;          /* this is a list of pairs */
     358  GData *attributes;
    359359  char *timestr;
    360360  time_t time;
  • perlconfig.c

    r7dcef03 r8f95fc4  
    6969}
    7070
     71static void owl_perlconfig_store_attribute(GQuark key_id, gpointer data, gpointer h)
     72{
     73  (void)hv_store(h, g_quark_to_string(key_id), strlen(data),
     74                 owl_new_sv(data), 0);
     75}
     76
    7177CALLER_OWN SV *owl_perlconfig_message2hashref(const owl_message *m)
    7278{
     
    7682  char *ptr, *utype, *blessas;
    7783  const char *f;
    78   int i;
    79   const owl_pair *pair;
    8084  const owl_filter *wrap;
    8185
     
    112116  }
    113117
    114   for (i = 0; i < m->attributes->len; i++) {
    115     pair = m->attributes->pdata[i];
    116     (void)hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)),
    117                    owl_new_sv(owl_pair_get_value(pair)),0);
    118   }
     118  g_datalist_foreach(&((owl_message *)m)->attributes, owl_perlconfig_store_attribute, h);
    119119 
    120120  MSG2H(h, type);
Note: See TracChangeset for help on using the changeset viewer.