osc/packOSC.c
static void packOSC_sendtyped(t_packOSC *x, t_symbol *s, int argc, t_atom *argv)
{
    char            messageName[MAXPDSTRING];
    unsigned int	nTypeTags = 0, typeStrTotalSize = 0;
    unsigned int    argsSize = sizeof(typedArg)*argc;
    char*           typeStr = NULL; /* might not be used */
    typedArg*       args = (typedArg*)getbytes(argsSize);
    unsigned int    i, nTagsWithData, nArgs, blobCount;
    unsigned int    m, j, k;
    char            c;

    if (args == NULL)
    {
        error("packOSC: unable to allocate %lu bytes for args", (long)argsSize);
        return;
    }
    messageName[0] = '\0'; /* empty */
    if(x->x_prefix) /* if there is a prefix, prefix it to the path */
    {
        size_t len = strlen(x->x_prefix);
        if(len >= MAXPDSTRING)
        len = MAXPDSTRING-1;

        strncpy(messageName, x->x_prefix, MAXPDSTRING);
        atom_string(&argv[0], messageName+len, (unsigned)(MAXPDSTRING-len));
    }
    else
        atom_string(&argv[0], messageName, MAXPDSTRING); /* the OSC address string */

    if (x->x_typetags & 2)
    { /* second arg is typestring */
        /* we need to find out how long the type string is before we copy it*/
        nTypeTags = (unsigned int)strlen(atom_getsymbol(&argv[1])->s_name);
        typeStrTotalSize = nTypeTags + 2;
        typeStr = (char*)getzbytes(typeStrTotalSize);
        if (typeStr == NULL)
        {
            error("packOSC: unable to allocate %u bytes for typeStr", nTypeTags);
            return;
        }
        typeStr[0] = ',';
        atom_string(&argv[1], &typeStr[1], typeStrTotalSize);
#ifdef DEBUG
        post("typeStr: %s, nTypeTags %lu", typeStr, nTypeTags);
#endif
        nArgs = argc-2;
        for (m = nTagsWithData = blobCount = 0; m < nTypeTags; ++m)
        {
#ifdef DEBUG
            post("typeStr[%d] %c", m+1, typeStr[m+1]);
#endif
            if ((c = typeStr[m+1]) == 0) break;
            if (!(c == 'T' || c == 'F' || c == 'N' || c == 'I'))
            {
                ++nTagsWithData; /* anything other than these tags have at least one data byte */
/*
    OSC-blob
    An int32 size count, followed by that many 8-bit bytes of arbitrary binary data, 
    followed by 0-3 additional zero bytes to make the total number of bits a multiple of 32.
*/
                if (c == 'b') blobCount++; /* b probably has more than one byte, so set a flag */
            }
        }
        if (((blobCount == 0)&&(nTagsWithData != nArgs)) || ((blobCount != 0)&&(nTagsWithData > nArgs)))
        {
            error("packOSC: Tags count %d doesn't match argument count %d", nTagsWithData, nArgs);
            goto cleanup;
        }
        if (blobCount > 1)
        {
            error("packOSC: Only one blob per packet at the moment...");
            goto cleanup;
        }
        for (j = k = 0; j < m; ++j) /* m is the number of tags */
        {
            c = typeStr[j+1];
            if (c == 'b')
            { /* A blob has to be the last item, until we get more elaborate. */
                if (j != m-1)
                {
                    error("packOSC: Since I don't know how big the blob is, Blob must be the last item in the list");
                    goto cleanup;
                }
                /* Pack all the remaining arguments as a blob */
                for (; k < nArgs; ++k)
                {
                    args[k] = packOSC_blob(&argv[k+2]);
                    /* Make sure it was blobbable */
                    if (args[k].type != BLOB_osc) goto cleanup;
                }
            }
            else if (!(c == 'T' || c == 'F' || c == 'N' || c == 'I')) /* not no data */
            {
                args[k] = packOSC_forceatom(&argv[k+2], c);
                ++k;
            }
        }
        if(packOSC_writetypedmessage(x, x->x_oscbuf, messageName, nArgs, args, typeStr))
        {
            error("packOSC: usage error, write-msg failed.");
            goto cleanup;
        }
    }
    else
    {
        for (i = 0; i < (unsigned)(argc-1); i++)
        {
            args[i] = packOSC_parseatom(&argv[i+1]);
#ifdef DEBUG
            switch (args[i].type)
            {
                case INT_osc:
                    post("packOSC: cell-cont: %d\n", args[i].datum.i);
                    break;
                case FLOAT_osc:
                    post("packOSC: cell-cont: %f\n", args[i].datum.f);
                    break;
                case STRING_osc:
                    post("packOSC: cell-cont: %s\n", args[i].datum.s);
                    break;
                case NOTYPE_osc:
                    post("packOSC: unknown type\n");
                    break;
            }
            post("packOSC:   type-id: %d\n", args[i].type);
#endif
        }
        if(packOSC_writemessage(x, x->x_oscbuf, messageName, i, args))
        {
            error("packOSC: usage error, write-msg failed.");
            goto cleanup;
        }
    }

    if(!x->x_bundle)
    {
        packOSC_sendbuffer(x);
        OSC_initBuffer(x->x_oscbuf, x->x_buflength, x->x_bufferForOSCbuf);
    }

cleanup:
    if (typeStr != NULL) freebytes(typeStr, typeStrTotalSize);
    if (args != NULL) freebytes(args, argsSize);
}
/* this is the real and only sending routine now, for both typed and */
/* undtyped mode. */
this is the real and only sending routine now, for both typed and
undtyped mode.
this is the real and only sending routine now for both typed and undtyped mode .
static void packOSC_sendtyped(t_packOSC *x, t_symbol *s, int argc, t_atom *argv)
{
    char            messageName[MAXPDSTRING];
    unsigned int	nTypeTags = 0, typeStrTotalSize = 0;
    unsigned int    argsSize = sizeof(typedArg)*argc;
    char*           typeStr = NULL; 
    typedArg*       args = (typedArg*)getbytes(argsSize);
    unsigned int    i, nTagsWithData, nArgs, blobCount;
    unsigned int    m, j, k;
    char            c;
    if (args == NULL)
    {
        error("packOSC: unable to allocate %lu bytes for args", (long)argsSize);
        return;
    }
    messageName[0] = '\0'; 
    if(x->x_prefix) 
    {
        size_t len = strlen(x->x_prefix);
        if(len >= MAXPDSTRING)
        len = MAXPDSTRING-1;
        strncpy(messageName, x->x_prefix, MAXPDSTRING);
        atom_string(&argv[0], messageName+len, (unsigned)(MAXPDSTRING-len));
    }
    else
        atom_string(&argv[0], messageName, MAXPDSTRING); 
    if (x->x_typetags & 2)
    { 
        nTypeTags = (unsigned int)strlen(atom_getsymbol(&argv[1])->s_name);
        typeStrTotalSize = nTypeTags + 2;
        typeStr = (char*)getzbytes(typeStrTotalSize);
        if (typeStr == NULL)
        {
            error("packOSC: unable to allocate %u bytes for typeStr", nTypeTags);
            return;
        }
        typeStr[0] = ',';
        atom_string(&argv[1], &typeStr[1], typeStrTotalSize);
#ifdef DEBUG
        post("typeStr: %s, nTypeTags %lu", typeStr, nTypeTags);
#endif
        nArgs = argc-2;
        for (m = nTagsWithData = blobCount = 0; m < nTypeTags; ++m)
        {
#ifdef DEBUG
            post("typeStr[%d] %c", m+1, typeStr[m+1]);
#endif
            if ((c = typeStr[m+1]) == 0) break;
            if (!(c == 'T' || c == 'F' || c == 'N' || c == 'I'))
            {
                ++nTagsWithData; 
                if (c == 'b') blobCount++; 
            }
        }
        if (((blobCount == 0)&&(nTagsWithData != nArgs)) || ((blobCount != 0)&&(nTagsWithData > nArgs)))
        {
            error("packOSC: Tags count %d doesn't match argument count %d", nTagsWithData, nArgs);
            goto cleanup;
        }
        if (blobCount > 1)
        {
            error("packOSC: Only one blob per packet at the moment...");
            goto cleanup;
        }
        for (j = k = 0; j < m; ++j) 
        {
            c = typeStr[j+1];
            if (c == 'b')
            { 
                if (j != m-1)
                {
                    error("packOSC: Since I don't know how big the blob is, Blob must be the last item in the list");
                    goto cleanup;
                }
                for (; k < nArgs; ++k)
                {
                    args[k] = packOSC_blob(&argv[k+2]);
                    if (args[k].type != BLOB_osc) goto cleanup;
                }
            }
            else if (!(c == 'T' || c == 'F' || c == 'N' || c == 'I')) 
            {
                args[k] = packOSC_forceatom(&argv[k+2], c);
                ++k;
            }
        }
        if(packOSC_writetypedmessage(x, x->x_oscbuf, messageName, nArgs, args, typeStr))
        {
            error("packOSC: usage error, write-msg failed.");
            goto cleanup;
        }
    }
    else
    {
        for (i = 0; i < (unsigned)(argc-1); i++)
        {
            args[i] = packOSC_parseatom(&argv[i+1]);
#ifdef DEBUG
            switch (args[i].type)
            {
                case INT_osc:
                    post("packOSC: cell-cont: %d\n", args[i].datum.i);
                    break;
                case FLOAT_osc:
                    post("packOSC: cell-cont: %f\n", args[i].datum.f);
                    break;
                case STRING_osc:
                    post("packOSC: cell-cont: %s\n", args[i].datum.s);
                    break;
                case NOTYPE_osc:
                    post("packOSC: unknown type\n");
                    break;
            }
            post("packOSC:   type-id: %d\n", args[i].type);
#endif
        }
        if(packOSC_writemessage(x, x->x_oscbuf, messageName, i, args))
        {
            error("packOSC: usage error, write-msg failed.");
            goto cleanup;
        }
    }
    if(!x->x_bundle)
    {
        packOSC_sendbuffer(x);
        OSC_initBuffer(x->x_oscbuf, x->x_buflength, x->x_bufferForOSCbuf);
    }
cleanup:
    if (typeStr != NULL) freebytes(typeStr, typeStrTotalSize);
    if (args != NULL) freebytes(args, argsSize);
}
static void packOSC_sendtyped ( t_packOSC * x , t_symbol * s , int argc , t_atom * argv ) { char messageName [ MAXPDSTRING ] ; unsigned int nTypeTags = 0 , typeStrTotalSize = 0 ; unsigned int argsSize = sizeof ( typedArg ) * argc ; char * typeStr = NULL ; typedArg * args = ( typedArg * ) getbytes ( argsSize ) ; unsigned int i , nTagsWithData , nArgs , blobCount ; unsigned int m , j , k ; char c ; if ( args == NULL ) { error ( " " , ( long ) argsSize ) ; return ; } messageName [ 0 ] = ' \0 ' ; if ( x -> x_prefix ) { size_t len = strlen ( x -> x_prefix ) ; if ( len >= MAXPDSTRING ) len = MAXPDSTRING - 1 ; strncpy ( messageName , x -> x_prefix , MAXPDSTRING ) ; atom_string ( & argv [ 0 ] , messageName + len , ( unsigned ) ( MAXPDSTRING - len ) ) ; } else atom_string ( & argv [ 0 ] , messageName , MAXPDSTRING ) ; if ( x -> x_typetags & 2 ) { nTypeTags = ( unsigned int ) strlen ( atom_getsymbol ( & argv [ 1 ] ) -> s_name ) ; typeStrTotalSize = nTypeTags + 2 ; typeStr = ( char * ) getzbytes ( typeStrTotalSize ) ; if ( typeStr == NULL ) { error ( " " , nTypeTags ) ; return ; } typeStr [ 0 ] = ' ' ; atom_string ( & argv [ 1 ] , & typeStr [ 1 ] , typeStrTotalSize ) ; #ifdef DEBUG post ( " " , typeStr , nTypeTags ) ; #endif nArgs = argc - 2 ; for ( m = nTagsWithData = blobCount = 0 ; m < nTypeTags ; ++ m ) { #ifdef DEBUG post ( " " , m + 1 , typeStr [ m + 1 ] ) ; #endif if ( ( c = typeStr [ m + 1 ] ) == 0 ) break ; if ( ! ( c == ' ' || c == ' ' || c == ' ' || c == ' ' ) ) { ++ nTagsWithData ; if ( c == ' ' ) blobCount ++ ; } } if ( ( ( blobCount == 0 ) && ( nTagsWithData != nArgs ) ) || ( ( blobCount != 0 ) && ( nTagsWithData > nArgs ) ) ) { error ( " " , nTagsWithData , nArgs ) ; goto cleanup ; } if ( blobCount > 1 ) { error ( " " ) ; goto cleanup ; } for ( j = k = 0 ; j < m ; ++ j ) { c = typeStr [ j + 1 ] ; if ( c == ' ' ) { if ( j != m - 1 ) { error ( " " ) ; goto cleanup ; } for ( ; k < nArgs ; ++ k ) { args [ k ] = packOSC_blob ( & argv [ k + 2 ] ) ; if ( args [ k ] . type != BLOB_osc ) goto cleanup ; } } else if ( ! ( c == ' ' || c == ' ' || c == ' ' || c == ' ' ) ) { args [ k ] = packOSC_forceatom ( & argv [ k + 2 ] , c ) ; ++ k ; } } if ( packOSC_writetypedmessage ( x , x -> x_oscbuf , messageName , nArgs , args , typeStr ) ) { error ( " " ) ; goto cleanup ; } } else { for ( i = 0 ; i < ( unsigned ) ( argc - 1 ) ; i ++ ) { args [ i ] = packOSC_parseatom ( & argv [ i + 1 ] ) ; #ifdef DEBUG switch ( args [ i ] . type ) { case INT_osc : post ( " \n " , args [ i ] . datum . i ) ; break ; case FLOAT_osc : post ( " \n " , args [ i ] . datum . f ) ; break ; case STRING_osc : post ( " \n " , args [ i ] . datum . s ) ; break ; case NOTYPE_osc : post ( " \n " ) ; break ; } post ( " \n " , args [ i ] . type ) ; #endif } if ( packOSC_writemessage ( x , x -> x_oscbuf , messageName , i , args ) ) { error ( " " ) ; goto cleanup ; } } if ( ! x -> x_bundle ) { packOSC_sendbuffer ( x ) ; OSC_initBuffer ( x -> x_oscbuf , x -> x_buflength , x -> x_bufferForOSCbuf ) ; } cleanup : if ( typeStr != NULL ) freebytes ( typeStr , typeStrTotalSize ) ; if ( args != NULL ) freebytes ( args , argsSize ) ; }
this is the real and only sending routine now, for both typed and
undtyped mode.
this is the real and only sending routine now for both typed and undtyped mode .
/* might not be used */ /* empty */ /* if there is a prefix, prefix it to the path */ /* the OSC address string */ /* second arg is typestring */ /* we need to find out how long the type string is before we copy it*/ /* anything other than these tags have at least one data byte */ /*
    OSC-blob
    An int32 size count, followed by that many 8-bit bytes of arbitrary binary data, 
    followed by 0-3 additional zero bytes to make the total number of bits a multiple of 32.
*/ /* b probably has more than one byte, so set a flag */ /* m is the number of tags */ /* A blob has to be the last item, until we get more elaborate. */ /* Pack all the remaining arguments as a blob */ /* Make sure it was blobbable */ /* not no data */