/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>

#include <descrip.h>

#include "simple_stomp.h"

static void desc2buf(struct dsc$descriptor *desc, char *buf)
{
    int slen;
    char *sptr;
    if(desc->dsc$b_class == DSC$K_CLASS_S)
    {
        slen = desc->dsc$w_length;
        sptr = desc->dsc$a_pointer;
    }
    if(desc->dsc$b_class == DSC$K_CLASS_VS)
    {
        slen =*((short int *)desc->dsc$a_pointer);
        sptr = desc->dsc$a_pointer + sizeof(short int);
    }
    memcpy(buf, sptr, slen);
    buf[slen] = 0;
}

static void print(char *msg)
{
   printf("Error: %s\n", msg);
}

void stomp_send(struct dsc$descriptor *host,
                int port,
                struct dsc$descriptor *dest,
                struct dsc$descriptor *data)
{
    char host2[256];
    char dest2[256];
    char data2[256];
    simple_stomp_t ctx;
    desc2buf(host, host2);
    desc2buf(dest, dest2);
    desc2buf(data, data2);
    simple_stomp_debug(0);
    simple_stomp_init(&ctx, host2, port, print);
    simple_stomp_write(&ctx, dest2, data2);
    simple_stomp_close(&ctx);
}
