X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fflac%2Futils.c;h=03dbdc8bec8362cc5b39dcfc17195feba00cf211;hb=0b736a56051b9b94b69c21d960fe79e64251478e;hp=ea5c3171074ef960c489396d0709e02bce74f4c0;hpb=8ad9a12b0337937ea961d4a51202dbe6e54b41c0;p=platform%2Fupstream%2Fflac.git diff --git a/src/flac/utils.c b/src/flac/utils.c index ea5c317..03dbdc8 100644 --- a/src/flac/utils.c +++ b/src/flac/utils.c @@ -1,5 +1,5 @@ /* flac - Command-line FLAC encoder/decoder - * Copyright (C) 2002,2003,2004,2005,2006 Josh Coalson + * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -11,23 +11,28 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #if HAVE_CONFIG_H # include #endif -#include "utils.h" -#include "FLAC/assert.h" -#include "FLAC/metadata.h" #include #include #include #include #include +#include "utils.h" +#include "FLAC/assert.h" +#include "FLAC/metadata.h" +#include "share/compat.h" +#ifndef _WIN32 +#include +#include +#endif const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK"; @@ -55,7 +60,7 @@ static FLAC__bool local__parse_timecode_(const char *s, double *value) { double ret; unsigned i; - char c; + char c, *endptr; /* parse [0-9][0-9]*: */ c = *s++; @@ -71,21 +76,18 @@ static FLAC__bool local__parse_timecode_(const char *s, double *value) } ret = (double)i * 60.; - /* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, so support different locales) */ + /* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, to support different locales) */ if(strspn(s, "1234567890.,") != strlen(s)) return false; - { - const char *p = strpbrk(s, ".,"); - if(p && 0 != strpbrk(++p, ".,")) - return false; - } - ret += atof(s); + ret += strtod(s, &endptr); + if (endptr == s || *endptr) + return false; *value = ret; return true; } -static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index) +static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *indx) { FLAC__bool got_track = false, got_index = false; unsigned t = 0, i = 0; @@ -112,7 +114,7 @@ static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *tr return false; } *track = t; - *index = i; + *indx = i; return got_track && got_index; } @@ -121,20 +123,20 @@ static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *tr * does not require sorted cuesheets). but if it's not sorted, picking a * nearest cue point has no significance. */ -static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned index, FLAC__uint64 total_samples, FLAC__bool look_forward) +static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned indx, FLAC__uint64 total_samples, FLAC__bool look_forward) { int t, i; if(look_forward) { for(t = 0; t < (int)cuesheet->num_tracks; t++) for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++) - if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= index)) + if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= indx)) return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset; return total_samples; } else { for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--) for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--) - if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= index)) + if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= indx)) return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset; return 0; } @@ -149,9 +151,103 @@ void flac__utils_printf(FILE *stream, int level, const char *format, ...) va_start(args, format); - (void) vfprintf(stream, format, args); + (void) flac_vfprintf(stream, format, args); + + va_end(args); + +#ifdef _MSC_VER + if(stream == stderr) + fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */ +#endif + } +} + +/* variables and functions for console status output */ +static FLAC__bool is_name_printed; +static int stats_char_count = 0; +static int console_width; +static int console_chars_left; + +int get_console_width(void) +{ + int width = 80; +#ifdef _WIN32 + width = win_get_console_width(); +#else + struct winsize w; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) width = w.ws_col; +#endif + return width; +} + +size_t strlen_console(const char *text) +{ +#ifdef _WIN32 + return strlen_utf8(text); +#else + size_t len; + wchar_t *wtmp; + + len = strlen(text)+1; + wtmp = (wchar_t *)malloc(len*sizeof(wchar_t)); + if (wtmp == NULL) return len-1; + mbstowcs(wtmp, text, len); + len = wcswidth(wtmp, len); + free(wtmp); + + return len; +#endif +} + +void stats_new_file(void) +{ + is_name_printed = false; +} + +void stats_clear(void) +{ + while (stats_char_count > 0 && stats_char_count--) + fprintf(stderr, "\b"); +} + +void stats_print_name(int level, const char *name) +{ + int len; + if (flac__utils_verbosity_ >= level) { + stats_clear(); + if(is_name_printed) return; + + console_width = get_console_width(); + len = strlen_console(name)+2; + console_chars_left = console_width - (len % console_width); + flac_fprintf(stderr, "%s: ", name); + is_name_printed = true; + } +} + +void stats_print_info(int level, const char *format, ...) +{ + char tmp[80]; + int len, cleared_len; + + if (flac__utils_verbosity_ >= level) { + va_list args; + va_start(args, format); + len = vsnprintf(tmp, sizeof(tmp), format, args); va_end(args); + if (len < 0 || len == sizeof(tmp)) { + tmp[sizeof(tmp)-1] = '\0'; + len = sizeof(tmp)-1; + } + cleared_len = stats_char_count; + stats_clear(); + if (len >= console_chars_left) { + while (cleared_len > 0 && cleared_len--) fprintf(stderr, " "); + fprintf(stderr, "\n"); + console_chars_left = console_width; + } + stats_char_count = fprintf(stderr, "%s", tmp); } } @@ -283,11 +379,7 @@ FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__ FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); FLAC__ASSERT(strlen(CHANNEL_MASK_TAG+1+2+16+1) <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */ entry.entry = (FLAC__byte*)tag; -#if defined _MSC_VER || defined __MINGW32__ - if((entry.length = _snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag)) -#else - if((entry.length = snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag)) -#endif + if((entry.length = flac_snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag)) return false; if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true)) return false;