libisdn
strstream.c
Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  *
00005  */
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <stdarg.h>
00009 #include <string.h>
00010 
00011 #include "strstream.h"
00012 
00013 int strstream_init(struct strstream *s, const int size)
00014 {
00015         if (!s || !size)
00016                 return -1;
00017 
00018         s->buf = malloc(size);
00019         if (!s->buf)
00020                 return -1;
00021 
00022         memset(s->buf, 0, size);
00023         s->offset = 0;
00024         s->size   = size;
00025         return 0;
00026 }
00027 
00028 int strstream_init_static(struct strstream *s, char *buf, const int size)
00029 {
00030         if (!s || !size || !buf)
00031                 return -1;
00032 
00033         s->buf = buf;
00034         memset(s->buf, 0, size);
00035         s->offset = 0;
00036         s->size   = size;
00037         return 0;
00038 }
00039 
00040 int strstream_printf(struct strstream *s, const char *fmt, ...)
00041 {
00042         int ret = 0;
00043         va_list ap;
00044 
00045         if (!s || !fmt)
00046                 return -1;
00047 
00048         va_start(ap, fmt);
00049         ret = vsnprintf(&s->buf[s->offset], s->size - s->offset, fmt, ap);
00050         va_end(ap);
00051 
00052         if (ret > 0) {
00053                 s->offset += ret;
00054                 s->buf[s->size - 1] = '\0';
00055         }
00056         return ret;
00057 }
00058 
00059 int strstream_puts(struct strstream *s, const char *str)
00060 {
00061         if (!s || !str)
00062                 return -1;
00063 
00064         if (strstream_left(s) <= strlen(str))
00065                 return -1;
00066 
00067         strncat(&s->buf[s->offset], str, s->size - s->offset);
00068         s->buf[s->size - 1] = '\0';
00069         s->offset = strlen(s->buf);
00070         return 0;
00071 }
00072 
00073 int strstream_printhex(struct strstream *s, const char *buf, const int size)
00074 {
00075         static const char hex[16] = {
00076                 '0', '1', '2', '3', '4', '5',
00077                 '6', '7', '8', '9', 'a', 'b', 'c',
00078                 'e', 'f'
00079         };
00080 
00081         if (!s || !buf || !size)
00082                 return -1;
00083 
00084         if (strstream_left(s) < ((size << 1) + size))
00085                 return -1;
00086 
00087         for (int x = 0; x < size; x++) {
00088                 s->buf[s->offset++] = hex[((buf)[x] & 0xf0) >> 4];
00089                 s->buf[s->offset++] = hex[((buf)[x] & 0x0f)];
00090                 if (x + 1 < size)
00091                         s->buf[s->offset++] = ' ';
00092         }
00093         s->buf[s->offset] = '\0';
00094 
00095         return 0;
00096 }
00097 
00098 int strstream_length(struct strstream *s)
00099 {
00100         return s->offset;
00101 }
00102 
00103 int strstream_size(struct strstream *s)
00104 {
00105         return s->size;
00106 }
00107 
00108 int strstream_left(struct strstream *s)
00109 {
00110         return (s->size - s->offset);
00111 }
00112 
00113 const char *strstream_get(struct strstream *s)
00114 {
00115         return s->buf;
00116 }