utils.c (6882B)
1 /* flac - Command-line FLAC encoder/decoder 2 * Copyright (C) 2002,2003,2004,2005 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 <stdarg.h> 27 #include <stdlib.h> 28 #include <string.h> 29 30 int flac__utils_verbosity_ = 2; 31 32 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value) 33 { 34 FLAC__uint64 ret = 0; 35 char c; 36 37 if(*s == '\0') 38 return false; 39 40 while('\0' != (c = *s++)) 41 if(c >= '0' && c <= '9') 42 ret = ret * 10 + (c - '0'); 43 else 44 return false; 45 46 *value = ret; 47 return true; 48 } 49 50 static FLAC__bool local__parse_timecode_(const char *s, double *value) 51 { 52 double ret; 53 unsigned i; 54 char c; 55 56 /* parse [0-9][0-9]*: */ 57 c = *s++; 58 if(c >= '0' && c <= '9') 59 i = (c - '0'); 60 else 61 return false; 62 while(':' != (c = *s++)) { 63 if(c >= '0' && c <= '9') 64 i = i * 10 + (c - '0'); 65 else 66 return false; 67 } 68 ret = (double)i * 60.; 69 70 /* parse [0-9]*[.]?[0-9]* i.e. a sign-less rational number */ 71 if(strspn(s, "1234567890.") != strlen(s)) 72 return false; 73 { 74 const char *p = strchr(s, '.'); 75 if(p && 0 != strchr(++p, '.')) 76 return false; 77 } 78 ret += atof(s); 79 80 *value = ret; 81 return true; 82 } 83 84 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index) 85 { 86 FLAC__bool got_track = false, got_index = false; 87 unsigned t = 0, i = 0; 88 char c; 89 90 while(end? s < end : *s != '\0') { 91 c = *s++; 92 if(c >= '0' && c <= '9') { 93 t = t * 10 + (c - '0'); 94 got_track = true; 95 } 96 else if(c == '.') 97 break; 98 else 99 return false; 100 } 101 while(end? s < end : *s != '\0') { 102 c = *s++; 103 if(c >= '0' && c <= '9') { 104 i = i * 10 + (c - '0'); 105 got_index = true; 106 } 107 else 108 return false; 109 } 110 *track = t; 111 *index = i; 112 return got_track && got_index; 113 } 114 115 /* 116 * @@@ this only works with sorted cuesheets (the spec strongly recommends but 117 * does not require sorted cuesheets). but if it's not sorted, picking a 118 * nearest cue point has no significance. 119 */ 120 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned index, FLAC__uint64 total_samples, FLAC__bool look_forward) 121 { 122 int t, i; 123 if(look_forward) { 124 for(t = 0; t < (int)cuesheet->num_tracks; t++) 125 for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++) 126 if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= index)) 127 return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset; 128 return total_samples; 129 } 130 else { 131 for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--) 132 for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--) 133 if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= index)) 134 return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset; 135 return 0; 136 } 137 } 138 139 void flac__utils_printf(FILE *stream, int level, const char *format, ...) 140 { 141 if(flac__utils_verbosity_ >= level) { 142 va_list args; 143 144 FLAC__ASSERT(0 != format); 145 146 va_start(args, format); 147 148 (void) vfprintf(stream, format, args); 149 150 va_end(args); 151 } 152 } 153 154 #ifdef FLAC__VALGRIND_TESTING 155 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 156 { 157 size_t ret = fwrite(ptr, size, nmemb, stream); 158 if(!ferror(stream)) 159 fflush(stream); 160 return ret; 161 } 162 #endif 163 164 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec) 165 { 166 FLAC__uint64 val; 167 FLAC__bool is_negative = false; 168 169 FLAC__ASSERT(0 != spec); 170 171 spec->is_relative = false; 172 spec->value_is_samples = true; 173 spec->value.samples = 0; 174 175 if(0 != s) { 176 if(s[0] == '-') { 177 is_negative = true; 178 spec->is_relative = true; 179 s++; 180 } 181 else if(s[0] == '+') { 182 spec->is_relative = true; 183 s++; 184 } 185 186 if(local__parse_uint64_(s, &val)) { 187 spec->value_is_samples = true; 188 spec->value.samples = (FLAC__int64)val; 189 if(is_negative) 190 spec->value.samples = -(spec->value.samples); 191 } 192 else { 193 double d; 194 if(!local__parse_timecode_(s, &d)) 195 return false; 196 spec->value_is_samples = false; 197 spec->value.seconds = d; 198 if(is_negative) 199 spec->value.seconds = -(spec->value.seconds); 200 } 201 } 202 203 return true; 204 } 205 206 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate) 207 { 208 FLAC__ASSERT(0 != spec); 209 if(!spec->value_is_samples) { 210 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate); 211 spec->value_is_samples = true; 212 } 213 } 214 215 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec) 216 { 217 const char *start = s, *end = 0; 218 219 FLAC__ASSERT(0 != spec); 220 221 spec->has_start_point = spec->has_end_point = false; 222 223 s = strchr(s, '-'); 224 225 if(0 != s) { 226 if(s == start) 227 start = 0; 228 end = s+1; 229 if(*end == '\0') 230 end = 0; 231 } 232 233 if(start) { 234 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index)) 235 return false; 236 spec->has_start_point = true; 237 } 238 239 if(end) { 240 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index)) 241 return false; 242 spec->has_end_point = true; 243 } 244 245 return true; 246 } 247 248 void flac__utils_canonicalize_cue_specification(const utils__CueSpecification *cue_spec, const FLAC__StreamMetadata_CueSheet *cuesheet, FLAC__uint64 total_samples, utils__SkipUntilSpecification *skip_spec, utils__SkipUntilSpecification *until_spec) 249 { 250 FLAC__ASSERT(0 != cue_spec); 251 FLAC__ASSERT(0 != cuesheet); 252 FLAC__ASSERT(0 != total_samples); 253 FLAC__ASSERT(0 != skip_spec); 254 FLAC__ASSERT(0 != until_spec); 255 256 skip_spec->is_relative = false; 257 skip_spec->value_is_samples = true; 258 259 until_spec->is_relative = false; 260 until_spec->value_is_samples = true; 261 262 if(cue_spec->has_start_point) 263 skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false); 264 else 265 skip_spec->value.samples = 0; 266 267 if(cue_spec->has_end_point) 268 until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true); 269 else 270 until_spec->value.samples = total_samples; 271 }