Add 2003 to copyright notice
[platform/upstream/flac.git] / src / flac / utils.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2002,2003  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 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "utils.h"
24 #include "FLAC/assert.h"
25 #include <math.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
30 {
31         FLAC__uint64 ret = 0;
32         char c;
33
34         if(*s == '\0')
35                 return false;
36
37         while('\0' != (c = *s++))
38                 if(c >= '0' && c <= '9')
39                         ret = ret * 10 + (c - '0');
40                 else
41                         return false;
42
43         *value = ret;
44         return true;
45 }
46
47 static FLAC__bool local__parse_timecode_(const char *s, double *value)
48 {
49         double ret;
50         unsigned i;
51         char c;
52
53         /* parse [0-9][0-9]*: */
54         c = *s++;
55         if(c >= '0' && c <= '9')
56                 i = (c - '0');
57         else
58                 return false;
59         while(':' != (c = *s++)) {
60                 if(c >= '0' && c <= '9')
61                         i = i * 10 + (c - '0');
62                 else
63                         return false;
64         }
65         ret = (double)i * 60.;
66
67         /* parse [0-9]*[.]?[0-9]* i.e. a sign-less rational number */
68         if(strspn(s, "1234567890.") != strlen(s))
69                 return false;
70         {
71                 const char *p = strchr(s, '.');
72                 if(p && 0 != strchr(++p, '.'))
73                         return false;
74         }
75         ret += atof(s);
76
77         *value = ret;
78         return true;
79 }
80
81 #ifdef FLAC__VALGRIND_TESTING
82 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
83 {
84         size_t ret = fwrite(ptr, size, nmemb, stream);
85         if(!ferror(stream))
86                 fflush(stream);
87         return ret;
88 }
89 #endif
90
91 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
92 {
93         FLAC__uint64 val;
94         FLAC__bool is_negative = false;
95
96         FLAC__ASSERT(0 != spec);
97
98         spec->is_relative = false;
99         spec->value_is_samples = true;
100         spec->value.samples = 0;
101
102         if(0 != s) {
103                 if(s[0] == '-') {
104                         is_negative = true;
105                         spec->is_relative = true;
106                         s++;
107                 }
108                 else if(s[0] == '+') {
109                         spec->is_relative = true;
110                         s++;
111                 }
112
113                 if(local__parse_uint64_(s, &val)) {
114                         spec->value_is_samples = true;
115                         spec->value.samples = (FLAC__int64)val;
116                         if(is_negative)
117                                 spec->value.samples = -(spec->value.samples);
118                 }
119                 else {
120                         double d;
121                         if(!local__parse_timecode_(s, &d))
122                                 return false;
123                         spec->value_is_samples = false;
124                         spec->value.seconds = d;
125                         if(is_negative)
126                                 spec->value.seconds = -(spec->value.seconds);
127                 }
128         }
129
130         return true;
131 }
132
133 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
134 {
135         FLAC__ASSERT(0 != spec);
136         if(!spec->value_is_samples) {
137                 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
138                 spec->value_is_samples = true;
139         }
140 }