File: rxpdecode.c

package info (click to toggle)
roy 1.0.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,316 kB
  • ctags: 1,564
  • sloc: ansic: 14,459; sh: 8,259; makefile: 322
file content (139 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download
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

/*
 * Copyright (C) 1999-2003, Ian Main <imain@stemwinder.org>.
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

#include <roy.h>

void
decode_msg (char *msg, unsigned int len);

void
decode_msg (char *msg, unsigned int len)
{
    RXpDecoder *decoder;
    unsigned int length;
    RBuf *bufval;
    int intval;
    unsigned int uintval;
    float floatval;
    double doubleval;
    RXpDataType type;


    decoder = rxp_decoder_new (msg, len);
    length = rxp_decoder_msg_length (decoder);
    printf ("Total message length is %u bytes.\n", length);
    
    while (1) {

        type = rxp_decode_peek_next_type (decoder);

	if (type == 0) {
	    break;
	}

        if (type == RXP_TYPE_RBUF) {
	    if (rxp_decode_rbuf (decoder, &bufval)) {
		printf ("Couldn't decode rbuf.\n");
		break;
	    }
            printf ("RBuf: '%s'\n", rbuf_str (bufval));
            rbuf_free (bufval);
        } else if (type == RXP_TYPE_NULL_RBUF) {
	    if (rxp_decode_rbuf (decoder, &bufval)) {
		printf ("Couldn't decode NULL rbuf.\n");
		break;
	    }
            printf ("NULL RBuf\n");
        } else if (type == RXP_TYPE_INT32) {
	    if (rxp_decode_rint32 (decoder, &intval)) {
		printf ("Couldn't decode int32.\n");
		break;
	    }
            printf ("rint32: %d\n", intval);
        } else if (type == RXP_TYPE_UINT32) {
	    if (rxp_decode_ruint32 (decoder, &uintval)) {
		printf ("Couldn't decode int32.\n");
		break;
	    }
            printf ("ruint32: %d\n", uintval);
        } else if (type == RXP_TYPE_FLOAT) {
	    if (rxp_decode_float (decoder, &floatval)) {
		printf ("Couldn't decode float.\n");
		break;
	    }
            printf ("float: %f\n", floatval);
        } else if (type == RXP_TYPE_DOUBLE) {
	    if (rxp_decode_double (decoder, &doubleval)) {
		printf ("Couldn't decode double.\n");
		break;
	    }
            printf ("double: %f\n", doubleval);
        }
    }
    rxp_decoder_free (decoder);
}

int main (int argc, char *argv[])
{
    RXpDecoder *decoder;
    char *buffer;
    int len;
    FILE *fp;
    
    rinit ();
  
    if (argc != 2) {
        printf ("Usage: rxpdecode <filename>\n");
        exit (0);
    }

    fp = fopen (argv[1], "r");
    if (!fp) {
        fprintf (stderr, "Error opening %s for writing: %s\n", argv[1], strerror (errno));
        exit (0);
    }

    buffer = rmem_alloc (sizeof (rint32));
    fread (buffer, sizeof (rint32), 1, fp); 

    decoder = rxp_decoder_new (buffer, sizeof (rint32));

    len = rxp_decoder_msg_length (decoder);

    buffer = rmem_realloc (buffer, len);
    fread (buffer + sizeof (rint32), len - sizeof (rint32), 1, fp); 

    decode_msg (buffer, len);
    
    rxp_decoder_free (decoder);

    rcleanup ();

    return 0;
}