initial checkin

This commit is contained in:
chudov
2008-10-13 19:25:11 +00:00
parent 2e379c72e2
commit 36757fca7a
937 changed files with 184964 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
# metaflac - Command-line FLAC metadata editor
# Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# 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.
bin_PROGRAMS = metaflac
AM_CFLAGS = @OGG_CFLAGS@
EXTRA_DIST = \
Makefile.lite \
metaflac.dsp \
metaflac.vcproj
metaflac_SOURCES = \
main.c \
operations.c \
operations_shorthand_cuesheet.c \
operations_shorthand_picture.c \
operations_shorthand_seektable.c \
operations_shorthand_streaminfo.c \
operations_shorthand_vorbiscomment.c \
options.c \
usage.c \
utils.c \
operations.h \
operations_shorthand.h \
options.h \
usage.h \
utils.h
metaflac_LDFLAGS =
metaflac_LDADD = \
$(top_builddir)/src/share/grabbag/libgrabbag.la \
$(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \
$(top_builddir)/src/share/getopt/libgetopt.a \
$(top_builddir)/src/share/utf8/libutf8.la \
$(top_builddir)/src/libFLAC/libFLAC.la \
@OGG_LIBS@ \
@LIBICONV@ \
@MINGW_WINSOCK_LIBS@ \
-lm

View File

@@ -0,0 +1,49 @@
# metaflac - Command-line FLAC metadata editor
# Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# 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.
#
# GNU makefile
#
topdir = ../..
libdir = $(topdir)/obj/$(BUILD)/lib
PROGRAM_NAME = metaflac
INCLUDES = -I./include -I$(topdir)/include -I$(OGG_INCLUDE_DIR)
ifeq ($(OS),Darwin)
EXPLICIT_LIBS = $(libdir)/libgrabbag.a $(libdir)/libFLAC.a $(libdir)/libreplaygain_analysis.a $(libdir)/libgetopt.a $(libdir)/libutf8.a $(OGG_LIB_DIR)/libogg.a -liconv -lm
else
LIBS = -lgrabbag -lFLAC -lreplaygain_analysis -lgetopt -lutf8 -L$(OGG_LIB_DIR) -logg -lm
endif
SRCS_C = \
main.c \
operations.c \
operations_shorthand_cuesheet.c \
operations_shorthand_picture.c \
operations_shorthand_seektable.c \
operations_shorthand_streaminfo.c \
operations_shorthand_vorbiscomment.c \
options.c \
usage.c \
utils.c
include $(topdir)/build/exe.mk
# DO NOT DELETE THIS LINE -- make depend depends on it.

48
flac/src/metaflac/main.c Normal file
View File

@@ -0,0 +1,48 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "operations.h"
#include "options.h"
#include <locale.h>
int main(int argc, char *argv[])
{
CommandLineOptions options;
int ret = 0;
#ifdef __EMX__
_response(&argc, &argv);
_wildcard(&argc, &argv);
#endif
setlocale(LC_ALL, "");
init_options(&options);
if(parse_options(argc, argv, &options))
ret = !do_operations(&options);
else
ret = 1;
free_options(&options);
return ret;
}

View File

@@ -0,0 +1,152 @@
# Microsoft Developer Studio Project File - Name="metaflac" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=metaflac - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "metaflac.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "metaflac.mak" CFG="metaflac - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "metaflac - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "metaflac - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "metaflac - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "..\..\obj\release\bin"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /I "." /I "..\..\include" /D "NDEBUG" /D "FLAC__NO_DLL" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /c
# SUBTRACT CPP /YX /Yc /Yu
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 ..\..\obj\release\lib\grabbag_static.lib ..\..\obj\release\lib\libFLAC_static.lib ..\..\obj\release\lib\replaygain_analysis_static.lib ..\..\obj\release\lib\getopt_static.lib ..\..\obj\release\lib\utf8_static.lib ..\..\obj\release\lib\ogg_static.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "metaflac - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "..\..\obj\debug\bin"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c
# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I "..\..\include" /D "_DEBUG" /D "DEBUG" /D "FLAC__NO_DLL" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c
# SUBTRACT CPP /YX /Yc /Yu
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo

View File

@@ -0,0 +1,424 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Name="metaflac"
ProjectGUID="{4cefbc87-c215-11db-8314-0800200c9a66}"
RootNamespace="metaflac"
Keyword="Win32Proj"
>
<Platforms>
<Platform
Name="Win32"
/>
<Platform
Name="x64"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="..\..\obj\debug\bin"
IntermediateDirectory="Debug"
ConfigurationType="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories=".;..\..\include"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;FLAC__HAS_OGG;FLAC__NO_DLL;DEBUG"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
CompileAs="0"
DisableSpecificWarnings="4267;4996"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="..\..\obj\release\lib\ogg_static.lib"
LinkIncremental="2"
IgnoreDefaultLibraryNames="uuid.lib"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="..\..\obj\release\bin"
IntermediateDirectory="Release"
ConfigurationType="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
EnableIntrinsicFunctions="true"
FavorSizeOrSpeed="1"
OmitFramePointers="true"
WholeProgramOptimization="true"
AdditionalIncludeDirectories=".;..\..\include"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;FLAC__HAS_OGG;FLAC__NO_DLL"
RuntimeLibrary="0"
BufferSecurityCheck="false"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
CompileAs="0"
DisableSpecificWarnings="4267;4996"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="..\..\obj\release\lib\ogg_static.lib"
LinkIncremental="1"
IgnoreDefaultLibraryNames="uuid.lib"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
LinkTimeCodeGeneration="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Debug|x64"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
ConfigurationType="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
TargetEnvironment="3"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories=".;..\..\include"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;FLAC__HAS_OGG;FLAC__NO_DLL;DEBUG"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
CompileAs="0"
DisableSpecificWarnings="4267;4996"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="..\..\obj\release\lib\ogg_static.lib"
LinkIncremental="2"
IgnoreDefaultLibraryNames="uuid.lib"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="17"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|x64"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
ConfigurationType="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
TargetEnvironment="3"
/>
<Tool
Name="VCCLCompilerTool"
EnableIntrinsicFunctions="true"
FavorSizeOrSpeed="1"
OmitFramePointers="true"
WholeProgramOptimization="true"
AdditionalIncludeDirectories=".;..\..\include"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;FLAC__HAS_OGG;FLAC__NO_DLL"
RuntimeLibrary="0"
BufferSecurityCheck="false"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
CompileAs="0"
DisableSpecificWarnings="4267;4996"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="..\..\obj\release\lib\ogg_static.lib"
LinkIncremental="1"
IgnoreDefaultLibraryNames="uuid.lib"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
LinkTimeCodeGeneration="1"
TargetMachine="17"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\operations.h"
>
</File>
<File
RelativePath=".\options.h"
>
</File>
<File
RelativePath=".\usage.h"
>
</File>
<File
RelativePath=".\utils.h"
>
</File>
</Filter>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\main.c"
>
</File>
<File
RelativePath=".\operations.c"
>
</File>
<File
RelativePath=".\operations_shorthand_cuesheet.c"
>
</File>
<File
RelativePath=".\operations_shorthand_picture.c"
>
</File>
<File
RelativePath=".\operations_shorthand_seektable.c"
>
</File>
<File
RelativePath=".\operations_shorthand_streaminfo.c"
>
</File>
<File
RelativePath=".\operations_shorthand_vorbiscomment.c"
>
</File>
<File
RelativePath=".\options.c"
>
</File>
<File
RelativePath=".\usage.c"
>
</File>
<File
RelativePath=".\utils.c"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,682 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "operations.h"
#include "usage.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "FLAC/metadata.h"
#include "share/alloc.h"
#include "share/grabbag.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "operations_shorthand.h"
static void show_version(void);
static FLAC__bool do_major_operation(const CommandLineOptions *options);
static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options);
static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
static FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
static FLAC__bool do_shorthand_operations(const CommandLineOptions *options);
static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options);
static FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert);
static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime);
static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write);
static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number);
static void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application);
/* from operations_shorthand_seektable.c */
extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
/* from operations_shorthand_streaminfo.c */
extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
/* from operations_shorthand_vorbiscomment.c */
extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw);
/* from operations_shorthand_cuesheet.c */
extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
/* from operations_shorthand_picture.c */
extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
FLAC__bool do_operations(const CommandLineOptions *options)
{
FLAC__bool ok = true;
if(options->show_long_help) {
long_usage(0);
}
if(options->show_version) {
show_version();
}
else if(options->args.checks.num_major_ops > 0) {
FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0);
FLAC__ASSERT(options->args.checks.num_major_ops == 1);
FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations);
ok = do_major_operation(options);
}
else if(options->args.checks.num_shorthand_ops > 0) {
FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations);
ok = do_shorthand_operations(options);
}
return ok;
}
/*
* local routines
*/
void show_version(void)
{
printf("metaflac %s\n", FLAC__VERSION_STRING);
}
FLAC__bool do_major_operation(const CommandLineOptions *options)
{
unsigned i;
FLAC__bool ok = true;
/* to die after first error, v--- add '&& ok' here */
for(i = 0; i < options->num_files; i++)
ok &= do_major_operation_on_file(options->filenames[i], options);
return ok;
}
FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
{
FLAC__bool ok = true, needs_write = false, is_ogg = false;
FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
if(0 == chain)
die("out of memory allocating chain");
/*@@@@ lame way of guessing the file type */
if(strlen(filename) >= 4 && (0 == strcmp(filename+strlen(filename)-4, ".oga") || 0 == strcmp(filename+strlen(filename)-4, ".ogg")))
is_ogg = true;
if(! (is_ogg? FLAC__metadata_chain_read_ogg(chain, filename) : FLAC__metadata_chain_read(chain, filename)) ) {
print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
FLAC__metadata_chain_delete(chain);
return false;
}
switch(options->ops.operations[0].type) {
case OP__LIST:
ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
break;
case OP__APPEND:
ok = do_major_operation__append(chain, options);
needs_write = true;
break;
case OP__REMOVE:
ok = do_major_operation__remove(chain, options);
needs_write = true;
break;
case OP__REMOVE_ALL:
ok = do_major_operation__remove_all(chain, options);
needs_write = true;
break;
case OP__MERGE_PADDING:
FLAC__metadata_chain_merge_padding(chain);
needs_write = true;
break;
case OP__SORT_PADDING:
FLAC__metadata_chain_sort_padding(chain);
needs_write = true;
break;
default:
FLAC__ASSERT(0);
return false;
}
if(ok && needs_write) {
if(options->use_padding)
FLAC__metadata_chain_sort_padding(chain);
ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
if(!ok)
print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
}
FLAC__metadata_chain_delete(chain);
return ok;
}
FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
{
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
FLAC__StreamMetadata *block;
FLAC__bool ok = true;
unsigned block_number;
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
block_number = 0;
do {
block = FLAC__metadata_iterator_get_block(iterator);
ok &= (0 != block);
if(!ok)
fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
block_number++;
} while(ok && FLAC__metadata_iterator_next(iterator));
FLAC__metadata_iterator_delete(iterator);
return ok;
}
FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
{
(void) chain, (void) options;
fprintf(stderr, "ERROR: --append not implemented yet\n");
return false;
}
FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
{
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
FLAC__bool ok = true;
unsigned block_number;
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
block_number = 0;
while(ok && FLAC__metadata_iterator_next(iterator)) {
block_number++;
if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) {
ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
if(options->use_padding)
ok &= FLAC__metadata_iterator_next(iterator);
}
}
FLAC__metadata_iterator_delete(iterator);
return ok;
}
FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
{
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
FLAC__bool ok = true;
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
while(ok && FLAC__metadata_iterator_next(iterator)) {
ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
if(options->use_padding)
ok &= FLAC__metadata_iterator_next(iterator);
}
FLAC__metadata_iterator_delete(iterator);
return ok;
}
FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
{
unsigned i;
FLAC__bool ok = true;
/* to die after first error, v--- add '&& ok' here */
for(i = 0; i < options->num_files; i++)
ok &= do_shorthand_operations_on_file(options->filenames[i], options);
/* check if OP__ADD_REPLAY_GAIN requested */
if(ok && options->num_files > 0) {
for(i = 0; i < options->ops.num_operations; i++) {
if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime);
}
}
return ok;
}
FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
{
unsigned i;
FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
if(0 == chain)
die("out of memory allocating chain");
if(!FLAC__metadata_chain_read(chain, filename)) {
print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
return false;
}
for(i = 0; i < options->ops.num_operations && ok; i++) {
/*
* Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
* --add-seekpoint and --import-cuesheet-from are used.
*/
if(options->ops.operations[i].type != OP__ADD_SEEKPOINT)
ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
/* The following seems counterintuitive but the meaning
* of 'use_padding' is 'try to keep the overall metadata
* to its original size, adding or truncating extra
* padding if necessary' which is why we need to turn it
* off in this case. If we don't, the extra padding block
* will just be truncated.
*/
if(options->ops.operations[i].type == OP__ADD_PADDING)
use_padding = false;
}
/*
* Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
* --add-seekpoint and --import-cuesheet-from are used.
*/
for(i = 0; i < options->ops.num_operations && ok; i++) {
if(options->ops.operations[i].type == OP__ADD_SEEKPOINT)
ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
}
if(ok && needs_write) {
if(use_padding)
FLAC__metadata_chain_sort_padding(chain);
ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
if(!ok)
print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
}
FLAC__metadata_chain_delete(chain);
return ok;
}
FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert)
{
FLAC__bool ok = true;
switch(operation->type) {
case OP__SHOW_MD5SUM:
case OP__SHOW_MIN_BLOCKSIZE:
case OP__SHOW_MAX_BLOCKSIZE:
case OP__SHOW_MIN_FRAMESIZE:
case OP__SHOW_MAX_FRAMESIZE:
case OP__SHOW_SAMPLE_RATE:
case OP__SHOW_CHANNELS:
case OP__SHOW_BPS:
case OP__SHOW_TOTAL_SAMPLES:
case OP__SET_MD5SUM:
case OP__SET_MIN_BLOCKSIZE:
case OP__SET_MAX_BLOCKSIZE:
case OP__SET_MIN_FRAMESIZE:
case OP__SET_MAX_FRAMESIZE:
case OP__SET_SAMPLE_RATE:
case OP__SET_CHANNELS:
case OP__SET_BPS:
case OP__SET_TOTAL_SAMPLES:
ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
break;
case OP__SHOW_VC_VENDOR:
case OP__SHOW_VC_FIELD:
case OP__REMOVE_VC_ALL:
case OP__REMOVE_VC_FIELD:
case OP__REMOVE_VC_FIRSTFIELD:
case OP__SET_VC_FIELD:
case OP__IMPORT_VC_FROM:
case OP__EXPORT_VC_TO:
ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
break;
case OP__IMPORT_CUESHEET_FROM:
case OP__EXPORT_CUESHEET_TO:
ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
break;
case OP__IMPORT_PICTURE_FROM:
case OP__EXPORT_PICTURE_TO:
ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
break;
case OP__ADD_SEEKPOINT:
ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
break;
case OP__ADD_REPLAY_GAIN:
/* this command is always executed last */
ok = true;
break;
case OP__ADD_PADDING:
ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
return ok;
}
FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime)
{
FLAC__StreamMetadata streaminfo;
float *title_gains = 0, *title_peaks = 0;
float album_gain, album_peak;
unsigned sample_rate = 0;
unsigned bits_per_sample = 0;
unsigned channels = 0;
unsigned i;
const char *error;
FLAC__bool first = true;
FLAC__ASSERT(num_files > 0);
for(i = 0; i < num_files; i++) {
FLAC__ASSERT(0 != filenames[i]);
if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
return false;
}
if(first) {
first = false;
sample_rate = streaminfo.data.stream_info.sample_rate;
bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
channels = streaminfo.data.stream_info.channels;
}
else {
if(sample_rate != streaminfo.data.stream_info.sample_rate) {
fprintf(stderr, "%s: ERROR: sample rate of %u Hz does not match previous files' %u Hz\n", filenames[i], streaminfo.data.stream_info.sample_rate, sample_rate);
return false;
}
if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
fprintf(stderr, "%s: ERROR: resolution of %u bps does not match previous files' %u bps\n", filenames[i], streaminfo.data.stream_info.bits_per_sample, bits_per_sample);
return false;
}
if(channels != streaminfo.data.stream_info.channels) {
fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
return false;
}
}
if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
return false;
}
if(channels != 1 && channels != 2) {
fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
return false;
}
}
FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
if(!grabbag__replaygain_init(sample_rate)) {
FLAC__ASSERT(0);
/* double protection */
fprintf(stderr, "internal error\n");
return false;
}
if(
0 == (title_gains = (float*)safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) ||
0 == (title_peaks = (float*)safe_malloc_mul_2op_(sizeof(float), /*times*/num_files))
)
die("out of memory allocating space for title gains/peaks");
for(i = 0; i < num_files; i++) {
if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
free(title_gains);
free(title_peaks);
return false;
}
}
grabbag__replaygain_get_album(&album_gain, &album_peak);
for(i = 0; i < num_files; i++) {
if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
free(title_gains);
free(title_peaks);
return false;
}
}
free(title_gains);
free(title_peaks);
return true;
}
FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
{
FLAC__StreamMetadata *padding = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
while(FLAC__metadata_iterator_next(iterator))
;
padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
if(0 == padding)
die("out of memory allocating PADDING block");
padding->length = length;
if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename);
FLAC__metadata_object_delete(padding);
FLAC__metadata_iterator_delete(iterator);
return false;
}
FLAC__metadata_iterator_delete(iterator);
*needs_write = true;
return true;
}
FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
{
unsigned i, j;
FLAC__bool matches_number = false, matches_type = false;
FLAC__bool has_block_number_arg = false;
for(i = 0; i < options->args.num_arguments; i++) {
if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
if(block->type != FLAC__METADATA_TYPE_APPLICATION || !options->args.arguments[i].value.block_type.entries[j].filter_application_by_id || 0 == memcmp(options->args.arguments[i].value.block_type.entries[j].application_id, block->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
matches_type = true;
}
}
}
else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
has_block_number_arg = true;
for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
if(options->args.arguments[i].value.block_number.entries[j] == block_number)
matches_number = true;
}
}
}
if(!has_block_number_arg)
matches_number = true;
if(options->args.checks.has_block_type) {
FLAC__ASSERT(!options->args.checks.has_except_block_type);
}
else if(options->args.checks.has_except_block_type)
matches_type = !matches_type;
else
matches_type = true;
return matches_number && matches_type;
}
void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
{
unsigned i, j;
/*@@@ yuck, should do this with a varargs function or something: */
#define PPR if(filename)printf("%s:",filename);
PPR; printf("METADATA block #%u\n", block_number);
PPR; printf(" type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
PPR; printf(" is last: %s\n", block->is_last? "true":"false");
PPR; printf(" length: %u\n", block->length);
switch(block->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
PPR; printf(" minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
PPR; printf(" maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
PPR; printf(" minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
PPR; printf(" maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
PPR; printf(" sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
PPR; printf(" channels: %u\n", block->data.stream_info.channels);
PPR; printf(" bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
#ifdef _MSC_VER
PPR; printf(" total samples: %I64u\n", block->data.stream_info.total_samples);
#else
PPR; printf(" total samples: %llu\n", (unsigned long long)block->data.stream_info.total_samples);
#endif
PPR; printf(" MD5 signature: ");
for(i = 0; i < 16; i++) {
printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
}
printf("\n");
break;
case FLAC__METADATA_TYPE_PADDING:
/* nothing to print */
break;
case FLAC__METADATA_TYPE_APPLICATION:
PPR; printf(" application ID: ");
for(i = 0; i < 4; i++)
printf("%02x", block->data.application.id[i]);
printf("\n");
PPR; printf(" data contents:\n");
if(0 != block->data.application.data) {
if(hexdump_application)
hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " ");
else
(void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
}
break;
case FLAC__METADATA_TYPE_SEEKTABLE:
PPR; printf(" seek points: %u\n", block->data.seek_table.num_points);
for(i = 0; i < block->data.seek_table.num_points; i++) {
if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
#ifdef _MSC_VER
PPR; printf(" point %u: sample_number=%I64u, stream_offset=%I64u, frame_samples=%u\n", i, block->data.seek_table.points[i].sample_number, block->data.seek_table.points[i].stream_offset, block->data.seek_table.points[i].frame_samples);
#else
PPR; printf(" point %u: sample_number=%llu, stream_offset=%llu, frame_samples=%u\n", i, (unsigned long long)block->data.seek_table.points[i].sample_number, (unsigned long long)block->data.seek_table.points[i].stream_offset, block->data.seek_table.points[i].frame_samples);
#endif
}
else {
PPR; printf(" point %u: PLACEHOLDER\n", i);
}
}
break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
PPR; printf(" vendor string: ");
write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
PPR; printf(" comments: %u\n", block->data.vorbis_comment.num_comments);
for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
PPR; printf(" comment[%u]: ", i);
write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
}
break;
case FLAC__METADATA_TYPE_CUESHEET:
PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
#ifdef _MSC_VER
PPR; printf(" lead-in: %I64u\n", block->data.cue_sheet.lead_in);
#else
PPR; printf(" lead-in: %llu\n", (unsigned long long)block->data.cue_sheet.lead_in);
#endif
PPR; printf(" is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false");
PPR; printf(" number of tracks: %u\n", block->data.cue_sheet.num_tracks);
for(i = 0; i < block->data.cue_sheet.num_tracks; i++) {
const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i;
const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1);
const FLAC__bool is_leadout = is_last && track->num_indices == 0;
PPR; printf(" track[%u]\n", i);
#ifdef _MSC_VER
PPR; printf(" offset: %I64u\n", track->offset);
#else
PPR; printf(" offset: %llu\n", (unsigned long long)track->offset);
#endif
if(is_last) {
PPR; printf(" number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID");
}
else {
PPR; printf(" number: %u\n", (unsigned)track->number);
}
if(!is_leadout) {
PPR; printf(" ISRC: %s\n", track->isrc);
PPR; printf(" type: %s\n", track->type == 1? "DATA" : "AUDIO");
PPR; printf(" pre-emphasis: %s\n", track->pre_emphasis? "true":"false");
PPR; printf(" number of index points: %u\n", track->num_indices);
for(j = 0; j < track->num_indices; j++) {
const FLAC__StreamMetadata_CueSheet_Index *index = track->indices+j;
PPR; printf(" index[%u]\n", j);
#ifdef _MSC_VER
PPR; printf(" offset: %I64u\n", index->offset);
#else
PPR; printf(" offset: %llu\n", (unsigned long long)index->offset);
#endif
PPR; printf(" number: %u\n", (unsigned)index->number);
}
}
}
break;
case FLAC__METADATA_TYPE_PICTURE:
PPR; printf(" type: %u (%s)\n", block->data.picture.type, block->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED? FLAC__StreamMetadata_Picture_TypeString[block->data.picture.type] : "UNDEFINED");
PPR; printf(" MIME type: %s\n", block->data.picture.mime_type);
PPR; printf(" description: %s\n", block->data.picture.description);
PPR; printf(" width: %u\n", (unsigned)block->data.picture.width);
PPR; printf(" height: %u\n", (unsigned)block->data.picture.height);
PPR; printf(" depth: %u\n", (unsigned)block->data.picture.depth);
PPR; printf(" colors: %u%s\n", (unsigned)block->data.picture.colors, block->data.picture.colors? "" : " (unindexed)");
PPR; printf(" data length: %u\n", (unsigned)block->data.picture.data_length);
PPR; printf(" data:\n");
if(0 != block->data.picture.data)
hexdump(filename, block->data.picture.data, block->data.picture.data_length, " ");
break;
default:
PPR; printf(" data contents:\n");
if(0 != block->data.unknown.data)
hexdump(filename, block->data.unknown.data, block->length, " ");
break;
}
#undef PPR
}

View File

@@ -0,0 +1,26 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#ifndef metaflac__operations_h
#define metaflac__operations_h
#include "options.h"
FLAC__bool do_operations(const CommandLineOptions *options);
#endif

View File

@@ -0,0 +1,25 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#include "options.h"
FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw);

View File

@@ -0,0 +1,217 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdio.h> /* for snprintf() */
#include <string.h>
#include "options.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "share/grabbag.h"
#include "operations_shorthand.h"
static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link);
static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename);
FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
{
FLAC__bool ok = true;
FLAC__StreamMetadata *cuesheet = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
FLAC__uint64 lead_out_offset = 0;
FLAC__bool is_cdda = false;
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
do {
FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
lead_out_offset = block->data.stream_info.total_samples;
if(lead_out_offset == 0) {
fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
FLAC__metadata_iterator_delete(iterator);
return false;
}
is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (block->data.stream_info.sample_rate == 44100);
}
else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
cuesheet = block;
} while(FLAC__metadata_iterator_next(iterator));
if(lead_out_offset == 0) {
fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
FLAC__metadata_iterator_delete(iterator);
return false;
}
switch(operation->type) {
case OP__IMPORT_CUESHEET_FROM:
if(0 != cuesheet) {
fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
ok = false;
}
else {
ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link);
if(ok) {
/* append CUESHEET block */
while(FLAC__metadata_iterator_next(iterator))
;
if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename);
FLAC__metadata_object_delete(cuesheet);
ok = false;
}
}
}
break;
case OP__EXPORT_CUESHEET_TO:
if(0 == cuesheet) {
fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
ok = false;
}
else
ok = export_cs_to(filename, cuesheet, operation->argument.filename.value);
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
FLAC__metadata_iterator_delete(iterator);
return ok;
}
/*
* local routines
*/
FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link)
{
FILE *f;
const char *error_message;
char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0;
unsigned last_line_read;
if(0 == cs_filename || strlen(cs_filename) == 0) {
fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
return false;
}
if(0 == strcmp(cs_filename, "-"))
f = stdin;
else
f = fopen(cs_filename, "r");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, cs_filename, strerror(errno));
return false;
}
*cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, is_cdda, lead_out_offset);
if(f != stdin)
fclose(f);
if(0 == *cuesheet) {
fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message);
return false;
}
if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message);
return false;
}
/* if we're expecting CDDA, warn about non-compliance */
if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message);
(*cuesheet)->data.cue_sheet.is_cd = false;
}
/* add seekpoints for each index point if required */
if(0 != seekpoint_specification) {
char spec[128];
unsigned track, index;
const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet;
if(0 == *seekpoint_specification)
*seekpoint_specification = local_strdup("");
for(track = 0; track < cs->num_tracks; track++) {
const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track;
for(index = 0; index < tr->num_indices; index++) {
#ifdef _MSC_VER
sprintf(spec, "%I64u;", tr->offset + tr->indices[index].offset);
#else
sprintf(spec, "%llu;", (unsigned long long)(tr->offset + tr->indices[index].offset));
#endif
local_strcat(seekpoint_specification, spec);
}
}
}
*needs_write = true;
return true;
}
FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
{
FILE *f;
char *ref = 0;
size_t reflen;
if(0 == cs_filename || strlen(cs_filename) == 0) {
fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
return false;
}
if(0 == strcmp(cs_filename, "-"))
f = stdout;
else
f = fopen(cs_filename, "w");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, cs_filename, strerror(errno));
return false;
}
reflen = strlen(filename) + 7 + 1;
if(0 == (ref = malloc(reflen))) {
fprintf(stderr, "%s: ERROR: allocating memory\n", filename);
return false;
}
#if defined _MSC_VER || defined __MINGW32__
_snprintf(ref, reflen, "\"%s\" FLAC", filename);
#else
snprintf(ref, reflen, "\"%s\" FLAC", filename);
#endif
grabbag__cuesheet_emit(f, cuesheet, ref);
free(ref);
if(f != stdout)
fclose(f);
return true;
}

