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