1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "share/grabbag.h"
24 #include "flac/assert.h"
25 #include <stdlib.h> /* for atoi() */
29 /* There's no strtoll() in MSVC6 so we just write a specialized one */
30 static FLAC__int64 local__strtoll(const char *src, char **endptr)
32 FLAC__bool neg = false;
35 FLAC__ASSERT(0 != src);
40 while(0 != (c = *src)) {
50 return neg? -ret : ret;
54 FLAC__bool grabbag__seektable_convert_specification_to_template(const char *spec, FLAC__bool only_explicit_placeholders, FLAC__uint64 total_samples_to_encode, unsigned sample_rate, FLAC__StreamMetadata *seektable_template, FLAC__bool *spec_has_real_points)
59 FLAC__ASSERT(0 != spec);
60 FLAC__ASSERT(0 != seektable_template);
61 FLAC__ASSERT(seektable_template->type = FLAC__METADATA_TYPE_SEEKTABLE);
63 if(0 != spec_has_real_points)
64 *spec_has_real_points = false;
66 for(pt = spec, i = 0; pt && *pt; i++) {
67 const char *q = strchr(pt, ';');
71 if(0 == strncmp(pt, "X;", 2)) { /* -S X */
72 if(!FLAC__metadata_object_seektable_template_append_placeholders(seektable_template, 1))
75 else if(q[-1] == 'x') { /* -S #x */
76 if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
77 if(0 != spec_has_real_points)
78 *spec_has_real_points = true;
79 if(!only_explicit_placeholders) {
80 const int n = (unsigned)atoi(pt);
82 if(!FLAC__metadata_object_seektable_template_append_spaced_points(seektable_template, (unsigned)n, total_samples_to_encode))
87 else if(q[-1] == 's') { /* -S #s */
88 if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
89 FLAC__ASSERT(sample_rate > 0);
90 if(0 != spec_has_real_points)
91 *spec_has_real_points = true;
92 if(!only_explicit_placeholders) {
93 const double sec = atof(pt);
95 unsigned samples = (unsigned)(sec * (double)sample_rate);
97 /* +1 for the initial point at sample 0 */
98 if(!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(seektable_template, samples, total_samples_to_encode))
106 if(0 != spec_has_real_points)
107 *spec_has_real_points = true;
108 if(!only_explicit_placeholders) {
111 const FLAC__int64 n = local__strtoll(pt, &endptr);
113 const FLAC__int64 n = (FLAC__int64)strtoll(pt, &endptr, 10);
116 (n > 0 || (endptr > pt && *endptr == ';')) && /* is a valid number (extra check needed for "0") */
117 (total_samples_to_encode == 0 || (FLAC__uint64)n < total_samples_to_encode) /* number is not >= the known total_samples_to_encode */
119 if(!FLAC__metadata_object_seektable_template_append_point(seektable_template, (FLAC__uint64)n))
128 if(!FLAC__metadata_object_seektable_template_sort(seektable_template, /*compact=*/true))