diff --git a/Makefile b/Makefile index 9f0aac9..0a372cb 100644 --- a/Makefile +++ b/Makefile @@ -101,6 +101,7 @@ dist: $(PACKAGE)-$(VERSION)/tests/variable.c \ $(PACKAGE)-$(VERSION)/tests/COPYING \ $(PACKAGE)-$(VERSION)/tests/Makefile \ + $(PACKAGE)-$(VERSION)/tests/python.sh \ $(PACKAGE)-$(VERSION)/tests/tests.sh \ $(PACKAGE)-$(VERSION)/tests/project.conf \ $(PACKAGE)-$(VERSION)/tools/configctl.c \ diff --git a/include/System/string.h b/include/System/string.h index 7f0bdb0..3d45bca 100644 --- a/include/System/string.h +++ b/include/System/string.h @@ -34,6 +34,7 @@ typedef char String; /* functions */ String * string_new(String const * string); String * string_new_append(String const * string, ...); +String * string_new_format(String const * format, ...); String * string_new_length(String const * string, size_t length); void string_delete(String * string); diff --git a/src/string.c b/src/string.c index 8242628..15acf07 100644 --- a/src/string.c +++ b/src/string.c @@ -13,7 +13,7 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ /* TODO: - * - add string_new_format(), string_rtrim(), string_ltrim()... */ + * - add string_rtrim(), string_ltrim()... */ @@ -68,6 +68,37 @@ String * string_new_append(String const * string, ...) } +/* string_new_format */ +String * string_new_format(String const * format, ...) +{ + String * ret; + va_list ap; + int len; + size_t s; + + va_start(ap, format); + len = vsnprintf(NULL, 0, format, ap); + va_end(ap); + if(len < 0) + { + error_set_code(1, "%s", strerror(errno)); + return NULL; + } + s = (size_t)len + 1; + if((ret = object_new(s)) == NULL) + return NULL; + va_start(ap, format); + if(vsnprintf(ret, s, format, ap) != len) + { + error_set_code(1, "%s", strerror(errno)); + object_delete(ret); + ret = NULL; + } + va_end(ap); + return ret; +} + + /* string_new_length */ String * string_new_length(String const * string, size_t length) { diff --git a/tests/Makefile b/tests/Makefile index ed6c3ea..f445583 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -39,7 +39,7 @@ variable_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) variable: $(variable_OBJS) $(CC) -o variable $(variable_OBJS) $(variable_LDFLAGS) -tests.log: includes string tests.sh variable +tests.log: includes python.sh string tests.sh variable ./tests.sh -P "$(PREFIX)" -- "tests.log" includes.o: includes.c diff --git a/tests/project.conf b/tests/project.conf index 4727179..e298f6c 100644 --- a/tests/project.conf +++ b/tests/project.conf @@ -4,7 +4,7 @@ cflags_force=-W cflags=-Wall -g -O2 ldflags_force=-L ../src -lSystem ldflags=-L$(PREFIX)/lib -Wl,-rpath,"$(PWD)/../src" -dist=COPYING,Makefile,tests.sh +dist=COPYING,Makefile,python.sh,tests.sh [includes] type=binary @@ -20,7 +20,7 @@ depends=../src/string.c [tests.log] type=script script=./tests.sh -depends=includes,string,tests.sh,variable +depends=includes,python.sh,string,tests.sh,variable [variable] type=binary diff --git a/tests/python.sh b/tests/python.sh new file mode 100755 index 0000000..19561ed --- /dev/null +++ b/tests/python.sh @@ -0,0 +1,24 @@ +#!/bin/sh +#$Id$ +#Copyright (c) 2014 Pierre Pronchery +#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 . + + + +#variables +#executables +MAKE="make" + + +(cd ../src/python && $MAKE clean all) diff --git a/tests/string.c b/tests/string.c index bb7c447..e6dcbb4 100644 --- a/tests/string.c +++ b/tests/string.c @@ -30,6 +30,7 @@ static int _test(char const * progname, String * s) printf("%s: Testing %s\n", progname, "string_get_length()"); if((len = string_get_length(s)) == 0) return 2; + printf("\"%s\" => %lu\n", s, len); /* string_clear */ printf("%s: Testing %s\n", progname, "string_clear()"); string_clear(s); @@ -50,5 +51,9 @@ int main(int argc, char * argv[]) return 2; ret |= _test(argv[0], s); string_delete(s); + if((s = string_new_format("%s", argv[0])) == NULL) + return 2; + ret |= _test(argv[0], s); + string_delete(s); return 0; } diff --git a/tests/tests.sh b/tests/tests.sh index 1829c7a..5d1c7d3 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -101,6 +101,8 @@ echo "Performing tests:" 1>&2 _test "includes" _test "string" _test "variable" +echo "Expected failures:" 1>&2 +_fail "python.sh" if [ -n "$FAILED" ]; then echo "Failed tests:$FAILED" 1>&2 exit 2