[864ed35] | 1 | #define OWL_PERL |
---|
[bd3f232] | 2 | #include "owl.h" |
---|
| 3 | |
---|
[e19eb97] | 4 | void owl_style_create_perl(owl_style *s, const char *name, SV *obj) |
---|
[bd3f232] | 5 | { |
---|
| 6 | s->name=owl_strdup(name); |
---|
[864ed35] | 7 | s->perlobj = SvREFCNT_inc(obj); |
---|
[bd3f232] | 8 | } |
---|
| 9 | |
---|
[1fdab04] | 10 | int owl_style_matches_name(const owl_style *s, const char *name) |
---|
[bd3f232] | 11 | { |
---|
| 12 | if (!strcmp(s->name, name)) return(1); |
---|
| 13 | return(0); |
---|
| 14 | } |
---|
| 15 | |
---|
[1fdab04] | 16 | const char *owl_style_get_name(const owl_style *s) |
---|
[ef56a67] | 17 | { |
---|
| 18 | return(s->name); |
---|
| 19 | } |
---|
| 20 | |
---|
[1fdab04] | 21 | const char *owl_style_get_description(const owl_style *s) |
---|
[f1e629d] | 22 | { |
---|
[864ed35] | 23 | SV *sv = NULL; |
---|
| 24 | OWL_PERL_CALL_METHOD(s->perlobj, |
---|
| 25 | "description", |
---|
| 26 | /* no args */, |
---|
| 27 | "Error in style_get_description: %s", |
---|
| 28 | 0, |
---|
| 29 | sv = SvREFCNT_inc(POPs); |
---|
| 30 | ); |
---|
| 31 | if(sv) { |
---|
| 32 | return SvPV_nolen(sv_2mortal(sv)); |
---|
| 33 | } else { |
---|
| 34 | return "[error getting description]"; |
---|
| 35 | } |
---|
[f1e629d] | 36 | } |
---|
| 37 | |
---|
[5639bf2] | 38 | /* Use style 's' to format message 'm' into fmtext 'fm'. |
---|
| 39 | * 'fm' should already be be initialzed |
---|
| 40 | */ |
---|
[1fdab04] | 41 | void owl_style_get_formattext(const owl_style *s, owl_fmtext *fm, const owl_message *m) |
---|
[bd3f232] | 42 | { |
---|
[e19eb97] | 43 | const char *body; |
---|
[65b2173] | 44 | char *indent; |
---|
[864ed35] | 45 | int curlen; |
---|
[bd3f232] | 46 | |
---|
[864ed35] | 47 | SV *sv = NULL; |
---|
| 48 | |
---|
| 49 | /* Call the perl object */ |
---|
| 50 | OWL_PERL_CALL_METHOD(s->perlobj, |
---|
| 51 | "format_message", |
---|
[1639ff7] | 52 | XPUSHs(sv_2mortal(owl_perlconfig_message2hashref(m)));, |
---|
[864ed35] | 53 | "Error in format_message: %s", |
---|
| 54 | 0, |
---|
| 55 | sv = SvREFCNT_inc(POPs); |
---|
| 56 | ); |
---|
[282ec9b] | 57 | |
---|
[864ed35] | 58 | if(sv) { |
---|
| 59 | body = SvPV_nolen(sv); |
---|
| 60 | } else { |
---|
| 61 | body = "<unformatted message>"; |
---|
[bd3f232] | 62 | } |
---|
[864ed35] | 63 | |
---|
| 64 | /* indent and ensure ends with a newline */ |
---|
| 65 | indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10); |
---|
| 66 | owl_text_indent(indent, body, OWL_TAB); |
---|
| 67 | curlen = strlen(indent); |
---|
| 68 | if (curlen==0 || indent[curlen-1] != '\n') { |
---|
| 69 | indent[curlen] = '\n'; |
---|
| 70 | indent[curlen+1] = '\0'; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /* fmtext_append. This needs to change */ |
---|
| 74 | owl_fmtext_append_ztext(fm, indent); |
---|
| 75 | |
---|
| 76 | owl_free(indent); |
---|
| 77 | if(sv) |
---|
[e574a00] | 78 | SvREFCNT_dec(sv); |
---|
[bd3f232] | 79 | } |
---|
| 80 | |
---|
[1fdab04] | 81 | int owl_style_validate(const owl_style *s) { |
---|
[864ed35] | 82 | if (!s || !s->perlobj || !SvOK(s->perlobj)) { |
---|
[f1e629d] | 83 | return -1; |
---|
| 84 | } |
---|
[864ed35] | 85 | return 0; |
---|
[f1e629d] | 86 | } |
---|
| 87 | |
---|
[bd3f232] | 88 | void owl_style_free(owl_style *s) |
---|
| 89 | { |
---|
| 90 | if (s->name) owl_free(s->name); |
---|
[864ed35] | 91 | SvREFCNT_dec(s->perlobj); |
---|
[bd3f232] | 92 | } |
---|