qdevice: Add functions to manipulate dynar string

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
This commit is contained in:
Jan Friesse 2016-04-05 18:00:52 +02:00
parent 2c05051a16
commit 5ed0acf586
3 changed files with 290 additions and 0 deletions

108
qdevices/dynar-str.c Normal file
View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2015-2016 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Jan Friesse (jfriesse@redhat.com)
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Red Hat, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "dynar-str.h"
int
dynar_str_cpy(struct dynar *dest, const char *str)
{
if (strlen(str) > dynar_max_size(dest)) {
return (-1);
}
dynar_clean(dest);
return (dynar_str_cat(dest, str));
}
int
dynar_str_cat(struct dynar *dest, const char *str)
{
return (dynar_cat(dest, str, strlen(str)));
}
int
dynar_str_catf(struct dynar *dest, const char *format, ...)
{
int to_write;
int written;
va_list ap;
size_t allocated;
char buf;
char *p;
/*
* Find out how much bytes is needed
*/
va_start(ap, format);
to_write = vsnprintf(&buf, sizeof(buf), format, ap);
va_end(ap);
if (to_write < 0) {
return (-1);
}
if (to_write < sizeof(buf)) {
/*
* Writing 1 byte string (snprintf writes also '\0') means string is empty
*/
return (0);
}
allocated = to_write + 1;
if (dynar_prealloc(dest, allocated) != 0) {
return (-1);
}
p = dynar_data(dest) + dynar_size(dest);
va_start(ap, format);
written = vsnprintf(p, allocated, format, ap);
va_end(ap);
if (written >= allocated) {
return (-1);
}
dest->size += written;
return (written);
}

55
qdevices/dynar-str.h Normal file
View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2015-2016 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Jan Friesse (jfriesse@redhat.com)
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Red Hat, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _DYNAR_STR_H_
#define _DYNAR_STR_H_
#include "dynar.h"
#ifdef __cplusplus
extern "C" {
#endif
extern int dynar_str_cpy(struct dynar *dest, const char *str);
extern int dynar_str_cat(struct dynar *dest, const char *str);
extern int dynar_str_catf(struct dynar *dest, const char *format, ...)
__attribute__((__format__(__printf__, 2, 3)));
#ifdef __cplusplus
}
#endif
#endif /* _DYNAR_STR_H_ */

127
qdevices/test-dynar.c Normal file
View File

@ -0,0 +1,127 @@
/*
* Copyright (c) 2015-2016 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Jan Friesse (jfriesse@redhat.com)
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Red Hat, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "dynar.h"
#include "dynar-str.h"
int
main(void)
{
struct dynar str;
dynar_init(&str, 3);
assert(dynar_cat(&str, "a", 1) == 0);
assert(dynar_size(&str) == 1);
assert(dynar_cat(&str, "b", 1) == 0);
assert(dynar_size(&str) == 2);
assert(dynar_cat(&str, "c", 1) == 0);
assert(dynar_size(&str) == 3);
assert(dynar_cat(&str, "d", 1) != 0);
assert(memcmp(dynar_data(&str), "abc", 3) == 0);
dynar_clean(&str);
assert(dynar_size(&str) == 0);
dynar_set_max_size(&str, 4);
assert(dynar_cat(&str, "a", 1) == 0);
assert(dynar_size(&str) == 1);
assert(memcmp(dynar_data(&str), "a", 1) == 0);
assert(dynar_cat(&str, "b", 1) == 0);
assert(dynar_size(&str) == 2);
assert(dynar_cat(&str, "c", 1) == 0);
assert(dynar_size(&str) == 3);
assert(dynar_cat(&str, "d", 1) == 0);
assert(dynar_size(&str) == 4);
assert(memcmp(dynar_data(&str), "abcd", 4) == 0);
assert(dynar_str_cpy(&str, "e") == 0);
assert(dynar_size(&str) == 1);
assert(memcmp(dynar_data(&str), "e", 1) == 0);
assert(dynar_str_cpy(&str, "fgh") == 0);
assert(dynar_size(&str) == 3);
assert(memcmp(dynar_data(&str), "fgh", 3) == 0);
assert(dynar_str_cpy(&str, "fghi") == 0);
assert(dynar_size(&str) == 4);
assert(memcmp(dynar_data(&str), "fghi", 4) == 0);
assert(dynar_str_cpy(&str, "fghij") != 0);
assert(dynar_size(&str) == 4);
assert(dynar_str_cat(&str, "a") != 0);
assert(dynar_str_cpy(&str, "") == 0);
assert(dynar_size(&str) == 0);
assert(dynar_str_cat(&str, "a") == 0);
assert(dynar_size(&str) == 1);
assert(memcmp(dynar_data(&str), "a", 1) == 0);
assert(dynar_str_cat(&str, "b") == 0);
assert(dynar_size(&str) == 2);
assert(memcmp(dynar_data(&str), "ab", 2) == 0);
assert(dynar_str_cat(&str, "cd") == 0);
assert(dynar_size(&str) == 4);
assert(memcmp(dynar_data(&str), "abcb", 1) == 0);
assert(dynar_str_cpy(&str, "") == 0);
assert(dynar_str_catf(&str, "%s", "a") == 1);
assert(memcmp(dynar_data(&str), "a", 1) == 0);
assert(dynar_str_catf(&str, "%s", "ab") == 2);
assert(memcmp(dynar_data(&str), "aab", 3) == 0);
assert(dynar_str_cpy(&str, "") == 0);
assert(dynar_str_catf(&str, "%s", "abc") == 3);
assert(dynar_str_cpy(&str, "") == 0);
assert(dynar_str_catf(&str, "%s", "abcd") == -1);
assert(dynar_str_cpy(&str, "a") == 0);
assert(dynar_str_catf(&str, "%s", "") == 0);
assert(memcmp(dynar_data(&str), "a", 1) == 0);
dynar_destroy(&str);
dynar_init(&str, 5);
assert(dynar_str_catf(&str, "%s", "abcd") == 4);
dynar_destroy(&str);
dynar_init(&str, 5);
assert(dynar_str_catf(&str, "%s", "") == 0);
assert(dynar_str_catf(&str, "%s", "abc") == 3);
assert(dynar_str_catf(&str, "%s", "d") == 1);
assert(memcmp(dynar_data(&str), "abcd", 4) == 0);
dynar_destroy(&str);
return (0);
}