Skip to content

Commit d28800d

Browse files
committed
search-helpers/nemo-mso-to-txt.c: Refactor and improve error
handling.
1 parent f8c3d9a commit d28800d

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

search-helpers/nemo-mso-to-txt.c

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,43 @@ iterate_thru_levels (GString *collective,
111111
}
112112
}
113113

114+
gchar *
115+
run_regex_replace (const gchar *pattern,
116+
gchar *input,
117+
const gchar *replacement,
118+
GError **error)
119+
{
120+
GRegex *re;
121+
gchar *out;
122+
123+
out = NULL;
124+
125+
re = g_regex_new (pattern,
126+
G_REGEX_OPTIMIZE,
127+
0,
128+
error);
129+
130+
if (re == NULL) {
131+
return NULL;
132+
}
133+
134+
out = g_regex_replace_literal (re, input, -1, 0, replacement, 0, error);
135+
g_free (input);
136+
g_regex_unref (re);
137+
138+
return out;
139+
}
140+
114141
int
115142
main (int argc, char *argv[])
116143
{
117144
GsfInput *input;
118145
GsfInfile *toplevel;
119146
GString *collective;
120-
GRegex *re;
121147
GError *error;
122148
GFile *file;
123149
gchar *filename;
124-
gchar *xml_strip_out;
125-
gchar *space_strip_out;
150+
gchar *content;
126151

127152
if (argc < 2) {
128153
g_printerr ("Need a filename\n");
@@ -149,6 +174,7 @@ main (int argc, char *argv[])
149174
if (error != NULL)
150175
{
151176
g_critical ("Could not load mso file: %s", error->message);
177+
g_object_unref (input);
152178
g_error_free (error);
153179
return 1;
154180
}
@@ -160,33 +186,40 @@ main (int argc, char *argv[])
160186
g_object_unref (toplevel);
161187
g_object_unref (input);
162188

163-
re = g_regex_new ("<[^>]+>",
164-
G_REGEX_OPTIMIZE,
165-
0,
166-
&error);
189+
content = g_string_free (collective, FALSE);
167190

168-
xml_strip_out = g_regex_replace_literal (re, collective->str, -1, 0, " ", 0, &error);
169-
g_string_free (collective, TRUE);
170-
g_regex_unref (re);
191+
if (content == NULL) {
192+
goto out;
193+
}
171194

172-
re = g_regex_new ("\\s+",
173-
G_REGEX_OPTIMIZE,
174-
0,
175-
&error);
195+
content = run_regex_replace ("<[^>]+>",
196+
content,
197+
"",
198+
&error);
176199

177-
space_strip_out = g_regex_replace_literal (re, xml_strip_out, -1, 0, " ", 0, &error);
178-
g_free (xml_strip_out);
179-
g_regex_unref (re);
200+
if (content == NULL) {
201+
goto out;
202+
}
203+
204+
content = run_regex_replace ("\\s+",
205+
content,
206+
" ",
207+
&error);
208+
209+
if (content == NULL) {
210+
goto out;
211+
}
212+
213+
g_printf ("%s", content);
214+
g_free (content);
180215

216+
out:
181217
if (error != NULL)
182218
{
183-
g_critical ("Could extract strings from mso file: %s", error->message);
219+
g_critical ("Could not extract strings from mso 2003+ file: %s", error->message);
184220
g_error_free (error);
185221
return 1;
186222
}
187223

188-
g_printf ("%s", space_strip_out);
189-
g_free (space_strip_out);
190-
191224
return 0;
192225
}

0 commit comments

Comments
 (0)