1 /* Buffer formatting tests */
5 int print(SilcStack stack, SilcBuffer buf, void *value, void *context)
7 fwrite(silc_buffer_data(buf), 1, silc_buffer_len(buf), stdout);
8 if (!silc_buffer_strchr(buf, '\n', TRUE))
11 return silc_buffer_len(buf);
14 int main(int argc, char **argv)
16 SilcBool success = FALSE;
17 char string[1024], *astring;
20 if (argc > 1 && !strcmp(argv[1], "-d")) {
23 silc_log_debug_hexdump(TRUE);
24 silc_log_set_debug_string("*buf*,*regex*,*errno*");
27 silc_snprintf(string, sizeof(string), "This is foobar");
28 silc_buffer_set(&buf, string, strlen(string));
29 SILC_LOG_DEBUG(("sed 's/foo/bar/'"));
30 SILC_LOG_DEBUG(("string: %s", string));
31 if (silc_buffer_format(&buf,
32 SILC_STR_REGEX("foo", 0),
33 SILC_STR_STRING("bar"),
37 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
38 if (strcmp("This is barbar", silc_buffer_data(&buf)))
41 silc_snprintf(string, sizeof(string), "This is foobar string!!");
42 astring = silc_memdup(string, strlen(string));
43 silc_buffer_set(&buf, astring, strlen(astring) + 1);
44 SILC_LOG_DEBUG(("sed 's/foo/barbar/g'"));
45 SILC_LOG_DEBUG(("string: %s", astring));
46 if (silc_buffer_format(&buf,
47 SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
48 SILC_STR_REGEX_INCLUSIVE),
49 SILC_STR_REPLACE("barbar", 6),
53 silc_buffer_printf(&buf, TRUE);
54 if (strcmp("This is barbarbar string!!", silc_buffer_data(&buf)))
56 silc_buffer_purge(&buf);
58 silc_snprintf(string, sizeof(string), "This is foobar string foo!!");
59 astring = silc_memdup(string, strlen(string));
60 silc_buffer_set(&buf, astring, strlen(astring) + 1);
61 SILC_LOG_DEBUG(("sed 's/foo//g'"));
62 SILC_LOG_DEBUG(("string: %s", astring));
63 if (silc_buffer_format(&buf,
64 SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
65 SILC_STR_REGEX_INCLUSIVE),
66 SILC_STR_REPLACE("", 0),
70 silc_buffer_printf(&buf, TRUE);
71 if (strcmp("This is bar string !!", silc_buffer_data(&buf)))
73 silc_buffer_purge(&buf);
75 silc_snprintf(string, sizeof(string), "This is foobar\n");
76 silc_buffer_set(&buf, string, strlen(string));
77 SILC_LOG_DEBUG(("sed 's/\\n/\\0/'"));
78 SILC_LOG_DEBUG(("string: %s", string));
79 if (silc_buffer_format(&buf,
80 SILC_STR_REGEX("\n", 0),
85 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
86 if (strcmp("This is foobar", silc_buffer_data(&buf)))
89 silc_snprintf(string, sizeof(string), "foo\nfoobar\nbarfoofoo\nbar\n\nfoo");
90 silc_buffer_set(&buf, string, strlen(string));
91 SILC_LOG_DEBUG(("sed 's/foo/bar/g'"));
92 SILC_LOG_DEBUG(("string: %s", string));
93 if (silc_buffer_format(&buf,
94 SILC_STR_REGEX("foo", SILC_STR_REGEX_NL |
96 SILC_STR_STRING("bar"),
100 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
101 if (strcmp("bar\nbarbar\nbarbarbar\nbar\n\nbar", silc_buffer_data(&buf)))
104 silc_snprintf(string, sizeof(string),
105 "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
106 silc_buffer_set(&buf, string, strlen(string));
107 SILC_LOG_DEBUG(("sed '/baz/s/foo/bar/"));
108 SILC_LOG_DEBUG(("string: %s", string));
109 if (silc_buffer_format(&buf,
110 SILC_STR_REGEX("baz", SILC_STR_REGEX_NL),
111 SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
112 SILC_STR_STRING("bar"),
114 SILC_STR_END, SILC_STR_END) < 0)
116 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
117 if (strcmp("foo\nbazbarbar\nbarfoofoo\nbar\nbaz\nbazbar",
118 silc_buffer_data(&buf)))
121 silc_snprintf(string, sizeof(string),
122 "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
123 silc_buffer_set(&buf, string, strlen(string));
124 SILC_LOG_DEBUG(("sed '/baz/!s/foo/bar/"));
125 SILC_LOG_DEBUG(("string: %s", string));
126 if (silc_buffer_format(&buf,
127 SILC_STR_REGEX("baz", SILC_STR_REGEX_NL |
129 SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
130 SILC_STR_STRING("bar"),
132 SILC_STR_END, SILC_STR_END) < 0)
134 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
135 if (strcmp("bar\nbazfoobar\nbarbarbar\nbar\nbaz\nbazfoo",
136 silc_buffer_data(&buf)))
139 SILC_LOG_DEBUG(("Print all lines starting with 'R'"));
140 silc_snprintf(string, sizeof(string),
141 "Rfoo\nbazfoobar\nRbarfoofoo\nRbar\nbaz\nRbazfoo");
142 silc_buffer_set(&buf, string, strlen(string));
143 SILC_LOG_DEBUG(("string: %s", string));
144 if (silc_buffer_unformat(&buf,
145 SILC_STR_REGEX("^R", SILC_STR_REGEX_NL),
146 SILC_STR_FUNC(print, NULL, NULL),
147 SILC_STR_END, SILC_STR_END) < 0)
153 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
154 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");