src/misc_support.cpp

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

Generated on Mon Aug 20 17:48:46 2007 for id3lib by  doxygen 1.5.2