forked from sergiotarxz/mangareader
Removing string manipulation functions from manga.c.
We now use the gobject MgUtilString for that.
This commit is contained in:
parent
dbf5a2ff32
commit
280b0445a2
2
Makefile
2
Makefile
@ -6,4 +6,4 @@ LDFLAGS := $(shell pkg-config --libs ${LIBS})
|
||||
CC_COMMAND := ${CC} ${INCDIR} ${CFLAGS}
|
||||
all: build
|
||||
build:
|
||||
${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
|
||||
${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
|
||||
|
@ -24,12 +24,7 @@ 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);
|
||||
|
47
manga.c
47
manga.c
@ -4,6 +4,8 @@
|
||||
#include <pcre2.h>
|
||||
#endif
|
||||
|
||||
#include <openmg/util/string.h>
|
||||
|
||||
#include <manga.h>
|
||||
|
||||
// TODO: Split this file and delete it.
|
||||
@ -12,20 +14,6 @@ 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;
|
||||
@ -55,13 +43,6 @@ 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; i<splitted_string->n_strings; i++) {
|
||||
@ -73,11 +54,13 @@ 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);
|
||||
@ -92,24 +75,22 @@ iterate_string_to_split(struct SplittedString *splitted_string, pcre2_code *re,
|
||||
if (rc < 0) {
|
||||
struct String *current_substring =
|
||||
&splitted_string->substrings [*offset];
|
||||
current_substring->content = alloc_string (subject_size
|
||||
- *start_pos);
|
||||
copy_substring (subject, current_substring->content,
|
||||
subject_size,
|
||||
*start_pos,
|
||||
current_substring->content = mg_util_string_alloc_string (string_util,
|
||||
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 = alloc_string (
|
||||
ovector[0] - *start_pos);
|
||||
copy_substring (subject, splitted_string->substrings[*offset]
|
||||
.content,
|
||||
subject_size,
|
||||
*start_pos,
|
||||
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,
|
||||
ovector[0] - *start_pos);
|
||||
splitted_string->substrings[*offset].size =
|
||||
ovector[0] - *start_pos;
|
||||
|
24
src/manga.c
24
src/manga.c
@ -1,8 +1,10 @@
|
||||
#include <glib-object.h>
|
||||
|
||||
#include <manga.h>
|
||||
#include <openmg/util/string.h>
|
||||
#include <openmg/manga.h>
|
||||
|
||||
#include <manga.h>
|
||||
|
||||
struct _MgManga {
|
||||
GObject parent_instance;
|
||||
char *image_url;
|
||||
@ -143,12 +145,20 @@ 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 = 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));
|
||||
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));
|
||||
return self;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <libxml/HTMLparser.h>
|
||||
#include <libxml/xpath.h>
|
||||
|
||||
#include <openmg/util/string.h>
|
||||
#include <openmg/util/xml.h>
|
||||
|
||||
struct _MgUtilXML {
|
||||
@ -56,6 +57,7 @@ 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;
|
||||
}
|
||||
@ -65,11 +67,9 @@ 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 = alloc_string(content_len);
|
||||
copy_substring ((char *) attr->children->content, return_value,
|
||||
content_len,
|
||||
0,
|
||||
content_len);
|
||||
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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user