View File

@@ -0,0 +1,173 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include "options.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "share/grabbag.h" /* for grabbag__picture_parse_specification() etc */
#include "operations_shorthand.h"
static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write);
static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename);
FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
{
FLAC__bool ok = true, has_type1 = false, has_type2 = false;
FLAC__StreamMetadata *picture = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
switch(operation->type) {
case OP__IMPORT_PICTURE_FROM:
ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write);
if(ok) {
/* append PICTURE block */
while(FLAC__metadata_iterator_next(iterator))
;
if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) {
print_error_with_chain_status(chain, "%s: ERROR: adding new PICTURE block to metadata", filename);
FLAC__metadata_object_delete(picture);
ok = false;
}
}
if(ok) {
/* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
while(FLAC__metadata_iterator_prev(iterator))
;
do {
FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_PICTURE) {
if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
if(has_type1) {
print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
ok = false;
}
has_type1 = true;
}
else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
if(has_type2) {
print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
ok = false;
}
has_type2 = true;
}
}
} while(FLAC__metadata_iterator_next(iterator));
}
break;
case OP__EXPORT_PICTURE_TO:
{
const Argument_BlockNumber *a = operation->argument.export_picture_to.block_number_link;
int block_number = (a && a->num_entries > 0)? (int)a->entries[0] : -1;
unsigned i = 0;
do {
FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_PICTURE && (block_number < 0 || i == (unsigned)block_number))
picture = block;
i++;
} while(FLAC__metadata_iterator_next(iterator) && 0 == picture);
if(0 == picture) {
if(block_number < 0)
fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block\n", filename);
else
fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block at block #%d\n", filename, block_number);
ok = false;
}
else
ok = export_pic_to(filename, picture, operation->argument.filename.value);
}
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
FLAC__metadata_iterator_delete(iterator);
return ok;
}
/*
* local routines
*/
FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write)
{
const char *error_message;
if(0 == specification || strlen(specification) == 0) {
fprintf(stderr, "%s: ERROR: empty picture specification\n", filename);
return false;
}
*picture = grabbag__picture_parse_specification(specification, &error_message);
if(0 == *picture) {
fprintf(stderr, "%s: ERROR: while parsing picture specification \"%s\": %s\n", filename, specification, error_message);
return false;
}
if(!FLAC__format_picture_is_legal(&(*picture)->data.picture, &error_message)) {
fprintf(stderr, "%s: ERROR: new PICTURE block for \"%s\" is illegal: %s\n", filename, specification, error_message);
return false;
}
*needs_write = true;
return true;
}
FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename)
{
FILE *f;
const FLAC__uint32 len = picture->data.picture.data_length;
if(0 == pic_filename || strlen(pic_filename) == 0) {
fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
return false;
}
if(0 == strcmp(pic_filename, "-"))
f = grabbag__file_get_binary_stdout();
else
f = fopen(pic_filename, "wb");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno));
return false;
}
if(fwrite(picture->data.picture.data, 1, len, f) != len) {
fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename);
return false;
}
if(f != stdout)
fclose(f);
return true;
}

View File

@@ -0,0 +1,217 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "utils.h"
#include "FLAC/assert.h"
#include "FLAC/stream_decoder.h"
#include "FLAC/metadata.h"
#include "share/grabbag.h"
#include "operations_shorthand.h"
static FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write)
{
FLAC__bool ok = true, found_seektable_block = false;
FLAC__StreamMetadata *block = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
FLAC__uint64 total_samples = 0;
unsigned sample_rate = 0;
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
do {
block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
sample_rate = block->data.stream_info.sample_rate;
total_samples = block->data.stream_info.total_samples;
}
else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE)
found_seektable_block = true;
} while(!found_seektable_block && FLAC__metadata_iterator_next(iterator));
if(total_samples == 0) {
fprintf(stderr, "%s: ERROR: cannot add seekpoints because STREAMINFO block does not specify total_samples\n", filename);
return false;
}
if(!found_seektable_block) {
/* create a new block */
block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
if(0 == block)
die("out of memory allocating SEEKTABLE block");
while(FLAC__metadata_iterator_prev(iterator))
;
if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
print_error_with_chain_status(chain, "%s: ERROR: adding new SEEKTABLE block to metadata", filename);
FLAC__metadata_object_delete(block);
return false;
}
/* iterator is left pointing to new block */
FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
}
FLAC__metadata_iterator_delete(iterator);
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
if(!grabbag__seektable_convert_specification_to_template(specification, /*only_explicit_placeholders=*/false, total_samples, sample_rate, block, /*spec_has_real_points=*/0)) {
fprintf(stderr, "%s: ERROR (internal) preparing seektable with seekpoints\n", filename);
return false;
}
ok = populate_seekpoint_values(filename, block, needs_write);
if(ok)
(void) FLAC__format_seektable_sort(&block->data.seek_table);
return ok;
}
/*
* local routines
*/
typedef struct {
FLAC__StreamMetadata_SeekTable *seektable_template;
FLAC__uint64 samples_written;
FLAC__uint64 audio_offset, last_offset;
unsigned first_seekpoint_to_check;
FLAC__bool error_occurred;
FLAC__StreamDecoderErrorStatus error_status;
} ClientData;
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
ClientData *cd = (ClientData*)client_data;
(void)buffer;
FLAC__ASSERT(0 != cd);
if(!cd->error_occurred) {
const unsigned blocksize = frame->header.blocksize;
const FLAC__uint64 frame_first_sample = cd->samples_written;
const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
FLAC__uint64 test_sample;
unsigned i;
for(i = cd->first_seekpoint_to_check; i < cd->seektable_template->num_points; i++) {
test_sample = cd->seektable_template->points[i].sample_number;
if(test_sample > frame_last_sample) {
break;
}
else if(test_sample >= frame_first_sample) {
cd->seektable_template->points[i].sample_number = frame_first_sample;
cd->seektable_template->points[i].stream_offset = cd->last_offset - cd->audio_offset;
cd->seektable_template->points[i].frame_samples = blocksize;
cd->first_seekpoint_to_check++;
/* DO NOT: "break;" and here's why:
* The seektable template may contain more than one target
* sample for any given frame; we will keep looping, generating
* duplicate seekpoints for them, and we'll clean it up later,
* just before writing the seektable back to the metadata.
*/
}
else {
cd->first_seekpoint_to_check++;
}
}
cd->samples_written += blocksize;
if(!FLAC__stream_decoder_get_decode_position(decoder, &cd->last_offset))
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
else
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
ClientData *cd = (ClientData*)client_data;
(void)decoder;
FLAC__ASSERT(0 != cd);
if(!cd->error_occurred) { /* don't let multiple errors overwrite the first one */
cd->error_occurred = true;
cd->error_status = status;
}
}
FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
{
FLAC__StreamDecoder *decoder;
ClientData client_data;
FLAC__bool ok = true;
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
client_data.seektable_template = &block->data.seek_table;
client_data.samples_written = 0;
/* client_data.audio_offset must be determined later */
client_data.first_seekpoint_to_check = 0;
client_data.error_occurred = false;
decoder = FLAC__stream_decoder_new();
if(0 == decoder) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) creating the decoder instance\n", filename);
return false;
}
FLAC__stream_decoder_set_md5_checking(decoder, false);
FLAC__stream_decoder_set_metadata_ignore_all(decoder);
if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, /*metadata_callback=*/0, error_callback_, &client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
ok = false;
}
if(ok && !FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
ok = false;
}
if(ok && !FLAC__stream_decoder_get_decode_position(decoder, &client_data.audio_offset)) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file\n", filename);
ok = false;
}
client_data.last_offset = client_data.audio_offset;
if(ok && !FLAC__stream_decoder_process_until_end_of_stream(decoder)) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
ok = false;
}
if(ok && client_data.error_occurred) {
fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%u:%s)\n", filename, (unsigned)client_data.error_status, FLAC__StreamDecoderErrorStatusString[client_data.error_status]);
ok = false;
}
*needs_write = true;
FLAC__stream_decoder_delete(decoder);
return ok;
}

View File

@@ -0,0 +1,129 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "options.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "FLAC/metadata.h"
#include <string.h>
#include "operations_shorthand.h"
FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
{
unsigned i;
FLAC__bool ok = true;
FLAC__StreamMetadata *block;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
block = FLAC__metadata_iterator_get_block(iterator);
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_STREAMINFO);
if(prefix_with_filename)
printf("%s:", filename);
switch(operation->type) {
case OP__SHOW_MD5SUM:
for(i = 0; i < 16; i++)
printf("%02x", block->data.stream_info.md5sum[i]);
printf("\n");
break;
case OP__SHOW_MIN_BLOCKSIZE:
printf("%u\n", block->data.stream_info.min_blocksize);
break;
case OP__SHOW_MAX_BLOCKSIZE:
printf("%u\n", block->data.stream_info.max_blocksize);
break;
case OP__SHOW_MIN_FRAMESIZE:
printf("%u\n", block->data.stream_info.min_framesize);
break;
case OP__SHOW_MAX_FRAMESIZE:
printf("%u\n", block->data.stream_info.max_framesize);
break;
case OP__SHOW_SAMPLE_RATE:
printf("%u\n", block->data.stream_info.sample_rate);
break;
case OP__SHOW_CHANNELS:
printf("%u\n", block->data.stream_info.channels);
break;
case OP__SHOW_BPS:
printf("%u\n", block->data.stream_info.bits_per_sample);
break;
case OP__SHOW_TOTAL_SAMPLES:
#ifdef _MSC_VER
printf("%I64u\n", block->data.stream_info.total_samples);
#else
printf("%llu\n", (unsigned long long)block->data.stream_info.total_samples);
#endif
break;
case OP__SET_MD5SUM:
memcpy(block->data.stream_info.md5sum, operation->argument.streaminfo_md5.value, 16);
*needs_write = true;
break;
case OP__SET_MIN_BLOCKSIZE:
block->data.stream_info.min_blocksize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_MAX_BLOCKSIZE:
block->data.stream_info.max_blocksize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_MIN_FRAMESIZE:
block->data.stream_info.min_framesize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_MAX_FRAMESIZE:
block->data.stream_info.max_framesize = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_SAMPLE_RATE:
block->data.stream_info.sample_rate = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_CHANNELS:
block->data.stream_info.channels = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_BPS:
block->data.stream_info.bits_per_sample = operation->argument.streaminfo_uint32.value;
*needs_write = true;
break;
case OP__SET_TOTAL_SAMPLES:
block->data.stream_info.total_samples = operation->argument.streaminfo_uint64.value;
*needs_write = true;
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
FLAC__metadata_iterator_delete(iterator);
return ok;
}

View File

@@ -0,0 +1,368 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "options.h"
#include "utils.h"
#include "FLAC/assert.h"
#include "share/grabbag.h" /* for grabbag__file_get_filesize() */
#include "share/utf8.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "operations_shorthand.h"
static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw);
FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw)
{
FLAC__bool ok = true, found_vc_block = false;
FLAC__StreamMetadata *block = 0;
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
if(0 == iterator)
die("out of memory allocating iterator");
FLAC__metadata_iterator_init(iterator, chain);
do {
block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
found_vc_block = true;
} while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
if(!found_vc_block) {
/* create a new block if necessary */
if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) {
block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
if(0 == block)
die("out of memory allocating VORBIS_COMMENT block");
while(FLAC__metadata_iterator_next(iterator))
;
if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename);
return false;
}
/* iterator is left pointing to new block */
FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
}
else {
FLAC__metadata_iterator_delete(iterator);
return ok;
}
}
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
switch(operation->type) {
case OP__SHOW_VC_VENDOR:
write_vc_field(prefix_with_filename? filename : 0, &block->data.vorbis_comment.vendor_string, raw, stdout);
break;
case OP__SHOW_VC_FIELD:
write_vc_fields(prefix_with_filename? filename : 0, operation->argument.vc_field_name.value, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, stdout);
break;
case OP__REMOVE_VC_ALL:
ok = remove_vc_all(filename, block, needs_write);
break;
case OP__REMOVE_VC_FIELD:
ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write);
break;
case OP__REMOVE_VC_FIRSTFIELD:
ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
break;
case OP__SET_VC_FIELD:
ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
break;
case OP__IMPORT_VC_FROM:
ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw);
break;
case OP__EXPORT_VC_TO:
ok = export_vc_to(filename, block, &operation->argument.filename, raw);
break;
default:
ok = false;
FLAC__ASSERT(0);
break;
};
FLAC__metadata_iterator_delete(iterator);
return ok;
}
/*
* local routines
*/
FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
{
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
FLAC__ASSERT(0 != needs_write);
if(0 != block->data.vorbis_comment.comments) {
FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0);
if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
*needs_write = true;
}
else {
FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0);
}
return true;
}
FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
{
int n;
FLAC__ASSERT(0 != needs_write);
n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name);
if(n < 0) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
else if(n > 0)
*needs_write = true;
return true;
}
FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
{
int n;
FLAC__ASSERT(0 != needs_write);
n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name);
if(n < 0) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
else if(n > 0)
*needs_write = true;
return true;
}
FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw)
{
FLAC__StreamMetadata_VorbisComment_Entry entry;
char *converted;
FLAC__ASSERT(0 != block);
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
FLAC__ASSERT(0 != field);
FLAC__ASSERT(0 != needs_write);
if(field->field_value_from_file) {
/* read the file into 'data' */
FILE *f = 0;
char *data = 0;
const off_t size = grabbag__file_get_filesize(field->field_value);
if(size < 0) {
fprintf(stderr, "%s: ERROR: can't open file '%s' for '%s' tag value\n", filename, field->field_value, field->field_name);
return false;
}
if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is too large\n", filename, field->field_value, field->field_name);
return false;
}
if(0 == (data = malloc(size+1)))
die("out of memory allocating tag value");
data[size] = '\0';
if(0 == (f = fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
fprintf(stderr, "%s: ERROR: while reading file '%s' for '%s' tag value: %s\n", filename, field->field_value, field->field_name, strerror(errno));
free(data);
if(f)
fclose(f);
return false;
}
fclose(f);
if(strlen(data) != (size_t)size) {
free(data);
fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value has embedded NULs\n", filename, field->field_value, field->field_name);
return false;
}
/* move 'data' into 'converted', converting to UTF-8 if necessary */
if(raw) {
converted = data;
}
else if(utf8_encode(data, &converted) >= 0) {
free(data);
}
else {
free(data);
fprintf(stderr, "%s: ERROR: converting file '%s' contents to UTF-8 for tag value\n", filename, field->field_value);
return false;
}
/* create and entry and append it */
if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
free(converted);
fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is not valid UTF-8\n", filename, field->field_value, field->field_name);
return false;
}
free(converted);
if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
*needs_write = true;
return true;
}
else {
FLAC__bool needs_free = false;
if(raw) {
entry.entry = (FLAC__byte *)field->field;
}
else if(utf8_encode(field->field, &converted) >= 0) {
entry.entry = (FLAC__byte *)converted;
needs_free = true;
}
else {
fprintf(stderr, "%s: ERROR: converting comment '%s' to UTF-8\n", filename, field->field);
return false;
}
entry.length = strlen((const char *)entry.entry);
if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
if(needs_free)
free(converted);
/*
* our previous parsing has already established that the field
* name is OK, so it must be the field value
*/
fprintf(stderr, "%s: ERROR: tag value for '%s' is not valid UTF-8\n", filename, field->field_name);
return false;
}
if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
if(needs_free)
free(converted);
fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
return false;
}
*needs_write = true;
if(needs_free)
free(converted);
return true;
}
}
FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
{
FILE *f;
char line[65536];
FLAC__bool ret;
if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
return false;
}
if(0 == strcmp(vc_filename->value, "-"))
f = stdin;
else
f = fopen(vc_filename->value, "r");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, vc_filename->value, strerror(errno));
return false;
}
ret = true;
while(ret && !feof(f)) {
fgets(line, sizeof(line), f);
if(!feof(f)) {
char *p = strchr(line, '\n');
if(0 == p) {
fprintf(stderr, "%s: ERROR: line too long, aborting\n", vc_filename->value);
ret = false;
}
else {
const char *violation;
Argument_VcField field;
*p = '\0';
memset(&field, 0, sizeof(Argument_VcField));
field.field_value_from_file = false;
if(!parse_vorbis_comment_field(line, &field.field, &field.field_name, &field.field_value, &field.field_value_length, &violation)) {
FLAC__ASSERT(0 != violation);
fprintf(stderr, "%s: ERROR: malformed vorbis comment field \"%s\",\n %s\n", vc_filename->value, line, violation);
ret = false;
}
else {
ret = set_vc_field(filename, block, &field, needs_write, raw);
}
if(0 != field.field)
free(field.field);
if(0 != field.field_name)
free(field.field_name);
if(0 != field.field_value)
free(field.field_value);
}
}
};
if(f != stdin)
fclose(f);
return ret;
}
FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw)
{
FILE *f;
FLAC__bool ret;
if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
return false;
}
if(0 == strcmp(vc_filename->value, "-"))
f = stdout;
else
f = fopen(vc_filename->value, "w");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, vc_filename->value, strerror(errno));
return false;
}
ret = true;
write_vc_fields(0, 0, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, f);
if(f != stdout)
fclose(f);
return ret;
}

1109
flac/src/metaflac/options.c Normal file

File diff suppressed because it is too large Load Diff

215
flac/src/metaflac/options.h Normal file
View File

