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

📄 misc_support.cpp

📁 本人收集整理的一份c/c++跨平台网络库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// $Id: misc_support.cpp,v 1.39 2002/09/19 10:20:45 t1mpy Exp $

// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
// Copyright 1999, 2000  Scott Thomas Haug
// Copyright 2002 Thijmen Klok (thijmen@id3lib.org)

// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// 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 Library General Public
// License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

// The id3lib authors encourage improvements and optimisations to be sent to
// the id3lib coordinator.  Please see the README file for details on where to
// send such submissions.  See the AUTHORS file for a list of people who have
// contributed to id3lib.  See the ChangeLog file for a list of changes to
// id3lib.  These files are distributed with id3lib at
// http://download.sourceforge.net/id3lib/

//#include <ctype.h>
#include <stdio.h>

#include "misc_support.h"
//#include "field.h"
#include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h"

//using namespace dami;

char *ID3_GetString(const ID3_Frame *frame, ID3_FieldID fldName)
{
  char *text = NULL;
//  if (NULL != frame)
  ID3_Field* fld;
  if (NULL != frame && NULL != (fld = frame->GetField(fldName)))
  {
//    ID3_Field* fld = frame->GetField(fldName);
    ID3_TextEnc enc = fld->GetEncoding();
    fld->SetEncoding(ID3TE_ISO8859_1);
    size_t nText = fld->Size();
    text = new char[nText + 1];
    fld->Get(text, nText + 1);
    fld->SetEncoding(enc);
  }
  return text;
}

char *ID3_GetString(const ID3_Frame *frame, ID3_FieldID fldName, size_t nIndex)
{
  char *text = NULL;
  if (NULL != frame)
  {
    size_t nText = frame->GetField(fldName)->Size();
    text = new char[nText + 1];
    frame->GetField(fldName)->Get(text, nText + 1, nIndex);
  }
  return text;
}

void ID3_FreeString(char *str)
{
  if(str != NULL)
    delete [] str;
}

char *ID3_GetArtist(const ID3_Tag *tag)
{
  char *sArtist = NULL;
  if (NULL == tag)
  {
    return sArtist;
  }

  ID3_Frame *frame = NULL;
  if ((frame = tag->Find(ID3FID_LEADARTIST)) ||
      (frame = tag->Find(ID3FID_BAND))       ||
      (frame = tag->Find(ID3FID_CONDUCTOR))  ||
      (frame = tag->Find(ID3FID_COMPOSER)))
  {
    sArtist = ID3_GetString(frame, ID3FN_TEXT);
  }
  return sArtist;
}

ID3_Frame* ID3_AddArtist(ID3_Tag *tag, const char *text, bool replace)
{
  ID3_Frame* frame = NULL;
  if (NULL != tag && NULL != text && strlen(text) > 0)
  {
    if (replace)
    {
      ID3_RemoveArtists(tag);
    }
    if (replace ||
        (tag->Find(ID3FID_LEADARTIST) == NULL &&
         tag->Find(ID3FID_BAND)       == NULL &&
         tag->Find(ID3FID_CONDUCTOR)  == NULL &&
         tag->Find(ID3FID_COMPOSER)   == NULL))
    {
      frame = new ID3_Frame(ID3FID_LEADARTIST);
      if (frame)
      {
        frame->GetField(ID3FN_TEXT)->Set(text);
        tag->AttachFrame(frame);
      }
    }
  }
  return frame;
}

size_t ID3_RemoveArtists(ID3_Tag *tag)
{
  size_t num_removed = 0;
  ID3_Frame *frame = NULL;

  if (NULL == tag)
  {
    return num_removed;
  }

  while ((frame = tag->Find(ID3FID_LEADARTIST)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }
  while ((frame = tag->Find(ID3FID_BAND)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }
  while ((frame = tag->Find(ID3FID_CONDUCTOR)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }
  while ((frame = tag->Find(ID3FID_COMPOSER)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }

  return num_removed;
}

char *ID3_GetAlbum(const ID3_Tag *tag)
{
  char *sAlbum = NULL;
  if (NULL == tag)
  {
    return sAlbum;
  }

  ID3_Frame *frame = tag->Find(ID3FID_ALBUM);
  if (frame != NULL)
  {
    sAlbum = ID3_GetString(frame, ID3FN_TEXT);
  }
  return sAlbum;
}

ID3_Frame* ID3_AddAlbum(ID3_Tag *tag, const char *text, bool replace)
{
  ID3_Frame* frame = NULL;
  if (NULL != tag && NULL != text && strlen(text) > 0)
  {
    if (replace)
    {
      ID3_RemoveAlbums(tag);
    }
    if (replace || tag->Find(ID3FID_ALBUM) == NULL)
    {
      frame = new ID3_Frame(ID3FID_ALBUM);
      if (frame)
      {
        frame->GetField(ID3FN_TEXT)->Set(text);
        tag->AttachFrame(frame);
      }
    }
  }

  return frame;
}

size_t ID3_RemoveAlbums(ID3_Tag *tag)
{
  size_t num_removed = 0;
  ID3_Frame *frame = NULL;

  if (NULL == tag)
  {
    return num_removed;
  }

  while ((frame = tag->Find(ID3FID_ALBUM)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }

  return num_removed;
}

char *ID3_GetTitle(const ID3_Tag *tag)
{
  char *sTitle = NULL;
  if (NULL == tag)
  {
    return sTitle;
  }

  ID3_Frame *frame = tag->Find(ID3FID_TITLE);
  if (frame != NULL)
  {
    sTitle = ID3_GetString(frame, ID3FN_TEXT);
  }
  return sTitle;
}

ID3_Frame* ID3_AddTitle(ID3_Tag *tag, const char *text, bool replace)
{
  ID3_Frame* frame = NULL;
  if (NULL != tag && NULL != text && strlen(text) > 0)
  {
    if (replace)
    {
      ID3_RemoveTitles(tag);
    }
    if (replace || tag->Find(ID3FID_TITLE) == NULL)
    {
      frame = new ID3_Frame(ID3FID_TITLE);
      if (frame)
      {
        frame->GetField(ID3FN_TEXT)->Set(text);
        tag->AttachFrame(frame);
      }
    }
  }

  return frame;
}

size_t ID3_RemoveTitles(ID3_Tag *tag)
{
  size_t num_removed = 0;
  ID3_Frame *frame = NULL;

  if (NULL == tag)
  {
    return num_removed;
  }

  while ((frame = tag->Find(ID3FID_TITLE)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }

  return num_removed;
}

char *ID3_GetYear(const ID3_Tag *tag)
{
  char *sYear = NULL;
  if (NULL == tag)
  {
    return sYear;
  }

  ID3_Frame *frame = tag->Find(ID3FID_YEAR);
  if (frame != NULL)
  {
    sYear = ID3_GetString(frame, ID3FN_TEXT);
  }
  return sYear;
}

ID3_Frame* ID3_AddYear(ID3_Tag *tag, const char *text, bool replace)
{
  ID3_Frame* frame = NULL;
  if (NULL != tag && NULL != text && strlen(text) > 0)
  {
    if (replace)
    {
      ID3_RemoveYears(tag);
    }
    if (replace || tag->Find(ID3FID_YEAR) == NULL)
    {
      frame = new ID3_Frame(ID3FID_YEAR);
      if (NULL != frame)
      {
        frame->GetField(ID3FN_TEXT)->Set(text);
        tag->AttachFrame(frame);
      }
    }
  }

  return frame;
}

size_t ID3_RemoveYears(ID3_Tag *tag)
{
  size_t num_removed = 0;
  ID3_Frame *frame = NULL;

  if (NULL == tag)
  {
    return num_removed;
  }

  while ((frame = tag->Find(ID3FID_YEAR)))
  {
    frame = tag->RemoveFrame(frame);
    delete frame;
    num_removed++;
  }

  return num_removed;
}

char *ID3_GetComment(const ID3_Tag *tag, const char* desc)
{
  char *comment = NULL;
  if (NULL == tag)
  {
    return comment;
  }

  ID3_Frame* frame = NULL;
  if (desc)
  {
    frame = tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, desc);
  }
  else
  {
    frame = tag->Find(ID3FID_COMMENT);
    if(frame == tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, STR_V1_COMMENT_DESC))
      frame = tag->Find(ID3FID_COMMENT);
  }

  if (frame)
    comment = ID3_GetString(frame, ID3FN_TEXT);
  return comment;
}

ID3_Frame* ID3_AddComment(ID3_Tag *tag, const char *text, bool replace)
{
  return ID3_AddComment(tag, text, "", replace);
}

ID3_Frame* ID3_AddComment(ID3_Tag *tag, const char *text,
                          const char *desc, bool replace)
{
  return ID3_AddComment(tag, text, desc, "XXX", replace);
}

ID3_Frame* ID3_AddComment(ID3_Tag *tag, const char *text,
                          const char *desc, const char* lang, bool replace)
{
  ID3_Frame* frame = NULL;
  if (NULL != tag  &&
      NULL != text &&
      NULL != desc &&
      strlen(text) > 0)
  {
    bool bAdd = true;
    if (replace)
    {
      ID3_RemoveComments(tag, desc);
    }
    else
    {
      // See if there is already a comment with this description
      ID3_Tag::Iterator* iter = tag->CreateIterator();
      ID3_Frame* frame = NULL;
      while ((frame = iter->GetNext()) != NULL)
      {
        if (frame->GetID() == ID3FID_COMMENT)
        {
          char *tmp_desc = ID3_GetString(frame, ID3FN_DESCRIPTION);
          if (strcmp(tmp_desc, desc) == 0)
          {
            bAdd = false;
          }
          delete [] tmp_desc;
          if (!bAdd)
          {
            break;
          }
        }
      }
      delete iter;
    }
    if (bAdd)
    {
      frame = new ID3_Frame(ID3FID_COMMENT);
      if (NULL != frame)
      {
        frame->GetField(ID3FN_LANGUAGE)->Set(lang);
        frame->GetField(ID3FN_DESCRIPTION)->Set(desc);
        frame->GetField(ID3FN_TEXT)->Set(text);
        tag->AttachFrame(frame);
      }
    }
  }
  return frame;
}

// Remove all comments with the given description (remove all comments if
// desc is NULL)
size_t ID3_RemoveComments(ID3_Tag *tag, const char *desc)
{
  size_t num_removed = 0;

  if (NULL == tag)
  {
    return num_removed;
  }

  ID3_Tag::Iterator* iter = tag->CreateIterator();
  ID3_Frame* frame = NULL;
  while ((frame = iter->GetNext()) != NULL)
  {
    if (frame->GetID() == ID3FID_COMMENT)
    {
      bool remove = false;
      // A null description means remove all comments
      if (NULL == desc)
      {
        remove = true;
      }
      else
      {
        // See if the description we have matches the description of the
        // current comment.  If so, set the "remove the comment" flag to true.
        char *tmp_desc = ID3_GetString(frame, ID3FN_DESCRIPTION);
        remove = (strcmp(tmp_desc, desc) == 0);
        delete [] tmp_desc;
      }
      if (remove)
      {
        frame = tag->RemoveFrame(frame);
        delete frame;
        num_removed++;
      }
    }
  }
  delete iter;

  return num_removed;
}

char *ID3_GetTrack(const ID3_Tag *tag)
{
  char *sTrack = NULL;
  if (NULL == tag)
  {
    return sTrack;
  }

  ID3_Frame *frame = tag->Find(ID3FID_TRACKNUM);
  if (frame != NULL)
  {
    sTrack = ID3_GetString(frame, ID3FN_TEXT);
  }
  return sTrack;
}

size_t ID3_GetTrackNum(const ID3_Tag *tag)
{
  char *sTrack = ID3_GetTrack(tag);
  size_t nTrack = 0;
  if (NULL != sTrack)
  {
    nTrack = atoi(sTrack);
    delete [] sTrack;
  }
  return nTrack;
}

ID3_Frame* ID3_AddTrack(ID3_Tag *tag, uchar trk, uchar ttl, bool replace)
{
  ID3_Frame* frame = NULL;
  if (NULL != tag && trk > 0)
  {
    if (replace)
    {
      ID3_RemoveTracks(tag);
    }
    if (replace || NULL == tag->Find(ID3FID_TRACKNUM))
    {
      frame = new ID3_Frame(ID3FID_TRACKNUM);
      if (frame)
      {
        char *sTrack = NULL;
        if (0 == ttl)
        {
          sTrack = new char[4];
          sprintf(sTrack, "%lu", (luint) trk);
        }
        else
        {
          sTrack = new char[8];
          sprintf(sTrack, "%lu/%lu", (luint) trk, (luint) ttl);
        }

        frame->GetField(ID3FN_TEXT)->Set(sTrack);
        tag->AttachFrame(frame);

        delete [] sTrack;
      }
    }
  }

  return frame;
}

//following routine courtesy of John George
int ID3_GetPictureData(const ID3_Tag *tag, const char *TempPicPath)
{
  if (NULL == tag)
    return 0;
  else
  {
    ID3_Frame* frame = NULL;
    frame = tag->Find(ID3FID_PICTURE);
    if (frame != NULL)
    {
      ID3_Field* myField = frame->GetField(ID3FN_DATA);
      if (myField != NULL)
      {
        myField->ToFile(TempPicPath);
        return (int)myField->Size();
      }
      else return 0;
    }
    else return 0;
  }
}

//following routine courtesy of John George
char* ID3_GetPictureMimeType(const ID3_Tag *tag)
{
  char* sPicMimetype = NULL;
  if (NULL == tag)
    return sPicMimetype;

  ID3_Frame* frame = NULL;
  frame = tag->Find(ID3FID_PICTURE);
  if (frame != NULL)
  {
    sPicMimetype = ID3_GetString(frame, ID3FN_MIMETYPE);
  }
  return sPicMimetype;
}

//following routine courtesy of John George
bool ID3_HasPicture(const ID3_Tag* tag)
{
  if (NULL == tag)
    return false;
  else
  {
    ID3_Frame* frame = tag->Find(ID3FID_PICTURE);
    if (frame != NULL)
    {
      ID3_Field* myField = frame->GetField(ID3FN_DATA);
      if (myField != NULL)
        return true;
      else
        return false;
    }
    else return false;
  }
}

⌨️ 快捷键说明

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