Unify some duplicate internal functions.

This commit is contained in:
Tsuda Kageyu 2015-11-30 16:35:37 +09:00
parent bccd57a470
commit eb6542bc8b
3 changed files with 65 additions and 33 deletions

View File

@ -23,13 +23,15 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include <algorithm>
#include <vector>
#include <tbytevector.h>
#include <tdebug.h>
#include <tstring.h>
#include "rifffile.h"
#include <algorithm>
#include <vector>
#include "riffutils.h"
using namespace TagLib;
@ -175,7 +177,7 @@ void RIFF::File::setChunkData(const ByteVector &name, const ByteVector &data, bo
// Couldn't find an existing chunk, so let's create a new one.
uint i = d->chunks.size() - 1;
uint i = d->chunks.size() - 1;
ulong offset = d->chunks[i].offset + d->chunks[i].size;
// First we update the global size
@ -189,7 +191,7 @@ void RIFF::File::setChunkData(const ByteVector &name, const ByteVector &data, bo
// And update our internal structure
if (offset & 1) {
if(offset & 1) {
d->chunks[i].padding = 1;
offset++;
}
@ -231,19 +233,6 @@ void RIFF::File::removeChunk(const ByteVector &name)
// private members
////////////////////////////////////////////////////////////////////////////////
static bool isValidChunkID(const ByteVector &name)
{
if(name.size() != 4) {
return false;
}
for(int i = 0; i < 4; i++) {
if(name[i] < 32 || name[i] > 127) {
return false;
}
}
return true;
}
void RIFF::File::read()
{
bool bigEndian = (d->endianness == BigEndian);
@ -257,7 +246,7 @@ void RIFF::File::read()
ByteVector chunkName = readBlock(4);
uint chunkSize = readBlock(4).toUInt(bigEndian);
if(!isValidChunkID(chunkName)) {
if(!isValidChunkName(chunkName)) {
debug("RIFF::File::read() -- Chunk '" + chunkName + "' has invalid ID");
setValid(false);
break;

55
taglib/riff/riffutils.h Normal file
View File

@ -0,0 +1,55 @@
/***************************************************************************
copyright : (C) 2015 by Tsuda Kageyu
email : tsuda.kageyu@gmail.com
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifndef TAGLIB_RIFFUTILS_H
#define TAGLIB_RIFFUTILS_H
// THIS FILE IS NOT A PART OF THE TAGLIB API
#ifndef DO_NOT_DOCUMENT // tell Doxygen not to document this header
namespace TagLib
{
namespace RIFF
{
static bool isValidChunkName(const ByteVector &name)
{
if(name.size() != 4)
return false;
for(ByteVector::ConstIterator it = name.begin(); it != name.end(); ++it) {
const uchar c = static_cast<uchar>(*it);
if(c < 32 || 127 < c)
return false;
}
return true;
}
}
}
#endif
#endif

View File

@ -27,25 +27,13 @@
#include <tfile.h>
#include "infotag.h"
#include "riffutils.h"
using namespace TagLib;
using namespace RIFF::Info;
namespace
{
inline bool isValidChunkID(const ByteVector &name)
{
if(name.size() != 4)
return false;
for(int i = 0; i < 4; i++) {
if(name[i] < 32 || name[i] > 127)
return false;
}
return true;
}
const RIFF::Info::StringHandler defaultStringHandler;
const RIFF::Info::StringHandler *stringHandler = &defaultStringHandler;
}
@ -197,7 +185,7 @@ String RIFF::Info::Tag::fieldText(const ByteVector &id) const
void RIFF::Info::Tag::setFieldText(const ByteVector &id, const String &s)
{
// id must be four-byte long pure ascii string.
if(!isValidChunkID(id))
if(!isValidChunkName(id))
return;
if(!s.isEmpty())
@ -258,7 +246,7 @@ void RIFF::Info::Tag::parse(const ByteVector &data)
break;
const ByteVector id = data.mid(p, 4);
if(isValidChunkID(id)) {
if(isValidChunkName(id)) {
const String text = stringHandler->parse(data.mid(p + 8, size));
d->fieldListMap[id] = text;
}