author | Myhailo Danylenko <isbear@isbear.org.ua> |
Fri, 09 Dec 2016 03:14:57 +0200 | |
changeset 93 | d59df8a9413d |
parent 48 | 307314cdbb39 |
permissions | -rw-r--r-- |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1 |
# HG changeset patch |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
2 |
# Parent 00b93fdbb1de8e77b6992cf799715c1da3e61746 |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
3 |
[work-in-progress] Use templates for statusbars |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
4 |
|
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
5 |
diff -r 00b93fdbb1de mcabber/CMakeLists.txt |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
6 |
--- a/mcabber/CMakeLists.txt Thu Aug 09 00:54:37 2012 +0300 |
48 | 7 |
+++ b/mcabber/CMakeLists.txt Tue Aug 28 20:37:28 2012 +0300 |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
8 |
@@ -154,8 +154,8 @@ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
9 |
|
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
10 |
## Define targets |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
11 |
set ( mcabber_SUBSYSTEMS |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
12 |
- caps commands compl events hbuf help histolog hooks |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
13 |
- modules nohtml otr pgp roster screen settings utf8 utils |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
14 |
+ caps commands compl events hbuf help histolog hooks modules |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
15 |
+ nohtml otr parser pgp roster screen settings templates utf8 utils |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
16 |
xmpp xmpp_helper xmpp_iq xmpp_iqrequest xmpp_muc xmpp_s10n ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
17 |
if ( NOT MODULES_ENABLE ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
18 |
list ( APPEND mcabber_SUBSYSTEMS extcmd fifo ) |
48 | 19 |
diff -r 00b93fdbb1de mcabber/mcabber/hooks.c |
20 |
--- a/mcabber/mcabber/hooks.c Thu Aug 09 00:54:37 2012 +0300 |
|
21 |
+++ b/mcabber/mcabber/hooks.c Tue Aug 28 20:37:28 2012 +0300 |
|
22 |
@@ -612,6 +612,10 @@ |
|
23 |
ns[0] = imstatus2char[new_status]; |
|
24 |
||
25 |
hk_run_handlers(HOOK_MY_STATUS_CHANGE, args); |
|
26 |
+ |
|
27 |
+ // FIXME not when modules_enable, but when templates_enable |
|
28 |
+ settings_set(SETTINGS_TYPE_OPTION, "my_status", ns); |
|
29 |
+ settings_tmpl_set("my_message", msg); |
|
30 |
} |
|
31 |
#endif |
|
32 |
||
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
33 |
diff -r 00b93fdbb1de mcabber/mcabber/parser.c |
45 | 34 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
48 | 35 |
+++ b/mcabber/mcabber/parser.c Tue Aug 28 20:37:28 2012 +0300 |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
36 |
@@ -0,0 +1,1116 @@ |
45 | 37 |
+ |
38 |
+/* Copyright 2012 Myhailo Danylenko |
|
39 |
+ * |
|
40 |
+ * This file is part of mcabber |
|
41 |
+ * |
|
42 |
+ * mcabber is free software: you can redistribute it and/or |
|
43 |
+ * modify it under the terms of the GNU General Public License as published |
|
44 |
+ * by the Free Software Foundation, either version 2 of the License, or |
|
45 |
+ * (at your option) any later version. |
|
46 |
+ * |
|
47 |
+ * This program is distributed in the hope that it will be useful, |
|
48 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
49 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
50 |
+ * GNU General Public License for more details. |
|
51 |
+ * |
|
52 |
+ * You should have received a copy of the GNU General Public License |
|
53 |
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
54 |
+ |
|
55 |
+#include <glib.h> |
|
56 |
+ |
|
57 |
+#include <ctype.h> // is* |
|
58 |
+#include <stdlib.h> // strtol |
|
59 |
+#include <string.h> // strchr |
|
60 |
+ |
|
61 |
+#include "parser.h" |
|
62 |
+ |
|
63 |
+#include "config.h" |
|
64 |
+ |
|
65 |
+// |
|
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
66 |
+// Common |
45 | 67 |
+// |
68 |
+ |
|
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
69 |
+// XXX one for all 'mcabber' quark? to utils? |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
70 |
+GQuark tmpl_gerror_quark ( void ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
71 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
72 |
+ return g_quark_from_static_string ( "tmpl_gerror_quark" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
73 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
74 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
75 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
76 |
+// Globs |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
77 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
78 |
+ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
79 |
+// match tmpl_glob ( string, pattern, greedy, right-to-left ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
80 |
+// Performs matching of given pattern against given string, |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
81 |
+// returns first substring in input, that matched. In pattern, |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
82 |
+// there are currently two tokens recognized: '*' and '?'. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
83 |
+const gchar *tmpl_glob ( const char *str, gsize length, const char *pat, gsize patlen, gboolean rtl, gsize *ret_len ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
84 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
85 |
+ *ret_len = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
86 |
+ if ( rtl ) { // rtl |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
87 |
+ const char *p = pat + patlen - 1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
88 |
+ const char *s = str + length - 1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
89 |
+ const char *matchend = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
90 |
+ const char *patrollback = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
91 |
+ const char *strrollback = s; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
92 |
+ while ( p >= pat && s >= str ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
93 |
+ if ( *p == '*' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
94 |
+ if ( matchend == NULL && patrollback != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
95 |
+ matchend = strrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
96 |
+ patrollback = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
97 |
+ strrollback = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
98 |
+ p --; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
99 |
+ } else if ( *p == '?' || *p == *s ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
100 |
+ if ( patrollback == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
101 |
+ patrollback = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
102 |
+ strrollback = s; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
103 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
104 |
+ p --; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
105 |
+ s --; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
106 |
+ } else if ( patrollback != NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
107 |
+ p = patrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
108 |
+ s = -- strrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
109 |
+ } else { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
110 |
+ s --; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
111 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
112 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
113 |
+ // now s, p or both have finished run |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
114 |
+ if ( s >= str && patrollback == NULL ) // s remains and final star |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
115 |
+ s = str - 1; // value at the end of loop |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
116 |
+ if ( p >= pat ) { // p remains |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
117 |
+ while ( *p == '*' && p >= pat ) // ignore empty stars |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
118 |
+ p --; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
119 |
+ if ( p >= pat ) // symbols remain in pattern, no match |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
120 |
+ return NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
121 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
122 |
+ if ( matchend == NULL && patrollback != NULL ) // no stars |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
123 |
+ matchend = strrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
124 |
+ if ( matchend != NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
125 |
+ *ret_len = matchend - s; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
126 |
+ return s + 1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
127 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
128 |
+ return NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
129 |
+ } else { // ltr |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
130 |
+ const char * const pe = pat + patlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
131 |
+ const char * const se = str + length; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
132 |
+ const char *p = pat; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
133 |
+ const char *s = str; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
134 |
+ const char *matchstart = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
135 |
+ const char *patrollback = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
136 |
+ const char *strrollback = s; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
137 |
+ while ( p < pe && s < se ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
138 |
+ if ( *p == '*' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
139 |
+ if ( matchstart == NULL && patrollback != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
140 |
+ matchstart = strrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
141 |
+ patrollback = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
142 |
+ strrollback = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
143 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
144 |
+ } else if ( *p == '?' || *p == *s ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
145 |
+ if ( patrollback == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
146 |
+ patrollback = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
147 |
+ strrollback = s; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
148 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
149 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
150 |
+ s ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
151 |
+ } else if ( patrollback != NULL ) { // start/nomatch-recovery eat |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
152 |
+ p = patrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
153 |
+ s = ++ strrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
154 |
+ } else { // star-eat |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
155 |
+ s ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
156 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
157 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
158 |
+ // now s, p or both have finished run |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
159 |
+ if ( s < se && patrollback == NULL ) // s remains and final star |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
160 |
+ s = se; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
161 |
+ if ( p < pe ) { // p remains |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
162 |
+ while ( *p == '*' && p < pe ) // ignore empty stars |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
163 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
164 |
+ if ( p < pe ) // symbols remain in pattern, no match |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
165 |
+ return NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
166 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
167 |
+ if ( matchstart == NULL && patrollback != NULL ) // no stars |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
168 |
+ matchstart = strrollback; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
169 |
+ if ( matchstart != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
170 |
+ *ret_len = s - matchstart; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
171 |
+ return matchstart; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
172 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
173 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
174 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
175 |
+// match tmpl_greedy_glob ( string, pattern, right-to-left ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
176 |
+// The same as above, but greedy. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
177 |
+const gchar *tmpl_greedy_glob ( const char *str, gsize length, const char *pat, gsize patlen, gboolean rtl, gsize *ret_len ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
178 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
179 |
+ const char * const pe = pat + patlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
180 |
+ const char *p = pat; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
181 |
+ while ( p < pe ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
182 |
+ if ( *p == '*' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
183 |
+ gsize llen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
184 |
+ const char *left = tmpl_glob ( str, length, pat, p - pat, FALSE, &llen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
185 |
+ if ( left != NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
186 |
+ gsize rlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
187 |
+ const char *right = tmpl_glob ( left + llen, str + length - ( left + llen ), p + 1, pe - p - 1, TRUE, &rlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
188 |
+ if ( right != NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
189 |
+ *ret_len = right + rlen - left; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
190 |
+ return left; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
191 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
192 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
193 |
+ return NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
194 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
195 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
196 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
197 |
+ return tmpl_glob ( str, length, pat, patlen, rtl, ret_len ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
198 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
199 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
200 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
201 |
+// Simple math expressions with variables |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
202 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
203 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
204 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
205 |
+ no_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
206 |
+ noempty_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
207 |
+ paren_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
208 |
+ plus_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
209 |
+ minus_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
210 |
+ remainder_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
211 |
+ division_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
212 |
+ multiplication_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
213 |
+} math_operator_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
214 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
215 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
216 |
+ in_preargument, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
217 |
+ in_hexoctzero, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
218 |
+ in_number, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
219 |
+ in_variable, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
220 |
+ in_postparen, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
221 |
+ in_op, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
222 |
+} math_parser_state_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
223 |
+ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
224 |
+// - + * / % ( ) |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
225 |
+static gssize tmpl_math_expand_internal ( const char *str, gsize length, tmpl_variable_callback_t get_val, gpointer udata, GError **error, math_operator_t bound, gsize *proc_len ) |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
226 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
227 |
+ g_assert ( error != NULL && *error == NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
228 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
229 |
+ gssize result = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
230 |
+ const char * p = str; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
231 |
+ const char * const e = str + length; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
232 |
+ math_parser_state_t state = in_preargument; |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
233 |
+ const char * varname = NULL; // variable name start |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
234 |
+ gsize base = 10; // number base |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
235 |
+ gboolean invert = FALSE; // number change sign |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
236 |
+ gboolean errstate = bound >= noempty_op; // indicates erroneous state |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
237 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
238 |
+ // process first argument |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
239 |
+ while ( p < e && *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
240 |
+ if ( state == in_preargument ) { // initial space-skipping state |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
241 |
+ if ( isspace ( *p ) ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
242 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
243 |
+ else if ( *p == '-' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
244 |
+ invert = ! invert; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
245 |
+ errstate = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
246 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
247 |
+ } else if ( *p == '+' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
248 |
+ errstate = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
249 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
250 |
+ } else if ( *p == '(' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
251 |
+ gsize processed = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
252 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
253 |
+ result = tmpl_math_expand_internal ( p, e - p, get_val, udata, error, paren_op, &processed ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
254 |
+ if ( *error == NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
255 |
+ state = in_postparen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
256 |
+ errstate = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
257 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
258 |
+ } else if ( *p == '0' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
259 |
+ result = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
260 |
+ errstate = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
261 |
+ state = in_hexoctzero; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
262 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
263 |
+ } else if ( isdigit ( *p ) ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
264 |
+ result = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
265 |
+ base = 10; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
266 |
+ state = in_number; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
267 |
+ } else if ( isalpha ( *p ) || *p == '_' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
268 |
+ state = in_variable; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
269 |
+ errstate = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
270 |
+ varname = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
271 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
272 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
273 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EMATHWRONGARG, "Wrong symbol in math argument" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
274 |
+ } else if ( state == in_hexoctzero ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
275 |
+ if ( *p == 'x' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
276 |
+ errstate = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
277 |
+ base = 16; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
278 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
279 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
280 |
+ base = 8; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
281 |
+ state = in_number; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
282 |
+ } else if ( state == in_number ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
283 |
+ short d = -1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
284 |
+ if ( *p == '0' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
285 |
+ d = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
286 |
+ else if ( *p == '1' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
287 |
+ d = 1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
288 |
+ else if ( *p == '2' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
289 |
+ d = 2; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
290 |
+ else if ( *p == '3' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
291 |
+ d = 3; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
292 |
+ else if ( *p == '4' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
293 |
+ d = 4; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
294 |
+ else if ( *p == '5' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
295 |
+ d = 5; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
296 |
+ else if ( *p == '6' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
297 |
+ d = 6; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
298 |
+ else if ( *p == '7' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
299 |
+ d = 7; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
300 |
+ else if ( base <= 8 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
301 |
+ ; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
302 |
+ else if ( *p == '8' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
303 |
+ d = 8; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
304 |
+ else if ( *p == '9' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
305 |
+ d = 9; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
306 |
+ else if ( base <= 10 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
307 |
+ ; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
308 |
+ else if ( *p == 'a' || *p == 'A' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
309 |
+ d = 10; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
310 |
+ else if ( *p == 'b' || *p == 'B' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
311 |
+ d = 11; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
312 |
+ else if ( *p == 'c' || *p == 'C' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
313 |
+ d = 12; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
314 |
+ else if ( *p == 'd' || *p == 'D' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
315 |
+ d = 13; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
316 |
+ else if ( *p == 'e' || *p == 'E' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
317 |
+ d = 14; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
318 |
+ else if ( *p == 'f' || *p == 'F' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
319 |
+ d = 15; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
320 |
+ if ( d >= 0 ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
321 |
+ errstate = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
322 |
+ result = result * base + d; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
323 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
324 |
+ } else if ( ! errstate ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
325 |
+ state = in_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
326 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
327 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EMATHWRONGNUMBER, "Too short math number" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
328 |
+ } else if ( state == in_variable ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
329 |
+ if ( ! ( isalnum ( *p ) || *p == '_' ) ) { // XXX vars with '-' have to be expanded earlier |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
330 |
+ gsize vlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
331 |
+ const char * value = get_val ( varname, p - varname, udata, &vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
332 |
+ // TODO control recursion level |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
333 |
+ result = tmpl_math_expand_internal ( value, vlen, get_val, udata, error, no_op, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
334 |
+ if ( *error == NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
335 |
+ state = in_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
336 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
337 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
338 |
+ } else if ( state == in_postparen ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
339 |
+ if ( *p == ')' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
340 |
+ errstate = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
341 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
342 |
+ state = in_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
343 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
344 |
+ g_assert_not_reached (); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
345 |
+ } else if ( state == in_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
346 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
347 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
348 |
+ g_assert_not_reached (); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
349 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
350 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
351 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
352 |
+ // in_preargument - empty argument, possibly +-+- - 0 / errstate |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
353 |
+ // in_hexoctzero - 0 - 0 |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
354 |
+ // in_number - 0, 0x, 01, 0x1, 1 - failure at 0x (errstate), invert |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
355 |
+ // in_variable - varname - get var, invert |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
356 |
+ // in_postparen - eof - failure (errstate) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
357 |
+ // in_op - ok - invert, continue parsing |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
358 |
+ if ( errstate ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
359 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EMATHWRONGSTATE, "Wrong state at the end of first math argument parsing" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
360 |
+ else { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
361 |
+ if ( state == in_op ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
362 |
+ if ( invert ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
363 |
+ result = - result; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
364 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
365 |
+ // process operator+second argument pairs |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
366 |
+ while ( p < e && *error == NULL ) { |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
367 |
+ math_operator_t op = no_op; |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
368 |
+ if ( isspace ( *p ) ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
369 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
370 |
+ else if ( *p == '+' ) // set op |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
371 |
+ op = plus_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
372 |
+ else if ( *p == '-' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
373 |
+ op = minus_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
374 |
+ else if ( *p == '/' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
375 |
+ op = division_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
376 |
+ else if ( *p == '*' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
377 |
+ op = multiplication_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
378 |
+ else if ( *p == '%' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
379 |
+ op = remainder_op; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
380 |
+ else if ( *p == ')' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
381 |
+ if ( bound < paren_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
382 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EMATHUNBALPAREN, "Unbalanced parens in math expression" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
383 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
384 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
385 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
386 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EMATHWRONGOP, "Wrong math operator '%c'", *p ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
387 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
388 |
+ if ( op != no_op ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
389 |
+ // lower priority operator, returning result |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
390 |
+ if ( op < bound ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
391 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
392 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
393 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
394 |
+ gsize processed = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
395 |
+ gssize arg = tmpl_math_expand_internal ( p, e - p, get_val, udata, error, op, &processed ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
396 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
397 |
+ if ( op == plus_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
398 |
+ result += arg; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
399 |
+ else if ( op == minus_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
400 |
+ result -= arg; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
401 |
+ else if ( op == division_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
402 |
+ result /= arg; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
403 |
+ else if ( op == multiplication_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
404 |
+ result *= arg; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
405 |
+ else if ( op == remainder_op ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
406 |
+ result %= arg; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
407 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
408 |
+ g_assert_not_reached (); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
409 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
410 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
411 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
412 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
413 |
+ } else if ( state == in_variable ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
414 |
+ gsize vlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
415 |
+ const char * value = get_val ( varname, p - varname, udata, &vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
416 |
+ result = tmpl_math_expand_internal ( value, vlen, get_val, udata, error, no_op, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
417 |
+ if ( *error == NULL && invert ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
418 |
+ result = - result; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
419 |
+ } else if ( invert ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
420 |
+ result = - result; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
421 |
+ // XXX may add long conditional with g_assert here, but I'm lazy |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
422 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
423 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
424 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
425 |
+ // return result |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
426 |
+ if ( proc_len != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
427 |
+ *proc_len = p - str; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
428 |
+ return result; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
429 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
430 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
431 |
+// result tmpl_math_expand ( plainexpanded string, callback ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
432 |
+// Performs mathematical expansion on given string. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
433 |
+// Note, that it does not perform variable/quote/etc expansion, |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
434 |
+// if you want it, you should do that first. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
435 |
+gssize tmpl_math_expand ( const char *str, gsize length, tmpl_variable_callback_t get_val, gpointer udata, GError **ret_err ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
436 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
437 |
+ g_return_val_if_fail ( ret_err == NULL || *ret_err == NULL, -1 ); // XXX |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
438 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
439 |
+ GError * error = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
440 |
+ gssize result = tmpl_math_expand_internal ( str, length, get_val, udata, &error, no_op, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
441 |
+ if ( error != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
442 |
+ g_propagate_error ( ret_err, error ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
443 |
+ return result; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
444 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
445 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
446 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
447 |
+// Shell-like variable substitution |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
448 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
449 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
450 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
451 |
+ in_string, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
452 |
+ in_escape, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
453 |
+ in_var, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
454 |
+ in_plainvarstart, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
455 |
+ in_plainvar, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
456 |
+ in_prevarname, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
457 |
+ in_varnamestart, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
458 |
+ in_varname, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
459 |
+ in_varchop, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
460 |
+ in_varstrip, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
461 |
+ in_varstripchop, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
462 |
+ in_varslash, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
463 |
+ in_varmatch, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
464 |
+ in_varpresubst, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
465 |
+ in_varsubst, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
466 |
+ in_varcolon, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
467 |
+ in_varminus, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
468 |
+ in_varplus, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
469 |
+ in_varpos, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
470 |
+ in_postvarpos, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
471 |
+ in_varlen, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
472 |
+ in_varsubstring, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
473 |
+ in_varend, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
474 |
+ in_evalstart, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
475 |
+ in_mathexpand, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
476 |
+ in_dblparen_eol, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
477 |
+ in_varif, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
478 |
+ in_postvarif, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
479 |
+ in_varelse, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
480 |
+ nostate, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
481 |
+} var_parser_state_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
482 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
483 |
+const static char * const var_parser_statenames [ ] = { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
484 |
+ "in string", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
485 |
+ "in escape", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
486 |
+ "in var", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
487 |
+ "in plainvarstart", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
488 |
+ "in plainvar", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
489 |
+ "in prevarname", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
490 |
+ "in varnamestart", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
491 |
+ "in varname", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
492 |
+ "in varchop", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
493 |
+ "in varstrip", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
494 |
+ "in varstripchop", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
495 |
+ "in varslash", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
496 |
+ "in varmatch", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
497 |
+ "in varpresubst", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
498 |
+ "in varsubst", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
499 |
+ "in varcolon", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
500 |
+ "in varminus", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
501 |
+ "in varplus", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
502 |
+ "in varpos", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
503 |
+ "in postvarpos", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
504 |
+ "in varlen", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
505 |
+ "in varsubstring", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
506 |
+ "in varend", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
507 |
+ "in evalstart", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
508 |
+ "in mathexpand", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
509 |
+ "in dblparen eol", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
510 |
+ "in_varif", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
511 |
+ "in_postvarif", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
512 |
+ "in_varelse", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
513 |
+ "wrong state", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
514 |
+}; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
515 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
516 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
517 |
+ no_eol = 0x00, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
518 |
+ brace_eol = 0x01, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
519 |
+ slash_eol = 0x02, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
520 |
+ colon_eol = 0x04, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
521 |
+ paren_eol = 0x08, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
522 |
+ dblparen_eol = 0x10, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
523 |
+ balance_parens = 0x20, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
524 |
+} var_parser_flags_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
525 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
526 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
527 |
+ no_operation, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
528 |
+ expand_var, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
529 |
+ value_length, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
530 |
+} var_operation_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
531 |
+ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
532 |
+// expansion tmpl_expand ( template, callback ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
533 |
+// Parse template, substitute shell-like expressions: |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
534 |
+// * $var ${var} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
535 |
+// * ${var##pat} ${var#pat} ${var%%pat} ${var%pat} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
536 |
+// * ${var/pat} ${var/pat/repl} ${var//pat/repl} ${var/%pat/repl} ${var/#pat/repl} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
537 |
+// * ${var:+tmpl} ${var:-tmpl} ${var?tmpl:tmpl} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
538 |
+// * ${var:math} ${var:math:math} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
539 |
+// * ${#var} ${!var} ${!var[operation]} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
540 |
+// * $(( math )) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
541 |
+// * \n \t \e \$ \\ \X |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
542 |
+// Callback will be called to obtain variable values. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
543 |
+// Variable name should be: /^[[:alnum:]][[:alnum:]_-]*$/ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
544 |
+// For pattern rules see tmpl_glob (). |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
545 |
+// For math rules see tmpl_math_expand (). |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
546 |
+// TODO: |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
547 |
+// * validate variable name in ${!var} expression |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
548 |
+// * de-hardcode variable name validation rules (or allow pure-numeric names) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
549 |
+static gchar *tmpl_var_expand_internal ( const char *template, const char * const e, tmpl_variable_callback_t get_val, gpointer udata, gsize *ret_len, GError **error, var_parser_flags_t flags, gsize *proc_len ) |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
550 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
551 |
+ g_assert ( error != NULL && *error == NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
552 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
553 |
+ if ( template == NULL || len == 0 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
554 |
+ return NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
555 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
556 |
+ GString *result = g_string_new ( NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
557 |
+ const char *p = template; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
558 |
+ var_parser_state_t state = in_string; |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
559 |
+ const char *a1s = p; // string, varname |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
560 |
+ const gchar *value = NULL; // variable value |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
561 |
+ gsize vlen = 0; // variable value length |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
562 |
+ var_operation_t operation = no_operation; |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
563 |
+ gchar *pattern = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
564 |
+ gsize plen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
565 |
+ gboolean rtl = FALSE; // strip/chop |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
566 |
+ gboolean greedy = FALSE; // strip/chop |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
567 |
+ gboolean multiglob = FALSE; // replace |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
568 |
+ gsize parencount = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
569 |
+ gssize subsoffset = -1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
570 |
+ gssize subslength = -1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
571 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
572 |
+ while ( p < e && *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
573 |
+ if ( state == in_string ) { // nothing special |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
574 |
+ if ( *p == '\\' ) { // escape next char |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
575 |
+ g_string_append_len ( result, a1s, p - a1s ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
576 |
+ state = in_escape; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
577 |
+ } else if ( *p == '$' ) { // start variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
578 |
+ g_string_append_len ( result, a1s, p - a1s ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
579 |
+ state = in_var; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
580 |
+ } else if ( flags & balance_parens && *p == '(' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
581 |
+ parencount ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
582 |
+ } else if ( *p == ')' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
583 |
+ if ( flags & balance_parens && parencount > 0 ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
584 |
+ parencount --; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
585 |
+ } else if ( flags & dblparen_eol ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
586 |
+ g_string_append_len ( result, a1s, p - a1s ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
587 |
+ state = in_dblparen_eol; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
588 |
+ } else if ( flags & balance_parens ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
589 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EUNBALPAREN, "Unbalanced parentheses" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
590 |
+ } else if ( ( flags & brace_eol && *p == '}' ) || |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
591 |
+ ( flags & slash_eol && *p == '/' ) || |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
592 |
+ ( flags & colon_eol && parencount == 0 && *p == ':' ) ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
593 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
594 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
595 |
+ } else if ( state == in_escape ) { // escape (on escaped char) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
596 |
+ if ( *p == 'n' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
597 |
+ g_string_append_c ( result, '\n' ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
598 |
+ else if ( *p == 't' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
599 |
+ g_string_append_c ( result, '\t' ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
600 |
+ else if ( *p == 'e' ) // for some experimentation with colors... |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
601 |
+ g_string_append_c ( result, '\033' ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
602 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
603 |
+ g_string_append_c ( result, *p ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
604 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
605 |
+ state = in_string; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
606 |
+ a1s = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
607 |
+ } else if ( state == in_var ) { // some variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
608 |
+ if ( *p == '{' ) { // enclosed variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
609 |
+ state = in_prevarname; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
610 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
611 |
+ } else if ( *p == '(' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
612 |
+ state = in_evalstart; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
613 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
614 |
+ } else // unenclosed variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
615 |
+ state = in_plainvarstart; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
616 |
+ } else if ( state == in_plainvarstart ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
617 |
+ if ( isalpha ( *p ) || *p == '_' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
618 |
+ state = in_plainvar; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
619 |
+ a1s = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
620 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
621 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
622 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGVARNAME, "Wrong symbol at the start of variable name" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
623 |
+ } else if ( state == in_plainvar ) { // unenclosed variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
624 |
+ if ( ! ( isalnum ( *p ) || *p == '-' || *p == '_' ) ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
625 |
+ gsize vlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
626 |
+ const gchar *value = get_val ( a1s, p - a1s, udata, &vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
627 |
+ g_string_append_len ( result, value, vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
628 |
+ state = in_string; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
629 |
+ a1s = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
630 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
631 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
632 |
+ } else if ( state == in_prevarname ) { // allow ! and # at varname start |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
633 |
+ if ( *p == '!' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
634 |
+ operation = expand_var; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
635 |
+ p++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
636 |
+ } else if ( *p == '#' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
637 |
+ operation = value_length; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
638 |
+ p++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
639 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
640 |
+ operation = no_operation; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
641 |
+ state = in_varnamestart; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
642 |
+ } else if ( state == in_varnamestart ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
643 |
+ if ( isalpha ( *p ) || *p == '_' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
644 |
+ state = in_varname; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
645 |
+ a1s = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
646 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
647 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
648 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGVARNAME, "Wrong symbol at start of variable name" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
649 |
+ } else if ( state == in_varname ) { // enclosed variable name |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
650 |
+ if ( ! ( isalnum ( *p ) || *p == '-' || *p == '_' ) ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
651 |
+ value = get_val ( a1s, p - a1s, udata, &vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
652 |
+ if ( operation == value_length ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
653 |
+ if ( *p == '}' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
654 |
+ g_string_append_printf ( result, "%lu", vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
655 |
+ state = in_varend; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
656 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
657 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGVAREXP, "Wrong variable expansion expression" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
658 |
+ } else { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
659 |
+ if ( operation == expand_var && value != NULL && vlen > 0 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
660 |
+ value = get_val ( value, vlen, udata, &vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
661 |
+ if ( *p == '}' ) { // end of variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
662 |
+ g_string_append_len ( result, value, vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
663 |
+ state = in_varend; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
664 |
+ } else if ( *p == '#' ) { // strip expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
665 |
+ state = in_varstrip; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
666 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
667 |
+ } else if ( *p == '%' ) { // chop expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
668 |
+ state = in_varchop; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
669 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
670 |
+ } else if ( *p == '/' ) { // replace expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
671 |
+ state = in_varslash; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
672 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
673 |
+ } else if ( *p == ':' ) { // substring expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
674 |
+ state = in_varcolon; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
675 |
+ p ++; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
676 |
+ } else if ( *p == '?' ) { // conditional expression |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
677 |
+ state = in_varif; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
678 |
+ p ++; |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
679 |
+ } else // wrong symbols |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
680 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGVAREXP, "Wrong variable expansion expression" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
681 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
682 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
683 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
684 |
+ } else if ( state == in_varstrip ) { // one of strip expressions |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
685 |
+ if ( *p == '#' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
686 |
+ greedy = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
687 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
688 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
689 |
+ rtl = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
690 |
+ state = in_varstripchop; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
691 |
+ } else if ( state == in_varchop ) { // one of chop expressions |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
692 |
+ if ( *p == '%' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
693 |
+ greedy = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
694 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
695 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
696 |
+ rtl = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
697 |
+ state = in_varstripchop; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
698 |
+ } else if ( state == in_varstripchop ) { // pattern expressions |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
699 |
+ gsize elen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
700 |
+ gsize processed = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
701 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, brace_eol, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
702 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
703 |
+ gsize mlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
704 |
+ const char *match = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
705 |
+ if ( greedy ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
706 |
+ match = tmpl_greedy_glob ( value, vlen, expansion, elen, rtl, &mlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
707 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
708 |
+ match = tmpl_glob ( value, vlen, expansion, elen, rtl, &mlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
709 |
+ if ( mlen > 0 ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
710 |
+ if ( rtl ) { // % and %% |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
711 |
+ if ( match + mlen == value + vlen ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
712 |
+ vlen -= mlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
713 |
+ } else if ( match == value ) { // # and ## |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
714 |
+ value = match + mlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
715 |
+ vlen -= mlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
716 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
717 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
718 |
+ g_string_append_len ( result, value, vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
719 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
720 |
+ g_free ( expansion ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
721 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
722 |
+ state = in_varend; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
723 |
+ } else if ( state == in_varslash ) { // replace expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
724 |
+ multiglob = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
725 |
+ rtl = FALSE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
726 |
+ if ( *p == '#' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
727 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
728 |
+ else if ( *p == '%' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
729 |
+ rtl = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
730 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
731 |
+ } else if ( *p == '/' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
732 |
+ multiglob = TRUE; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
733 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
734 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
735 |
+ state = in_varmatch; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
736 |
+ } else if ( state == in_varmatch ) { // match part of replace expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
737 |
+ gsize processed = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
738 |
+ pattern = tmpl_var_expand_internal ( p, e, get_val, udata, &plen, error, brace_eol | slash_eol, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
739 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
740 |
+ state = in_varpresubst; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
741 |
+ } else if ( state == in_varpresubst ) { // skip slash |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
742 |
+ if ( *p == '/' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
743 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
744 |
+ state = in_varsubst; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
745 |
+ } else if ( state == in_varsubst ) { // replace expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
746 |
+ gsize processed = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
747 |
+ gsize slen = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
748 |
+ gchar *subst = tmpl_var_expand_internal ( p, e, get_val, udata, &slen, error, brace_eol, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
749 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
750 |
+ const char * start = value; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
751 |
+ const char * const ve = value + vlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
752 |
+ while ( start < ve ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
753 |
+ gsize mlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
754 |
+ const char *match = tmpl_glob ( start, ve - start, pattern, plen, rtl, &mlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
755 |
+ if ( mlen > 0 ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
756 |
+ g_string_append_len ( result, start, match - start ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
757 |
+ g_string_append_len ( result, subst, slen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
758 |
+ start = match + mlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
759 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
760 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
761 |
+ if ( ! multiglob ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
762 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
763 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
764 |
+ g_string_append_len ( result, start, ve - start ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
765 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
766 |
+ g_free ( subst ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
767 |
+ g_free ( pattern ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
768 |
+ pattern = NULL; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
769 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
770 |
+ state = in_varend; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
771 |
+ } else if ( state == in_varcolon ) { // substring or substitution expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
772 |
+ if ( *p == '-' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
773 |
+ state = in_varminus; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
774 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
775 |
+ } else if ( *p == '+' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
776 |
+ state = in_varplus; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
777 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
778 |
+ } else { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
779 |
+ subsoffset = -1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
780 |
+ subslength = -1; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
781 |
+ state = in_varpos; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
782 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
783 |
+ } else if ( state == in_varminus || state == in_varplus ) { // zero and non-zero substitution |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
784 |
+ gsize elen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
785 |
+ gsize processed = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
786 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, brace_eol, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
787 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
788 |
+ if ( ( vlen == 0 && state == in_varminus ) || ( vlen > 0 && state == in_varplus ) ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
789 |
+ g_string_append_len ( result, expansion, elen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
790 |
+ else if ( vlen > 0 && state == in_varminus ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
791 |
+ g_string_append_len ( result, value, vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
792 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
793 |
+ g_free ( expansion ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
794 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
795 |
+ state = in_varend; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
796 |
+ } else if ( state == in_varpos ) { // substring expression |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
797 |
+ gsize elen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
798 |
+ gsize processed = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
799 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, colon_eol | brace_eol | balance_parens, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
800 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
801 |
+ subsoffset = tmpl_math_expand_internal ( expansion, elen, get_val, udata, error, no_op, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
802 |
+ if ( subsoffset < 0 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
803 |
+ subsoffset = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
804 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
805 |
+ g_free ( expansion ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
806 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
807 |
+ state = in_postvarpos; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
808 |
+ } else if ( state == in_postvarpos ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
809 |
+ if ( *p == ':' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
810 |
+ state = in_varlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
811 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
812 |
+ } else if ( *p == '}' ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
813 |
+ state = in_varsubstring; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
814 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
815 |
+ g_assert_not_reached (); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
816 |
+ } else if ( state == in_varlen ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
817 |
+ gsize elen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
818 |
+ gsize processed = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
819 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, brace_eol | balance_parens, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
820 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
821 |
+ subslength = tmpl_math_expand_internal ( expansion, elen, get_val, udata, error, no_op, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
822 |
+ if ( subslength < 0 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
823 |
+ subslength = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
824 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
825 |
+ g_free ( expansion ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
826 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
827 |
+ state = in_varsubstring; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
828 |
+ } else if ( state == in_varsubstring ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
829 |
+ g_assert ( subsoffset >= 0 ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
830 |
+ if ( subslength == -1 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
831 |
+ subslength = vlen; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
832 |
+ if ( subsoffset < vlen ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
833 |
+ if ( subsoffset + subslength > vlen ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
834 |
+ g_string_append_len ( result, value + subsoffset, vlen - subsoffset ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
835 |
+ else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
836 |
+ g_string_append_len ( result, value + subsoffset, subslength ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
837 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
838 |
+ state = in_varend; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
839 |
+ } else if ( state == in_varif ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
840 |
+ gsize elen = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
841 |
+ gsize processed = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
842 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, colon_eol | brace_eol, &processed ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
843 |
+ if ( *error == NULL && vlen != 0 ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
844 |
+ g_string_append_len ( result, expansion, elen ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
845 |
+ g_free ( expansion ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
846 |
+ p += processed; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
847 |
+ state = in_postvarif; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
848 |
+ } else if ( state == in_postvarif ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
849 |
+ if ( *p == ':' ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
850 |
+ state = in_varelse; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
851 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
852 |
+ } else if ( *p == '}' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
853 |
+ state = in_varend; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
854 |
+ else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
855 |
+ g_assert_not_reached (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
856 |
+ } else if ( state == in_varelse ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
857 |
+ gsize elen = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
858 |
+ gsize processed = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
859 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, brace_eol, &processed ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
860 |
+ if ( *error == NULL && vlen == 0 ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
861 |
+ g_string_append_len ( result, expansion, elen ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
862 |
+ g_free ( expansion ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
863 |
+ p += processed; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
864 |
+ state = in_varend; |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
865 |
+ } else if ( state == in_varend ) { // end of enclosed variable |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
866 |
+ if ( *p == '}' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
867 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
868 |
+ state = in_string; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
869 |
+ a1s = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
870 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
871 |
+ g_assert_not_reached (); // this state must be used only on '}' or EOL |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
872 |
+ } else if ( state == in_evalstart ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
873 |
+ if ( *p == '(' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
874 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
875 |
+ state = in_mathexpand; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
876 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
877 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EUNBALPAREN, "Command evaluation is not supported" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
878 |
+ } else if ( state == in_mathexpand ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
879 |
+ gsize elen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
880 |
+ gsize processed = 0; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
881 |
+ gchar *expansion = tmpl_var_expand_internal ( p, e, get_val, udata, &elen, error, dblparen_eol | balance_parens, &processed ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
882 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
883 |
+ gssize value = tmpl_math_expand_internal ( expansion, elen, get_val, udata, error, no_op, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
884 |
+ if ( *error == NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
885 |
+ g_string_append_printf ( result, "%li", value ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
886 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
887 |
+ g_free ( expansion ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
888 |
+ p += processed; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
889 |
+ state = in_string; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
890 |
+ a1s = p; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
891 |
+ } else if ( state == in_dblparen_eol ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
892 |
+ if ( *p == ')' ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
893 |
+ p ++; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
894 |
+ break; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
895 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
896 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EUNBALPAREN, "Unbalanced parentheses" ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
897 |
+ } else |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
898 |
+ g_assert_not_reached (); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
899 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
900 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
901 |
+ if ( *error == NULL ) { |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
902 |
+ if ( state == in_plainvar ) { // plain variable at the end of template |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
903 |
+ gsize vlen = 0; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
904 |
+ const gchar *value = get_val ( a1s, p - a1s, udata, &vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
905 |
+ g_string_append_len ( result, value, vlen ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
906 |
+ } else if ( state == in_string ) // end of string |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
907 |
+ g_string_append_len ( result, a1s, p - a1s ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
908 |
+ else if ( state != in_dblparen_eol ) |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
909 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGENDSTATE, "Parser stopped in incorrect state '%s'", var_parser_statenames [ state < nostate ? state : nostate ] ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
910 |
+ } |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
911 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
912 |
+ if ( pattern != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
913 |
+ g_free ( pattern ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
914 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
915 |
+ if ( proc_len != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
916 |
+ *proc_len = p - template; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
917 |
+ if ( ret_len != NULL ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
918 |
+ *ret_len = result -> len; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
919 |
+ return g_string_free ( result, FALSE ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
920 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
921 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
922 |
+// public wrapper |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
923 |
+gchar *tmpl_var_expand ( const char *template, gsize len, tmpl_variable_callback_t get_val, gpointer udata, gsize *ret_len, GError **ret_err ) |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
924 |
+{ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
925 |
+ g_return_val_if_fail ( ret_err == NULL || *ret_err == NULL, NULL ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
926 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
927 |
+ GError * error = NULL; |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
928 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
929 |
+ gchar * result = tmpl_var_expand_internal ( template, template + len, get_val, udata, ret_len, &error, no_eol, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
930 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
931 |
+ if ( error != NULL ) { |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
932 |
+ g_propagate_error ( ret_err, error ); |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
933 |
+ g_free ( result ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
934 |
+ return NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
935 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
936 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
937 |
+ return result; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
938 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
939 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
940 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
941 |
+// Simple markup language |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
942 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
943 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
944 |
+/* |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
945 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
946 |
+ ml_color, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
947 |
+ ml_fill_right, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
948 |
+ ml_fill_left, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
949 |
+ ml_fill_both, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
950 |
+} markup_link_type_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
951 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
952 |
+typedef guint markup_color_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
953 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
954 |
+typedef struct { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
955 |
+ markup_link_type_t type; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
956 |
+ markup_color_t color; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
957 |
+ const char * s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
958 |
+ gssize len; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
959 |
+ gboolean permanent; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
960 |
+ guint width; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
961 |
+} markup_link_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
962 |
+*/ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
963 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
964 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
965 |
+ mp_in_string, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
966 |
+ mp_in_percent, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
967 |
+ mp_in_color, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
968 |
+ mp_nostate, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
969 |
+} markup_parser_state_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
970 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
971 |
+const static char * const markup_parser_statenames [ ] = { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
972 |
+ "in string", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
973 |
+ "in percent", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
974 |
+ "in color", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
975 |
+ "wrong state", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
976 |
+}; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
977 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
978 |
+void tmpl_markup_free ( markup_link_t * link ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
979 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
980 |
+ markup_link_t * current = link; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
981 |
+ while ( current != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
982 |
+ markup_link_t * tmp = current -> next; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
983 |
+ if ( link -> permanent ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
984 |
+ g_free ( link -> s ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
985 |
+ g_slice_free ( markup_link_t, current ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
986 |
+ current = tmp; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
987 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
988 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
989 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
990 |
+markup_link_t * link tmpl_markup_permanentize ( markup_link_t * link ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
991 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
992 |
+ markup_link_t * current; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
993 |
+ for ( current = link; current -> next != NULL; current = current -> next ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
994 |
+ current -> s = g_strndup ( current -> s, current -> s - current -> e ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
995 |
+ current -> permanent = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
996 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
997 |
+ return link; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
998 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
999 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1000 |
+static tmpl_markup_link_t * tmpl_append_markup_link ( markup_link_t *last, markup_link_type_t type, markup_color_t color, const gchar * s, const gchar * e ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1001 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1002 |
+ if ( type == ml_color ) { // it is not splitter |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1003 |
+ if ( last -> s == NULL || last -> len == 0 ) { // append to empty string -- overwrite color & string |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1004 |
+ last -> s = s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1005 |
+ last -> len = e - s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1006 |
+ last -> color = color; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1007 |
+ return last; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1008 |
+ } else if ( last -> color == color ) { // color unchanged |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1009 |
+ if ( s == e ) // appending empty string - do nothing |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1010 |
+ return last; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1011 |
+ else if ( last -> s + last -> len == s ) { // appending continuation of current string |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1012 |
+ last -> len += e - s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1013 |
+ return last; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1014 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1015 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1016 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1017 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1018 |
+ tmpl_markup_link_t * link = g_slice_new ( markup_link_t ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1019 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1020 |
+ link -> type = type; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1021 |
+ link -> color = color; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1022 |
+ link -> s = s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1023 |
+ link -> len = e - s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1024 |
+ link -> permanent = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1025 |
+ link -> next = last -> next; // allow insertion |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1026 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1027 |
+ last -> next = link; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1028 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1029 |
+ return link; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1030 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1031 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1032 |
+static markup_link_t * tmpl_markup_expand_internal ( markup_link_t * chain, const char * template, const char * const e, tmpl_shortcut_callback_t get_shortcut, gpointer udata, GError **error ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1033 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1034 |
+ tmpl_markup_link_t * last = chain; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1035 |
+ markup_parser_state_t state = mp_in_string; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1036 |
+ const char * p = template; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1037 |
+ const char * s = template; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1038 |
+ markup_color_t color = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1039 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1040 |
+ while ( p < e && *error == NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1041 |
+ if ( state == mp_in_string ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1042 |
+ if ( *p == '%' ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1043 |
+ last = tmpl_append_markup_link ( last, ml_color, last -> color, s, p - s ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1044 |
+ state = mp_in_percent; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1045 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1046 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1047 |
+ } else if ( state == mp_in_percent ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1048 |
+ if ( *p == '%' ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1049 |
+ s = p; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1050 |
+ state = mp_in_string |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1051 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1052 |
+ } else if ( isalpha ( *p ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1053 |
+ gsize slen = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1054 |
+ const char * shortcut = get_shortcut ( *p, &slen, udata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1055 |
+ last = tmpl_markup_expand_internal ( last, shortcut, shortcut + slen, get_shortcut, udata, error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1056 |
+ state = mp_in_string; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1057 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1058 |
+ } else if ( isdigit ( *p ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1059 |
+ color = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1060 |
+ state = mp_in_color; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1061 |
+ } else if ( *p == '<' ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1062 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1063 |
+ last = tmpl_append_markup_link ( last, ml_fill_left, last -> color, NULL, 0 ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1064 |
+ s = p; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1065 |
+ state = mp_in_string; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1066 |
+ } else if ( *p == '|' ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1067 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1068 |
+ last = tmpl_append_markup_link ( last, ml_fill_both, last -> color, NULL, 0 ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1069 |
+ s = p; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1070 |
+ state = mp_in_string; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1071 |
+ } else if ( *p == '>' ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1072 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1073 |
+ last = tmpl_append_markup_link ( last, ml_fill_right, last -> color, NULL, 0 ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1074 |
+ s = p; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1075 |
+ state = mp_in_string; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1076 |
+ } else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1077 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGMARKUP, "Wrong symbol in markup expression: '%c'", *p ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1078 |
+ } else if ( state == mp_in_color ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1079 |
+ short d = -1; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1080 |
+ if ( *p == '0' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1081 |
+ d = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1082 |
+ else if ( *p == '1' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1083 |
+ d = 1; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1084 |
+ else if ( *p == '2' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1085 |
+ d = 2; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1086 |
+ else if ( *p == '3' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1087 |
+ d = 3; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1088 |
+ else if ( *p == '4' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1089 |
+ d = 4; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1090 |
+ else if ( *p == '5' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1091 |
+ d = 5; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1092 |
+ else if ( *p == '6' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1093 |
+ d = 6; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1094 |
+ else if ( *p == '7' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1095 |
+ d = 7; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1096 |
+ else if ( *p == '8' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1097 |
+ d = 8; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1098 |
+ else if ( *p == '9' ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1099 |
+ d = 9; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1100 |
+ if ( d >= 0 ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1101 |
+ color = color * 10 + d; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1102 |
+ p ++; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1103 |
+ } else { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1104 |
+ last = tmpl_append_markup_link ( last, ml_color, color, NULL, 0 ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1105 |
+ s = p; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1106 |
+ state = mp_in_string; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1107 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1108 |
+ } else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1109 |
+ g_assert_not_reached (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1110 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1111 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1112 |
+ if ( error == NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1113 |
+ if ( state == mp_in_string ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1114 |
+ last = tmpl_append_markup_chunk ( last, ml_color, last -> color, s, e ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1115 |
+ else if ( state == mp_in_color ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1116 |
+ last = tmpl_append_markup_link ( last, ml_color, color, NULL, 0 ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1117 |
+ else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1118 |
+ g_set_error ( error, TMPL_GERROR_QUARK, TMPL_EWRONGMARKUP, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1119 |
+ "Markup parser ended in a wrong state '%s'", |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1120 |
+ markup_parser_statenames [ state < mp_nostate ? state : mp_nostate ] ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1121 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1122 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1123 |
+ return last; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1124 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1125 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1126 |
+// public wrapper |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1127 |
+markup_link_t * tmpl_markup_expand ( const char *template, gsize len, tmpl_chunk_callback_t get_chunk, gpointer udata, GError **ret_err ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1128 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1129 |
+ g_return_val_if_fail ( ret_err == NULL || *ret_err == NULL, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1130 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1131 |
+ GError * error = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1132 |
+ markup_link_t * result = g_slice_new ( tmpl_markup_link_t ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1133 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1134 |
+ result -> type = ml_color; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1135 |
+ result -> color = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1136 |
+ result -> s = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1137 |
+ result -> len = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1138 |
+ result -> permanent = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1139 |
+ result -> next = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1140 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1141 |
+ tmpl_markup_expand_internal ( result, template, template + len, get_chunk, udata, &error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1142 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1143 |
+ if ( error != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1144 |
+ g_propagate_error ( ret_err, error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1145 |
+ tmpl_markup_free ( result ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1146 |
+ return NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1147 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1148 |
+ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1149 |
+ return result; |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1150 |
+} |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1151 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1152 |
+/* vim: se ts=4 sw=4: */ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1153 |
diff -r 00b93fdbb1de mcabber/mcabber/parser.h |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1154 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
48 | 1155 |
+++ b/mcabber/mcabber/parser.h Tue Aug 28 20:37:28 2012 +0300 |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1156 |
@@ -0,0 +1,154 @@ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1157 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1158 |
+#ifndef MCABBER_PARSER_H |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1159 |
+#define MCABBER_PARSER_H 1 |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1160 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1161 |
+/* Copyright 2012 Myhailo Danylenko |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1162 |
+ * |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1163 |
+ * This file is part of mcabber |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1164 |
+ * |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1165 |
+ * mcabber free software: you can redistribute it and/or |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1166 |
+ * modify it under the terms of the GNU General Public License as published |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1167 |
+ * by the Free Software Foundation, either version 2 of the License, or |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1168 |
+ * (at your option) any later version. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1169 |
+ * |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1170 |
+ * This program is distributed in the hope that it will be useful, |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1171 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1172 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1173 |
+ * GNU General Public License for more details. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1174 |
+ * |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1175 |
+ * You should have received a copy of the GNU General Public License |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1176 |
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1177 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1178 |
+#include <glib.h> |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1179 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1180 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1181 |
+// Design notes: |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1182 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1183 |
+// Please, keep this part of code free of direct mcabber internals references, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1184 |
+// as a sort of in-tree library. This way it is much easier to test and debug. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1185 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1186 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1187 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1188 |
+// Typedefs |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1189 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1190 |
+ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1191 |
+#define TMPL_GERROR_QUARK tmpl_gerror_quark () |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1192 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1193 |
+#define TMPL_EWRONGVAREXP ( 0x01 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1194 |
+#define TMPL_EWRONGVARNAME ( 0x03 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1195 |
+#define TMPL_EUNBALPAREN ( 0x04 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1196 |
+#define TMPL_EWRONGENDSTATE ( 0x02 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1197 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1198 |
+#define TMPL_EMATHWRONGARG ( 0x05 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1199 |
+#define TMPL_EMATHWRONGNUMBER ( 0x06 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1200 |
+#define TMPL_EMATHUNBALPAREN ( 0x07 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1201 |
+#define TMPL_EMATHWRONGOP ( 0x08 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1202 |
+#define TMPL_EMATHWRONGSTATE ( 0x09 ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1203 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1204 |
+#define TMPL_EWRONGMARKUP ( 0x0A ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1205 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1206 |
+typedef const char *(*tmpl_variable_callback_t) ( const gchar *name, gsize len, gpointer udata, gsize *ret_len ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1207 |
+typedef const char *(*tmpl_shortcut_callback_t) ( const gchar name, gpointer udata, gsize *ret_len ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1208 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1209 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1210 |
+ ml_color, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1211 |
+ ml_fill_right, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1212 |
+ ml_fill_left, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1213 |
+ ml_fill_both, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1214 |
+} markup_link_type_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1215 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1216 |
+typedef guint markup_color_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1217 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1218 |
+typedef struct { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1219 |
+ markup_link_type_t type; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1220 |
+ markup_color_t color; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1221 |
+ const char * s; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1222 |
+ gsize len; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1223 |
+ gboolean permanent; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1224 |
+ gsize width; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1225 |
+} markup_link_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1226 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1227 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1228 |
+// Common |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1229 |
+// |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1230 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1231 |
+GQuark tmpl_gerror_quark ( void ); |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1232 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1233 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1234 |
+// Simple glob matching |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1235 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1236 |
+ |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1237 |
+// match tmpl_glob ( string, pattern, right-to-left ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1238 |
+// Performs matching of given pattern against given string, |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1239 |
+// returns substring, that matched. In pattern, there are currently |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1240 |
+// two tokens recognized: '*' and '?'. Two flags determine, if |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1241 |
+// '*' should be greedy and which end of string must match the |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1242 |
+// pattern (i.e. have anchor). |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1243 |
+const char * tmpl_glob ( const char * str, gsize strlen, const char * pat, gsize patlen, gboolean rtl, gsize * ret_len ); |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1244 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1245 |
+// match tmpl_greedy_glob ( string, pattern, rigt-to-left ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1246 |
+// The same, as above, but greedy. |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1247 |
+const char * tmpl_greedy_glob ( const char * str, gsize strlen, const char * pat, gsize patlen, gboolean rtl, gsize * ret_len ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1248 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1249 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1250 |
+// Math expansion |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1251 |
+// |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1252 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1253 |
+// result tmpl_math_expand ( string, callback ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1254 |
+// Performs mathematical expansion of given string. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1255 |
+// Note, that $var expressions are not recognized, you have to |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1256 |
+// supply already expanded string here. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1257 |
+// Supported operators: |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1258 |
+// ( ) + - * / % |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1259 |
+gssize tmpl_math_expand ( const char * str, gsize len, tmpl_variable_callback_t get_val, gpointer udata, GError ** error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1260 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1261 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1262 |
+// Variable expansion |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1263 |
+// |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1264 |
+ |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1265 |
+// expansion tmpl_expand ( template, callback ) |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1266 |
+// Parse template, substitute shell-like expressions: |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1267 |
+// * $var ${var} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1268 |
+// * ${var##pat} ${var#pat} ${var%%pat} ${var%pat} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1269 |
+// * ${var/pat} ${var/pat/repl} ${var//pat/repl} ${var/%pat/repl} ${var/#pat/repl} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1270 |
+// * ${var:+tmpl} ${var:-tmpl} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1271 |
+// * ${var:mathexp} ${var:mathexp:mathexp} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1272 |
+// * ${#var} ${!var} ${!var[operation]} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1273 |
+// * $(( mathexp )) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1274 |
+// * \n \t \e \$ \\ \X |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1275 |
+// Callback will be called to obtain variable values. |
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1276 |
+// Variable name should be: /^[[:alnum:]][[:alnum:]_-]*$/ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1277 |
+// For pattern rules see tmpl_glob (). |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1278 |
+// For math expansion rules see tmpl_math_expand (). |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1279 |
+// Returned value should be g_free'd. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1280 |
+gchar *tmpl_var_expand ( const char * str, gsize len, tmpl_variable_callback_t get_val, gpointer udata, gsize *ret_len, GError ** error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1281 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1282 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1283 |
+// Simple markup parsing |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1284 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1285 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1286 |
+// tmpl_markup_link_free ( chain ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1287 |
+// Frees markup links chain (and associated strings, if permanentized). |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1288 |
+void tmpl_markup_free ( markup_link_t * chain ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1289 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1290 |
+// chain tmpl_markup_permanentize ( chain ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1291 |
+// Strdups strings, used in markup to make them independent of chunk expansions used. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1292 |
+markup_link_t * tmpl_markup_permanentize ( markup_link_t * chain ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1293 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1294 |
+// chain tmpl_markup_expand ( template, callback ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1295 |
+// Parses simple markup language: |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1296 |
+// * %% - '%' symbol |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1297 |
+// * %a - %Z - insert shortcut with this name (expand it as well) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1298 |
+// * %123 - text after this will be marked with color 123 |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1299 |
+// * %> %< %| - text padding/trimming marks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1300 |
+// Returns a chain of markup_link_t structs. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1301 |
+// String pointers in links can be NULL. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1302 |
+// String pointers in links point to parts of template or returned by shortcut callback values, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1303 |
+// so, take care not to modify/free any of them, while using chain. Alternatively, you can |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1304 |
+// tmpl_markup_permanentize () markup. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1305 |
+// Width fields are reserved for user, they are not set. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1306 |
+// User have to free returned markup chain with tmpl_markup_free (). |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1307 |
+markup_link_t * tmpl_markup_expand ( const char * str, gsize len, tmpl_shortcut_callback_t get_chunk, gpointer udata, GError ** error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1308 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1309 |
+#endif |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1310 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1311 |
diff -r 00b93fdbb1de mcabber/mcabber/settings.c |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1312 |
--- a/mcabber/mcabber/settings.c Thu Aug 09 00:54:37 2012 +0300 |
48 | 1313 |
+++ b/mcabber/mcabber/settings.c Tue Aug 28 20:37:28 2012 +0300 |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1314 |
@@ -363,6 +363,32 @@ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1315 |
return 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1316 |
} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1317 |
|
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1318 |
+// settings_tmpl_set(option, value) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1319 |
+// Sets option, escaping it for use in templates. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1320 |
+// Could be more efficient with notifiers instead of guards. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1321 |
+void settings_tmpl_set(const gchar *key, const gchar *value) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1322 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1323 |
+ if (value && *value) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1324 |
+ const gchar *p = strchr(value, '%'); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1325 |
+ if (p) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1326 |
+ GString *result = g_string_new(NULL); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1327 |
+ const gchar *s = value; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1328 |
+ while (p) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1329 |
+ g_string_append_len(result, s, p - s + 1); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1330 |
+ g_string_append_c(result, '%'); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1331 |
+ s = ++ p; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1332 |
+ p = strchr(s, '%'); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1333 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1334 |
+ if (*s) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1335 |
+ g_string_append(result, s); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1336 |
+ settings_set(SETTNIGS_TYPE_OPTION, key, result -> str); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1337 |
+ g_string_free(result, TRUE); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1338 |
+ return; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1339 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1340 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1341 |
+ settings_set ( SETTNIGS_TYPE_OPTION, key, value ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1342 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1343 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1344 |
// settings_get_status_msg(status) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1345 |
// Return a string with the current status message: |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1346 |
// - if there is a user-defined message ("message" option), |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1347 |
diff -r 00b93fdbb1de mcabber/mcabber/settings.h |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1348 |
--- a/mcabber/mcabber/settings.h Thu Aug 09 00:54:37 2012 +0300 |
48 | 1349 |
+++ b/mcabber/mcabber/settings.h Tue Aug 28 20:37:28 2012 +0300 |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1350 |
@@ -37,6 +37,7 @@ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1351 |
void settings_opt_set_raw(const gchar *key, const gchar *value); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1352 |
void settings_set(guint type, const gchar *key, const gchar *value); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1353 |
void settings_del(guint type, const gchar *key); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1354 |
+void settings_tmpl_set(const gchar *key, const gchar *value); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1355 |
const gchar *settings_get(guint type, const gchar *key); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1356 |
int settings_get_int(guint type, const gchar *key); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1357 |
const gchar *settings_get_status_msg(enum imstatus status); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1358 |
diff -r 00b93fdbb1de mcabber/mcabber/templates.c |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1359 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
48 | 1360 |
+++ b/mcabber/mcabber/templates.c Tue Aug 28 20:37:28 2012 +0300 |
1361 |
@@ -0,0 +1,467 @@ |
|
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1362 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1363 |
+/* Copyright 2012 Myhailo Danylenko |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1364 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1365 |
+ * This file is part of mcabber |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1366 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1367 |
+ * mcabber is free software: you can redistribute it and/or |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1368 |
+ * modify it under the terms of the GNU General Public License as published |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1369 |
+ * by the Free Software Foundation, either version 2 of the License, or |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1370 |
+ * (at your option) any later version. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1371 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1372 |
+ * This program is distributed in the hope that it will be useful, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1373 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1374 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1375 |
+ * GNU General Public License for more details. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1376 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1377 |
+ * You should have received a copy of the GNU General Public License |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1378 |
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1379 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1380 |
+#include <glib.h> |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1381 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1382 |
+#include <string.h> // strlen |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1383 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1384 |
+#include "logprint.h" |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1385 |
+#include "settings.h" |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1386 |
+#include "main.h" // main_context |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1387 |
+#include "templates.h" |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1388 |
+#include "parser.h" |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1389 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1390 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1391 |
+// globals |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1392 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1393 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1394 |
+typedef enum { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1395 |
+ markup_template, // fully processed $ and % templates |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1396 |
+ shortcut_template, // no-callback $-only but %-propagating templates |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1397 |
+ var_template, // $-only templates with callback |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1398 |
+} template_type_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1399 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1400 |
+typedef union { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1401 |
+ tmpl_var_callback_t var; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1402 |
+ tmpl_markup_callback_t markup; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1403 |
+} template_callback_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1404 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1405 |
+typedef struct { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1406 |
+ gchar * name; // variable name |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1407 |
+ template_type_t type; // can be only one (for simplicity) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1408 |
+ template_callback_t callback; // |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1409 |
+ gpointer userdata; // userdata for callback |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1410 |
+ GSList * guards; // var-guards |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1411 |
+ gboolean in_use; // to mark markup & var templates for drop |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1412 |
+ gboolean var_changed; // var-reevaluate flag |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1413 |
+ gboolean markup_changed; // markup-reevaluate flag |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1414 |
+ gboolean run_callback; // rerun callback flag |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1415 |
+ gchar * var_expansion; // var-expansion of template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1416 |
+ GSList * templates; // markup templates, that use this shortcut template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1417 |
+ GSList * shortcuts; // shortcut templates in use by this markup template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1418 |
+} template_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1419 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1420 |
+typedef struct { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1421 |
+ gchar * name; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1422 |
+ GSList * templates; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1423 |
+} tmpl_guard_t; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1424 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1425 |
+static GHashTable * tmpl_templates = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1426 |
+static GHashTable * tmpl_guards = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1427 |
+static guint tmpl_attached_id = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1428 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1429 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1430 |
+// predeclarations |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1431 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1432 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1433 |
+static gchar *tmpl_guard ( const gchar *key, const gchar *new_value ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1434 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1435 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1436 |
+// code |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1437 |
+// |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1438 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1439 |
+// [cb] drops template from guard's 'templates' list |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1440 |
+static void tmpl_unguard ( gpointer data, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1441 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1442 |
+ tmpl_guard_t *guard = data; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1443 |
+ template_t *template = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1444 |
+ guard -> templates = g_slist_remove ( guard -> templates, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1445 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1446 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1447 |
+// [cb] drops template from shortcut's 'templates' list |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1448 |
+static void tmpl_unshortcut ( gpointer data, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1449 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1450 |
+ template_t * shortcut = data; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1451 |
+ template_t * template = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1452 |
+ shortcut -> templates = g_slist_remove ( shortcut -> templates, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1453 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1454 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1455 |
+// [destructor cb] releases guard hash table entry |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1456 |
+static void tmpl_free_guard ( gpointer data ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1457 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1458 |
+ tmpl_guard_t * guard = data; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1459 |
+ settings_del_guard ( guard -> name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1460 |
+ g_slist_free ( guard -> templates ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1461 |
+ g_free ( guard -> name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1462 |
+ g_slice_free ( tmpl_guard_t, guard ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1463 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1464 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1465 |
+// [destructor cb] releases taken guards and frees command |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1466 |
+static void tmpl_free_template ( gpointer data ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1467 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1468 |
+ template_t * template = data; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1469 |
+ // not running unshortcut, as it was done earlier |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1470 |
+ // or it is global destruction and chunk may no longer exist |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1471 |
+ g_slist_free ( template -> shortcuts ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1472 |
+ g_slist_free ( template -> templates ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1473 |
+ g_slist_foreach ( template -> guards, tmpl_unguard, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1474 |
+ g_slist_free ( template -> guards ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1475 |
+ g_free ( template -> name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1476 |
+ g_free ( template -> var_expansion ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1477 |
+ g_slice_free ( template_t, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1478 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1479 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1480 |
+// [cb] sets var-changed flag on template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1481 |
+static void template_set_var_changed ( gpointer data, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1482 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1483 |
+ template_t * template = data; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1484 |
+ template -> var_changed = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1485 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1486 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1487 |
+// [cb] sets markup-changed flag on template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1488 |
+static void template_set_markup_changed ( gpointer data, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1489 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1490 |
+ template_t * template = data; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1491 |
+ template -> markup_changed = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1492 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1493 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1494 |
+// install guard (name must be glib-allocated string) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1495 |
+// XXX we are always using the same callback? |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1496 |
+static void tmpl_install_guard ( gchar *name, template_t *template, settings_guard_t callback ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1497 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1498 |
+ tmpl_guard_t *guard = g_hash_table_lookup ( tmpl_guards, name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1499 |
+ if ( guard == NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1500 |
+ if ( ! settings_set_guard ( name, callback ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1501 |
+ scr_log_print ( LPRINT_LOGNORM, "Error: Unable to install guard for variable '%s' for template '%s'.", name, template -> name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1502 |
+ g_free ( name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1503 |
+ } else { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1504 |
+ guard = g_slice_new ( tmpl_guard_t ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1505 |
+ guard -> name = name; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1506 |
+ guard -> templates = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1507 |
+ g_hash_table_replace ( tmpl_guards, guard -> name, guard ); // to be sure |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1508 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1509 |
+ } else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1510 |
+ g_free ( name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1511 |
+ if ( ! g_slist_find ( template -> guards, guard ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1512 |
+ template -> guards = g_slist_prepend ( template -> guards, guard ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1513 |
+ guard -> templates = g_slist_prepend ( guard -> templates, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1514 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1515 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1516 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1517 |
+// [parser cb] provides mcabber option values & reinstalls guards |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1518 |
+static const char *tmpl_get_var ( const gchar *name, gsize len, gpointer udata, gsize *ret_len ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1519 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1520 |
+ const char *result = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1521 |
+ if ( name != NULL && len > 0 ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1522 |
+ template_t * template = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1523 |
+ gchar * var = g_strndup ( name, len ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1524 |
+ result = settings_opt_get ( var ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1525 |
+ // consumes var |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1526 |
+ tmpl_install_guard ( var, template, tmpl_guard ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1527 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1528 |
+ if ( ret_len != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1529 |
+ if ( result != NULL ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1530 |
+ *ret_len = strlen ( result ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1531 |
+ else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1532 |
+ *ret_len = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1533 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1534 |
+ return result; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1535 |
+} |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1536 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1537 |
+// [parser cb] create & evaluate shortcut template, add to dependencies, return expansion |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1538 |
+static const char *tmpl_get_shortcut ( const gchar name, gpointer udata, gsize *ret_len ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1539 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1540 |
+ gchar optname [ 7 ] = "tmpl_X\0"; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1541 |
+ template_t * template = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1542 |
+ template_t * shortcut; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1543 |
+ optname [ 5 ] = name; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1544 |
+ shortcut = g_hash_table_lookup ( templates, optname ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1545 |
+ if ( shortcut != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1546 |
+ if ( shortcut -> type != shortcut_template ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1547 |
+ scr_log_print ( LPRINT_LOGNORM, "Error: Conflicting type of template for shortcut '%c'.", name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1548 |
+ return NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1549 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1550 |
+ // link as dependency |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1551 |
+ if ( ! g_slist_find ( template -> shortcuts, shortcut ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1552 |
+ shortcut -> templates = g_slist_prepend ( shortcut -> templates, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1553 |
+ template -> shortcuts = g_slist_prepend ( template -> shortcuts, shortcut ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1554 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1555 |
+ } else { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1556 |
+ // create new |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1557 |
+ shortcut = template_new ( name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1558 |
+ if ( shortcut == NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1559 |
+ scr_log_print ( LPRINT_LOGNORM, "Error: Unable to create template for shortcut '%c'.", name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1560 |
+ return NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1561 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1562 |
+ shortcut -> type = shortcut_template; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1563 |
+ shortcut -> markup_changed = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1564 |
+ // consumes optname |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1565 |
+ tmpl_install_guard ( g_strndup ( optname, 6 ), shortcut, tmpl_guard ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1566 |
+ // link as dependency |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1567 |
+ shortcut -> templates = g_slist_prepend ( shortcut -> templates, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1568 |
+ template -> shortcuts = g_slist_prepend ( template -> shortcuts, shortcut ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1569 |
+ // update expansion |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1570 |
+ if ( shortcut -> var_changed ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1571 |
+ GError *error = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1572 |
+ shortcut -> var_changed = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1573 |
+ shortcut -> var_expansion = tmpl_var_expand ( expression, strlen ( expression ), tmpl_get_var, template, NULL, &error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1574 |
+ if ( error != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1575 |
+ scr_log_print ( LPRINT_LOGNORM, "Error: Expansion error on shortcut '%c': %s", name, error -> message ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1576 |
+ g_error_free ( error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1577 |
+ return NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1578 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1579 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1580 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1581 |
+ return shortcut -> var_expansion; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1582 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1583 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1584 |
+// [cb] |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1585 |
+// update expansion of all $-changed templates, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1586 |
+// reinstall guards, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1587 |
+// mark dependencies as %-changed, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1588 |
+// call $-callbacks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1589 |
+static void evaluate_template1 ( gpointer key, gpointer value, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1590 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1591 |
+ template_t * template = value; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1592 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1593 |
+ if ( template -> changed && template -> in_use ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1594 |
+ const gchar * expression = settings_opt_get ( template -> name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1595 |
+ gchar * expansion = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1596 |
+ // release guards (but do not free them) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1597 |
+ g_slist_foreach ( template -> guards, tmpl_unguard, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1598 |
+ g_slist_free ( template -> guards ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1599 |
+ template -> guards = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1600 |
+ // re-install guards & get updated expansion |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1601 |
+ if ( expression != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1602 |
+ GError *error = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1603 |
+ expansion = tmpl_var_expand ( expression, strlen ( expression ), tmpl_get_var, template, NULL, &error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1604 |
+ if ( error != NULL ) { |
48 | 1605 |
+ scr_log_print ( LPRINT_LOGNORM, "Error: Expansion error on template '%s': %s", template -> name, error -> message ); |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1606 |
+ g_error_free ( error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1607 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1608 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1609 |
+ // re-install guard on template itself |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1610 |
+ tmpl_install_guard ( g_strdup ( template -> name ), template, tmpl_guard ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1611 |
+ template -> changed = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1612 |
+ // check, if expansion has changed |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1613 |
+ if ( g_strcmp0 ( expansion, template -> var_expansion ) || |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1614 |
+ ( template -> type == var_template && template -> run_callback ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1615 |
+ g_free ( template -> var_expansion ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1616 |
+ template -> var_expansion = expansion; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1617 |
+ if ( template -> type == var_template ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1618 |
+ // pass result to callback |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1619 |
+ template -> run_callback = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1620 |
+ template -> callback.var ( expansion, template -> userdata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1621 |
+ } else if ( template -> type == shortcut_template ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1622 |
+ // mark %-dependencies as %-changed |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1623 |
+ g_slist_foreach ( template -> templates, tmpl_set_markup_changed, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1624 |
+ else if ( template -> type == markup_template ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1625 |
+ // mark self as %-changed |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1626 |
+ template -> markup_changed = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1627 |
+ else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1628 |
+ g_assert_not_reached (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1629 |
+ } else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1630 |
+ g_free ( expansion ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1631 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1632 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1633 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1634 |
+// [cb] |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1635 |
+// drop dependencies for unused root templates, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1636 |
+// evaluate %-changed templates, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1637 |
+// rebuild dependencies, call %-callbacks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1638 |
+static void evaluate_template2 ( gpointer key, gpointer value, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1639 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1640 |
+ template_t * template = value; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1641 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1642 |
+ if ( template -> type == markup_template ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1643 |
+ if ( ! template -> in_use ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1644 |
+ // release chunks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1645 |
+ g_slist_foreach ( template -> shortcuts, tmpl_unshortcut, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1646 |
+ g_slist_free ( template -> shortcuts ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1647 |
+ template -> shortcuts = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1648 |
+ } else if ( template -> markup_changed || template -> run_callback ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1649 |
+ // release chunks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1650 |
+ g_slist_foreach ( template -> shortcuts, tmpl_unshortcut, template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1651 |
+ g_slist_free ( template -> shortcuts ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1652 |
+ template -> shortcuts = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1653 |
+ // evaluate template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1654 |
+ GError * error = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1655 |
+ markup_link_t * expansion = tmpl_markup_expand ( template -> var_expansion, strlen ( template -> var_expansion ), tmpl_get_shortcut, template, NULL, &error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1656 |
+ if ( error != NULL ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1657 |
+ scr_log_print ( LPRINT_LOGNORM, "Error: Markup error on template '%s': %s.", template -> name, error -> message ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1658 |
+ g_error_free ( error ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1659 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1660 |
+ template -> run_callback = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1661 |
+ template -> callback.markup ( expansion, template -> userdata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1662 |
+ if ( expansion != NULL ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1663 |
+ tmpl_markup_free ( expansion ); // TODO cache / check / ... |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1664 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1665 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1666 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1667 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1668 |
+// [cb] mark deleted templates for removal |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1669 |
+static gboolean tmpl_drop_unused_templates ( gpointer key, gpointer value, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1670 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1671 |
+ template_t * template = value; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1672 |
+ if ( template -> type == shortcut_template ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1673 |
+ // drop chunk templates with no dependent root templates |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1674 |
+ return template -> templates == NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1675 |
+ else |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1676 |
+ // drop user templates, marked as unused |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1677 |
+ return ! template -> in_use; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1678 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1679 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1680 |
+// [cb] mark unused guards for removal |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1681 |
+static gboolean tmpl_drop_unused_guards ( gpointer key, gpointer value, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1682 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1683 |
+ tmpl_guard_t * guard = value; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1684 |
+ if ( guard -> templates == NULL ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1685 |
+ return TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1686 |
+ return FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1687 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1688 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1689 |
+// [idle cb] update commands/guards & call cbs when necessary |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1690 |
+static gboolean reevaluate_templates ( gpointer data ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1691 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1692 |
+ // allow reschedule in a process of reevaluation |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1693 |
+ tmpl_attached_id = 0; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1694 |
+ // update expansion of var-changed templates, call var-callbacks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1695 |
+ g_hash_table_foreach ( tmpl_templates, evaluate_template1, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1696 |
+ // release shortcuts of unused markup templates, reevaluate markup-changed templates, call markup-callbacks |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1697 |
+ g_hash_table_foreach ( tmpl_templates, evaluate_template2, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1698 |
+ // drop removed shortcut- and unused markup- and var- templates |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1699 |
+ g_hash_table_foreach_remove ( tmpl_templates, tmpl_drop_unused_templates, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1700 |
+ // free unused guards TODO do only when needed |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1701 |
+ g_hash_table_foreach_remove ( tmpl_guards, tmpl_drop_unused_guards, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1702 |
+ // always return false, this is oneshot idle call |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1703 |
+ return FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1704 |
+} |
33
ce47dc7fc6c0
Add use-gslice and templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1705 |
+ |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1706 |
+// schedule templates reevaluation |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1707 |
+static void tmpl_schedule_rerun ( void ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1708 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1709 |
+ if ( tmpl_attached_id == 0 ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1710 |
+ GSource * source = g_idle_source_new (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1711 |
+ g_source_set_callback ( source, reevaluate_templates, NULL, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1712 |
+ tmpl_attached_id = g_source_attach ( source, main_context ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1713 |
+ g_source_unref ( source ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1714 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1715 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1716 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1717 |
+// [guard] generic guard for variable |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1718 |
+static gchar *tmpl_guard ( const gchar *key, const gchar *new_value ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1719 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1720 |
+ if ( g_strcmp0 ( new_value, settings_opt_get ( key ) ) ) { |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1721 |
+ // mark dependent commands as modified |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1722 |
+ tmpl_guard_t *guard = g_hash_table_lookup ( tmpl_guards, key ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1723 |
+ g_slist_foreach ( guard -> templates, template_set_var_changed, NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1724 |
+ // schedule changed templates reevaluation |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1725 |
+ tmpl_schedule_rerun (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1726 |
+ } |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1727 |
+ return g_strdup ( new_value ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1728 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1729 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1730 |
+// allocates new template and sets generic values |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1731 |
+static template_t *template_new ( const gchar * name ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1732 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1733 |
+ g_assert ( name != NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1734 |
+ // check for existing template |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1735 |
+ template_t * template = g_hash_table_lookup ( tmpl_templates, name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1736 |
+ if ( template != NULL ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1737 |
+ return NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1738 |
+ // create new |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1739 |
+ template = g_slice_new ( template_t ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1740 |
+ template -> name = g_strdup ( name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1741 |
+ template -> userdata = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1742 |
+ template -> in_use = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1743 |
+ template -> guards = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1744 |
+ template -> var_changed = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1745 |
+ template -> markup_changed = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1746 |
+ template -> run_callback = FALSE; // XXX |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1747 |
+ template -> var_expansion = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1748 |
+ template -> templates = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1749 |
+ template -> shortcuts = NULL; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1750 |
+ return template; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1751 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1752 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1753 |
+// public |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1754 |
+gboolean markup_template_add ( const gchar * name, tmpl_markup_callback_t callback, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1755 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1756 |
+ g_assert ( callback != NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1757 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1758 |
+ template_t * template = template_new ( name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1759 |
+ if ( template == NULL ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1760 |
+ return FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1761 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1762 |
+ // fill specific fields |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1763 |
+ template -> type = markup_template; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1764 |
+ template -> callback.markup = callback; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1765 |
+ template -> userdata = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1766 |
+ template -> markup_changed = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1767 |
+ // schedule new template evaluation |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1768 |
+ tmpl_schedule_rerun (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1769 |
+ return TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1770 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1771 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1772 |
+// public |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1773 |
+gboolean var_template_add ( const gchar * name, tmpl_var_callback_t callback, gpointer udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1774 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1775 |
+ g_assert ( callback != NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1776 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1777 |
+ template_t * template = template_new ( name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1778 |
+ if ( template == NULL ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1779 |
+ return FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1780 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1781 |
+ // fill specific fields |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1782 |
+ template -> type = var_template; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1783 |
+ template -> callback.var = callback; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1784 |
+ template -> userdata = udata; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1785 |
+ template -> markup_changed = FALSE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1786 |
+ // schedule new template evaluation |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1787 |
+ tmpl_schedule_rerun (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1788 |
+ return TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1789 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1790 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1791 |
+// public |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1792 |
+void template_set_in_use ( const gchar * name, gboolean in_use ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1793 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1794 |
+ g_assert ( name != NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1795 |
+ template_t * template = g_hash_table_lookup ( tmpl_templates, name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1796 |
+ g_assert ( template != NULL ); // XXX |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1797 |
+ template -> in_use = in_use; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1798 |
+ tmpl_schedule_rerun (); // XXX |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1799 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1800 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1801 |
+// public |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1802 |
+void template_rerun_callback ( const gchar * name ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1803 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1804 |
+ g_assert ( name != NULL ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1805 |
+ template_t * template = g_hash_table_lookup ( tmpl_templates, name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1806 |
+ g_assert ( template != NULL && template -> type != shortcut_template ); // XXX |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1807 |
+ template -> run_callback = TRUE; |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1808 |
+ tmpl_schedule_rerun (); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1809 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1810 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1811 |
+// private |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1812 |
+void templates_init ( void ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1813 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1814 |
+ // the key will be freed by destruction cb |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1815 |
+ tmpl_guards = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, tmpl_free_guard ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1816 |
+ tmpl_templates = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, tmpl_free_template ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1817 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1818 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1819 |
+// private |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1820 |
+void templates_uninit ( void ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1821 |
+{ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1822 |
+ if ( tmpl_attached_id != 0 ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1823 |
+ g_source_remove ( tmpl_attached_id ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1824 |
+ g_hash_table_destroy ( tmpl_templates ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1825 |
+ g_hash_table_destroy ( tmpl_guards ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1826 |
+} |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1827 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1828 |
+/* vim: se ts=4 sw=4: */ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1829 |
diff -r 00b93fdbb1de mcabber/mcabber/templates.h |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1830 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
48 | 1831 |
+++ b/mcabber/mcabber/templates.h Tue Aug 28 20:37:28 2012 +0300 |
47
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1832 |
@@ -0,0 +1,54 @@ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1833 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1834 |
+#ifndef MCABBER_TEMPLATES_H |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1835 |
+#define MCABBER_TEMPLATES_H 1 |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1836 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1837 |
+/* Copyright 2012 Myhailo Danylenko |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1838 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1839 |
+ * This file is part of mcabber |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1840 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1841 |
+ * mcabber is free software: you can redistribute it and/or |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1842 |
+ * modify it under the terms of the GNU General Public License as published |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1843 |
+ * by the Free Software Foundation, either version 2 of the License, or |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1844 |
+ * (at your option) any later version. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1845 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1846 |
+ * This program is distributed in the hope that it will be useful, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1847 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1848 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1849 |
+ * GNU General Public License for more details. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1850 |
+ * |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1851 |
+ * You should have received a copy of the GNU General Public License |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1852 |
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1853 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1854 |
+#include <glib.h> |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1855 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1856 |
+#include <mcabber/parser.h> |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1857 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1858 |
+// Type for template callback |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1859 |
+typedef void (*tmpl_var_callback_t) ( const gchar * expansion, gpointer udata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1860 |
+typedef void (*tmpl_markup_callback_t) ( const markup_link_t * expansion, gpointer udata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1861 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1862 |
+// success template_add ( option name, cb, cb udata ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1863 |
+// Adds given mcabber option to list of watched templates. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1864 |
+// If any option, used in that template (or template itself) will change, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1865 |
+// callback will be called with new expansion of template. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1866 |
+gboolean var_template_add ( const gchar * name, tmpl_var_callback_t callback, gpointer udata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1867 |
+gboolean markup_template_add ( const gchar * name, tmpl_markup_callback_t callback, gpointer udata ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1868 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1869 |
+// template_set_in_use ( option name, used flag ) |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1870 |
+// Marks template as (un)used. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1871 |
+// Note: Template will be actually removed only on next evaluation run, |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1872 |
+// though call to this function schedules such run. This way, you can |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1873 |
+// mark a bunch of templates as unused and then mark some of them as used. |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1874 |
+void template_set_in_use ( const gchar * name, gboolean in_use ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1875 |
+void template_rerun_callback ( const gchar * name ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1876 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1877 |
+// XXX do we need this? |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1878 |
+// void tmpl_schedule_rerun ( void ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1879 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1880 |
+// private |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1881 |
+void templates_init ( void ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1882 |
+void templates_uninit ( void ); |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1883 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1884 |
+#endif |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1885 |
+ |
e935062a8180
Some updates to templates
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
1886 |
+/* vim: se ts=4 sw=4: */ |