add 2206 to copyright notice
[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  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include "share/grabbag.h"
20 #include "FLAC/assert.h"
21 #include <stdlib.h> /* for atoi() */
22 #include <string.h>
23
24 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)
25 {
26         unsigned i;
27         const char *pt;
28
29         FLAC__ASSERT(0 != spec);
30         FLAC__ASSERT(0 != seektable_template);
31         FLAC__ASSERT(seektable_template->type = FLAC__METADATA_TYPE_SEEKTABLE);
32
33         if(0 != spec_has_real_points)
34                 *spec_has_real_points = false;
35
36         for(pt = spec, i = 0; pt && *pt; i++) {
37                 const char *q = strchr(pt, ';');
38                 FLAC__ASSERT(0 != q);
39
40                 if(q > pt) {
41                         if(0 == strncmp(pt, "X;", 2)) { /* -S X */
42                                 if(!FLAC__metadata_object_seektable_template_append_placeholders(seektable_template, 1))
43                                         return false;
44                         }
45                         else if(q[-1] == 'x') { /* -S #x */
46                                 if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
47                                         if(0 != spec_has_real_points)
48                                                 *spec_has_real_points = true;
49                                         if(!only_explicit_placeholders) {
50                                                 if(!FLAC__metadata_object_seektable_template_append_spaced_points(seektable_template, atoi(pt), total_samples_to_encode))
51                                                         return false;
52                                         }
53                                 }
54                         }
55                         else if(q[-1] == 's') { /* -S #s */
56                                 if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
57                                         FLAC__ASSERT(sample_rate > 0);
58                                         if(0 != spec_has_real_points)
59                                                 *spec_has_real_points = true;
60                                         if(!only_explicit_placeholders) {
61                                                 double sec = atof(pt);
62                                                 if(sec > 0.0) {
63 #if defined _MSC_VER || defined __MINGW32__
64                                                         /* with MSVC you have to spoon feed it the casting */
65                                                         unsigned n = (unsigned)((double)(FLAC__int64)total_samples_to_encode / (sec * (double)sample_rate));
66 #else
67                                                         unsigned n = (unsigned)((double)total_samples_to_encode / (sec * (double)sample_rate));
68 #endif
69                                                         if(!FLAC__metadata_object_seektable_template_append_spaced_points(seektable_template, n, total_samples_to_encode))
70                                                                 return false;
71                                                 }
72                                         }
73                                 }
74                         }
75                         else { /* -S # */
76                                 if(0 != spec_has_real_points)
77                                         *spec_has_real_points = true;
78                                 if(!only_explicit_placeholders) {
79                                         FLAC__uint64 n = (unsigned)atoi(pt);
80                                         if(!FLAC__metadata_object_seektable_template_append_point(seektable_template, n))
81                                                 return false;
82                                 }
83                         }
84                 }
85
86                 pt = ++q;
87         }
88
89         if(!FLAC__metadata_object_seektable_template_sort(seektable_template, /*compact=*/true))
90                 return false;
91
92         return true;
93 }