diff --git a/Makefile b/Makefile index 63a09b6..48899cf 100644 --- a/Makefile +++ b/Makefile @@ -6,4 +6,4 @@ LDFLAGS := $(shell pkg-config --libs ${LIBS}) CC_COMMAND := ${CC} ${INCDIR} ${CFLAGS} all: build build: - ${CC_COMMAND} src/util/string.c src/util/xml.c src/util/soup.c src/view/list_view_manga.c src/view/main_view.c src/manga.c src/backend/readmng.c manga.c main.c -o main ${LDFLAGS} -ggdb + ${CC_COMMAND} src/util/xml.c src/util/soup.c src/view/list_view_manga.c src/view/main_view.c src/manga.c src/backend/readmng.c manga.c main.c -o main ${LDFLAGS} -ggdb diff --git a/include/manga.h b/include/manga.h index 708b4d0..ff8a9a7 100644 --- a/include/manga.h +++ b/include/manga.h @@ -24,7 +24,12 @@ struct String { struct SplittedString * split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size); +char * +alloc_string(size_t len); void splitted_string_free (struct SplittedString *splitted_string); char * match_1 (char *re_str, char *subject); +void +copy_substring(const char *origin, char *dest, size_t dest_len, size_t start, + size_t len); diff --git a/manga.c b/manga.c index f272349..317a04b 100644 --- a/manga.c +++ b/manga.c @@ -4,8 +4,6 @@ #include #endif -#include - #include // TODO: Split this file and delete it. @@ -14,6 +12,20 @@ iterate_string_to_split(struct SplittedString *splitted_string, pcre2_code *re, int *will_break, const char *subject, size_t subject_size, size_t *start_pos, size_t *offset); +void +copy_substring(const char *origin, char *dest, size_t dest_len, size_t start, + size_t len) { + size_t copying_offset = 0; + while (copying_offset < len) { + if (!(start+copying_offset <=dest_len)) { + fprintf(stderr, "Read attempt out of bounds.%ld %ld %ld\n", dest_len, start, len); + break; + } + dest[copying_offset] = origin[start+copying_offset]; + copying_offset++; + } + dest[len] = '\0'; +} struct SplittedString * split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size) { pcre2_code_8 *re; @@ -43,6 +55,13 @@ split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size return splitted_string; } + +char * +alloc_string(size_t len) { + char * return_value = NULL; + return g_malloc (len + 1 * sizeof *return_value); +} + void splitted_string_free (struct SplittedString *splitted_string) { for (int i = 0; in_strings; i++) { @@ -54,13 +73,11 @@ splitted_string_free (struct SplittedString *splitted_string) { } static void -iterate_string_to_split(struct SplittedString *splitted_string, - pcre2_code *re, int *will_break, const char *subject, +iterate_string_to_split(struct SplittedString *splitted_string, pcre2_code *re, int *will_break, const char *subject, size_t subject_size, size_t *start_pos, size_t *offset) { pcre2_match_data_8 *match_data; PCRE2_SIZE *ovector; int rc; - MgUtilString *string_util = mg_util_string_new (); splitted_string->n_strings++; match_data = pcre2_match_data_create_from_pattern_8 (re, NULL); @@ -75,22 +92,24 @@ iterate_string_to_split(struct SplittedString *splitted_string, if (rc < 0) { struct String *current_substring = &splitted_string->substrings [*offset]; - current_substring->content = mg_util_string_alloc_string (string_util, + current_substring->content = alloc_string (subject_size + - *start_pos); + copy_substring (subject, current_substring->content, + subject_size, + *start_pos, subject_size - *start_pos); - mg_util_string_copy_substring (string_util, subject, - current_substring->content, subject_size, - *start_pos, subject_size - *start_pos); current_substring->size = subject_size - *start_pos; *will_break = 1; goto cleanup_iterate_string_to_split; } ovector = pcre2_get_ovector_pointer_8(match_data); - splitted_string->substrings[*offset].content = - mg_util_string_alloc_string (string_util, ovector[0] - *start_pos); - mg_util_string_copy_substring (string_util, - subject, splitted_string->substrings[*offset].content, - subject_size, *start_pos, + splitted_string->substrings[*offset].content = alloc_string ( + ovector[0] - *start_pos); + copy_substring (subject, splitted_string->substrings[*offset] + .content, + subject_size, + *start_pos, ovector[0] - *start_pos); splitted_string->substrings[*offset].size = ovector[0] - *start_pos; diff --git a/src/manga.c b/src/manga.c index a2b03b6..a67bff4 100644 --- a/src/manga.c +++ b/src/manga.c @@ -1,9 +1,7 @@ #include -#include -#include - #include +#include struct _MgManga { GObject parent_instance; @@ -145,20 +143,12 @@ mg_manga_get_property (GObject *object, MgManga * mg_manga_new (const char *const image_url, const char *const title, const char *id) { MgManga *self = NULL; - MgUtilString *string_util = mg_util_string_new (); self = MG_MANGA ((g_object_new (MG_TYPE_MANGA, NULL))); - self->image_url = mg_util_string_alloc_string (string_util, - strlen (image_url)); - self->title = mg_util_string_alloc_string (string_util, - strlen (title)); - self->id = mg_util_string_alloc_string (string_util, - strlen (id)); - mg_util_string_copy_substring (string_util, - image_url, self->image_url, - strlen(image_url) + 1, 0, strlen (image_url)); - mg_util_string_copy_substring (string_util, - title, self->title, strlen(title) + 1, 0, strlen (title)); - mg_util_string_copy_substring (string_util, - id, self->id, strlen(id) + 1, 0, strlen (id)); + self->image_url = alloc_string (strlen (image_url)); + self->title = alloc_string (strlen (title)); + self->id = alloc_string (strlen (id)); + copy_substring (image_url, self->image_url, strlen(image_url) + 1, 0, strlen (image_url)); + copy_substring (title, self->title, strlen(title) + 1, 0, strlen (title)); + copy_substring (id, self->id, strlen(id) + 1, 0, strlen (id)); return self; } diff --git a/src/util/xml.c b/src/util/xml.c index 716b791..2d32e8b 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -3,7 +3,6 @@ #include #include -#include #include struct _MgUtilXML { @@ -57,7 +56,6 @@ mg_util_xml_find_class (MgUtilXML *self, xmlNodePtr node, char *class, char * mg_util_xml_get_attr (MgUtilXML *self, xmlNodePtr const node, const char *attr_name) { char *return_value = NULL; - MgUtilString *string_util = mg_util_string_new (); if (!node) { return NULL; } @@ -67,9 +65,11 @@ mg_util_xml_get_attr (MgUtilXML *self, xmlNodePtr const node, const char *attr_n if (!attr->children->content) continue; size_t content_len = strlen((char *) attr->children->content); - return_value = mg_util_string_alloc_string (string_util, content_len); - mg_util_string_copy_substring (string_util, (char *) attr->children->content, - return_value, content_len, 0, content_len); + return_value = alloc_string(content_len); + copy_substring ((char *) attr->children->content, return_value, + content_len, + 0, + content_len); break; } }