@@ -0,0 +1,215 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#ifndef metaflac__options_h
#define metaflac__options_h
#include "FLAC/format.h"
#if 0
/*[JEC] was:#if HAVE_GETOPT_LONG*/
/*[JEC] see flac/include/share/getopt.h as to why the change */
# include <getopt.h>
#else
# include "share/getopt.h"
#endif
extern struct share__option long_options_[];
typedef enum {
OP__SHOW_MD5SUM,
OP__SHOW_MIN_BLOCKSIZE,
OP__SHOW_MAX_BLOCKSIZE,
OP__SHOW_MIN_FRAMESIZE,
OP__SHOW_MAX_FRAMESIZE,
OP__SHOW_SAMPLE_RATE,
OP__SHOW_CHANNELS,
OP__SHOW_BPS,
OP__SHOW_TOTAL_SAMPLES,
OP__SET_MD5SUM,
OP__SET_MIN_BLOCKSIZE,
OP__SET_MAX_BLOCKSIZE,
OP__SET_MIN_FRAMESIZE,
OP__SET_MAX_FRAMESIZE,
OP__SET_SAMPLE_RATE,
OP__SET_CHANNELS,
OP__SET_BPS,
OP__SET_TOTAL_SAMPLES,
OP__SHOW_VC_VENDOR,
OP__SHOW_VC_FIELD,
OP__REMOVE_VC_ALL,
OP__REMOVE_VC_FIELD,
OP__REMOVE_VC_FIRSTFIELD,
OP__SET_VC_FIELD,
OP__IMPORT_VC_FROM,
OP__EXPORT_VC_TO,
OP__IMPORT_CUESHEET_FROM,
OP__EXPORT_CUESHEET_TO,
OP__IMPORT_PICTURE_FROM,
OP__EXPORT_PICTURE_TO,
OP__ADD_SEEKPOINT,
OP__ADD_REPLAY_GAIN,
OP__ADD_PADDING,
OP__LIST,
OP__APPEND,
OP__REMOVE,
OP__REMOVE_ALL,
OP__MERGE_PADDING,
OP__SORT_PADDING
} OperationType;
typedef enum {
ARG__BLOCK_NUMBER,
ARG__BLOCK_TYPE,
ARG__EXCEPT_BLOCK_TYPE,
ARG__DATA_FORMAT,
ARG__FROM_FILE
} ArgumentType;
typedef struct {
FLAC__byte value[16];
} Argument_StreaminfoMD5;
typedef struct {
FLAC__uint32 value;
} Argument_StreaminfoUInt32;
typedef struct {
FLAC__uint64 value;
} Argument_StreaminfoUInt64;
typedef struct {
char *value;
} Argument_VcFieldName;
typedef struct {
char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
char *field_name;
/* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
unsigned field_value_length;
char *field_value;
FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */
} Argument_VcField;
typedef struct {
char *value;
} Argument_String;
typedef struct {
unsigned num_entries;
unsigned *entries;
} Argument_BlockNumber;
typedef struct {
FLAC__MetadataType type;
char application_id[4]; /* only relevant if type == FLAC__STREAM_METADATA_TYPE_APPLICATION */
FLAC__bool filter_application_by_id;
} Argument_BlockTypeEntry;
typedef struct {
unsigned num_entries;
Argument_BlockTypeEntry *entries;
} Argument_BlockType;
typedef struct {
FLAC__bool is_binary;
} Argument_DataFormat;
typedef struct {
char *file_name;
} Argument_FromFile;
typedef struct {
char *specification;
} Argument_AddSeekpoint;
typedef struct {
char *filename;
Argument_AddSeekpoint *add_seekpoint_link;
} Argument_ImportCuesheetFrom;
typedef struct {
char *filename;
const Argument_BlockNumber *block_number_link; /* may be NULL to mean 'first PICTURE block' */
} Argument_ExportPictureTo;
typedef struct {
unsigned length;
} Argument_AddPadding;
typedef struct {
OperationType type;
union {
Argument_StreaminfoMD5 streaminfo_md5;
Argument_StreaminfoUInt32 streaminfo_uint32;
Argument_StreaminfoUInt64 streaminfo_uint64;
Argument_VcFieldName vc_field_name;
Argument_VcField vc_field;
Argument_String filename;
Argument_String specification;
Argument_ImportCuesheetFrom import_cuesheet_from;
Argument_ExportPictureTo export_picture_to;
Argument_AddSeekpoint add_seekpoint;
Argument_AddPadding add_padding;
} argument;
} Operation;
typedef struct {
ArgumentType type;
union {
Argument_BlockNumber block_number;
Argument_BlockType block_type;
Argument_DataFormat data_format;
Argument_FromFile from_file;
} value;
} Argument;
typedef struct {
FLAC__bool preserve_modtime;
FLAC__bool prefix_with_filename;
FLAC__bool utf8_convert;
FLAC__bool use_padding;
FLAC__bool cued_seekpoints;
FLAC__bool show_long_help;
FLAC__bool show_version;
FLAC__bool application_data_format_is_hexdump;
struct {
Operation *operations;
unsigned num_operations;
unsigned capacity;
} ops;
struct {
struct {
unsigned num_shorthand_ops;
unsigned num_major_ops;
FLAC__bool has_block_type;
FLAC__bool has_except_block_type;
} checks;
Argument *arguments;
unsigned num_arguments;
unsigned capacity;
} args;
unsigned num_files;
char **filenames;
} CommandLineOptions;
void init_options(CommandLineOptions *options);
FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options);
void free_options(CommandLineOptions *options);
#endif

325
flac/src/metaflac/usage.c Normal file
View File

