File: parse.c

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (182 lines) | stat: -rw-r--r-- 3,638 bytes parent folder | download | duplicates (9)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *   DIS/x : An implementation of the IEEE 1278.1 protocol
 *
 *   Copyright (C) 1996-1998, Riley Rainey (rrainey@ix.netcom.com)
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of either:
 *
 *   a) the GNU Library General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your
 *   option) any later version.  A description of the terms and conditions
 *   of the GLPL may be found in the "COPYING.LIB" file.
 *
 *   b) the "Artistic License" which comes with this Kit.  Information
 *   about this license may be found in the "Artistic" file.
 *
 *   This library 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
 *   Library General Public License or the Artistic License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   Information describing how to contact the author can be found in the
 *   README file.
 */

#include <dis/dis.h>
#include <string.h>
#include <stdlib.h>

#define DMAX 16  /* maximum number of delimiter chars */
#define BMAX 64  /* maximum incoming string length */

/*
 *  Generate a DIS entity ID from a string
 *
 *  C-style hexidecimal numbers may be used in the input stream:
 *
 *    1/1/0xfffe, and 0xff/0xff/0x1 are both valid
 *
 *  Example invalid strings:
 *
 *    1/1/1,000    Entity ID field contains an invalid character
 *    1/1/1000000  Entity ID field > 0xffff
 *
 *  Return values:
 *
 *    0 success
 *    1 parse error
 *    2 incoming string buffer too large; (max is 64 characters)
 *    3 one or more of the fields contains an invalid value (<0 or >0xffff)
 *    4 invalid character in string
 */

int
DISParseEntityID (dis_entity_id *p, 
				  const char * buf, 
				  int bufsize,
				  const char *delim)
{
	char pdelim[DMAX+1];
	char tbuf[BMAX+1];
	char *cur, *next, *endptr;
	long rval;
	int result = 1;

	memset ( p, 0, sizeof(dis_entity_id));

	/*
	 *  Buffer too large?
	 */

	if (bufsize > BMAX ) {
		return 2;
	}

	strncpy ( tbuf, buf, BMAX );
	tbuf[BMAX] = '\0';

	if (delim) {
		strncpy(pdelim, delim, DMAX);
		pdelim[DMAX] = '\0';
	}
	else {
		strcpy( pdelim, ":./" );
	}

	cur = tbuf;

	next = strpbrk ( cur, pdelim );

	if ( next != NULL ) {

		/*
		 * Once we get a delimiter, all other delimeters must match
		 */

		pdelim[0] = *next;
		pdelim[1] = '\0';

		/*
		 *  Get Site ID
		 */

		endptr = next;
		rval = strtol ( cur, &endptr, 0 );

		if (rval < 0 || rval > 0xffff) {
			return 3;
		}
		else {
			p->sim_id.site_id = (unsigned short) rval;
		}

		/*
		 *  Ensure strtol stopped parsing at the correct spot
		 */

		if ( endptr != next ) {
			return 4;
		}

		cur = next+1;

		next = strpbrk ( cur, pdelim );

		if ( next != NULL ) {

			/*
			 *  Get application ID
			 */

			endptr = next;
			rval = strtol ( cur, &endptr, 0 );

			if (rval < 0 || rval > 0xffff) {
				return 3;
			}
			else {
				p->sim_id.application_id = (unsigned short) rval;
			}

			/*
			 *  Ensure strtol stopped parsing at the correct spot
			 */

			if ( endptr != next ) {
				return 4;
			}

			/*
			 *  Get Entity ID
			 */

			cur = next+1;

			rval = strtol ( cur, NULL, 0 );

			if (rval < 0 || rval > 0xffff) {
				return 3;
			}
			else {
				p->entity_id = (unsigned short) rval;
			}

			result = 0;

		}
	}

	return result;
}