| GirToBac 0.0 Create FB Headers from GObject Introspection *.gir files

- Main Page
- Tutorial
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Macros Pages
GirToBac_RepData.bas
Go to the documentation of this file.
1 /'* \file GirToBac_RepData.bas
2 \brief Helper Classes list and replacements.
3
4 This file contains two helper classes for a list (first in first out
5 stack) and for a replacement UDT, that checks and replaces a text.
6 It also contains the parser for the configuration file.
7
8 '/
9
10
11 /'* \brief Object to check texts (Su) and provide replacements (Er)
12
13 This object can get feeded by search and replacement texts. After
14 adding some pairs the RepData::rep() member function is used to
15 receive the replacement text for a match or the original text
16 otherwise. The search is done case-sensitive. Search text must not
17 contain CHR(0) to CHR(2), because these characters are used
18 internally.
19
20 The object counts the number of entries and the number of matches
21 for each entry. The list member function executes a callback
22 function on each entry.
23
24 '/
25 TYPE RepData
26 AS CONST gchar PTR Na '*< the name of the XML-tag
27 AS STRING _
28 Er, _ '*< buffer holding replacement texts and match-entry-counters
29 Su = MKI(0) & CHR(1) '*< buffer holding the entry counter, search texts and the start of their replacements
30 DECLARE CONSTRUCTOR(BYVAL T AS CONST gchar PTR)
31 DECLARE FUNCTION Az() AS INTEGER
32 DECLARE SUB add(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR)
33 DECLARE FUNCTION rep(BYVAL T AS CONST gchar PTR) AS CONST ZSTRING PTR
34 DECLARE SUB list(BYREF Li AS STRING)
35 END TYPE
36
37 '* \brief the constructor stores the tag name
38 CONSTRUCTOR RepData(BYVAL T AS CONST gchar PTR)
39 Na = T
40 END CONSTRUCTOR
41
42 /'* \brief provide number of entries in the buffer
43 \returns entries in buffer
44
45 Each time a text gets added to the UDT a counter gets increased.
46 This function provides the counter result. (An entry can get added
47 only once, a duplicate doesn't get counted.)
48
49 '/
50 FUNCTION RepData.Az() AS INTEGER : RETURN *CAST(INTEGER PTR, SADD(Su))
51 END FUNCTION
52
53
54 /'* \brief Generate a list of un-used symbols
55 \param Li string to list un-used symbols
56
57 Each symbol-found gets counted. This procedure adds the un-used
58 symbols to the list in the string.
59
60 '/
61 SUB RepData.list(BYREF Li AS STRING)
62 VAR r = "", a = 6, e = INSTR(a, Su, CHR(2)), t = ""
63 WHILE e > a
64 VAR l = e - a : e += 1
65 VAR x = INSTR(e, Su, CHR(1)), z = SADD(Er) + VALINT("&h" & MID(Su, e, x - e))
66 VAR n = *CAST(INTEGER PTR, z - 4)
67 IF 0 = n THEN Li &= NL !"\t" & MID(Su, a, l) & " <" & *Na & ">"
68 a = x + 1 : e = INSTR(a, Su, CHR(2))
69 WEND
70 END SUB
71
72
73 /'* \brief Add entries to buffers
74 \param S The text to search for
75 \param E The replacement for this search text
76 \returns Zero on success (error text otherwise)
77
78 This function gets called to add some text to the UDT. It adds both,
79 the text to search for and the text replacement to the buffers and
80 sets their counter to zero. In case of duplicates or illegal
81 characters in the search string an error text gets returned (and
82 zero on success). There's no checking of the replacement string, it
83 mustn't contain zero characters.
84
85 '/
86 SUB RepData.add(BYVAL S AS CONST ZSTRING PTR, BYVAL E AS CONST ZSTRING PTR)
87 IF *S = "" THEN ?"Empty <" & *Na & "> search attribute!" : EXIT SUB
88 IF INSTR(*S, ANY !"\000\001\002") THEN ?"Undefined char in <" & *Na & ">: " & *S : EXIT SUB
89 VAR c = CHR(1) & *S & CHR(2) : IF INSTR(5, Su, c) THEN ?"Duplicated <" & *Na & ">: " & *S : EXIT SUB
90 *CAST(INTEGER PTR, SADD(Su)) += 1 : Su &= MID(c, 2) & HEX(LEN(Er) + 4) & CHR(1)
91 Er &= MKI(0) & *E & CHR(0)
92 END SUB
93
94
95 /'* \brief Get replacement for a text (if any))
96 \param T The text to search for
97 \returns The replacemnt (if any) or the original text
98
99 This function searches for a text in the search buffer. On found the
100 text (case-sensitive) it returns a pointer to the replacement string
101 (defined when calling the function RepData::add()). In case of no
102 match in the search buffer a pointer to the original text gets
103 returned.
104
105 '/
106 FUNCTION RepData.rep(BYVAL T AS CONST gchar PTR) AS CONST ZSTRING PTR
107 IF 0 = T THEN RETURN @"/'unknown'/"
108 VAR a = INSTR(Su, CHR(1) & *T & CHR(2)) : IF a THEN a += LEN(*T) + 2 ELSE RETURN T
109 VAR e = INSTR(a, Su, CHR(1)) + 1
110 DIM AS ZSTRING PTR z = SADD(Er) + VALINT("&h" & MID(Su, a, e - a))
111 *CAST(INTEGER PTR, z - 4) += 1 : RETURN z
112 END FUNCTION
113
114 /'* \brief A simple fifo stack list
115
116 This is a simple first in first out stack. It's used to store the
117 symbols for the ordered part of the header (passX). The entries are
118 separated by ; characters.
119
120 '/
121 TYPE Stack
122 AS STRING Dat = ";" '*< The buffer for entries, ; separated
123 AS INTEGER A = 1 '*< Start position of next entry (-1)
124 DECLARE SUB add(BYVAL AS CONST gchar PTR)
125 DECLARE FUNCTION nxt() AS STRING
126 DECLARE FUNCTION find(BYVAL T AS CONST gchar PTR) AS INTEGER
127 END TYPE
128
129
130 /'* \brief Add entries to Stack data
131 \param S The text to add
132
133 This SUB gets called to add a word to the Stack. It adds the text
134 and the separator character ';'. It's possible to add more than one
135 word, but the text must not contain the separator character. Here
136 it's used for symbol names (without white-spaces).
137
138 '/
139 SUB Stack.add(BYVAL S AS CONST gchar PTR)
140 IF 0 = find(S) THEN Dat &= *S & ";" : EXIT SUB
141 ?"Duplicated <first>: " & *S
142 END SUB
143
144
145 /'* \brief The text of the next entry
146 \returns Retrieves the next entry
147
148 The stack is meant to add symbols in the required order (read from
149 the configuration file) and retrieve the symbol names later on in
150 the given order, each entry once. To retrieve the entries a second
151 time the position counter needs to get re-initialized (A = 2).)
152
153 '/
154 FUNCTION Stack.nxt() AS STRING
155 VAR x = A : A = INSTR(A + 1, Dat, ";") : IF 0 = A THEN RETURN ""
156 x += 1 : RETURN MID(Dat, x, A - x)
157 END FUNCTION
158
159
160 /'* \brief Search the stack for a text
161 \param T Text to search for
162 \returns Text position in stack buffer (or zero)
163
164 This function is used to find a text in the text buffer. On found
165 the text position gets returned (zero otherwise).)
166
167 '/
168 FUNCTION Stack.find(BYVAL T AS CONST gchar PTR) AS INTEGER
169 RETURN INSTR(Dat, ";" & *T & ";")
170 END FUNCTION
171
172
173 DIM SHARED AS RepData _
174 FB_TYP = @"type", _ '*< Replacements for type declarations
175 FB_NAM = @"name" '*< Replacements for symbols (names)
176 DIM SHARED AS Stack _
177 FIRST '*< FiFo stack for ordered elements (pass X))
178
179
180 '* The <A HREF="https://developer.gnome.org/glib/stable/glib-Simple-XML-Subset-Parser.html"> GMarkupParser </A> for the configuration file \em xyz.GirToBac.
181 '& SUB_CDECL G2b_parser(){};
182 G2b_parser:
183 _START_PARSER(G2b)
184
185 SELECT CASE *element_name
186 CASE "first"
187 VAR n = find_value("search", AttNams, AttVals) '*< local variable
188 IF 0 = n THEN EXIT SELECT
189 FIRST.add(n)
190 CASE "type"
191 VAR s = find_value("search", AttNams, AttVals) '*< local variable
192 IF 0 = s THEN EXIT SELECT
193 VAR r = find_value("replace", AttNams, AttVals) '*< local variable
194 VAR a = find_value("add", AttNams, AttVals) '*< local variable
195 IF r THEN FB_TYP.add(s, r) : EXIT SELECT
196
197 VAR e = *s & *a '*< local variable
198 FB_TYP.add(s, e)
199 CASE "name"
200 VAR s = find_value("search", AttNams, AttVals) '*< local variable
201 IF 0 = s THEN EXIT SELECT
202 VAR r = find_value("replace", AttNams, AttVals) '*< local variable
203 VAR a = find_value("add", AttNams, AttVals) '*< local variable
204 IF r THEN FB_NAM.add(s, r) : EXIT SELECT
205
206 VAR e = *s & *a '*< local variable
207 FB_NAM.add(s, SADD(e))
208
209 _END_PARSER(G2b)
210
211 SELECT CASE *element_name
212 CASE "type", "name", "first"
213
214 _NEW_PARSER(G2b)