1 /* hello world - multiple pieces of data, includes substitution and
2  *               inserting data into the middle of a string */
3 
4 #include "ex_hello_world.h" /* helper functions */
5 
main(void)6 int main(void)
7 {
8   Vstr_base *s1 = hw_init();
9   Vstr_ref *ref = NULL;
10 
11   vstr_add_cstr_ptr(s1, s1->len, "Hello");
12 
13   vstr_add_rep_chr(s1, s1->len, 'W', 5); /* add "WWWWWW" */
14 
15   if (s1->conf->malloc_bad)
16     errno = ENOMEM, err(EXIT_FAILURE, "Add string data");
17 
18   /* substitute an 'o' for a 'W' */
19   if (!vstr_sub_rep_chr(s1, strlen("HelloWW"), 1, 'o', 1))
20     errno = ENOMEM, err(EXIT_FAILURE, "Substitute string data");
21 
22   /* substitute "WWW" for a "rld\n" -- */
23   if (!vstr_sub_cstr_buf(s1, strlen("HelloWoW"), strlen("WWW"), "rld\n"))
24     errno = ENOMEM, err(EXIT_FAILURE, "Substitute string data");
25 
26   if (!(ref = vstr_ref_make_ptr((char *)"XYZ ", vstr_ref_cb_free_ref)))
27     errno = ENOMEM, err(EXIT_FAILURE, "Create data reference");
28   /* now ref->ptr is "XYZ " */
29 
30   /* add space after "Hello", by skipping "XYZ" in reference */
31   vstr_add_ref(s1, strlen("Hello"), ref, strlen("XYZ"), strlen(" "));
32 
33   vstr_ref_del(ref); /* delete our reference to the Vstr_ref */
34 
35   if (s1->conf->malloc_bad)
36     errno = ENOMEM, err(EXIT_FAILURE, "Add string data");
37 
38   while (io_put(s1, STDOUT_FILENO) != IO_NONE) {}
39 
40   exit (hw_exit(s1));
41 }
42