File: range.c

package info (click to toggle)
gphoto2 2.0final-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,572 kB
  • ctags: 5,902
  • sloc: ansic: 45,989; sh: 7,219; makefile: 1,059; xml: 474; yacc: 318
file content (247 lines) | stat: -rw-r--r-- 6,833 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* range.c
 *
 * Copyright (C) 2002 Lutz Mller <lutz@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Lesser General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include "range.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "main.h"

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

/* 
  str_grab_nat() - Grab positive decimal integer (natural number) from str 
  	starting with pos byte. On return pos points to the first byte after 
	grabbed integer.
*/

static int
str_grab_nat(const char *str, int *pos)
{
	int	result, old_pos = *pos;
	char 	*buf;

	if (!(buf = (char*)strdup(str))) /* we can not modify str */
		return -1;
	
	*pos += strspn(&buf[*pos], "0123456789");
	buf[*pos] = 0;
	result = atoi(&buf[old_pos]);
	free(buf);
	return result;
}

/*
  parse_range() - Intentionally, parse list of images given in range. Syntax
  	of range is:
		( m | m-n ) { , ( m | m-n ) }
	where m,n are decimal integers with 1 <= m <= n <= MAX_IMAGE_NUMBER.
	Ranges are XOR (exclusive or), so that 
		1-5,3,7
	is equivalent to
		1,2,4,5,7	
	Conversion from 1-based to 0-based numbering is performed, that is, 
	n-th image corresponds to (n-1)-th byte in index. The value of 
	index[n] is either 0 or 1. 0 means not selected, 1 selected. 
	index passed to parse_range() must be an array of MAX_IMAGE_NUMBER 
	char-s set to 0 (use memset() or bzero()).
*/
		
static int
parse_range_rec (const char *range, unsigned int start, char *index,
		 GPContext *context)
{
	int	i = 0, l = -1, r = -1, j;
	char buf[1024];

	do {
		strncpy (buf, range, sizeof (buf) - 1);
		buf[sizeof (buf) - 1] = 0;
		strncat (buf, "\n", sizeof (buf) - strlen (buf) - 1);
		buf[sizeof (buf) - 1] = 0;
		
		switch (range[start + i]) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			for (j = 0; j < start + i; j++) {
				strncat (buf, " ", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
			}
			l = str_grab_nat (range + start, &i);
			for (; j < start + i; j++) {
				strncat (buf, "^", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
			}
			
			if (l <= 0) {
				gp_context_error (context, _("%s\n"
					"Image IDs must be a number greater "
					"than zero."), buf);
				return (GP_ERROR_BAD_PARAMETERS);
			}
			if (l > MAX_IMAGE_NUMBER) {
				gp_context_error (context, _("%s\n"
					"Image ID %i too high."), buf, l);
				return (GP_ERROR_BAD_PARAMETERS);
			}
			break;
		
		case '-' :

			/* If we already have r, then something is wrong. */
			if (r > 0) {
				for (j = 0; j < start + i; j++) {
					strncat (buf, " ", sizeof (buf) - strlen (buf) - 1);
					buf[sizeof (buf) - 1] = 0;
				}
				strncat (buf, "^", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
				gp_context_error (context, _("%s\n"
					"Ranges must be separated by ','."),
					buf);
				return (GP_ERROR_BAD_PARAMETERS);
			}

			/* If we have not l, then something is wrong, too. */
			if (l <= 0) {
				for (j = 0; j < start + i; j++) {
					strncat (buf, " ", sizeof (buf) - strlen (buf) - 1);
					buf[sizeof (buf) - 1] = 0;
				}
				strncat (buf, "^", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
				gp_context_error (context, _("%s\n"
					"Ranges need to start with "
					"a number."), buf);
				return (GP_ERROR_BAD_PARAMETERS);
			}

			i++;
			for (j = 0; j < start + i; j++) {
				strncat (buf, " ", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
			}
			r = str_grab_nat (range + start, &i);
			for (; j < start + i; j++) {
				strncat (buf, "^", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
			}
			buf[sizeof (buf) - 1] = 0;
			if (r <= 0) {
				gp_context_error (context, _("%s\n"
					"Image IDs must be a number greater "
					"than zero."), buf);
				return (GP_ERROR_BAD_PARAMETERS);
			}
			if (r > MAX_IMAGE_NUMBER) {
				gp_context_error (context, _("%s\n"
					"Image ID %i too high."), buf, r);
				return (GP_ERROR_BAD_PARAMETERS);
			}
			break;
			
		case '\0' :
			break; 
			
		default :
			for (j = 0; j < start + i; j++) {
				strncat (buf, " ", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
			}
			strncat (buf, "^", sizeof (buf) - strlen (buf) - 1);
			buf[sizeof (buf) - 1] = 0;
			gp_context_error (context, _("%s\n"
				"Unexpected "
				"character '%c'."), buf, range[start + i]);
			return (GP_ERROR_BAD_PARAMETERS);
		}
	} while ((range[start + i] != '\0') &&
		 (range[start + i] != ','));

	if (i) {
		if (0 < r) { /* update range of bytes */ 
			if (r < l) {
				strncpy (buf, range, sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
				strncat (buf, "\n", sizeof (buf) - strlen (buf) - 1);
				buf[sizeof (buf) - 1] = 0;
				for (j = 0; j < start; j++) {
					strncat (buf, " ", sizeof (buf) - strlen (buf) - 1);
					buf[sizeof (buf) - 1] = 0;
				}
				for (; j < start + i; j++) {
					strncat (buf, "^", sizeof (buf) - strlen (buf) - 1);
					buf[sizeof (buf) - 1] = 0;
				}
				gp_context_error (context, _("%s\n"
					"Decreasing ranges "
					"are not allowed. You specified a "
					"range from %i to %i."), buf, l, r);
				return (GP_ERROR_BAD_PARAMETERS);
			}

			for (j = l; j <= r; j++)
				index[j - 1] ^= 1; /* convert to 0-based numbering */
		} else  /* update single byte */
			index[l - 1] ^= 1; /* convert to 0-based numbering */
	}

	/*
	 * If there is another range, parse it.
	 */
	if (range[start + i] == ',') 
		return parse_range_rec (range, start + i + 1, index, context);
	
	return (GP_OK);
}

int
parse_range (const char *range, char *index, GPContext *context)
{
	return (parse_range_rec (range, 0, index, context));
}