summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1564b26)
raw | patch | inline | side by side (parent: 1564b26)
author | Sebastian Harl <sh@teamix.net> | |
Wed, 12 Sep 2012 13:31:44 +0000 (15:31 +0200) | ||
committer | Sebastian Harl <sh@teamix.net> | |
Wed, 12 Sep 2012 13:31:44 +0000 (15:31 +0200) |
For now, the class provides a method to append to the string in a sprintf()
fashion.
fashion.
src/Makefile.am | patch | blob | history | |
src/junos.h | patch | blob | history | |
src/strbuf.c | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile.am b/src/Makefile.am
index ffee374c83fa8c351fc59c3e8369f2eaf5d1d2b5..fa170d33df06637f4387c06ca8050d110d987415 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
libjunos_la_SOURCES = junos.c junos.h \
libjunos_features.h \
access_ssh.c \
- netrc.c
+ netrc.c \
+ strbuf.c
libjunos_la_CFLAGS = $(AM_CFLAGS) \
@LIBSSH2_CFLAGS@ \
@LIBXML2_CFLAGS@
diff --git a/src/junos.h b/src/junos.h
index 5f31e917de5140306ad4fbaa92498b4736171318..055cd03204a472ae53c4b13c21ee5bf58f5694c8 100644 (file)
--- a/src/junos.h
+++ b/src/junos.h
typedef struct junos junos_t;
+/* string buffer */
+
+typedef struct junos_strbuf junos_strbuf_t;
+
+/* netrc */
+
typedef struct {
char *machine;
char *login;
void
junos_free(junos_t *junos);
+/*
+ * string buffer
+ */
+
+junos_strbuf_t *
+junos_strbuf_new(size_t size);
+
+void
+junos_strbuf_free(junos_strbuf_t *strbuf);
+
+ssize_t
+junos_strbuf_sprintf(junos_strbuf_t *strbuf, const char *fmt, ...);
+
+ssize_t
+junos_strbuf_vsprintf(junos_strbuf_t *strbuf, const char *fmt, va_list ap);
+
+char *
+junos_strbuf_string(junos_strbuf_t *strbuf);
+
+size_t
+junos_strbuf_len(junos_strbuf_t *strbuf);
+
/*
* netrc
*/
diff --git a/src/strbuf.c b/src/strbuf.c
--- /dev/null
+++ b/src/strbuf.c
@@ -0,0 +1,164 @@
+/*
+ * libJUNOS - src/strbuf.c
+ * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * 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 HOLDERS 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.
+ */
+
+/*
+ * String buffer object.
+ */
+
+#include "junos.h"
+
+#include <assert.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+/*
+ * private data structures
+ */
+
+struct junos_strbuf {
+ char *string;
+ size_t size;
+ size_t pos;
+};
+
+/*
+ * private helper functions
+ */
+
+static int
+strbuf_resize(junos_strbuf_t *strbuf)
+{
+ char *tmp;
+
+ tmp = realloc(strbuf->string, 2 * strbuf->size);
+ if (! tmp)
+ return -1;
+
+ strbuf->string = tmp;
+ strbuf->size *= 2;
+
+ strbuf->string[strbuf->pos] = '\0';
+ return 0;
+} /* strbuf_resize */
+
+/*
+ * public API
+ */
+
+junos_strbuf_t *
+junos_strbuf_new(size_t size)
+{
+ junos_strbuf_t *strbuf;
+
+ strbuf = calloc(1, sizeof(*strbuf));
+ if (! strbuf)
+ return NULL;
+
+ if (! size)
+ return strbuf;
+
+ strbuf->string = malloc(size);
+ if (strbuf->string) {
+ strbuf->string[0] = '\0';
+ strbuf->size = size;
+ }
+
+ return strbuf;
+} /* junos_strbuf_new */
+
+void
+junos_strbuf_free(junos_strbuf_t *strbuf)
+{
+ if (! strbuf)
+ return;
+
+ free(strbuf->string);
+ free(strbuf);
+} /* junos_strbuf_free */
+
+ssize_t
+junos_strbuf_vsprintf(junos_strbuf_t *strbuf, const char *fmt, va_list ap)
+{
+ int status;
+
+ if (! strbuf)
+ return -1;
+
+ assert(strbuf->string[strbuf->pos] == '\0');
+
+ if (strbuf->pos >= strbuf->size)
+ if (strbuf_resize(strbuf))
+ return -1;
+
+ status = vsnprintf(strbuf->string + strbuf->pos,
+ strbuf->size - strbuf->pos, fmt, ap);
+
+ if (status < 0)
+ return status;
+
+ if ((size_t)status >= strbuf->size - strbuf->pos) {
+ strbuf_resize(strbuf);
+ return junos_strbuf_vsprintf(strbuf, fmt, ap);
+ }
+
+ strbuf->pos += (size_t)status;
+ return (ssize_t)strbuf->pos;
+} /* junos_strbuf_vsprintf */
+
+ssize_t
+junos_strbuf_sprintf(junos_strbuf_t *strbuf, const char *fmt, ...)
+{
+ va_list ap;
+ ssize_t status;
+
+ va_start(ap, fmt);
+ status = junos_strbuf_vsprintf(strbuf, fmt, ap);
+ va_end(ap);
+
+ return status;
+} /* junos_strbuf_sprintf */
+
+char *
+junos_strbuf_string(junos_strbuf_t *strbuf)
+{
+ if (! strbuf)
+ return NULL;
+ return strbuf->string;
+} /* junos_strbuf_string */
+
+size_t
+junos_strbuf_len(junos_strbuf_t *strbuf)
+{
+ if (! strbuf)
+ return 0;
+ return strbuf->pos;
+} /* junos_strbuf_string */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+