提交 ed9788c7 编写于 作者: R Richard W.M. Jones

Add extra utility functions to buf.c

	* src/buf.c, src/buf.h: Added utility functions
	  virBufferURIEncodeString and virBufferAddChar to the
	  buffer module.
上级 a11494fd
Mon Dec 17 10:01:00 UTC 2007 Richard W.M. Jones <rjones@redhat.com>
Add extra utility functions to buf.c
* src/buf.c, src/buf.h: Added utility functions
virBufferURIEncodeString and virBufferAddChar to the
buffer module.
Sat Dec 15 12:12:14 EST 2007 Daniel P. Berrange <berrange@redhat.com>
* src/libvirt.c: Return error code if passed unsupported
......
......@@ -16,6 +16,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "buf.h"
/**
......@@ -49,7 +51,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len)
/**
* virBufferAdd:
* @buf: the buffer to dump
* @buf: the buffer to add to
* @str: the string
* @len: the number of bytes to add
*
......@@ -78,13 +80,41 @@ virBufferAdd(virBufferPtr buf, const char *str, int len)
return (-1);
}
}
/* XXX: memmove() is 2x slower than memcpy(), do we really need it? */
memmove(&buf->content[buf->use], str, len);
memcpy (&buf->content[buf->use], str, len);
buf->use += len;
buf->content[buf->use] = 0;
return (0);
}
/**
* virBufferAddChar:
* @buf: the buffer to add to
* @c: the character to add
*
* Add a single character 'c' to a buffer.
*
* Returns 0 if successful, -1 in the case of error.
*/
int
virBufferAddChar (virBufferPtr buf, char c)
{
unsigned int needSize;
if (buf == NULL)
return -1;
needSize = buf->use + 2;
if (needSize > buf->size)
if (!virBufferGrow (buf, needSize - buf->use))
return -1;
buf->content[buf->use++] = c;
buf->content[buf->use] = 0;
return 0;
}
/**
* virBufferNew:
* @size: creation size in bytes
......@@ -266,6 +296,56 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
return (0);
}
/**
* virBufferURIEncodeString:
* @buf: the buffer to append to
* @str: the string argument which will be URI-encoded
*
* Append the string to the buffer. The string will be URI-encoded
* during the append (ie any non alpha-numeric characters are replaced
* with '%xx' hex sequences).
*
* Returns 0 successful, -1 in case of internal or API error.
*/
int
virBufferURIEncodeString (virBufferPtr buf, const char *str)
{
int grow_size = 0;
const char *p;
unsigned char uc;
const char *hex = "0123456789abcdef";
for (p = str; *p; ++p) {
/* This may not work on EBCDIC. */
if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9'))
grow_size++;
else
grow_size += 3; /* %ab */
}
if (virBufferGrow (buf, grow_size) == -1)
return -1;
for (p = str; *p; ++p) {
/* This may not work on EBCDIC. */
if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9'))
buf->content[buf->use++] = *p;
else {
uc = (unsigned char) *p;
buf->content[buf->use++] = '%';
buf->content[buf->use++] = hex[uc >> 4];
buf->content[buf->use++] = hex[uc & 0xf];
}
}
buf->content[buf->use] = '\0';
return 0;
}
/**
* virBufferStrcat:
* @buf: the buffer to dump
......
......@@ -30,9 +30,11 @@ virBufferPtr virBufferNew(unsigned int size);
void virBufferFree(virBufferPtr buf);
char *virBufferContentAndFree(virBufferPtr buf);
int virBufferAdd(virBufferPtr buf, const char *str, int len);
int virBufferAddChar(virBufferPtr buf, char c);
int virBufferVSprintf(virBufferPtr buf, const char *format, ...)
ATTRIBUTE_FORMAT(printf, 2, 3);
int virBufferStrcat(virBufferPtr buf, ...);
int virBufferEscapeString(virBufferPtr buf, const char *format, const char *str);
int virBufferURIEncodeString (virBufferPtr buf, const char *str);
#endif /* __VIR_BUFFER_H__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册