]> code.delx.au - pulseaudio/blobdiff - polyp/tagstruct.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / tagstruct.c
index 55132caee57d5329ee670a09fdef85e34a2452d2..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_BOOLEAN_TRUE = '1',
     TAG_BOOLEAN_FALSE = '0',
     TAG_TIMEVAL = 'T',
+    TAG_USEC = 'U'  /* 64bit unsigned */
 };
 
 struct pa_tagstruct {
@@ -94,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((char*) (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) {
@@ -155,12 +167,39 @@ void pa_tagstruct_put_timeval(struct pa_tagstruct*t, const struct timeval *tv) {
     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;
     
@@ -288,3 +327,32 @@ int pa_tagstruct_get_timeval(struct pa_tagstruct*t, struct timeval *tv) {
     
 }
 
+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;
+}