]> code.delx.au - pulseaudio/blobdiff - polyp/tagstruct.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / tagstruct.c
index 9578a9eb64db57f5d8d2cc958ac0795c6b23b2e4..a6dad868c87ae8504190ac735bbc0b55fccdba6a 100644 (file)
@@ -4,17 +4,17 @@
   This file is part of polypaudio.
  
   polypaudio 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; either version 2 of the License,
-  or (at your option) any later version.
+  it under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
  
   polypaudio 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.
+  Lesser General Public License for more details.
  
-  You should have received a copy of the GNU General Public License
-  along with polypaudio; if not, write to the Free Software
+  You should have received a copy of the GNU Lesser General Public
+  License along with polypaudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
 ***/
@@ -25,6 +25,8 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <sys/time.h>
 #include <netinet/in.h>
 #include <assert.h>
 
 
 enum tags {
     TAG_STRING = 't',
+    TAG_NULL_STRING = 'N',
     TAG_U32 = 'L',
     TAG_S32 = 'l',
     TAG_U16 = 'S',
     TAG_S16 = 's',
     TAG_U8 = 'B',
     TAG_S8 = 'b',
+    TAG_U64 = 'R',
+    TAG_S64 = 'r',
     TAG_SAMPLE_SPEC = 'a',
-    TAG_ARBITRARY = 'x'
+    TAG_ARBITRARY = 'x',
+    TAG_BOOLEAN_TRUE = '1',
+    TAG_BOOLEAN_FALSE = '0',
+    TAG_TIMEVAL = 'T',
+    TAG_USEC = 'U'  /* 64bit unsigned */
 };
 
 struct pa_tagstruct {
@@ -91,12 +100,18 @@ static void extend(struct pa_tagstruct*t, size_t l) {
 
 void pa_tagstruct_puts(struct pa_tagstruct*t, const char *s) {
     size_t l;
-    assert(t && s);
-    l = strlen(s)+2;
-    extend(t, l);
-    t->data[t->length] = TAG_STRING;
-    strcpy(t->data+t->length+1, s);
-    t->length += l;
+    assert(t);
+    if (s) {
+        l = strlen(s)+2;
+        extend(t, l);
+        t->data[t->length] = TAG_STRING;
+        strcpy((char*) (t->data+t->length+1), s);
+        t->length += l;
+    } else {
+        extend(t, 1);
+        t->data[t->length] = TAG_NULL_STRING;
+        t->length += 1;
+    }
 }
 
 void pa_tagstruct_putu32(struct pa_tagstruct*t, uint32_t i) {
@@ -125,7 +140,6 @@ void pa_tagstruct_put_sample_spec(struct pa_tagstruct *t, const struct pa_sample
     t->length += 7;
 }
 
-
 void pa_tagstruct_put_arbitrary(struct pa_tagstruct *t, const void *p, size_t length) {
     assert(t && p);
 
@@ -137,12 +151,55 @@ void pa_tagstruct_put_arbitrary(struct pa_tagstruct *t, const void *p, size_t le
     t->length += 5+length;
 }
 
+void pa_tagstruct_put_boolean(struct pa_tagstruct*t, int b) {
+    assert(t);
+    extend(t, 1);
+    t->data[t->length] = b ? TAG_BOOLEAN_TRUE : TAG_BOOLEAN_FALSE;
+    t->length += 1;
+}
+
+void pa_tagstruct_put_timeval(struct pa_tagstruct*t, const struct timeval *tv) {
+    assert(t);
+    extend(t, 9);
+    t->data[t->length] = TAG_TIMEVAL;
+    *((uint32_t*) (t->data+t->length+1)) = htonl(tv->tv_sec);
+    *((uint32_t*) (t->data+t->length+5)) = htonl(tv->tv_usec);
+    t->length += 9;
+}
+
+void pa_tagstruct_put_usec(struct pa_tagstruct*t, pa_usec_t u) {
+    assert(t);
+    extend(t, 9);
+    t->data[t->length] = TAG_USEC;
+    *((uint32_t*) (t->data+t->length+1)) = htonl((uint32_t) (u >> 32));
+    *((uint32_t*) (t->data+t->length+5)) = htonl((uint32_t) u);
+    t->length += 9;
+}
+
+void pa_tagstruct_putu64(struct pa_tagstruct*t, uint64_t u) {
+    assert(t);
+    extend(t, 9);
+    t->data[t->length] = TAG_U64;
+    *((uint32_t*) (t->data+t->length+1)) = htonl((uint32_t) (u >> 32));
+    *((uint32_t*) (t->data+t->length+5)) = htonl((uint32_t) u);
+    t->length += 9;
+}
+
 int pa_tagstruct_gets(struct pa_tagstruct*t, const char **s) {
     int error = 0;
     size_t n;
     char *c;
     assert(t && s);
 
+    if (t->rindex+1 > t->length)
+        return -1;
+
+    if (t->data[t->rindex] == TAG_NULL_STRING) {
+        t->rindex++;
+        *s = NULL;
+        return 0;
+    }
+    
     if (t->rindex+2 > t->length)
         return -1;
     
@@ -238,3 +295,64 @@ const uint8_t* pa_tagstruct_data(struct pa_tagstruct*t, size_t *l) {
     return t->data;
 }
 
+int pa_tagstruct_get_boolean(struct pa_tagstruct*t, int *b) {
+    assert(t && b);
+
+    if (t->rindex+1 > t->length)
+        return -1;
+
+    if (t->data[t->rindex] == TAG_BOOLEAN_TRUE)
+        *b = 1;
+    else if (t->data[t->rindex] == TAG_BOOLEAN_FALSE)
+        *b = 0;
+    else
+        return -1;
+    
+    t->rindex +=1;
+    return 0;
+}
+
+int pa_tagstruct_get_timeval(struct pa_tagstruct*t, struct timeval *tv) {
+
+    if (t->rindex+9 > t->length)
+        return -1;
+
+    if (t->data[t->rindex] != TAG_TIMEVAL)
+        return -1;
+    
+    tv->tv_sec = ntohl(*((uint32_t*) (t->data+t->rindex+1)));
+    tv->tv_usec = ntohl(*((uint32_t*) (t->data+t->rindex+5)));
+    t->rindex += 9;
+    return 0;
+    
+}
+
+int pa_tagstruct_get_usec(struct pa_tagstruct*t, pa_usec_t *u) {
+    assert(t && u);
+
+    if (t->rindex+9 > t->length)
+        return -1;
+
+    if (t->data[t->rindex] != TAG_USEC)
+        return -1;
+
+    *u = (pa_usec_t) ntohl(*((uint32_t*) (t->data+t->rindex+1))) << 32;
+    *u |= (pa_usec_t) ntohl(*((uint32_t*) (t->data+t->rindex+5)));
+    t->rindex +=9;
+    return 0;
+}
+
+int pa_tagstruct_getu64(struct pa_tagstruct*t, uint64_t *u) {
+    assert(t && u);
+
+    if (t->rindex+9 > t->length)
+        return -1;
+
+    if (t->data[t->rindex] != TAG_U64)
+        return -1;
+
+    *u = (uint64_t) ntohl(*((uint32_t*) (t->data+t->rindex+1))) << 32;
+    *u |= (uint64_t) ntohl(*((uint32_t*) (t->data+t->rindex+5)));
+    t->rindex +=9;
+    return 0;
+}