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;
18 SilcBufferStruct buf, buf2;
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_STRING_APPEND("barbar"),
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\n");
59 silc_buffer_set(&buf, string, strlen(string));
60 SILC_LOG_DEBUG(("sed 's/\\n/\\0/'"));
61 SILC_LOG_DEBUG(("string: %s", string));
62 if (silc_buffer_format(&buf,
63 SILC_STR_REGEX("\n", 0),
68 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
69 if (strcmp("This is foobar", silc_buffer_data(&buf)))
72 silc_snprintf(string, sizeof(string), "foo\nfoobar\nbarfoofoo\nbar\n\nfoo");
73 silc_buffer_set(&buf, string, strlen(string));
74 SILC_LOG_DEBUG(("sed 's/foo/bar/g'"));
75 SILC_LOG_DEBUG(("string: %s", string));
76 if (silc_buffer_format(&buf,
77 SILC_STR_REGEX("foo", SILC_STR_REGEX_NL |
79 SILC_STR_STRING("bar"),
83 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
84 if (strcmp("bar\nbarbar\nbarbarbar\nbar\n\nbar", silc_buffer_data(&buf)))
87 silc_snprintf(string, sizeof(string),
88 "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
89 silc_buffer_set(&buf, string, strlen(string));
90 SILC_LOG_DEBUG(("sed '/baz/s/foo/bar/"));
91 SILC_LOG_DEBUG(("string: %s", string));
92 if (silc_buffer_format(&buf,
93 SILC_STR_REGEX("baz", SILC_STR_REGEX_NL),
94 SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
95 SILC_STR_STRING("bar"),
97 SILC_STR_END, SILC_STR_END) < 0)
99 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
100 if (strcmp("foo\nbazbarbar\nbarfoofoo\nbar\nbaz\nbazbar",
101 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 |
112 SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
113 SILC_STR_STRING("bar"),
115 SILC_STR_END, SILC_STR_END) < 0)
117 SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
118 if (strcmp("bar\nbazfoobar\nbarbarbar\nbar\nbaz\nbazfoo",
119 silc_buffer_data(&buf)))
122 SILC_LOG_DEBUG(("Print all lines starting with 'R'"));
123 silc_snprintf(string, sizeof(string),
124 "Rfoo\nbazfoobar\nRbarfoofoo\nRbar\nbaz\nRbazfoo");
125 silc_buffer_set(&buf, string, strlen(string));
126 SILC_LOG_DEBUG(("string: %s", string));
127 if (silc_buffer_unformat(&buf,
128 SILC_STR_REGEX("^R", SILC_STR_REGEX_NL),
129 SILC_STR_FUNC(print, NULL, NULL),
130 SILC_STR_END, SILC_STR_END) < 0)
136 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
137 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");