Changeset 864ed35 for style.c


Ignore:
Timestamp:
Apr 29, 2008, 1:21:13 AM (17 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
b67ab6b
Parents:
120291c
Message:
Initial step of moving styles from the current mishmash of different
options to a unified object interface.
No backwards-compatibility support yet.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • style.c

    r5a9f6fe r864ed35  
     1#define OWL_PERL
    12#include "owl.h"
    23
    34static const char fileIdent[] = "$Id$";
    45
    5 void owl_style_create_internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description)
     6void owl_style_create_perl(owl_style *s, char *name, SV *obj)
    67{
    7   s->type=OWL_STYLE_TYPE_INTERNAL;
    88  s->name=owl_strdup(name);
    9   if (description) {
    10     s->description=owl_strdup(description);
    11   } else {
    12     s->description=owl_sprintf("Owl internal style %s", name);
    13   }
    14   s->perlfuncname=NULL;
    15   s->formatfunc=formatfunc;
    16 }
    17 
    18 void owl_style_create_perl(owl_style *s, char *name, char *perlfuncname, char *description)
    19 {
    20   s->type=OWL_STYLE_TYPE_PERL;
    21   s->name=owl_strdup(name);
    22   s->perlfuncname=owl_strdup(perlfuncname);
    23   if (description) {
    24     s->description=owl_strdup(description);
    25   } else {
    26     s->description=owl_sprintf("User-defined perl style that calls %s",
    27                                perlfuncname);
    28   }
    29   s->formatfunc=NULL;
     9  s->perlobj = SvREFCNT_inc(obj);
    3010}
    3111
     
    4323char *owl_style_get_description(owl_style *s)
    4424{
    45   return(s->description);
     25  SV *sv = NULL;
     26  OWL_PERL_CALL_METHOD(s->perlobj,
     27                       "description",
     28                       /* no args */,
     29                       "Error in style_get_description: %s",
     30                       0,
     31                       sv = SvREFCNT_inc(POPs);
     32                       );
     33  if(sv) {
     34    return SvPV_nolen(sv_2mortal(sv));
     35  } else {
     36    return "[error getting description]";
     37  }
    4638}
    4739
     
    5143void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m)
    5244{
    53   if (s->type==OWL_STYLE_TYPE_INTERNAL) {
    54     (* s->formatfunc)(fm, m);
    55   } else if (s->type==OWL_STYLE_TYPE_PERL) {
    56     char *body, *indent;
    57     int curlen;
     45  char *body, *indent;
     46  int curlen;
    5847
    59     /* run the perl function */
    60     body=owl_perlconfig_getmsg(m, 1, s->perlfuncname);
    61     if (!strcmp(body, "")) {
    62       owl_free(body);
    63       body=owl_strdup("<unformatted message>");
    64     }
    65    
    66     /* indent and ensure ends with a newline */
    67     indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10);
    68     owl_text_indent(indent, body, OWL_TAB);
    69     curlen = strlen(indent);
    70     if (curlen==0 || indent[curlen-1] != '\n') {
    71       indent[curlen] = '\n';
    72       indent[curlen+1] = '\0';
    73     }
     48  SV *sv = NULL;
     49 
     50  /* Call the perl object */
     51  OWL_PERL_CALL_METHOD(s->perlobj,
     52                       "format_message",
     53                       XPUSHs(owl_perlconfig_message2hashref(m));,
     54                       "Error in format_message: %s",
     55                       0,
     56                       sv = SvREFCNT_inc(POPs);
     57                       );
    7458
    75     /* fmtext_append.  This needs to change */
    76     owl_fmtext_append_ztext(fm, indent);
    77    
    78     owl_free(indent);
    79     owl_free(body);
     59  if(sv) {
     60    body = SvPV_nolen(sv);
     61  } else {
     62    body = "<unformatted message>";
    8063  }
     64
     65  /* indent and ensure ends with a newline */
     66  indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10);
     67  owl_text_indent(indent, body, OWL_TAB);
     68  curlen = strlen(indent);
     69  if (curlen==0 || indent[curlen-1] != '\n') {
     70    indent[curlen] = '\n';
     71    indent[curlen+1] = '\0';
     72  }
     73
     74  /* fmtext_append.  This needs to change */
     75  owl_fmtext_append_ztext(fm, indent);
     76
     77  owl_free(indent);
     78  if(sv)
     79    SvREFCNT_dec(body);
    8180}
    8281
    8382int owl_style_validate(owl_style *s) {
    84   if (!s) {
    85     return -1;
    86   } else if (s->type==OWL_STYLE_TYPE_INTERNAL) {
    87     return 0;
    88   } else if (s->type==OWL_STYLE_TYPE_PERL
    89              && s->perlfuncname
    90              && owl_perlconfig_is_function(s->perlfuncname)) {
    91     return 0;
    92   } else {
     83  if (!s || !s->perlobj || !SvOK(s->perlobj)) {
    9384    return -1;
    9485  }
     86  return 0;
    9587}
    9688
     
    9890{
    9991  if (s->name) owl_free(s->name);
    100   if (s->description) owl_free(s->description);
    101   if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) {
    102     owl_free(s->perlfuncname);
    103   }
     92  SvREFCNT_dec(s->perlobj);
    10493}
Note: See TracChangeset for help on using the changeset viewer.