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}
|
CC_COMMAND := ${CC} ${INCDIR} ${CFLAGS}
|
||||||
all: build
|
all: build
|
||||||
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 *
|
struct SplittedString *
|
||||||
split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size);
|
split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size);
|
||||||
char *
|
|
||||||
alloc_string(size_t len);
|
|
||||||
void
|
void
|
||||||
splitted_string_free (struct SplittedString *splitted_string);
|
splitted_string_free (struct SplittedString *splitted_string);
|
||||||
char *
|
char *
|
||||||
match_1 (char *re_str, char *subject);
|
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>
|
#include <pcre2.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <openmg/util/string.h>
|
||||||
|
|
||||||
#include <manga.h>
|
#include <manga.h>
|
||||||
|
|
||||||
// TODO: Split this file and delete it.
|
// 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,
|
pcre2_code *re, int *will_break, const char *subject,
|
||||||
size_t subject_size, size_t *start_pos, size_t *offset);
|
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 *
|
struct SplittedString *
|
||||||
split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size) {
|
split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size) {
|
||||||
pcre2_code_8 *re;
|
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;
|
return splitted_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
|
||||||
alloc_string(size_t len) {
|
|
||||||
char * return_value = NULL;
|
|
||||||
return g_malloc (len + 1 * sizeof *return_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
splitted_string_free (struct SplittedString *splitted_string) {
|
splitted_string_free (struct SplittedString *splitted_string) {
|
||||||
for (int i = 0; i<splitted_string->n_strings; i++) {
|
for (int i = 0; i<splitted_string->n_strings; i++) {
|
||||||
@ -73,11 +54,13 @@ splitted_string_free (struct SplittedString *splitted_string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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) {
|
size_t subject_size, size_t *start_pos, size_t *offset) {
|
||||||
pcre2_match_data_8 *match_data;
|
pcre2_match_data_8 *match_data;
|
||||||
PCRE2_SIZE *ovector;
|
PCRE2_SIZE *ovector;
|
||||||
int rc;
|
int rc;
|
||||||
|
MgUtilString *string_util = mg_util_string_new ();
|
||||||
|
|
||||||
splitted_string->n_strings++;
|
splitted_string->n_strings++;
|
||||||
match_data = pcre2_match_data_create_from_pattern_8 (re, NULL);
|
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) {
|
if (rc < 0) {
|
||||||
struct String *current_substring =
|
struct String *current_substring =
|
||||||
&splitted_string->substrings [*offset];
|
&splitted_string->substrings [*offset];
|
||||||
current_substring->content = alloc_string (subject_size
|
current_substring->content = mg_util_string_alloc_string (string_util,
|
||||||
- *start_pos);
|
|
||||||
copy_substring (subject, current_substring->content,
|
|
||||||
subject_size,
|
|
||||||
*start_pos,
|
|
||||||
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;
|
current_substring->size = subject_size - *start_pos;
|
||||||
|
|
||||||
*will_break = 1;
|
*will_break = 1;
|
||||||
goto cleanup_iterate_string_to_split;
|
goto cleanup_iterate_string_to_split;
|
||||||
}
|
}
|
||||||
ovector = pcre2_get_ovector_pointer_8(match_data);
|
ovector = pcre2_get_ovector_pointer_8(match_data);
|
||||||
splitted_string->substrings[*offset].content = alloc_string (
|
splitted_string->substrings[*offset].content =
|
||||||
ovector[0] - *start_pos);
|
mg_util_string_alloc_string (string_util, ovector[0] - *start_pos);
|
||||||
copy_substring (subject, splitted_string->substrings[*offset]
|
mg_util_string_copy_substring (string_util,
|
||||||
.content,
|
subject, splitted_string->substrings[*offset].content,
|
||||||
subject_size,
|
subject_size, *start_pos,
|
||||||
*start_pos,
|
|
||||||
ovector[0] - *start_pos);
|
ovector[0] - *start_pos);
|
||||||
splitted_string->substrings[*offset].size =
|
splitted_string->substrings[*offset].size =
|
||||||
ovector[0] - *start_pos;
|
ovector[0] - *start_pos;
|
||||||
|
24
src/manga.c
24
src/manga.c
@ -1,8 +1,10 @@
|
|||||||
#include <glib-object.h>
|
#include <glib-object.h>
|
||||||
|
|
||||||
#include <manga.h>
|
#include <openmg/util/string.h>
|
||||||
#include <openmg/manga.h>
|
#include <openmg/manga.h>
|
||||||
|
|
||||||
|
#include <manga.h>
|
||||||
|
|
||||||
struct _MgManga {
|
struct _MgManga {
|
||||||
GObject parent_instance;
|
GObject parent_instance;
|
||||||
char *image_url;
|
char *image_url;
|
||||||
@ -143,12 +145,20 @@ mg_manga_get_property (GObject *object,
|
|||||||
MgManga *
|
MgManga *
|
||||||
mg_manga_new (const char *const image_url, const char *const title, const char *id) {
|
mg_manga_new (const char *const image_url, const char *const title, const char *id) {
|
||||||
MgManga *self = NULL;
|
MgManga *self = NULL;
|
||||||
|
MgUtilString *string_util = mg_util_string_new ();
|
||||||
self = MG_MANGA ((g_object_new (MG_TYPE_MANGA, NULL)));
|
self = MG_MANGA ((g_object_new (MG_TYPE_MANGA, NULL)));
|
||||||
self->image_url = alloc_string (strlen (image_url));
|
self->image_url = mg_util_string_alloc_string (string_util,
|
||||||
self->title = alloc_string (strlen (title));
|
strlen (image_url));
|
||||||
self->id = alloc_string (strlen (id));
|
self->title = mg_util_string_alloc_string (string_util,
|
||||||
copy_substring (image_url, self->image_url, strlen(image_url) + 1, 0, strlen (image_url));
|
strlen (title));
|
||||||
copy_substring (title, self->title, strlen(title) + 1, 0, strlen (title));
|
self->id = mg_util_string_alloc_string (string_util,
|
||||||
copy_substring (id, self->id, strlen(id) + 1, 0, strlen (id));
|
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;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <libxml/HTMLparser.h>
|
#include <libxml/HTMLparser.h>
|
||||||
#include <libxml/xpath.h>
|
#include <libxml/xpath.h>
|
||||||
|
|
||||||
|
#include <openmg/util/string.h>
|
||||||
#include <openmg/util/xml.h>
|
#include <openmg/util/xml.h>
|
||||||
|
|
||||||
struct _MgUtilXML {
|
struct _MgUtilXML {
|
||||||
@ -56,6 +57,7 @@ mg_util_xml_find_class (MgUtilXML *self, xmlNodePtr node, char *class,
|
|||||||
char *
|
char *
|
||||||
mg_util_xml_get_attr (MgUtilXML *self, xmlNodePtr const node, const char *attr_name) {
|
mg_util_xml_get_attr (MgUtilXML *self, xmlNodePtr const node, const char *attr_name) {
|
||||||
char *return_value = NULL;
|
char *return_value = NULL;
|
||||||
|
MgUtilString *string_util = mg_util_string_new ();
|
||||||
if (!node) {
|
if (!node) {
|
||||||
return NULL;
|
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;
|
if (!attr->children->content) continue;
|
||||||
size_t content_len = strlen((char *)
|
size_t content_len = strlen((char *)
|
||||||
attr->children->content);
|
attr->children->content);
|
||||||
return_value = alloc_string(content_len);
|
return_value = mg_util_string_alloc_string (string_util, content_len);
|
||||||
copy_substring ((char *) attr->children->content, return_value,
|
mg_util_string_copy_substring (string_util, (char *) attr->children->content,
|
||||||
content_len,
|
return_value, content_len, 0, content_len);
|
||||||
0,
|
|
||||||
content_len);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user