Skip to content
Snippets Groups Projects
Commit 0378698a authored by Jonathan Schöbel's avatar Jonathan Schöbel
Browse files

append/join Text

SH_Text_append_string can be used to append a string to the text,
SH_Text_append_text can be used to append another text to the text.
SH_Text_join is a wrapper for SH_Text_append_text, but also frees the
second text, thus joining the texts to a single one.
parent 08dc6d5f
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@ long_line_behaviour=1
long_line_column=72
[files]
current_page=7
current_page=21
FILE_NAME_0=607;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fconfigure.ac;0;8
FILE_NAME_1=395;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2FMakefile.am;0;8
FILE_NAME_2=847;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Fmain.c;0;8
......@@ -50,7 +50,7 @@ FILE_NAME_17=1043;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%
FILE_NAME_18=1113;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_cms.c;0;8
FILE_NAME_19=3059;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_data.c;0;8
FILE_NAME_20=3508;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_fragment.c;0;8
FILE_NAME_21=1860;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_text.c;0;8
FILE_NAME_21=5331;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_text.c;0;8
FILE_NAME_22=6057;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_validator.c;0;8
FILE_NAME_23=55;None;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftodo.txt;0;8
......
......@@ -173,3 +173,36 @@ SH_Text_append_string (struct SH_Text * text, char * string,
return TRUE;
}
bool
SH_Text_append_text (struct SH_Text * text, struct SH_Text * text2,
struct SH_Error * error)
{
if (!SH_Text_enlarge (text, text->length + text2->length + 1,
error))
{
return FALSE;
}
text->text = strcat (text->text, text2->text);
/* this is save as text->size is always at least one greater
* and SH_Text_enlarge hasn't failed. */
text->length += text2->length;
return TRUE;
}
bool
SH_Text_join (struct SH_Text * text, struct SH_Text * text2,
struct SH_Error * error)
{
if (!SH_Text_append_text (text, text2, error))
{
return FALSE;
}
SH_Text_free (text2, error);
return TRUE;
}
......@@ -47,4 +47,11 @@ bool SH_Text_enlarge (struct SH_Text * text, size_t size,
bool SH_Text_append_string (struct SH_Text * text, char * string,
struct SH_Error * error);
bool SH_Text_append_text (struct SH_Text * text, struct SH_Text * text2,
struct SH_Error * error);
bool SH_Text_join (struct SH_Text * text, struct SH_Text * text2,
struct SH_Error * error);
#endif /* _TEXT_H */
......@@ -144,6 +144,72 @@ START_TEST(test_text_enlarge)
}
END_TEST
START_TEST (test_text_append)
{
struct SH_Error error;
struct SH_Text * text1;
struct SH_Text * text2;
struct SH_Text * text3;
bool boolean;
text1 = SH_Text_new (NULL);
text2 = SH_Text_new (NULL);
text3 = SH_Text_new (NULL);
boolean = SH_Text_append_string (text1, "Text1", NULL);
ck_assert_int_eq (boolean, TRUE);
ck_assert_str_eq (text1->text, "Text1");
ck_assert_int_eq (text1->length, 5);
ck_assert_int_ge (text1->size, 6);
boolean = SH_Text_append_string (text2, "Text2", NULL);
ck_assert_int_eq (boolean, TRUE);
ck_assert_str_eq (text2->text, "Text2");
ck_assert_int_eq (text2->length, 5);
ck_assert_int_ge (text2->size, 6);
error.type = UNDEFINED;
boolean = SH_Text_append_string (text3, "Text3", &error);
ck_assert_int_eq (boolean, TRUE);
ck_assert_int_eq (error.type, SUCCESS);
ck_assert_str_eq (text3->text, "Text3");
ck_assert_int_eq (text3->length, 5);
ck_assert_int_ge (text3->size, 6);
boolean = SH_Text_append_text (text1, text2, NULL);
ck_assert_int_eq (boolean, TRUE);
ck_assert_str_eq (text1->text, "Text1Text2");
ck_assert_int_eq (text1->length, 10);
ck_assert_int_ge (text1->size, 11);
error.type = UNDEFINED;
boolean = SH_Text_append_text (text1, text3, &error);
ck_assert_int_eq (boolean, TRUE);
ck_assert_int_eq (error.type, SUCCESS);
ck_assert_str_eq (text1->text, "Text1Text2Text3");
ck_assert_int_eq (text1->length, 15);
ck_assert_int_ge (text1->size, 16);
boolean = SH_Text_join (text1, text2, NULL);
ck_assert_int_eq (boolean, TRUE);
ck_assert_str_eq (text1->text, "Text1Text2Text3Text2");
ck_assert_int_eq (text1->length, 20);
ck_assert_int_ge (text1->size, 21);
error.type = UNDEFINED;
boolean = SH_Text_join (text1, text3, &error);
ck_assert_int_eq (boolean, TRUE);
ck_assert_int_eq (error.type, SUCCESS);
ck_assert_str_eq (text1->text, "Text1Text2Text3Text2Text3");
ck_assert_int_eq (text1->length, 25);
ck_assert_int_ge (text1->size, 26);
SH_Text_free (text1, NULL);
}
END_TEST
Suite * text_suite (void)
{
Suite *s;
......@@ -156,6 +222,7 @@ Suite * text_suite (void)
tcase_add_test (tc_core, test_text);
tcase_add_test (tc_core, test_text_enlarge);
tcase_add_test (tc_core, test_text_append);
suite_add_tcase (s, tc_core);
return s;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment