Remove local_strtoull, windows has equivalent function _strtoui64
[flac.git] / src / share / grabbag / seektable.c
1 /* grabbag - Convenience lib for various routines common to several tools
2  * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "share/grabbag.h"
24 #include "share/compat.h"
25 #include "FLAC/assert.h"
26 #include <stdlib.h> /* for atoi() */
27 #include <string.h>
28
29 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)
30 {
31         unsigned i;
32         const char *pt;
33
34         FLAC__ASSERT(0 != spec);
35         FLAC__ASSERT(0 != seektable_template);
36         FLAC__ASSERT(seektable_template->type = FLAC__METADATA_TYPE_SEEKTABLE);
37
38         if(0 != spec_has_real_points)
39                 *spec_has_real_points = false;
40
41         for(pt = spec, i = 0; pt && *pt; i++) {
42                 const char *q = strchr(pt, ';');
43                 FLAC__ASSERT(0 != q);
44
45                 if(q > pt) {
46                         if(0 == strncmp(pt, "X;", 2)) { /* -S X */
47                                 if(!FLAC__metadata_object_seektable_template_append_placeholders(seektable_template, 1))
48                                         return false;
49                         }
50                         else if(q[-1] == 'x') { /* -S #x */
51                                 if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
52                                         if(0 != spec_has_real_points)
53                                                 *spec_has_real_points = true;
54                                         if(!only_explicit_placeholders) {
55                                                 const int n = (unsigned)atoi(pt);
56                                                 if(n > 0)
57                                                         if(!FLAC__metadata_object_seektable_template_append_spaced_points(seektable_template, (unsigned)n, total_samples_to_encode))
58                                                                 return false;
59                                         }
60                                 }
61                         }
62                         else if(q[-1] == 's') { /* -S #s */
63                                 if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
64                                         FLAC__ASSERT(sample_rate > 0);
65                                         if(0 != spec_has_real_points)
66                                                 *spec_has_real_points = true;
67                                         if(!only_explicit_placeholders) {
68                                                 const double sec = atof(pt);
69                                                 if(sec > 0.0) {
70                                                         unsigned samples = (unsigned)(sec * (double)sample_rate);
71                                                         if(samples > 0) {
72                                                                 /* +1 for the initial point at sample 0 */
73                                                                 if(!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(seektable_template, samples, total_samples_to_encode))
74                                                                         return false;
75                                                         }
76                                                 }
77                                         }
78                                 }
79                         }
80                         else { /* -S # */
81                                 if(0 != spec_has_real_points)
82                                         *spec_has_real_points = true;
83                                 if(!only_explicit_placeholders) {
84                                         char *endptr;
85                                         const FLAC__int64 n = (FLAC__int64)strtoll(pt, &endptr, 10);
86                                         if(
87                                                 (n > 0 || (endptr > pt && *endptr == ';')) && /* is a valid number (extra check needed for "0") */
88                                                 (total_samples_to_encode == 0 || (FLAC__uint64)n < total_samples_to_encode) /* number is not >= the known total_samples_to_encode */
89                                         )
90                                                 if(!FLAC__metadata_object_seektable_template_append_point(seektable_template, (FLAC__uint64)n))
91                                                         return false;
92                                 }
93                         }
94                 }
95
96                 pt = ++q;
97         }
98
99         if(!FLAC__metadata_object_seektable_template_sort(seektable_template, /*compact=*/true))
100                 return false;
101
102         return true;
103 }