Added string_replace()
This commit is contained in:
parent
07e414905f
commit
1d340abbe1
2
Makefile
2
Makefile
|
@ -40,6 +40,7 @@ dist:
|
||||||
$(PACKAGE)-$(VERSION)/include/System/plugin.h \
|
$(PACKAGE)-$(VERSION)/include/System/plugin.h \
|
||||||
$(PACKAGE)-$(VERSION)/include/System/string.h \
|
$(PACKAGE)-$(VERSION)/include/System/string.h \
|
||||||
$(PACKAGE)-$(VERSION)/include/System/token.h \
|
$(PACKAGE)-$(VERSION)/include/System/token.h \
|
||||||
|
$(PACKAGE)-$(VERSION)/include/System/variable.h \
|
||||||
$(PACKAGE)-$(VERSION)/include/System/Makefile \
|
$(PACKAGE)-$(VERSION)/include/System/Makefile \
|
||||||
$(PACKAGE)-$(VERSION)/include/System/project.conf \
|
$(PACKAGE)-$(VERSION)/include/System/project.conf \
|
||||||
$(PACKAGE)-$(VERSION)/src/array.c \
|
$(PACKAGE)-$(VERSION)/src/array.c \
|
||||||
|
@ -53,6 +54,7 @@ dist:
|
||||||
$(PACKAGE)-$(VERSION)/src/plugin.c \
|
$(PACKAGE)-$(VERSION)/src/plugin.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/string.c \
|
$(PACKAGE)-$(VERSION)/src/string.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/token.c \
|
$(PACKAGE)-$(VERSION)/src/token.c \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/variable.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/Makefile \
|
$(PACKAGE)-$(VERSION)/src/Makefile \
|
||||||
$(PACKAGE)-$(VERSION)/src/token.h \
|
$(PACKAGE)-$(VERSION)/src/token.h \
|
||||||
$(PACKAGE)-$(VERSION)/src/project.conf \
|
$(PACKAGE)-$(VERSION)/src/project.conf \
|
||||||
|
|
|
@ -28,5 +28,6 @@
|
||||||
# include "System/parser.h"
|
# include "System/parser.h"
|
||||||
# include "System/plugin.h"
|
# include "System/plugin.h"
|
||||||
# include "System/string.h"
|
# include "System/string.h"
|
||||||
|
# include "System/variable.h"
|
||||||
|
|
||||||
#endif /* !LIBSYSTEM_SYSTEM_H */
|
#endif /* !LIBSYSTEM_SYSTEM_H */
|
||||||
|
|
|
@ -38,6 +38,8 @@ install:
|
||||||
$(INSTALL) -m 0644 -- string.h $(DESTDIR)$(INCLUDEDIR)/System/string.h
|
$(INSTALL) -m 0644 -- string.h $(DESTDIR)$(INCLUDEDIR)/System/string.h
|
||||||
$(MKDIR) $(DESTDIR)$(INCLUDEDIR)/System
|
$(MKDIR) $(DESTDIR)$(INCLUDEDIR)/System
|
||||||
$(INSTALL) -m 0644 -- token.h $(DESTDIR)$(INCLUDEDIR)/System/token.h
|
$(INSTALL) -m 0644 -- token.h $(DESTDIR)$(INCLUDEDIR)/System/token.h
|
||||||
|
$(MKDIR) $(DESTDIR)$(INCLUDEDIR)/System
|
||||||
|
$(INSTALL) -m 0644 -- variable.h $(DESTDIR)$(INCLUDEDIR)/System/variable.h
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/array.h
|
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/array.h
|
||||||
|
@ -52,5 +54,6 @@ uninstall:
|
||||||
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/plugin.h
|
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/plugin.h
|
||||||
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/string.h
|
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/string.h
|
||||||
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/token.h
|
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/token.h
|
||||||
|
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/variable.h
|
||||||
|
|
||||||
.PHONY: all clean distclean install uninstall
|
.PHONY: all clean distclean install uninstall
|
||||||
|
|
|
@ -46,6 +46,8 @@ int parser_remove_callback(Parser * parser, ParserCallback callback);
|
||||||
int parser_add_filter(Parser * parser, ParserFilter filter, void * data);
|
int parser_add_filter(Parser * parser, ParserFilter filter, void * data);
|
||||||
int parser_remove_filter(Parser * parser, ParserFilter filter);
|
int parser_remove_filter(Parser * parser, ParserFilter filter);
|
||||||
|
|
||||||
|
int parser_inject(Parser * parser, char const * string);
|
||||||
|
|
||||||
int parser_scan(Parser * parser);
|
int parser_scan(Parser * parser);
|
||||||
int parser_scan_filter(Parser * parser);
|
int parser_scan_filter(Parser * parser);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
includes=array.h,buffer.h,config.h,error.h,event.h,file.h,hash.h,object.h,parser.h,plugin.h,string.h,token.h
|
includes=array.h,buffer.h,config.h,error.h,event.h,file.h,hash.h,object.h,parser.h,plugin.h,string.h,token.h,variable.h
|
||||||
dist=Makefile
|
dist=Makefile
|
||||||
|
|
||||||
[array.h]
|
[array.h]
|
||||||
|
@ -36,3 +36,6 @@ install=$(INCLUDEDIR)/System
|
||||||
|
|
||||||
[token.h]
|
[token.h]
|
||||||
install=$(INCLUDEDIR)/System
|
install=$(INCLUDEDIR)/System
|
||||||
|
|
||||||
|
[variable.h]
|
||||||
|
install=$(INCLUDEDIR)/System
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/* $Id$ */
|
/* $Id$ */
|
||||||
/* Copyright (c) 2009 Pierre Pronchery <khorben@defora.org> */
|
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
|
||||||
/* This file is part of DeforaOS System libSystem */
|
/* This file is part of DeforaOS System libSystem */
|
||||||
/* This program is free software: you can redistribute it and/or modify
|
/* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -49,4 +49,6 @@ String ** string_explode(String const * string, String const * separator);
|
||||||
String * string_find(String const * string, String const * key);
|
String * string_find(String const * string, String const * key);
|
||||||
ssize_t string_index(String const * string, String const * key);
|
ssize_t string_index(String const * string, String const * key);
|
||||||
|
|
||||||
|
int string_replace(String ** string, String const * what, String const * by);
|
||||||
|
|
||||||
#endif /* !LIBSYSTEM_STRING_H */
|
#endif /* !LIBSYSTEM_STRING_H */
|
||||||
|
|
|
@ -18,7 +18,7 @@ INSTALL = install
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
libSystem_OBJS = array.o buffer.o config.o error.o event.o hash.o object.o parser.o plugin.o string.o token.o
|
libSystem_OBJS = array.o buffer.o config.o error.o event.o hash.o object.o parser.o plugin.o string.o token.o variable.o
|
||||||
libSystem_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
|
libSystem_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
|
||||||
libSystem_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
|
libSystem_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
|
||||||
|
|
||||||
|
@ -62,6 +62,9 @@ string.o: string.c
|
||||||
token.o: token.c token.h
|
token.o: token.c token.h
|
||||||
$(CC) $(libSystem_CFLAGS) -c token.c
|
$(CC) $(libSystem_CFLAGS) -c token.c
|
||||||
|
|
||||||
|
variable.o: variable.c
|
||||||
|
$(CC) $(libSystem_CFLAGS) -c variable.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) -- $(libSystem_OBJS)
|
$(RM) -- $(libSystem_OBJS)
|
||||||
|
|
||||||
|
|
|
@ -213,6 +213,7 @@ int config_load(Config * config, char const * filename)
|
||||||
string_delete(section);
|
string_delete(section);
|
||||||
return error_set_code(1, "%s: %s", filename, strerror(errno));
|
return error_set_code(1, "%s: %s", filename, strerror(errno));
|
||||||
}
|
}
|
||||||
|
/* FIXME unescape backslashes (eg allow multiple lines) */
|
||||||
for(line = 0; (c = fgetc(fp)) != EOF; line++)
|
for(line = 0; (c = fgetc(fp)) != EOF; line++)
|
||||||
if(c == '#')
|
if(c == '#')
|
||||||
while((c = fgetc(fp)) != EOF && c != '\n');
|
while((c = fgetc(fp)) != EOF && c != '\n');
|
||||||
|
@ -425,6 +426,7 @@ void _save_foreach_section(void const * key, void * value, void * data)
|
||||||
|
|
||||||
if(*fp == NULL)
|
if(*fp == NULL)
|
||||||
return;
|
return;
|
||||||
|
/* FIXME escape lines with a backslash */
|
||||||
if(val == NULL || fprintf(*fp, "%s=%s\n", var, val) >= 0)
|
if(val == NULL || fprintf(*fp, "%s=%s\n", var, val) >= 0)
|
||||||
return;
|
return;
|
||||||
fclose(*fp);
|
fclose(*fp);
|
||||||
|
|
55
src/parser.c
55
src/parser.c
|
@ -35,6 +35,7 @@ typedef struct _ParserCallbackData ParserCallbackData;
|
||||||
struct _Parser
|
struct _Parser
|
||||||
{
|
{
|
||||||
/* parsing sources */
|
/* parsing sources */
|
||||||
|
char * inject;
|
||||||
char * filename;
|
char * filename;
|
||||||
FILE * fp;
|
FILE * fp;
|
||||||
char * string;
|
char * string;
|
||||||
|
@ -45,7 +46,6 @@ struct _Parser
|
||||||
unsigned int line;
|
unsigned int line;
|
||||||
unsigned int col;
|
unsigned int col;
|
||||||
int last;
|
int last;
|
||||||
unsigned int lookahead;
|
|
||||||
|
|
||||||
ParserFilter scanner;
|
ParserFilter scanner;
|
||||||
|
|
||||||
|
@ -80,21 +80,28 @@ int parser_scan_filter(Parser * parser)
|
||||||
int c = parser->last;
|
int c = parser->last;
|
||||||
size_t i;
|
size_t i;
|
||||||
ParserFilterData * pfd;
|
ParserFilterData * pfd;
|
||||||
int l;
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "DEBUG: %s(%p)\n", __func__, parser);
|
fprintf(stderr, "DEBUG: %s(%p)\n", __func__, parser);
|
||||||
#endif
|
#endif
|
||||||
if(parser->lookahead)
|
if(parser->inject)
|
||||||
parser->lookahead--;
|
{
|
||||||
|
c = parser->inject[0];
|
||||||
|
if((i = strlen(parser->inject)) > 1)
|
||||||
|
memmove(parser->inject, &parser->inject[1], i);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string_delete(parser->inject);
|
||||||
|
parser->inject = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
else if(parser->scanner(&c, parser) != 0)
|
else if(parser->scanner(&c, parser) != 0)
|
||||||
return EOF; /* FIXME report error */
|
return EOF; /* FIXME report error */
|
||||||
for(i = 0; i < parser->filters_cnt; i++)
|
for(i = 0; i < parser->filters_cnt; i++)
|
||||||
{
|
{
|
||||||
pfd = &parser->filters[i];
|
pfd = &parser->filters[i];
|
||||||
if((l = pfd->filter(&c, pfd->data)) < 0)
|
if(pfd->filter(&c, pfd->data) != 0)
|
||||||
return EOF;
|
return EOF;
|
||||||
parser->lookahead += l;
|
|
||||||
}
|
}
|
||||||
parser->last = c;
|
parser->last = c;
|
||||||
return c;
|
return c;
|
||||||
|
@ -181,6 +188,7 @@ static Parser * _new_do(ParserFilter scanner)
|
||||||
|
|
||||||
if((parser = object_new(sizeof(*parser))) == NULL)
|
if((parser = object_new(sizeof(*parser))) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
parser->inject = NULL;
|
||||||
parser->filename = NULL;
|
parser->filename = NULL;
|
||||||
parser->fp = NULL;
|
parser->fp = NULL;
|
||||||
parser->string = NULL;
|
parser->string = NULL;
|
||||||
|
@ -189,7 +197,6 @@ static Parser * _new_do(ParserFilter scanner)
|
||||||
parser->line = 1;
|
parser->line = 1;
|
||||||
parser->col = 1;
|
parser->col = 1;
|
||||||
parser->last = EOF;
|
parser->last = EOF;
|
||||||
parser->lookahead = 0;
|
|
||||||
parser->scanner = scanner;
|
parser->scanner = scanner;
|
||||||
parser->filters = NULL;
|
parser->filters = NULL;
|
||||||
parser->filters_cnt = 0;
|
parser->filters_cnt = 0;
|
||||||
|
@ -231,6 +238,7 @@ int parser_delete(Parser * parser)
|
||||||
fprintf(stderr, "DEBUG: %s(%p) \"%s\"\n", __func__, parser,
|
fprintf(stderr, "DEBUG: %s(%p) \"%s\"\n", __func__, parser,
|
||||||
parser->filename);
|
parser->filename);
|
||||||
#endif
|
#endif
|
||||||
|
free(parser->inject);
|
||||||
if(parser->fp != NULL
|
if(parser->fp != NULL
|
||||||
&& fclose(parser->fp) != 0)
|
&& fclose(parser->fp) != 0)
|
||||||
ret = error_set_code(1, "%s: %s", parser->filename,
|
ret = error_set_code(1, "%s: %s", parser->filename,
|
||||||
|
@ -282,6 +290,15 @@ int parser_get_token(Parser * parser, Token ** token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
/* parser_set_lookahead */
|
||||||
|
void parser_set_lookahead(Parser * parser, unsigned int lookahead)
|
||||||
|
{
|
||||||
|
parser->lookahead = lookahead;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* useful */
|
/* useful */
|
||||||
/* parser_add_callback */
|
/* parser_add_callback */
|
||||||
int parser_add_callback(Parser * parser, ParserCallback callback, void * data)
|
int parser_add_callback(Parser * parser, ParserCallback callback, void * data)
|
||||||
|
@ -318,6 +335,30 @@ int parser_add_filter(Parser * parser, ParserFilter filter, void * data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* parser_inject */
|
||||||
|
int parser_inject(Parser * parser, char const * string)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
char buf[2] = "\0";
|
||||||
|
|
||||||
|
if(string == NULL || string[0] == '\0')
|
||||||
|
return 0; /* don't bother */
|
||||||
|
c = parser->last;
|
||||||
|
parser->last = string[0];
|
||||||
|
if(parser->inject == NULL)
|
||||||
|
{
|
||||||
|
if((parser->inject = string_new(&string[1])) == NULL)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else if(string_append(&parser->inject, &string[1]) != 0)
|
||||||
|
return -1;
|
||||||
|
buf[0] = c;
|
||||||
|
if(c != EOF && string_append(&parser->inject, buf) != 0)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* parser_remove_callback */
|
/* parser_remove_callback */
|
||||||
int parser_remove_callback(Parser * parser, ParserCallback callback)
|
int parser_remove_callback(Parser * parser, ParserCallback callback)
|
||||||
/* FIXME untested */
|
/* FIXME untested */
|
||||||
|
|
|
@ -7,7 +7,7 @@ dist=Makefile,token.h
|
||||||
|
|
||||||
[libSystem]
|
[libSystem]
|
||||||
type=library
|
type=library
|
||||||
sources=array.c,buffer.c,config.c,error.c,event.c,hash.c,object.c,parser.c,plugin.c,string.c,token.c
|
sources=array.c,buffer.c,config.c,error.c,event.c,hash.c,object.c,parser.c,plugin.c,string.c,token.c,variable.c
|
||||||
ldflags=-ldl
|
ldflags=-ldl
|
||||||
install=$(LIBDIR)
|
install=$(LIBDIR)
|
||||||
|
|
||||||
|
|
37
src/string.c
37
src/string.c
|
@ -1,5 +1,5 @@
|
||||||
/* $Id$ */
|
/* $Id$ */
|
||||||
/* Copyright (c) 2010 Pierre Pronchery <khorben@defora.org> */
|
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
|
||||||
/* This file is part of DeforaOS System libSystem */
|
/* This file is part of DeforaOS System libSystem */
|
||||||
/* This program is free software: you can redistribute it and/or modify
|
/* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -249,3 +249,38 @@ ssize_t string_index(String const * string, String const * key)
|
||||||
return -1;
|
return -1;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* string_replace */
|
||||||
|
int string_replace(String ** string, String const * what, String const * by)
|
||||||
|
{
|
||||||
|
String * ret = NULL;
|
||||||
|
String const * p;
|
||||||
|
size_t len = string_length(what);
|
||||||
|
ssize_t index;
|
||||||
|
String * q;
|
||||||
|
|
||||||
|
for(p = *string; (index = string_index(p, what)) >= 0; p += index + len)
|
||||||
|
{
|
||||||
|
if((q = string_new_length(p, index)) == NULL
|
||||||
|
|| string_append(&ret, q) != 0
|
||||||
|
|| string_append(&ret, by) != 0)
|
||||||
|
{
|
||||||
|
string_delete(q);
|
||||||
|
string_delete(ret);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
string_delete(q);
|
||||||
|
}
|
||||||
|
if(ret != NULL)
|
||||||
|
{
|
||||||
|
if(string_append(&ret, p) != 0)
|
||||||
|
{
|
||||||
|
string_delete(ret);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
string_delete(*string);
|
||||||
|
*string = ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user