@@ -0,0 +1,325 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "usage.h"
#include "FLAC/format.h"
#include <stdarg.h>
#include <stdio.h>
static void usage_header(FILE *out)
{
fprintf(out, "==============================================================================\n");
fprintf(out, "metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
fprintf(out, "Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson\n");
fprintf(out, "\n");
fprintf(out, "This program is free software; you can redistribute it and/or\n");
fprintf(out, "modify it under the terms of the GNU General Public License\n");
fprintf(out, "as published by the Free Software Foundation; either version 2\n");
fprintf(out, "of the License, or (at your option) any later version.\n");
fprintf(out, "\n");
fprintf(out, "This program is distributed in the hope that it will be useful,\n");
fprintf(out, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
fprintf(out, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
fprintf(out, "GNU General Public License for more details.\n");
fprintf(out, "\n");
fprintf(out, "You should have received a copy of the GNU General Public License\n");
fprintf(out, "along with this program; if not, write to the Free Software\n");
fprintf(out, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
fprintf(out, "==============================================================================\n");
}
static void usage_summary(FILE *out)
{
fprintf(out, "Usage:\n");
fprintf(out, " metaflac [options] [operations] FLACfile [FLACfile ...]\n");
fprintf(out, "\n");
fprintf(out, "Use metaflac to list, add, remove, or edit metadata in one or more FLAC files.\n");
fprintf(out, "You may perform one major operation, or many shorthand operations at a time.\n");
fprintf(out, "\n");
fprintf(out, "Options:\n");
fprintf(out, "--preserve-modtime Preserve the original modification time in spite of edits\n");
fprintf(out, "--with-filename Prefix each output line with the FLAC file name\n");
fprintf(out, " (the default if more than one FLAC file is specified)\n");
fprintf(out, "--no-filename Do not prefix each output line with the FLAC file name\n");
fprintf(out, " (the default if only one FLAC file is specified)\n");
fprintf(out, "--no-utf8-convert Do not convert tags from UTF-8 to local charset,\n");
fprintf(out, " or vice versa. This is useful for scripts, and setting\n");
fprintf(out, " tags in situations where the locale is wrong.\n");
fprintf(out, "--dont-use-padding By default metaflac tries to use padding where possible\n");
fprintf(out, " to avoid rewriting the entire file if the metadata size\n");
fprintf(out, " changes. Use this option to tell metaflac to not take\n");
fprintf(out, " advantage of padding this way.\n");
}
int short_usage(const char *message, ...)
{
va_list args;
if(message) {
va_start(args, message);
(void) vfprintf(stderr, message, args);
va_end(args);
}
usage_header(stderr);
fprintf(stderr, "\n");
fprintf(stderr, "This is the short help; for full help use 'metaflac --help'\n");
fprintf(stderr, "\n");
usage_summary(stderr);
return message? 1 : 0;
}
int long_usage(const char *message, ...)
{
FILE *out = (message? stderr : stdout);
va_list args;
if(message) {
va_start(args, message);
(void) vfprintf(stderr, message, args);
va_end(args);
}
usage_header(out);
fprintf(out, "\n");
usage_summary(out);
fprintf(out, "\n");
fprintf(out, "Shorthand operations:\n");
fprintf(out, "--show-md5sum Show the MD5 signature from the STREAMINFO block.\n");
fprintf(out, "--show-min-blocksize Show the minimum block size from the STREAMINFO block.\n");
fprintf(out, "--show-max-blocksize Show the maximum block size from the STREAMINFO block.\n");
fprintf(out, "--show-min-framesize Show the minimum frame size from the STREAMINFO block.\n");
fprintf(out, "--show-max-framesize Show the maximum frame size from the STREAMINFO block.\n");
fprintf(out, "--show-sample-rate Show the sample rate from the STREAMINFO block.\n");
fprintf(out, "--show-channels Show the number of channels from the STREAMINFO block.\n");
fprintf(out, "--show-bps Show the # of bits per sample from the STREAMINFO block.\n");
fprintf(out, "--show-total-samples Show the total # of samples from the STREAMINFO block.\n");
fprintf(out, "\n");
fprintf(out, "--show-vendor-tag Show the vendor string from the VORBIS_COMMENT block.\n");
fprintf(out, "--show-tag=NAME Show all tags where the the field name matches 'NAME'.\n");
fprintf(out, "--remove-tag=NAME Remove all tags whose field name is 'NAME'.\n");
fprintf(out, "--remove-first-tag=NAME Remove first tag whose field name is 'NAME'.\n");
fprintf(out, "--remove-all-tags Remove all tags, leaving only the vendor string.\n");
fprintf(out, "--set-tag=FIELD Add a tag. The FIELD must comply with the Vorbis comment\n");
fprintf(out, " spec, of the form \"NAME=VALUE\". If there is currently\n");
fprintf(out, " no tag block, one will be created.\n");
fprintf(out, "--set-tag-from-file=FIELD Like --set-tag, except the VALUE is a filename\n");
fprintf(out, " whose contents will be read verbatim to set the tag value.\n");
fprintf(out, " Unless --no-utf8-convert is specified, the contents will\n");
fprintf(out, " be converted to UTF-8 from the local charset. This can\n");
fprintf(out, " be used to store a cuesheet in a tag (e.g.\n");
fprintf(out, " --set-tag-from-file=\"CUESHEET=image.cue\"). Do not try\n");
fprintf(out, " to store binary data in tag fields! Use APPLICATION\n");
fprintf(out, " blocks for that.\n");
fprintf(out, "--import-tags-from=FILE Import tags from a file. Use '-' for stdin. Each line\n");
fprintf(out, " should be of the form NAME=VALUE. Multi-line comments\n");
fprintf(out, " are currently not supported. Specify --remove-all-tags\n");
fprintf(out, " and/or --no-utf8-convert before --import-tags-from if\n");
fprintf(out, " necessary. If FILE is '-' (stdin), only one FLAC file\n");
fprintf(out, " may be specified.\n");
fprintf(out, "--export-tags-to=FILE Export tags to a file. Use '-' for stdout. Each line\n");
fprintf(out, " will be of the form NAME=VALUE. Specify\n");
fprintf(out, " --no-utf8-convert if necessary.\n");
fprintf(out, "--import-cuesheet-from=FILE Import a cuesheet from a file. Use '-' for stdin.\n");
fprintf(out, " Only one FLAC file may be specified. A seekpoint will be\n");
fprintf(out, " added for each index point in the cuesheet to the\n");
fprintf(out, " SEEKTABLE unless --no-cued-seekpoints is specified.\n");
fprintf(out, "--export-cuesheet-to=FILE Export CUESHEET block to a cuesheet file, suitable\n");
fprintf(out, " for use by CD authoring software. Use '-' for stdout.\n");
fprintf(out, " Only one FLAC file may be specified on the command line.\n");
fprintf(out, "--import-picture-from=FILENAME|SPECIFICATION Import a picture and store it in a\n");
fprintf(out, " PICTURE block. Either a filename for the picture file or\n");
fprintf(out, " a more complete specification form can be used. The\n");
fprintf(out, " SPECIFICATION is a string whose parts are separated by |\n");
fprintf(out, " characters. Some parts may be left empty to invoke\n");
fprintf(out, " default values. FILENAME is just shorthand for\n");
fprintf(out, " \"||||FILENAME\". The format of SPECIFICATION is:\n");
fprintf(out, " [TYPE]|[MIME-TYPE]|[DESCRIPTION]|[WIDTHxHEIGHTxDEPTH[/COLORS]]|FILE\n");
fprintf(out, " TYPE is optional; it is a number from one of:\n");
fprintf(out, " 0: Other\n");
fprintf(out, " 1: 32x32 pixels 'file icon' (PNG only)\n");
fprintf(out, " 2: Other file icon\n");
fprintf(out, " 3: Cover (front)\n");
fprintf(out, " 4: Cover (back)\n");
fprintf(out, " 5: Leaflet page\n");
fprintf(out, " 6: Media (e.g. label side of CD)\n");
fprintf(out, " 7: Lead artist/lead performer/soloist\n");
fprintf(out, " 8: Artist/performer\n");
fprintf(out, " 9: Conductor\n");
fprintf(out, " 10: Band/Orchestra\n");
fprintf(out, " 11: Composer\n");
fprintf(out, " 12: Lyricist/text writer\n");
fprintf(out, " 13: Recording Location\n");
fprintf(out, " 14: During recording\n");
fprintf(out, " 15: During performance\n");
fprintf(out, " 16: Movie/video screen capture\n");
fprintf(out, " 17: A bright coloured fish\n");
fprintf(out, " 18: Illustration\n");
fprintf(out, " 19: Band/artist logotype\n");
fprintf(out, " 20: Publisher/Studio logotype\n");
fprintf(out, " The default is 3 (front cover). There may only be one picture each\n");
fprintf(out, " of type 1 and 2 in a file.\n");
fprintf(out, " MIME-TYPE is optional; if left blank, it will be detected from the\n");
fprintf(out, " file. For best compatibility with players, use pictures with MIME\n");
fprintf(out, " type image/jpeg or image/png. The MIME type can also be --> to\n");
fprintf(out, " mean that FILE is actually a URL to an image, though this use is\n");
fprintf(out, " discouraged.\n");
fprintf(out, " DESCRIPTION is optional; the default is an empty string\n");
fprintf(out, " The next part specfies the resolution and color information. If\n");
fprintf(out, " the MIME-TYPE is image/jpeg, image/png, or image/gif, you can\n");
fprintf(out, " usually leave this empty and they can be detected from the file.\n");
fprintf(out, " Otherwise, you must specify the width in pixels, height in pixels,\n");
fprintf(out, " and color depth in bits-per-pixel. If the image has indexed colors\n");
fprintf(out, " you should also specify the number of colors used.\n");
fprintf(out, " FILE is the path to the picture file to be imported, or the URL if\n");
fprintf(out, " MIME type is -->\n");
fprintf(out, "--export-picture-to=FILE Export PICTURE block to a file. Use '-' for stdout.\n");
fprintf(out, " Only one FLAC file may be specified. The first PICTURE\n");
fprintf(out, " block will be exported unless --export-picture-to is\n");
fprintf(out, " preceded by a --block-number=# option to specify the exact\n");
fprintf(out, " metadata block to extract. Note that the block number is\n");
fprintf(out, " the one shown by --list.\n");
fprintf(out, "--add-replay-gain Calculates the title and album gains/peaks of the given\n");
fprintf(out, " FLAC files as if all the files were part of one album,\n");
fprintf(out, " then stores them in the VORBIS_COMMENT block. The tags\n");
fprintf(out, " are the same as those used by vorbisgain. Existing\n");
fprintf(out, " ReplayGain tags will be replaced. If only one FLAC file\n");
fprintf(out, " is given, the album and title gains will be the same.\n");
fprintf(out, " Since this operation requires two passes, it is always\n");
fprintf(out, " executed last, after all other operations have been\n");
fprintf(out, " completed and written to disk. All FLAC files specified\n");
fprintf(out, " must have the same resolution, sample rate, and number\n");
fprintf(out, " of channels. The sample rate must be one of 8, 11.025,\n");
fprintf(out, " 12, 16, 22.05, 24, 32, 44.1, or 48 kHz.\n");
fprintf(out, "--remove-replay-gain Removes the ReplayGain tags.\n");
fprintf(out, "--add-seekpoint={#|X|#x|#s} Add seek points to a SEEKTABLE block\n");
fprintf(out, " # : a specific sample number for a seek point\n");
fprintf(out, " X : a placeholder point (always goes at the end of the SEEKTABLE)\n");
fprintf(out, " #x : # evenly spaced seekpoints, the first being at sample 0\n");
fprintf(out, " #s : a seekpoint every # seconds; # does not have to be a whole number\n");
fprintf(out, " If no SEEKTABLE block exists, one will be created. If\n");
fprintf(out, " one already exists, points will be added to the existing\n");
fprintf(out, " table, and any duplicates will be turned into placeholder\n");
fprintf(out, " points. You may use many --add-seekpoint options; the\n");
fprintf(out, " resulting SEEKTABLE will be the unique-ified union of\n");
fprintf(out, " all such values. Example: --add-seekpoint=100x\n");
fprintf(out, " --add-seekpoint=3.5s will add 100 evenly spaced\n");
fprintf(out, " seekpoints and a seekpoint every 3.5 seconds.\n");
fprintf(out, "--add-padding=length Add a padding block of the given length (in bytes).\n");
fprintf(out, " The overall length of the new block will be 4 + length;\n");
fprintf(out, " the extra 4 bytes is for the metadata block header.\n");
fprintf(out, "\n");
fprintf(out, "Major operations:\n");
fprintf(out, "--version\n");
fprintf(out, " Show the metaflac version number.\n");
fprintf(out, "--list\n");
fprintf(out, " List the contents of one or more metadata blocks to stdout. By default,\n");
fprintf(out, " all metadata blocks are listed in text format. Use the following options\n");
fprintf(out, " to change this behavior:\n");
fprintf(out, "\n");
fprintf(out, " --block-number=#[,#[...]]\n");
fprintf(out, " An optional comma-separated list of block numbers to display. The first\n");
fprintf(out, " block, the STREAMINFO block, is block 0.\n");
fprintf(out, "\n");
fprintf(out, " --block-type=type[,type[...]]\n");
fprintf(out, " --except-block-type=type[,type[...]]\n");
fprintf(out, " An optional comma-separated list of block types to be included or ignored\n");
fprintf(out, " with this option. Use only one of --block-type or --except-block-type.\n");
fprintf(out, " The valid block types are: STREAMINFO, PADDING, APPLICATION, SEEKTABLE,\n");
fprintf(out, " VORBIS_COMMENT. You may narrow down the types of APPLICATION blocks\n");
fprintf(out, " displayed as follows:\n");
fprintf(out, " APPLICATION:abcd The APPLICATION block(s) whose textual repre-\n");
fprintf(out, " sentation of the 4-byte ID is \"abcd\"\n");
fprintf(out, " APPLICATION:0xXXXXXXXX The APPLICATION block(s) whose hexadecimal big-\n");
fprintf(out, " endian representation of the 4-byte ID is\n");
fprintf(out, " \"0xXXXXXXXX\". For the example \"abcd\" above the\n");
fprintf(out, " hexadecimal equivalalent is 0x61626364\n");
fprintf(out, "\n");
fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n");
fprintf(out, " the result is the logical AND of both arguments.\n");
fprintf(out, "\n");
#if 0
/*@@@ not implemented yet */
fprintf(out, " --data-format=binary|text\n");
fprintf(out, " By default a human-readable text representation of the data is displayed.\n");
fprintf(out, " You may specify --data-format=binary to dump the raw binary form of each\n");
fprintf(out, " metadata block. The output can be read in using a subsequent call to\n");
fprintf(out, " "metaflac --append --from-file=..."\n");
fprintf(out, "\n");
#endif
fprintf(out, " --application-data-format=hexdump|text\n");
fprintf(out, " If the application block you are displaying contains binary data but your\n");
fprintf(out, " --data-format=text, you can display a hex dump of the application data\n");
fprintf(out, " contents instead using --application-data-format=hexdump\n");
fprintf(out, "\n");
#if 0
/*@@@ not implemented yet */
fprintf(out, "--append\n");
fprintf(out, " Insert a metadata block from a file. The input file must be in the same\n");
fprintf(out, " format as generated with --list.\n");
fprintf(out, "\n");
fprintf(out, " --block-number=#\n");
fprintf(out, " Specify the insertion point (defaults to last block). The new block will\n");
fprintf(out, " be added after the given block number. This prevents the illegal insertion\n");
fprintf(out, " of a block before the first STREAMINFO block. You may not --append another\n");
fprintf(out, " STREAMINFO block.\n");
fprintf(out, "\n");
fprintf(out, " --from-file=filename\n");
fprintf(out, " Mandatory 'option' to specify the input file containing the block contents.\n");
fprintf(out, "\n");
fprintf(out, " --data-format=binary|text\n");
fprintf(out, " By default the block contents are assumed to be in binary format. You can\n");
fprintf(out, " override this by specifying --data-format=text\n");
fprintf(out, "\n");
#endif
fprintf(out, "--remove\n");
fprintf(out, " Remove one or more metadata blocks from the metadata. Unless\n");
fprintf(out, " --dont-use-padding is specified, the blocks will be replaced with padding.\n");
fprintf(out, " You may not remove the STREAMINFO block.\n");
fprintf(out, "\n");
fprintf(out, " --block-number=#[,#[...]]\n");
fprintf(out, " --block-type=type[,type[...]]\n");
fprintf(out, " --except-block-type=type[,type[...]]\n");
fprintf(out, " See --list above for usage.\n");
fprintf(out, "\n");
fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n");
fprintf(out, " the result is the logical AND of both arguments.\n");
fprintf(out, "\n");
fprintf(out, "--remove-all\n");
fprintf(out, " Remove all metadata blocks (except the STREAMINFO block) from the\n");
fprintf(out, " metadata. Unless --dont-use-padding is specified, the blocks will be\n");
fprintf(out, " replaced with padding.\n");
fprintf(out, "\n");
fprintf(out, "--merge-padding\n");
fprintf(out, " Merge adjacent PADDING blocks into single blocks.\n");
fprintf(out, "\n");
fprintf(out, "--sort-padding\n");
fprintf(out, " Move all PADDING blocks to the end of the metadata and merge them into a\n");
fprintf(out, " single block.\n");
return message? 1 : 0;
}

25
flac/src/metaflac/usage.h Normal file
View File

@@ -0,0 +1,25 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#ifndef metaflac__usage_h
#define metaflac__usage_h
int short_usage(const char *message, ...);
int long_usage(const char *message, ...);
#endif

258
flac/src/metaflac/utils.c Normal file
View File

@@ -0,0 +1,258 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "utils.h"
#include "FLAC/assert.h"
#include "share/alloc.h"
#include "share/utf8.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void die(const char *message)
{
FLAC__ASSERT(0 != message);
fprintf(stderr, "ERROR: %s\n", message);
exit(1);
}
#ifdef FLAC__VALGRIND_TESTING
size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#endif
char *local_strdup(const char *source)
{
char *ret;
FLAC__ASSERT(0 != source);
if(0 == (ret = strdup(source)))
die("out of memory during strdup()");
return ret;
}
void local_strcat(char **dest, const char *source)
{
size_t ndest, nsource;
FLAC__ASSERT(0 != dest);
FLAC__ASSERT(0 != source);
ndest = *dest? strlen(*dest) : 0;
nsource = strlen(source);
if(nsource == 0)
return;
*dest = (char*)safe_realloc_add_3op_(*dest, ndest, /*+*/nsource, /*+*/1);
if(0 == *dest)
die("out of memory growing string");
strcpy((*dest)+ndest, source);
}
void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent)
{
unsigned i, left = bytes;
const FLAC__byte *b = buf;
for(i = 0; i < bytes; i += 16) {
printf("%s%s%s%08X: "
"%02X %02X %02X %02X %02X %02X %02X %02X "
"%02X %02X %02X %02X %02X %02X %02X %02X "
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
filename? filename:"", filename? ":":"",
indent, i,
left > 0? (unsigned char)b[ 0] : 0,
left > 1? (unsigned char)b[ 1] : 0,
left > 2? (unsigned char)b[ 2] : 0,
left > 3? (unsigned char)b[ 3] : 0,
left > 4? (unsigned char)b[ 4] : 0,
left > 5? (unsigned char)b[ 5] : 0,
left > 6? (unsigned char)b[ 6] : 0,
left > 7? (unsigned char)b[ 7] : 0,
left > 8? (unsigned char)b[ 8] : 0,
left > 9? (unsigned char)b[ 9] : 0,
left > 10? (unsigned char)b[10] : 0,
left > 11? (unsigned char)b[11] : 0,
left > 12? (unsigned char)b[12] : 0,
left > 13? (unsigned char)b[13] : 0,
left > 14? (unsigned char)b[14] : 0,
left > 15? (unsigned char)b[15] : 0,
(left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
(left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
(left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
(left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
(left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
(left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
(left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
(left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
(left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
(left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
(left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
(left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
(left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
(left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
(left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
(left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '
);
left -= 16;
b += 16;
}
}
void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...)
{
const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain);
va_list args;
FLAC__ASSERT(0 != format);
va_start(args, format);
(void) vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, ", status = \"%s\"\n", FLAC__Metadata_ChainStatusString[status]);
if(status == FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE) {
fprintf(stderr, "\n"
"The FLAC file could not be opened. Most likely the file does not exist\n"
"or is not readable.\n"
);
}
else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE) {
fprintf(stderr, "\n"
"The file does not appear to be a FLAC file.\n"
);
}
else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE) {
fprintf(stderr, "\n"
"The FLAC file does not have write permissions.\n"
);
}
else if(status == FLAC__METADATA_CHAIN_STATUS_BAD_METADATA) {
fprintf(stderr, "\n"
"The metadata to be writted does not conform to the FLAC metadata\n"
"specifications.\n"
);
}
else if(status == FLAC__METADATA_CHAIN_STATUS_READ_ERROR) {
fprintf(stderr, "\n"
"There was an error while reading the FLAC file.\n"
);
}
else if(status == FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR) {
fprintf(stderr, "\n"
"There was an error while writing FLAC file; most probably the disk is\n"
"full.\n"
);
}
else if(status == FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR) {
fprintf(stderr, "\n"
"There was an error removing the temporary FLAC file.\n"
);
}
}
FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
{
static const char * const violations[] = {
"field name contains invalid character",
"field contains no '=' character"
};
char *p, *q, *s;
if(0 != field)
*field = local_strdup(field_ref);
s = local_strdup(field_ref);
if(0 == (p = strchr(s, '='))) {
free(s);
*violation = violations[1];
return false;
}
*p++ = '\0';
for(q = s; *q; q++) {
if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
free(s);
*violation = violations[0];
return false;
}
}
*name = local_strdup(s);
*value = local_strdup(p);
*length = strlen(p);
free(s);
return true;
}
void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__bool raw, FILE *f)
{
if(0 != entry->entry) {
if(filename)
fprintf(f, "%s:", filename);
if(!raw) {
/*
* WATCHOUT: comments that contain an embedded null will
* be truncated by utf_decode().
*/
char *converted;
if(utf8_decode((const char *)entry->entry, &converted) >= 0) {
(void) local_fwrite(converted, 1, strlen(converted), f);
free(converted);
}
else {
(void) local_fwrite(entry->entry, 1, entry->length, f);
}
}
else {
(void) local_fwrite(entry->entry, 1, entry->length, f);
}
}
putc('\n', f);
}
void write_vc_fields(const char *filename, const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry entry[], unsigned num_entries, FLAC__bool raw, FILE *f)
{
unsigned i;
const unsigned field_name_length = (0 != field_name)? strlen(field_name) : 0;
for(i = 0; i < num_entries; i++) {
if(0 == field_name || FLAC__metadata_object_vorbiscomment_entry_matches(entry[i], field_name, field_name_length))
write_vc_field(filename, entry + i, raw, f);
}
}

41
flac/src/metaflac/utils.h Normal file
View File

@@ -0,0 +1,41 @@
/* metaflac - Command-line FLAC metadata editor
* Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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.
*/
#ifndef metaflac__utils_h
#define metaflac__utils_h
#include "FLAC/metadata.h"
#include <stdio.h> /* for FILE */
void die(const char *message);
#ifdef FLAC__VALGRIND_TESTING
size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
#else
#define local_fwrite fwrite
#endif
char *local_strdup(const char *source);
void local_strcat(char **dest, const char *source);
void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent);
void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...);
FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation);
void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__bool raw, FILE *f);
void write_vc_fields(const char *filename, const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry entry[], unsigned num_entries, FLAC__bool raw, FILE *f);
#endif