Improve encapsulation of string data in SF_PRIVATE.

This commit is contained in:
Erik de Castro Lopo 2012-02-02 21:10:35 +11:00
parent b5c45de7bc
commit 3033376361
11 changed files with 80 additions and 73 deletions

View File

@ -1,3 +1,8 @@
2012-02-02 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/common.h src*.c
Improve encapsulation of string data in SF_PRIVATE.
2012-02-01 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/common.h src*.c

View File

@ -272,7 +272,7 @@ aiff_open (SF_PRIVATE *psf)
psf->sf.frames = 0 ;
} ;
psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
if ((error = aiff_write_header (psf, SF_FALSE)))
return error ;
@ -1410,7 +1410,7 @@ aiff_write_header (SF_PRIVATE *psf, int calc_length)
m [3].markerID, m [3].position, 8, "end loop", make_size_t (9)) ;
} ;
if (psf->str_flags & SF_STR_LOCATE_START)
if (psf->strings.flags & SF_STR_LOCATE_START)
aiff_write_strings (psf, SF_STR_LOCATE_START) ;
if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
@ -1470,7 +1470,7 @@ aiff_write_tailer (SF_PRIVATE *psf)
psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
} ;
if (psf->str_flags & SF_STR_LOCATE_END)
if (psf->strings.flags & SF_STR_LOCATE_END)
aiff_write_strings (psf, SF_STR_LOCATE_END) ;
/* Write the tailer. */
@ -1485,37 +1485,37 @@ aiff_write_strings (SF_PRIVATE *psf, int location)
{ int k, slen ;
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
{ if (psf->strings [k].type == 0)
{ if (psf->strings.data [k].type == 0)
break ;
if (psf->strings [k].flags != location)
if (psf->strings.data [k].flags != location)
continue ;
switch (psf->strings [k].type)
switch (psf->strings.data [k].type)
{ case SF_STR_SOFTWARE :
slen = strlen (psf->strings [k].str) ;
psf_binheader_writef (psf, "Em4mb", APPL_MARKER, slen + 4, m3ga_MARKER, psf->strings [k].str, make_size_t (slen + (slen & 1))) ;
slen = strlen (psf->strings.data [k].str) ;
psf_binheader_writef (psf, "Em4mb", APPL_MARKER, slen + 4, m3ga_MARKER, psf->strings.data [k].str, make_size_t (slen + (slen & 1))) ;
break ;
case SF_STR_TITLE :
psf_binheader_writef (psf, "EmS", NAME_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "EmS", NAME_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_COPYRIGHT :
psf_binheader_writef (psf, "EmS", c_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "EmS", c_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_ARTIST :
psf_binheader_writef (psf, "EmS", AUTH_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "EmS", AUTH_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_COMMENT :
psf_binheader_writef (psf, "EmS", ANNO_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "EmS", ANNO_MARKER, psf->strings.data [k].str) ;
break ;
/*
case SF_STR_DATE :
psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings.data [k].str) ;
break ;
*/
} ;

View File

@ -140,7 +140,7 @@ caf_open (SF_PRIVATE *psf)
psf->sf.frames = 0 ;
} ;
psf->str_flags = SF_STR_ALLOW_START ;
psf->strings.flags = SF_STR_ALLOW_START ;
/*
** By default, add the peak chunk to floating point files. Default behaviour
@ -565,7 +565,7 @@ caf_write_header (SF_PRIVATE *psf, int calc_length)
psf_binheader_writef (psf, "mE44444", desc.fmt_id, desc.fmt_flags, desc.pkt_bytes, desc.pkt_frames, desc.channels_per_frame, desc.bits_per_chan) ;
#if 0
if (psf->str_flags & SF_STR_LOCATE_START)
if (psf->strings.flags & SF_STR_LOCATE_START)
caf_write_strings (psf, SF_STR_LOCATE_START) ;
#endif

View File

@ -361,10 +361,12 @@ typedef struct sf_private_tag
/* Storage and housekeeping data for adding/reading strings from
** sound files.
*/
STR_DATA strings [SF_MAX_STRINGS] ;
char str_storage [SF_STR_BUFFER_LEN] ;
char *str_end ;
int str_flags ;
struct
{ STR_DATA data [SF_MAX_STRINGS] ;
char storage [SF_STR_BUFFER_LEN] ;
char *str_end ;
int flags ;
} strings ;
/* Guard value. If this changes the buffers above have overflowed. */
int Magick ;

View File

@ -534,7 +534,7 @@ flac_write_strings (SF_PRIVATE *psf, FLAC_PRIVATE* pflac)
int k, string_count = 0 ;
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
{ if (psf->strings [k].type != 0)
{ if (psf->strings.data [k].type != 0)
string_count ++ ;
} ;
@ -546,10 +546,10 @@ flac_write_strings (SF_PRIVATE *psf, FLAC_PRIVATE* pflac)
return ;
} ;
for (k = 0 ; k < SF_MAX_STRINGS && psf->strings [k].type != 0 ; k++)
for (k = 0 ; k < SF_MAX_STRINGS && psf->strings.data [k].type != 0 ; k++)
{ const char * key, * value ;
switch (psf->strings [k].type)
switch (psf->strings.data [k].type)
{ case SF_STR_SOFTWARE :
key = "software" ;
break ;
@ -584,7 +584,7 @@ flac_write_strings (SF_PRIVATE *psf, FLAC_PRIVATE* pflac)
continue ;
} ;
value = psf->strings [k].str ;
value = psf->strings.data [k].str ;
FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair (&entry, key, value) ;
FLAC__metadata_object_vorbiscomment_append_comment (pflac->metadata, entry, /* copy */ SF_FALSE) ;
@ -646,7 +646,7 @@ flac_open (SF_PRIVATE *psf)
psf->endian = SF_ENDIAN_BIG ;
psf->sf.seekable = 0 ;
psf->str_flags = SF_STR_ALLOW_START ;
psf->strings.flags = SF_STR_ALLOW_START ;
if ((error = flac_enc_init (psf)))
return error ;

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2008-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software ; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
@ -100,7 +100,7 @@ ogg_pcm_open (SF_PRIVATE *psf)
#endif
psf->sf.frames = SF_COUNT_MAX ; /* Unknown really */
psf->str_flags = SF_STR_ALLOW_START ;
psf->strings.flags = SF_STR_ALLOW_START ;
} ;
psf->bytewidth = 1 ;

View File

@ -116,7 +116,7 @@ ogg_speex_open (SF_PRIVATE *psf)
#endif
psf->sf.frames = SF_COUNT_MAX ; /* Unknown really */
psf->str_flags = SF_STR_ALLOW_START ;
psf->strings.flags = SF_STR_ALLOW_START ;
} ;
psf->bytewidth = 1 ;

View File

@ -366,10 +366,10 @@ vorbis_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
{ const char * name ;
if (psf->strings [k].type == 0)
if (psf->strings.data [k].type == 0)
break ;
switch (psf->strings [k].type)
switch (psf->strings.data [k].type)
{ case SF_STR_TITLE : name = "TITLE" ; break ;
case SF_STR_COPYRIGHT : name = "COPYRIGHT" ; break ;
case SF_STR_SOFTWARE : name = "SOFTWARE" ; break ;
@ -381,7 +381,7 @@ vorbis_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
default : continue ;
} ;
vorbis_comment_add_tag (&vdata->vcomment, name, psf->strings [k].str) ;
vorbis_comment_add_tag (&vdata->vcomment, name, psf->strings.data [k].str) ;
} ;
/* set up the analysis state and auxiliary encoding storage */
@ -525,7 +525,7 @@ ogg_vorbis_open (SF_PRIVATE *psf)
psf->write_double = vorbis_write_d ;
psf->sf.frames = SF_COUNT_MAX ; /* Unknown really */
psf->str_flags = SF_STR_ALLOW_START ;
psf->strings.flags = SF_STR_ALLOW_START ;
} ;
psf->bytewidth = 1 ;

View File

@ -602,7 +602,7 @@ rf64_write_header (SF_PRIVATE *psf, int calc_length)
#if 0
/* The LIST/INFO chunk. */
if (psf->str_flags & SF_STR_LOCATE_START)
if (psf->strings.flags & SF_STR_LOCATE_START)
wav_write_strings (psf, SF_STR_LOCATE_START) ;
if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2001-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
@ -43,9 +43,9 @@ psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
/* A few extra checks for write mode. */
if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
{ if ((psf->str_flags & SF_STR_ALLOW_START) == 0)
{ if ((psf->strings.flags & SF_STR_ALLOW_START) == 0)
return SFE_STR_NO_SUPPORT ;
if (psf->have_written && (psf->str_flags & SF_STR_ALLOW_END) == 0)
if (psf->have_written && (psf->strings.flags & SF_STR_ALLOW_END) == 0)
return SFE_STR_NO_SUPPORT ;
/* Only allow zero length strings for software. */
if (str_type != SF_STR_SOFTWARE && str_len == 0)
@ -55,17 +55,17 @@ psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
/* Find the next free slot in table. */
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
{ /* If we find a matching entry clear it. */
if (psf->strings [k].type == str_type)
psf->strings [k].type = -1 ;
if (psf->strings.data [k].type == str_type)
psf->strings.data [k].type = -1 ;
if (psf->strings [k].type == 0)
if (psf->strings.data [k].type == 0)
break ;
} ;
/* Determine flags */
str_flags = SF_STR_LOCATE_START ;
if (psf->file.mode == SFM_RDWR || psf->have_written)
{ if ((psf->str_flags & SF_STR_ALLOW_END) == 0)
{ if ((psf->strings.flags & SF_STR_ALLOW_END) == 0)
return SFE_STR_NO_ADD_END ;
str_flags = SF_STR_LOCATE_END ;
} ;
@ -74,19 +74,19 @@ psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
if (k >= SF_MAX_STRINGS)
return SFE_STR_MAX_COUNT ;
if (k == 0 && psf->str_end != NULL)
{ psf_log_printf (psf, "SFE_STR_WEIRD : k == 0 && psf->str_end != NULL\n") ;
if (k == 0 && psf->strings.str_end != NULL)
{ psf_log_printf (psf, "SFE_STR_WEIRD : k == 0 && psf->strings.str_end != NULL\n") ;
return SFE_STR_WEIRD ;
} ;
if (k != 0 && psf->str_end == NULL)
{ psf_log_printf (psf, "SFE_STR_WEIRD : k != 0 && psf->str_end == NULL\n") ;
if (k != 0 && psf->strings.str_end == NULL)
{ psf_log_printf (psf, "SFE_STR_WEIRD : k != 0 && psf->strings.str_end == NULL\n") ;
return SFE_STR_WEIRD ;
} ;
/* Special case for the first string. */
if (k == 0)
psf->str_end = psf->str_storage ;
psf->strings.str_end = psf->strings.storage ;
switch (str_type)
{ case SF_STR_SOFTWARE :
@ -127,29 +127,29 @@ psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
str_len = strlen (str) ;
len_remaining = SIGNED_SIZEOF (psf->str_storage) - (psf->str_end - psf->str_storage) ;
len_remaining = SIGNED_SIZEOF (psf->strings.storage) - (psf->strings.str_end - psf->strings.storage) ;
if (len_remaining < str_len + 2)
return SFE_STR_MAX_DATA ;
psf->strings [k].type = str_type ;
psf->strings [k].str = psf->str_end ;
psf->strings [k].flags = str_flags ;
psf->strings.data [k].type = str_type ;
psf->strings.data [k].str = psf->strings.str_end ;
psf->strings.data [k].flags = str_flags ;
memcpy (psf->str_end, str, str_len + 1) ;
memcpy (psf->strings.str_end, str, str_len + 1) ;
/* Plus one to catch string terminator. */
psf->str_end += str_len + 1 ;
psf->strings.str_end += str_len + 1 ;
psf->str_flags |= str_flags ;
psf->strings.flags |= str_flags ;
#if STRINGS_DEBUG
psf_log_printf (psf, "str_storage : %X\n", (int) psf->str_storage) ;
psf_log_printf (psf, "str_end : %X\n", (int) psf->str_end) ;
psf_log_printf (psf, "sizeof (str_storage) : %d\n", SIGNED_SIZEOF (psf->str_storage)) ;
psf_log_printf (psf, "used : %d\n", (int ) (psf->str_end - psf->str_storage)) ;
psf_log_printf (psf, "remaining : %d\n", SIGNED_SIZEOF (psf->str_storage) - (psf->str_end - psf->str_storage)) ;
psf_log_printf (psf, "str_storage : %X\n", (int) psf->strings.storage) ;
psf_log_printf (psf, "str_end : %X\n", (int) psf->strings.str_end) ;
psf_log_printf (psf, "sizeof (str_storage) : %d\n", SIGNED_SIZEOF (psf->strings.storage)) ;
psf_log_printf (psf, "used : %d\n", (int ) (psf->strings.str_end - psf->strings.storage)) ;
psf_log_printf (psf, "remaining : %d\n", SIGNED_SIZEOF (psf->strings.storage) - (psf->strings.str_end - psf->strings.storage)) ;
hexdump (psf->str_storage, 300) ;
hexdump (psf->strings.storage, 300) ;
#endif
return 0 ;
@ -168,8 +168,8 @@ psf_get_string (SF_PRIVATE *psf, int str_type)
{ int k ;
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
if (str_type == psf->strings [k].type)
return psf->strings [k].str ;
if (str_type == psf->strings.data [k].type)
return psf->strings.data [k].str ;
return NULL ;
} /* psf_get_string */
@ -179,7 +179,7 @@ psf_location_string_count (const SF_PRIVATE * psf, int location)
{ int k, count = 0 ;
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
if (psf->strings [k].type > 0 && psf->strings [k].flags & location)
if (psf->strings.data [k].type > 0 && psf->strings.data [k].flags & location)
count ++ ;
return count ;

View File

@ -189,7 +189,7 @@ wav_open (SF_PRIVATE *psf)
psf->container_data = wpriv ;
wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
{ if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
@ -1062,7 +1062,7 @@ wav_write_header (SF_PRIVATE *psf, int calc_length)
} ;
/* The LIST/INFO chunk. */
if (psf->str_flags & SF_STR_LOCATE_START)
if (psf->strings.flags & SF_STR_LOCATE_START)
wav_write_strings (psf, SF_STR_LOCATE_START) ;
if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
@ -1157,7 +1157,7 @@ wav_write_tailer (SF_PRIVATE *psf)
psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
} ;
if (psf->str_flags & SF_STR_LOCATE_END)
if (psf->strings.flags & SF_STR_LOCATE_END)
wav_write_strings (psf, SF_STR_LOCATE_END) ;
/* Write the tailer. */
@ -1179,38 +1179,38 @@ wav_write_strings (SF_PRIVATE *psf, int location)
psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
for (k = 0 ; k < SF_MAX_STRINGS ; k++)
{ if (psf->strings [k].type == 0)
{ if (psf->strings.data [k].type == 0)
break ;
if (psf->strings [k].type < 0 || psf->strings [k].flags != location)
if (psf->strings.data [k].type < 0 || psf->strings.data [k].flags != location)
continue ;
switch (psf->strings [k].type)
switch (psf->strings.data [k].type)
{ case SF_STR_SOFTWARE :
psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_TITLE :
psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_COPYRIGHT :
psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_ARTIST :
psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_COMMENT :
psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_DATE :
psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings.data [k].str) ;
break ;
case SF_STR_GENRE :
psf_binheader_writef (psf, "ms", IGNR_MARKER, psf->strings [k].str) ;
psf_binheader_writef (psf, "ms", IGNR_MARKER, psf->strings.data [k].str) ;
break ;
default :