[go: up one dir, main page]

Menu

[8f9142]: / src / bundle.c  Maximize  Restore  History

Download this file

150 lines (119 with data), 2.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2004 Steve Harris
*
* This program 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.
*
* This program 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.
*
* $Id$
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lo_types_internal.h"
#include "lo/lo.h"
lo_bundle lo_bundle_new(lo_timetag tt)
{
lo_bundle b = calloc(1, sizeof(struct _lo_bundle));
b->size = 4;
b->len = 0;
b->ts = tt;
b->msgs = calloc(b->size, sizeof(lo_message));
b->paths = calloc(b->size, sizeof(char *));
return b;
}
void lo_bundle_add_message(lo_bundle b, const char *path, lo_message m)
{
if (!m) {
return;
}
if (b->len >= b->size) {
b->size *= 2;
b->msgs = realloc(b->msgs, b->size * sizeof(lo_message));
b->paths = realloc(b->paths, b->size * sizeof(char *));
}
b->msgs[b->len] = m;
b->paths[b->len] = (char *)path;
(b->len)++;
}
size_t lo_bundle_length(lo_bundle b)
{
size_t size = 16; /* "#bundle" and the timetag */
int i;
if (!b) {
return 0;
}
size += b->len * 4; /* sizes */
for (i = 0; i < b->len; i++) {
size += lo_message_length(b->msgs[i], b->paths[i]);
}
return size;
}
void *lo_bundle_serialise(lo_bundle b, void *to, size_t *size)
{
size_t s, skip;
int32_t *bes;
int i;
char *pos;
lo_pcast32 be;
if (!b) {
return NULL;
}
s = lo_bundle_length(b);
if (size) {
*size = s;
}
if (!to) {
to = calloc(1, s);
}
pos = to;
strcpy(pos, "#bundle");
pos += 8;
be.nl = lo_htoo32(b->ts.sec);
memcpy(pos, &be, 4);
pos += 4;
be.nl = lo_htoo32(b->ts.frac);
memcpy(pos, &be, 4);
pos += 4;
for (i = 0; i < b->len; i++) {
lo_message_serialise(b->msgs[i], b->paths[i], pos + 4, &skip);
bes = (int32_t *)pos;
*bes = lo_htoo32(skip);
pos += skip + 4;
if (pos > (char *)to + s) {
fprintf(stderr, "liblo: data integrity error at message %d\n", i);
return NULL;
}
}
if (pos != to + s) {
fprintf(stderr, "liblo: data integrity error\n");
return NULL;
}
return to;
}
void lo_bundle_free(lo_bundle b)
{
if (!b) {
return;
}
free(b->msgs);
free(b->paths);
free(b);
}
void lo_bundle_pp(lo_bundle b)
{
int i;
if (!b) return;
printf("bundle(%f):\n", (double)b->ts.sec + b->ts.frac / 4294967296.0);
for (i = 0; i < b->len; i++) {
lo_message_pp(b->msgs[i]);
}
printf("\n");
}
/* vi:set ts=8 sts=4 sw=4: */