Revised the API

This commit is contained in:
Pierre Pronchery 2012-10-29 01:55:12 +01:00
parent 3b027c7e0b
commit eed6144f63
9 changed files with 123 additions and 28 deletions

View File

@ -62,6 +62,7 @@ dist:
$(PACKAGE)-$(VERSION)/include/System/plugin.h \
$(PACKAGE)-$(VERSION)/include/System/string.h \
$(PACKAGE)-$(VERSION)/include/System/token.h \
$(PACKAGE)-$(VERSION)/include/System/variable.h \
$(PACKAGE)-$(VERSION)/include/System/Makefile \
$(PACKAGE)-$(VERSION)/include/System/project.conf \
$(PACKAGE)-$(VERSION)/src/array.c \

View File

@ -28,5 +28,6 @@
# include "System/parser.h"
# include "System/plugin.h"
# include "System/string.h"
# include "System/variable.h"
#endif /* !LIBSYSTEM_SYSTEM_H */

View File

@ -38,6 +38,8 @@ install:
$(INSTALL) -m 0644 -- string.h $(DESTDIR)$(INCLUDEDIR)/System/string.h
$(MKDIR) $(DESTDIR)$(INCLUDEDIR)/System
$(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:
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/array.h
@ -52,5 +54,6 @@ uninstall:
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/plugin.h
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/string.h
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/token.h
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/System/variable.h
.PHONY: all clean distclean install uninstall

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2007-2012 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS System libSystem */
/* 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
@ -37,6 +37,7 @@ void event_delete(Event * event);
/* useful */
int event_loop(Event * event);
void event_loop_quit(Event * event);
int event_register_idle(Event * event, EventTimeoutFunc func, void * userdata);
int event_register_io_read(Event * event, int fd, EventIOFunc func,
void * userdata);
int event_register_io_write(Event * event, int fd, EventIOFunc func,

View File

@ -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
[array.h]
@ -36,3 +36,6 @@ install=$(INCLUDEDIR)/System
[token.h]
install=$(INCLUDEDIR)/System
[variable.h]
install=$(INCLUDEDIR)/System

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2011-2012 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS System libSystem */
/* 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
@ -26,6 +26,11 @@
typedef char String;
/* macros */
/* XXX for compatibility */
#define string_length(a) string_get_length(a)
/* functions */
String * string_new(String const * string);
String * string_new_append(String const * string, ...);
@ -33,10 +38,10 @@ String * string_new_length(String const * string, size_t length);
void string_delete(String * string);
/* accessors */
int string_set(String ** string, String const * string2);
size_t string_get_length(String const * string);
size_t string_get_size(String const * string);
/* returns */
size_t string_length(String const * string);
int string_set(String ** string, String const * string2);
/* useful */
int string_append(String ** string, String const * append);

61
include/System/variable.h Normal file
View File

@ -0,0 +1,61 @@
/* $Id$ */
/* Copyright (c) 2012 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS System libSystem */
/* 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
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef LIBSYSTEM_VARIABLE_H
# define LIBSYSTEM_VARIABLE_H
# include <sys/types.h>
# include "buffer.h"
# include "string.h"
/* Variable */
/* types */
typedef struct _Variable Variable;
typedef enum _VariableType
{
VT_NULL = 0,
VT_INT8,
VT_UINT8,
VT_INT16,
VT_UINT16,
VT_INT32,
VT_UINT32,
VT_INT64,
VT_UINT64,
VT_STRING
} VariableType;
/* functions */
Variable * variable_new(VariableType type);
void variable_delete(Variable * variable);
/* accessors */
int variable_get_as_integer(Variable * variable, int64_t integer);
int variable_get_as_uinteger(Variable * variable, uint64_t integer);
String * variable_get_as_string(Variable * variable);
int variable_set_from_integer(Variable * variable, int64_t integer);
int variable_set_from_uinteger(Variable * variable, uint64_t integer);
/* useful */
int variable_serialize(Variable * variable, Buffer * buffer);
#endif /* !LIBSYSTEM_VARIABLE_H */

View File

@ -147,9 +147,9 @@ int event_loop(Event * event)
while(event->loop && (timeout != NULL || event->fdmax != -1))
{
if(select(event->fdmax + 1, &rfds, &wfds, NULL, timeout) < 0)
return error_set_code(1, "%s", strerror(errno));
return -error_set_code(1, "%s", strerror(errno));
if(_loop_timeout(event) != 0)
return 1;
return -1;
_loop_io(event, event->reads, &rfds);
_loop_io(event, event->writes, &wfds);
if(event->timeout.tv_sec == (time_t)LONG_MAX
@ -193,7 +193,8 @@ static int _loop_timeout(Event * event)
et->timeout.tv_sec = et->initial.tv_sec + now.tv_sec;
et->timeout.tv_usec = et->initial.tv_usec + now.tv_usec;
if(et->initial.tv_sec < event->timeout.tv_sec
|| (et->initial.tv_sec == event->timeout.tv_sec
|| (et->initial.tv_sec
== event->timeout.tv_sec
&& et->initial.tv_usec
< event->timeout.tv_usec))
{
@ -269,6 +270,17 @@ void event_loop_quit(Event * event)
}
/* event_register_idle */
int event_register_idle(Event * event, EventTimeoutFunc func, void * data)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
return event_register_timeout(event, &tv, func, data);
}
/* event_register_io_read */
int event_register_io_read(Event * event, int fd, EventIOFunc func,
void * userdata)
@ -315,9 +327,9 @@ int event_register_timeout(Event * event, struct timeval * timeout,
struct timeval now;
if(gettimeofday(&now, NULL) != 0)
return error_set_code(1, "%s", strerror(errno));
return -error_set_code(1, "%s", strerror(errno));
if((eventtimeout = object_new(sizeof(*eventtimeout))) == NULL)
return 1;
return -1;
eventtimeout->initial.tv_sec = timeout->tv_sec;
eventtimeout->initial.tv_usec = timeout->tv_usec;
eventtimeout->timeout.tv_sec = now.tv_sec + timeout->tv_sec;

View File

@ -12,6 +12,8 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* TODO:
* - add string_new_format(), string_rtrim(), string_ltrim()... */
@ -87,10 +89,27 @@ void string_delete(String * string)
/* accessors */
/* string_get_length */
size_t string_get_length(String const * string)
{
size_t length;
for(length = 0; string[length] != '\0'; length++);
return length;
}
/* string_get_size */
size_t string_get_size(String const * string)
{
return string_get_length(string) + 1;
}
/* string_set */
int string_set(String ** string, String const * string2)
{
size_t len = string_length(string2);
size_t len = string_get_length(string2);
if(object_resize((Object**)string, len + 1) != 0)
return 1;
@ -100,27 +119,16 @@ int string_set(String ** string, String const * string2)
}
/* returns */
/* string_length */
size_t string_length(String const * string)
{
size_t length;
for(length = 0; string[length] != '\0'; length++);
return length;
}
/* useful */
/* string_append */
int string_append(String ** string, String const * append)
{
size_t slength = (*string != NULL) ? string_length(*string) : 0;
size_t slength = (*string != NULL) ? string_get_length(*string) : 0;
size_t alength;
if(append == NULL)
return error_set_code(1, "%s", strerror(EINVAL));
if((alength = string_length(append)) == 0)
if((alength = string_get_length(append)) == 0)
return 0;
if(object_resize((Object**)string, slength + alength + 1) != 0)
return 1;
@ -199,7 +207,7 @@ String ** string_explode(String const * string, String const * separator)
fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\")\n", __func__, string,
separator);
#endif
if(separator == NULL || (l = string_length(separator)) == 0)
if(separator == NULL || (l = string_get_length(separator)) == 0)
{
error_set_code(1, "%s", strerror(EINVAL));
return NULL;
@ -257,7 +265,7 @@ ssize_t string_index(String const * string, String const * key)
size_t len;
ssize_t i;
len = string_length(key);
len = string_get_length(key);
for(i = 0; string[i] != '\0' && string_compare_length(&string[i], key,
len) != 0; i++);
if(string[i] == '\0')
@ -271,7 +279,7 @@ int string_replace(String ** string, String const * what, String const * by)
{
String * ret = NULL;
String const * p;
size_t len = string_length(what);
size_t len = string_get_length(what);
ssize_t index;
String * q;