⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 field.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    _changed(false),
    _fixed_size(0),
    _num_items(0),
    _enc(ID3TE_NONE)
{
  this->Clear();
}

ID3_FieldImpl::ID3_FieldImpl(const ID3_FieldDef& def)
  : _id(def._id),
    _type(def._type),
    _spec_begin(def._spec_begin),
    _spec_end(def._spec_end),
    _flags(def._flags),
    _changed(false),
    _fixed_size(def._fixed_size),
    _num_items(0),
    _enc((_type == ID3FTY_TEXTSTRING) ? ID3TE_ASCII : ID3TE_NONE)
{
  this->Clear();
}

ID3_FieldImpl::~ID3_FieldImpl()
{
}

/** Clears any data and frees any memory associated with the field
 ** 
 ** \sa ID3_Tag::Clear()
 ** \sa ID3_Frame::Clear()
 **/
void ID3_FieldImpl::Clear()
{
  switch (_type)
  {
    case ID3FTY_INTEGER:
    {
      _integer = 0;
      break;
    }
    case ID3FTY_BINARY:
    {
      _binary.erase();
      if (_fixed_size > 0)
      {
        _binary.assign(_fixed_size, '\0');
      }
      break;
    }
    case ID3FTY_TEXTSTRING:
    {
      _text.erase();
      if (_fixed_size > 0)
      {
        if (this->GetEncoding() == ID3TE_UNICODE)
        {
          _text.assign(_fixed_size * 2, '\0');
        }
        else if (this->GetEncoding() == ID3TE_ASCII)
        {
          _text.assign(_fixed_size, '\0');
        }
      }
      break;
    }
    default:
    {
      break;
    }
  }
  _changed    = true;
  
  return ;
}

bool
ID3_FieldImpl::HasChanged() const
{
  return _changed;
}

/** \fn size_t ID3_Field::Size() const
 ** \brief Returns the size of a field.
 ** 
 ** The value returned is dependent on the type of the field.  For ASCII
 ** strings, this returns the number of characters in the field, no including
 ** any NULL-terminator.  The same holds true for Unicode---it returns the
 ** number of characters in the field, not bytes, and this does not include
 ** the Unicode BOM, which isn't put in a Unicode string obtained by the
 ** Get(unicode_t*, size_t, index_t) method anyway.  For binary and
 ** integer fields, this returns the number of bytes in the field.
 ** 
 ** \code
 **   size_t howBig = myFrame.GetField(ID3FN_DATA)->Size();
 ** \endcode
 ** 
 ** \return The size of the field, either in bytes (for binary or integer
 **         fields) or characters (for strings).
 **/

size_t ID3_FieldImpl::BinSize() const
{
  if (_fixed_size > 0)
  {
    return _fixed_size;
  }
  size_t size = this->Size();
  if (_type == ID3FTY_TEXTSTRING)
  {
    ID3_TextEnc enc = this->GetEncoding();
    if (enc == ID3TE_UNICODE && size > 0)
    {
      size++;
    }
    if (_flags & ID3FF_CSTR)
    {
      size++;
    }
    if (enc == ID3TE_UNICODE)
    {
      size *= 2;
    }
  }
  return size;
}

size_t ID3_FieldImpl::Size() const
{
  size_t size = 0;
  // check to see if we are within the legal limit for this field 0 means
  // arbitrary length field
  if (_fixed_size > 0)
  {
    size = _fixed_size;
  }
  else if (_type == ID3FTY_INTEGER)
  {
    size = sizeof(uint32);
  }
  else if (_type == ID3FTY_TEXTSTRING)
  {
    size = _text.size();
  }
  else
  {
    size = _binary.size();
  }

  return size;
}

bool ID3_FieldImpl::Parse(ID3_Reader& reader)
{
  bool success = false;
  switch (this->GetType())
  {
    case ID3FTY_INTEGER:
    {
      success = this->ParseInteger(reader);
      break;
    }
        
    case ID3FTY_BINARY:
    {
      success = this->ParseBinary(reader);
      break;
    }
        
    case ID3FTY_TEXTSTRING:
    {
      success = this->ParseText(reader);
      break;
    }

    default:
    {
      ID3D_WARNING( "ID3_FieldImpl::Parse(): unknown field type" );
      break;
    }
  }
  return success;
}

ID3_FrameDef* ID3_FindFrameDef(ID3_FrameID id)
{
  ID3_FrameDef  *info   = NULL;

  for (index_t cur = 0; ID3_FrameDefs[cur].eID != ID3FID_NOFRAME; cur++)
  {
    if (ID3_FrameDefs[cur].eID == id)
    {
      info = &ID3_FrameDefs[cur];
      break;
    }
  }
    
  return info;
}

ID3_FrameID
ID3_FindFrameID(const char *id)
{
  ID3_FrameID fid = ID3FID_NOFRAME;
  
  for (index_t cur = 0; ID3_FrameDefs[cur].eID != ID3FID_NOFRAME; cur++)
  {
    if (((strcmp(ID3_FrameDefs[cur].sShortTextID, id) == 0) &&
         strlen(id) == 3) ||
        ((strcmp(ID3_FrameDefs[cur].sLongTextID,  id) == 0) &&
         strlen(id) == 4))
    {
      fid = ID3_FrameDefs[cur].eID;
      break;
    }
  }
  
  return fid;
}

void ID3_FieldImpl::Render(ID3_Writer& writer) const
{
  switch (this->GetType()) 
  {
    case ID3FTY_INTEGER:
    {
      RenderInteger(writer);
      break;
    }
        
    case ID3FTY_BINARY:
    {
      RenderBinary(writer);
      break;
    }
        
    case ID3FTY_TEXTSTRING:
    {
      RenderText(writer);
      break;
    }
        
    default:
    {
      ID3D_WARNING ( "ID3D_FieldImpl::Render(): unknown field type" );
      break;
    }
  }
}

ID3_Field &
ID3_FieldImpl::operator=( const ID3_Field &rhs )
{
  const ID3_FieldImpl* fld = (const ID3_FieldImpl*) &rhs;
  if (this != &rhs && this->GetType() == fld->GetType())
  {
    switch (fld->GetType())
    {
      case ID3FTY_INTEGER:
      {
        this->SetInteger(fld->GetInteger());
        break;
      }
      case ID3FTY_TEXTSTRING:
      {
        this->SetEncoding(fld->GetEncoding());
        this->SetText(fld->GetText());
        break;
      }
      case ID3FTY_BINARY:
      {
        this->SetBinary(fld->GetBinary());
        break;
      }
      default:
      {
        break;
      }
    }
  }
  return *this;
}

bool ID3_FieldImpl::SetEncoding(ID3_TextEnc enc)
{
  bool changed = this->IsEncodable() && (enc != this->GetEncoding()) &&
    (ID3TE_NONE < enc && enc < ID3TE_NUMENCODINGS);
  if (changed)
  {
    _text = convert(_text, _enc, enc);
    _enc = enc;
    _changed = true;
  }
  return changed;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -