Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

src/misc_support.cpp

Go to the documentation of this file.
00001 // $Id: misc_support.cpp,v 1.33 2001/11/05 09:55:33 shadrack Exp $
00002 
00003 // id3lib: a C++ library for creating and manipulating id3v1/v2 tags
00004 // Copyright 1999, 2000  Scott Thomas Haug
00005 
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Library General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or (at your
00009 // option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful, but WITHOUT
00012 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 // License for more details.
00015 //
00016 // You should have received a copy of the GNU Library General Public License
00017 // along with this library; if not, write to the Free Software Foundation,
00018 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 
00020 // The id3lib authors encourage improvements and optimisations to be sent to
00021 // the id3lib coordinator.  Please see the README file for details on where to
00022 // send such submissions.  See the AUTHORS file for a list of people who have
00023 // contributed to id3lib.  See the ChangeLog file for a list of changes to
00024 // id3lib.  These files are distributed with id3lib at
00025 // http://download.sourceforge.net/id3lib/
00026 
00027 #if defined HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 
00031 
00032 
00033 #include <string.h>
00034 #include <stdlib.h>
00035 #include <ctype.h>
00036 #include <stdio.h>
00037 
00038 #include "misc_support.h"
00039 #include "field.h"
00040 #include "utils.h"
00041 
00042 using namespace dami;
00043 
00044 char *ID3_GetString(const ID3_Frame *frame, ID3_FieldID fldName)
00045 {
00046   char *text = NULL;
00047   if (NULL != frame)
00048   {
00049     ID3_Field* fld = frame->GetField(fldName);
00050     ID3_TextEnc enc = fld->GetEncoding();
00051     fld->SetEncoding(ID3TE_ASCII);
00052     size_t nText = fld->Size();
00053     text = new char[nText + 1];
00054     fld->Get(text, nText + 1);
00055     fld->SetEncoding(enc);
00056   }
00057   return text;
00058 }
00059 
00060 char *ID3_GetString(const ID3_Frame *frame, ID3_FieldID fldName, size_t nIndex)
00061 {
00062   char *text = NULL;
00063   if (NULL != frame)
00064   {
00065     size_t nText = frame->GetField(fldName)->Size();
00066     text = new char[nText + 1];
00067     frame->GetField(fldName)->Get(text, nText + 1, nIndex);
00068   }
00069   return text;
00070 }
00071 
00072 void ID3_FreeString(char *str)
00073 {
00074   if(str != NULL)
00075     delete [] str;
00076 }
00077 
00078 char *ID3_GetArtist(const ID3_Tag *tag)
00079 {
00080   char *sArtist = NULL;
00081   if (NULL == tag)
00082   {
00083     return sArtist;
00084   }
00085 
00086   ID3_Frame *frame = NULL;
00087   if ((frame = tag->Find(ID3FID_LEADARTIST)) ||
00088       (frame = tag->Find(ID3FID_BAND))       ||
00089       (frame = tag->Find(ID3FID_CONDUCTOR))  ||
00090       (frame = tag->Find(ID3FID_COMPOSER)))
00091   {
00092     sArtist = ID3_GetString(frame, ID3FN_TEXT);
00093   }
00094   return sArtist;
00095 }
00096 
00097 ID3_Frame* ID3_AddArtist(ID3_Tag *tag, const char *text, bool replace)
00098 {
00099   ID3_Frame* frame = NULL;
00100   if (NULL != tag && NULL != text && strlen(text) > 0)
00101   {
00102     if (replace)
00103     {
00104       ID3_RemoveArtists(tag);
00105     }
00106     if (replace ||
00107         (tag->Find(ID3FID_LEADARTIST) == NULL &&
00108          tag->Find(ID3FID_BAND)       == NULL &&
00109          tag->Find(ID3FID_CONDUCTOR)  == NULL &&
00110          tag->Find(ID3FID_COMPOSER)   == NULL))
00111     {
00112       frame = new ID3_Frame(ID3FID_LEADARTIST);
00113       if (frame)
00114       {
00115         frame->GetField(ID3FN_TEXT)->Set(text);
00116         tag->AttachFrame(frame);
00117       }
00118     }
00119   }
00120 
00121   return frame;
00122 }
00123 
00124 size_t ID3_RemoveArtists(ID3_Tag *tag)
00125 {
00126   size_t num_removed = 0;
00127   ID3_Frame *frame = NULL;
00128 
00129   if (NULL == tag)
00130   {
00131     return num_removed;
00132   }
00133 
00134   while ((frame = tag->Find(ID3FID_LEADARTIST)))
00135   {
00136     frame = tag->RemoveFrame(frame);
00137     delete frame;
00138     num_removed++;
00139   }
00140   while ((frame = tag->Find(ID3FID_BAND)))
00141   {
00142     frame = tag->RemoveFrame(frame);
00143     delete frame;
00144     num_removed++;
00145   }
00146   while ((frame = tag->Find(ID3FID_CONDUCTOR)))
00147   {
00148     frame = tag->RemoveFrame(frame);
00149     delete frame;
00150     num_removed++;
00151   }
00152   while ((frame = tag->Find(ID3FID_COMPOSER)))
00153   {
00154     frame = tag->RemoveFrame(frame);
00155     delete frame;
00156     num_removed++;
00157   }
00158 
00159   return num_removed;
00160 }
00161 
00162 char *ID3_GetAlbum(const ID3_Tag *tag)
00163 {
00164   char *sAlbum = NULL;
00165   if (NULL == tag)
00166   {
00167     return sAlbum;
00168   }
00169 
00170   ID3_Frame *frame = tag->Find(ID3FID_ALBUM);
00171   if (frame != NULL)
00172   {
00173     sAlbum = ID3_GetString(frame, ID3FN_TEXT);
00174   }
00175   return sAlbum;
00176 }
00177 
00178 ID3_Frame* ID3_AddAlbum(ID3_Tag *tag, const char *text, bool replace)
00179 {
00180   ID3_Frame* frame = NULL;
00181   if (NULL != tag && NULL != text && strlen(text) > 0)
00182   {
00183     if (replace)
00184     {
00185       ID3_RemoveAlbums(tag);
00186     }
00187     if (replace || tag->Find(ID3FID_ALBUM) == NULL)
00188     {
00189       frame = new ID3_Frame(ID3FID_ALBUM);
00190       if (frame)
00191       {
00192         frame->GetField(ID3FN_TEXT)->Set(text);
00193         tag->AttachFrame(frame);
00194       }
00195     }
00196   }
00197   
00198   return frame;
00199 }
00200 
00201 size_t ID3_RemoveAlbums(ID3_Tag *tag)
00202 {
00203   size_t num_removed = 0;
00204   ID3_Frame *frame = NULL;
00205 
00206   if (NULL == tag)
00207   {
00208     return num_removed;
00209   }
00210 
00211   while ((frame = tag->Find(ID3FID_ALBUM)))
00212   {
00213     frame = tag->RemoveFrame(frame);
00214     delete frame;
00215     num_removed++;
00216   }
00217 
00218   return num_removed;
00219 }
00220 
00221 char *ID3_GetTitle(const ID3_Tag *tag)
00222 {
00223   char *sTitle = NULL;
00224   if (NULL == tag)
00225   {
00226     return sTitle;
00227   }
00228 
00229   ID3_Frame *frame = tag->Find(ID3FID_TITLE);
00230   if (frame != NULL)
00231   {
00232     sTitle = ID3_GetString(frame, ID3FN_TEXT);
00233   }
00234   return sTitle;
00235 }
00236 
00237 ID3_Frame* ID3_AddTitle(ID3_Tag *tag, const char *text, bool replace)
00238 {
00239   ID3_Frame* frame = NULL;
00240   if (NULL != tag && NULL != text && strlen(text) > 0)
00241   {
00242     if (replace)
00243     {
00244       ID3_RemoveTitles(tag);
00245     }
00246     if (replace || tag->Find(ID3FID_TITLE) == NULL)
00247     {
00248       frame = new ID3_Frame(ID3FID_TITLE);
00249       if (frame)
00250       {
00251         frame->GetField(ID3FN_TEXT)->Set(text);
00252         tag->AttachFrame(frame);
00253       }
00254     }
00255   }
00256   
00257   return frame;
00258 }
00259 
00260 size_t ID3_RemoveTitles(ID3_Tag *tag)
00261 {
00262   size_t num_removed = 0;
00263   ID3_Frame *frame = NULL;
00264 
00265   if (NULL == tag)
00266   {
00267     return num_removed;
00268   }
00269 
00270   while ((frame = tag->Find(ID3FID_TITLE)))
00271   {
00272     frame = tag->RemoveFrame(frame);
00273     delete frame;
00274     num_removed++;
00275   }
00276 
00277   return num_removed;
00278 }
00279 
00280 char *ID3_GetYear(const ID3_Tag *tag)
00281 {
00282   char *sYear = NULL;
00283   if (NULL == tag)
00284   {
00285     return sYear;
00286   }
00287 
00288   ID3_Frame *frame = tag->Find(ID3FID_YEAR);
00289   if (frame != NULL)
00290   {
00291     sYear = ID3_GetString(frame, ID3FN_TEXT);
00292   }
00293   return sYear;
00294 }
00295 
00296 ID3_Frame* ID3_AddYear(ID3_Tag *tag, const char *text, bool replace)
00297 {
00298   ID3_Frame* frame = NULL;
00299   if (NULL != tag && NULL != text && strlen(text) > 0)
00300   {
00301     if (replace)
00302     {
00303       ID3_RemoveYears(tag);
00304     }
00305     if (replace || tag->Find(ID3FID_YEAR) == NULL)
00306     {
00307       frame = new ID3_Frame(ID3FID_YEAR);
00308       if (NULL != frame)
00309       {
00310         frame->GetField(ID3FN_TEXT)->Set(text);
00311         tag->AttachFrame(frame);
00312       }
00313     }
00314   }
00315   
00316   return frame;
00317 }
00318 
00319 size_t ID3_RemoveYears(ID3_Tag *tag)
00320 {
00321   size_t num_removed = 0;
00322   ID3_Frame *frame = NULL;
00323 
00324   if (NULL == tag)
00325   {
00326     return num_removed;
00327   }
00328 
00329   while ((frame = tag->Find(ID3FID_YEAR)))
00330   {
00331     frame = tag->RemoveFrame(frame);
00332     delete frame;
00333     num_removed++;
00334   }
00335 
00336   return num_removed;
00337 }
00338 
00339 char *ID3_GetComment(const ID3_Tag *tag, const char* desc)
00340 {
00341   char *comment = NULL;
00342   if (NULL == tag)
00343   {
00344     return comment;
00345   }
00346 
00347   ID3_Frame* frame = NULL;
00348   if (desc)
00349   {
00350     frame = tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, desc);
00351   }
00352   else
00353   {
00354     frame = tag->Find(ID3FID_COMMENT);
00355     if(frame == tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, STR_V1_COMMENT_DESC))
00356       {
00357         frame = tag->Find(ID3FID_COMMENT);
00358       }
00359   }
00360 
00361   if (frame)
00362   {
00363     comment = ID3_GetString(frame, ID3FN_TEXT);
00364   }
00365   return comment;
00366 }
00367 
00368 ID3_Frame* ID3_AddComment(ID3_Tag *tag, const char *text, bool replace)
00369 {
00370   return ID3_AddComment(tag, text, "", replace);
00371 }
00372 
00373 ID3_Frame* ID3_AddComment(ID3_Tag *tag, const char *text,
00374                           const char *desc, bool replace)
00375 {
00376   return ID3_AddComment(tag, text, desc, "XXX", replace);
00377 }
00378 
00379 ID3_Frame* ID3_AddComment(ID3_Tag *tag, const char *text,
00380                           const char *desc, const char* lang, bool replace)
00381 {
00382   ID3_Frame* frame = NULL;
00383   if (NULL != tag  &&
00384       NULL != text &&
00385       NULL != desc && 
00386       strlen(text) > 0)
00387   {
00388     bool bAdd = true;
00389     if (replace)
00390     {
00391       ID3_RemoveComments(tag, desc);
00392     }
00393     else
00394     {
00395       // See if there is already a comment with this description
00396       ID3_Tag::Iterator* iter = tag->CreateIterator();
00397       ID3_Frame* frame = NULL;
00398       while ((frame = iter->GetNext()) != NULL)
00399       {
00400         if (frame->GetID() == ID3FID_COMMENT)
00401         {
00402           char *tmp_desc = ID3_GetString(frame, ID3FN_DESCRIPTION);
00403           if (strcmp(tmp_desc, desc) == 0)
00404           {
00405             bAdd = false;
00406           }
00407           delete [] tmp_desc;
00408           if (!bAdd)
00409           {
00410             break;
00411           }
00412         }
00413       }
00414       delete iter;
00415     }
00416     if (bAdd)
00417     {
00418       frame = new ID3_Frame(ID3FID_COMMENT);
00419       if (NULL != frame)
00420       {
00421         frame->GetField(ID3FN_LANGUAGE)->Set(lang);
00422         frame->GetField(ID3FN_DESCRIPTION)->Set(desc);
00423         frame->GetField(ID3FN_TEXT)->Set(text);
00424         tag->AttachFrame(frame);
00425       }
00426     }
00427   }
00428   return frame;
00429 }
00430 
00431 // Remove all comments with the given description (remove all comments if
00432 // desc is NULL)
00433 size_t ID3_RemoveComments(ID3_Tag *tag, const char *desc)
00434 {
00435   size_t num_removed = 0;
00436 
00437   if (NULL == tag)
00438   {
00439     return num_removed;
00440   }
00441 
00442   ID3_Tag::Iterator* iter = tag->CreateIterator();
00443   ID3_Frame* frame = NULL;
00444   while ((frame = iter->GetNext()) != NULL)
00445   {
00446     if (frame->GetID() == ID3FID_COMMENT)
00447     {
00448       bool remove = false;
00449       // A null description means remove all comments
00450       if (NULL == desc)
00451       {
00452         remove = true;
00453       }
00454       else
00455       {
00456         // See if the description we have matches the description of the 
00457         // current comment.  If so, set the "remove the comment" flag to true.
00458         char *tmp_desc = ID3_GetString(frame, ID3FN_DESCRIPTION);
00459         remove = (strcmp(tmp_desc, desc) == 0);
00460         delete [] tmp_desc;
00461       }
00462       if (remove)
00463       {
00464         frame = tag->RemoveFrame(frame);
00465         delete frame;
00466         num_removed++;
00467       }
00468     }
00469   }
00470   delete iter;
00471 
00472   return num_removed;
00473 }
00474 
00475 char *ID3_GetTrack(const ID3_Tag *tag)
00476 {
00477   char *sTrack = NULL;
00478   if (NULL == tag)
00479   {
00480     return sTrack;
00481   }
00482 
00483   ID3_Frame *frame = tag->Find(ID3FID_TRACKNUM);
00484   if (frame != NULL)
00485   {
00486     sTrack = ID3_GetString(frame, ID3FN_TEXT);
00487   }
00488   return sTrack;
00489 }
00490 
00491 size_t ID3_GetTrackNum(const ID3_Tag *tag)
00492 {
00493   char *sTrack = ID3_GetTrack(tag);
00494   size_t nTrack = 0;
00495   if (NULL != sTrack)
00496   {
00497     nTrack = atoi(sTrack);
00498     delete [] sTrack;
00499   }
00500   return nTrack;
00501 }
00502 
00503 ID3_Frame* ID3_AddTrack(ID3_Tag *tag, uchar trk, uchar ttl, bool replace)
00504 {
00505   ID3_Frame* frame = NULL;
00506   if (NULL != tag && trk > 0)
00507   {
00508     if (replace)
00509     {
00510       ID3_RemoveTracks(tag);
00511     }
00512     if (replace || NULL == tag->Find(ID3FID_TRACKNUM))
00513     {
00514       frame = new ID3_Frame(ID3FID_TRACKNUM);
00515       if (frame)
00516       {
00517         char *sTrack = NULL;
00518         if (0 == ttl)
00519         {
00520           sTrack = new char[4];
00521           sprintf(sTrack, "%lu", (luint) trk);
00522         }
00523         else
00524         {
00525           sTrack = new char[8];
00526           sprintf(sTrack, "%lu/%lu", (luint) trk, (luint) ttl);
00527         }
00528         
00529         frame->GetField(ID3FN_TEXT)->Set(sTrack);
00530         tag->AttachFrame(frame);
00531 
00532         delete [] sTrack;
00533       }
00534     }
00535   }
00536   
00537   return frame;
00538 }
00539 
00540 size_t ID3_RemoveTracks(ID3_Tag *tag)
00541 {
00542   size_t num_removed = 0;
00543   ID3_Frame *frame = NULL;
00544 
00545   if (NULL == tag)
00546   {
00547     return num_removed;
00548   }
00549 
00550   while ((frame = tag->Find(ID3FID_TRACKNUM)))
00551   {
00552     frame = tag->RemoveFrame(frame);
00553     delete frame;
00554     num_removed++;
00555   }
00556 
00557   return num_removed;
00558 }
00559 
00560 char *ID3_GetGenre(const ID3_Tag *tag)
00561 {
00562   char *sGenre = NULL;
00563   if (NULL == tag)
00564   {
00565     return sGenre;
00566   }
00567 
00568   ID3_Frame *frame = tag->Find(ID3FID_CONTENTTYPE);
00569   if (frame != NULL)
00570   {
00571     sGenre = ID3_GetString(frame, ID3FN_TEXT);
00572   }
00573 
00574   return sGenre;
00575 }
00576 
00577 size_t ID3_GetGenreNum(const ID3_Tag *tag)
00578 {
00579   char *sGenre = ID3_GetGenre(tag);
00580   size_t ulGenre = 0xFF;
00581   if (NULL == sGenre)
00582   {
00583     return ulGenre;
00584   }
00585 
00586   // If the genre string begins with "(ddd)", where "ddd" is a number, then 
00587   // "ddd" is the genre number---get it
00588   if (sGenre[0] == '(')
00589   {
00590     char *pCur = &sGenre[1];
00591     while (isdigit(*pCur))
00592     {
00593       pCur++;
00594     }
00595     if (*pCur == ')')
00596     {
00597       // if the genre number is greater than 255, its invalid.
00598       ulGenre = dami::min(0xFF, atoi(&sGenre[1]));
00599     }
00600   }
00601 
00602   delete [] sGenre;
00603   return ulGenre;
00604 }
00605 
00606 ID3_Frame* ID3_AddGenre(ID3_Tag *tag, size_t genre, bool replace)
00607 {
00608   ID3_Frame* frame = NULL;
00609   if (NULL != tag && 0xFF != genre)
00610   {
00611     if (replace)
00612     {
00613       ID3_RemoveGenres(tag);
00614     }
00615     if (replace || NULL == tag->Find(ID3FID_CONTENTTYPE))
00616     {
00617       frame = new ID3_Frame(ID3FID_CONTENTTYPE);
00618       if (NULL != frame)
00619       {
00620         char sGenre[6];
00621         sprintf(sGenre, "(%lu)", (luint) genre);
00622 
00623         frame->GetField(ID3FN_TEXT)->Set(sGenre);
00624         tag->AttachFrame(frame);
00625       }
00626     }
00627   }
00628   
00629   return frame;
00630 }
00631 
00632 size_t ID3_RemoveGenres(ID3_Tag *tag)
00633 {
00634   size_t num_removed = 0;
00635   ID3_Frame *frame = NULL;
00636 
00637   if (NULL == tag)
00638   {
00639     return num_removed;
00640   }
00641 
00642   while ((frame = tag->Find(ID3FID_CONTENTTYPE)))
00643   {
00644     frame = tag->RemoveFrame(frame);
00645     delete frame;
00646     num_removed++;
00647   }
00648 
00649   return num_removed;
00650 }
00651 
00652 char *ID3_GetLyrics(const ID3_Tag *tag)
00653 {
00654   char *sLyrics = NULL;
00655   if (NULL == tag)
00656   {
00657     return sLyrics;
00658   }
00659 
00660   ID3_Frame *frame = tag->Find(ID3FID_UNSYNCEDLYRICS);
00661   if (frame != NULL)
00662   {
00663     sLyrics = ID3_GetString(frame, ID3FN_TEXT);
00664   }
00665   return sLyrics;
00666 }
00667 
00668 ID3_Frame* ID3_AddLyrics(ID3_Tag *tag, const char *text, bool replace)
00669 {
00670   return ID3_AddLyrics(tag, text, "", replace);
00671 }
00672 
00673 ID3_Frame* ID3_AddLyrics(ID3_Tag *tag, const char *text, const char* desc, 
00674                          bool replace)
00675 {
00676   return ID3_AddLyrics(tag, text, desc, "XXX", replace);
00677 }
00678 
00679 ID3_Frame* ID3_AddLyrics(ID3_Tag *tag, const char *text, const char* desc,
00680                          const char* lang, bool replace)
00681 {
00682   ID3_Frame* frame = NULL;
00683   if (NULL != tag && strlen(text) > 0)
00684   {
00685     if (replace)
00686     {
00687       ID3_RemoveLyrics(tag);
00688     }
00689     if (replace || tag->Find(ID3FID_UNSYNCEDLYRICS) == NULL)
00690     {
00691       frame = new ID3_Frame(ID3FID_UNSYNCEDLYRICS);
00692       if (NULL != frame)
00693       {
00694         frame->GetField(ID3FN_LANGUAGE)->Set(lang);
00695         frame->GetField(ID3FN_DESCRIPTION)->Set(desc);
00696         frame->GetField(ID3FN_TEXT)->Set(text);
00697         tag->AttachFrame(frame);
00698       }
00699     }
00700   }
00701   
00702   return frame;
00703 }
00704 
00705 size_t ID3_RemoveLyrics(ID3_Tag *tag)
00706 {
00707   size_t num_removed = 0;
00708   ID3_Frame *frame = NULL;
00709 
00710   if (NULL == tag)
00711   {
00712     return num_removed;
00713   }
00714 
00715   while ((frame = tag->Find(ID3FID_UNSYNCEDLYRICS)))
00716   {
00717     frame = tag->RemoveFrame(frame);
00718     delete frame;
00719     num_removed++;
00720   }
00721 
00722   return num_removed;
00723 }
00724 
00725 char *ID3_GetLyricist(const ID3_Tag *tag)
00726 {
00727   char *sLyricist = NULL;
00728   if (NULL == tag)
00729   {
00730     return sLyricist;
00731   }
00732 
00733   ID3_Frame *frame = tag->Find(ID3FID_LYRICIST);
00734   if (frame != NULL)
00735   {
00736     sLyricist = ID3_GetString(frame, ID3FN_TEXT);
00737   }
00738   return sLyricist;
00739 }
00740 
00741 ID3_Frame* ID3_AddLyricist(ID3_Tag *tag, const char *text, bool replace)
00742 {
00743   ID3_Frame* frame = NULL;
00744   if (NULL != tag && NULL != text && strlen(text) > 0)
00745   {
00746     if (replace)
00747     {
00748       ID3_RemoveLyricist(tag);
00749     }
00750     if (replace || (tag->Find(ID3FID_LYRICIST) == NULL))
00751     {    
00752       frame = new ID3_Frame(ID3FID_LYRICIST);
00753       if (frame)
00754       {
00755         frame->GetField(ID3FN_TEXT)->Set(text);
00756         tag->AttachFrame(frame);
00757       }
00758     }
00759   }
00760 
00761   return frame;
00762 }
00763 
00764 size_t ID3_RemoveLyricist(ID3_Tag *tag)
00765 {
00766   size_t num_removed = 0;
00767   ID3_Frame *frame = NULL;
00768 
00769   if (NULL == tag)
00770   {
00771     return num_removed;
00772   }
00773 
00774   while ((frame = tag->Find(ID3FID_LYRICIST)))
00775   {
00776     frame = tag->RemoveFrame(frame);
00777     delete frame;
00778     num_removed++;
00779   }
00780 
00781   return num_removed;
00782 }
00783 
00784 ID3_Frame* ID3_AddSyncLyrics(ID3_Tag *tag, const uchar *data, size_t datasize,
00785                              ID3_TimeStampFormat format, bool replace)
00786 {
00787   return ID3_AddSyncLyrics(tag, data, datasize, format, "", replace);
00788 }
00789 
00790 ID3_Frame* ID3_AddSyncLyrics(ID3_Tag *tag, const uchar *data, size_t datasize,
00791                              ID3_TimeStampFormat format, const char *desc, 
00792                              bool replace)
00793 {
00794   return ID3_AddSyncLyrics(tag, data, datasize, format, desc, "XXX", replace);
00795 }
00796 
00797 ID3_Frame* ID3_AddSyncLyrics(ID3_Tag *tag, const uchar *data, size_t datasize,
00798                              ID3_TimeStampFormat format, const char *desc, 
00799                              const char *lang, bool replace)
00800 {
00801   return ID3_AddSyncLyrics(tag, data, datasize, format, desc, lang, 
00802                            ID3CT_LYRICS, replace);
00803 }
00804 
00805 ID3_Frame* ID3_AddSyncLyrics(ID3_Tag *tag, const uchar *data, size_t datasize,
00806                              ID3_TimeStampFormat format, const char *desc, 
00807                              const char *lang, ID3_ContentType type, 
00808                              bool replace)
00809 {
00810   ID3_Frame* frame = NULL;
00811   // language and descriptor should be mandatory
00812   if ((NULL == lang) || (NULL == desc))
00813   {
00814     return NULL;
00815   }
00816 
00817   // check if a SYLT frame of this language or descriptor already exists
00818   ID3_Frame* frmExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_LANGUAGE, lang);
00819   if (!frmExist)
00820   {
00821     frmExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_DESCRIPTION, desc);
00822   }
00823 
00824   if (NULL != tag && NULL != data)
00825   {
00826     if (replace && frmExist)
00827     {
00828       frmExist = tag->RemoveFrame (frmExist);
00829       delete frmExist;
00830       frmExist = NULL;
00831     }
00832 
00833     // if the frame still exist, cannot continue
00834     if (frmExist)
00835     {
00836       return NULL;
00837     }
00838 
00839     ID3_Frame* frame = new ID3_Frame(ID3FID_SYNCEDLYRICS);
00840 
00841     frame->GetField(ID3FN_LANGUAGE)->Set(lang);
00842     frame->GetField(ID3FN_DESCRIPTION)->Set(desc);
00843     frame->GetField(ID3FN_TIMESTAMPFORMAT)->Set(format);
00844     frame->GetField(ID3FN_CONTENTTYPE)->Set(type);
00845     frame->GetField(ID3FN_DATA)->Set(data, datasize);
00846     tag->AttachFrame(frame);
00847   }
00848 
00849   return frame;
00850 }
00851 
00852 ID3_Frame *ID3_GetSyncLyricsInfo(const ID3_Tag *tag, const char *desc, 
00853                                  const char *lang,
00854                                  ID3_TimeStampFormat& format, 
00855                                  ID3_ContentType& type, size_t& size)
00856 {
00857   // check if a SYLT frame of this language or descriptor exists
00858   ID3_Frame* frmExist = NULL;
00859   if (NULL != lang)
00860   {
00861     // search through language
00862     frmExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_LANGUAGE, lang);
00863   }
00864   else if (NULL != desc)
00865   {
00866     // search through descriptor
00867     frmExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_DESCRIPTION, desc);
00868   }
00869   else
00870   {
00871     // both language and description not specified, search the first SYLT frame
00872     frmExist = tag->Find(ID3FID_SYNCEDLYRICS);
00873   }
00874   
00875   if (!frmExist)
00876   {
00877     return NULL;
00878   }
00879   
00880   // get the lyrics time stamp format
00881   format = static_cast<ID3_TimeStampFormat>(frmExist->GetField(ID3FN_TIMESTAMPFORMAT)->Get ());
00882   
00883   // get the lyrics content type
00884   type = static_cast<ID3_ContentType>(frmExist->GetField(ID3FN_CONTENTTYPE)->Get ());
00885   
00886   // get the lyrics size
00887   size = frmExist->GetField (ID3FN_DATA)->Size ();
00888   
00889   // return the frame pointer for further uses
00890   return frmExist;
00891 }
00892 
00893 ID3_Frame *ID3_GetSyncLyrics(const ID3_Tag *tag, const char *lang, 
00894                              const char *desc, const uchar *pData, size_t& size)
00895 {
00896   // check if a SYLT frame of this language or descriptor exists
00897   ID3_Frame* frmExist = NULL;
00898   if (NULL != lang)
00899   {
00900     // search through language
00901     frmExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_LANGUAGE, lang);
00902   }
00903   else if (NULL != desc)
00904   {
00905     // search through descriptor
00906     frmExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_DESCRIPTION, desc);
00907   }
00908   else
00909   {
00910     // both language and description not specified, search the first SYLT frame
00911     frmExist = tag->Find(ID3FID_SYNCEDLYRICS);
00912   }
00913 
00914   if (NULL == frmExist)
00915   {
00916     return NULL;
00917   }
00918   
00919   // get the lyrics size
00920   size = dami::min(size, frmExist->GetField(ID3FN_DATA)->Size());
00921 
00922   // get the lyrics data
00923   pData = frmExist->GetField (ID3FN_DATA)->GetRawBinary();
00924 
00925   // return the frame pointer for further uses
00926   return frmExist;
00927 }
00928 

Generated on Thu Jan 3 07:35:55 2002 for id3lib by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001