id3_tag.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // The authors have released ID3Lib as Public Domain (PD) and claim no copyright,
  2. // patent or other intellectual property protection in this work. This means that
  3. // it may be modified, redistributed and used in commercial and non-commercial
  4. // software and hardware without restrictions. ID3Lib is distributed on an "AS IS"
  5. // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  6. //
  7. // The ID3Lib authors encourage improvements and optimisations to be sent to the
  8. // ID3Lib coordinator, currently Dirk Mahoney ([email protected]). Approved
  9. // submissions may be altered, and will be included and released under these terms.
  10. //
  11. // Mon Nov 23 18:34:01 1998
  12. #include "id3_tag.h"
  13. luint ID3_Tag::instances = 0;
  14. ID3_Tag &operator << (ID3_Tag& tag, ID3_Frame& frame)
  15. {
  16. tag.AddFrame (&frame);
  17. return tag;
  18. }
  19. ID3_Tag &operator << (ID3_Tag& tag, ID3_Frame *frame)
  20. {
  21. tag.AddFrame (frame);
  22. return tag;
  23. }
  24. ID3_Tag::ID3_Tag()
  25. {
  26. syncBuffer=0;
  27. forcedPadding=0;
  28. SetupTag();
  29. instances++;
  30. }
  31. void ID3_Tag::ForcePading(luint _padding)
  32. {
  33. forcedPadding=_padding;
  34. }
  35. void ID3_Tag::SetupTag()
  36. {
  37. version = ID3_TAGVERSION;
  38. revision = ID3_TAGREVISION;
  39. frameList = NULL;
  40. binaryList = NULL;
  41. findCursor = NULL;
  42. syncOn = false;
  43. compression = false;
  44. padding = true;
  45. extendedHeader = false;
  46. globalUnsync = false;
  47. Clear();
  48. return ;
  49. }
  50. ID3_Tag::~ID3_Tag(void)
  51. {
  52. Clear();
  53. instances--;
  54. if (instances == 0)
  55. {}}
  56. void ID3_Tag::Clear(void)
  57. {
  58. if (frameList)
  59. {
  60. ClearList (frameList);
  61. frameList = NULL;
  62. }
  63. if (binaryList)
  64. {
  65. ClearList (binaryList);
  66. binaryList = NULL;
  67. }
  68. findCursor = NULL;
  69. hasChanged = true;
  70. free(syncBuffer);
  71. syncBuffer=0;
  72. return ;
  73. }
  74. void ID3_Tag::DeleteElem(ID3_Elem *cur)
  75. {
  76. if (cur)
  77. {
  78. if (cur->tagOwns)
  79. {
  80. if (cur->frame)
  81. {
  82. delete cur->frame;
  83. cur->frame = NULL;
  84. }
  85. if (cur->binary)
  86. {
  87. free(cur->binary);
  88. cur->binary = NULL;
  89. cur->binarySize=0;
  90. }
  91. }
  92. findCursor = NULL;
  93. hasChanged = true;
  94. free(cur);
  95. }
  96. return ;
  97. }
  98. void ID3_Tag::ClearList(ID3_Elem *list)
  99. {
  100. ID3_Elem *cur = list;
  101. while (cur)
  102. {
  103. ID3_Elem *next;
  104. next = cur->next;
  105. DeleteElem (cur);
  106. cur = next;
  107. }
  108. return ;
  109. }
  110. void ID3_Tag::AddFrame(ID3_Frame *newFrame, bool freeWhenDone)
  111. {
  112. ID3_Elem *elem;
  113. if (newFrame)
  114. {
  115. if (elem = (ID3_Elem *)calloc(1, sizeof(ID3_Elem)))
  116. {
  117. elem->next = frameList;
  118. elem->frame = newFrame;
  119. elem->binary = NULL;
  120. elem->binarySize = 0;
  121. elem->tagOwns = freeWhenDone;
  122. frameList = elem;
  123. findCursor = NULL;
  124. hasChanged = true;
  125. }
  126. else
  127. ID3_THROW (ID3E_NoMemory);
  128. }
  129. else
  130. ID3_THROW (ID3E_NoData);
  131. return ;
  132. }
  133. void ID3_Tag::RemoveFrame(ID3_Frame *frame)
  134. {
  135. ID3_Elem *elem = NULL;
  136. if (elem = Find (frame))
  137. RemoveFromList (elem, &frameList);
  138. return ;
  139. }
  140. void ID3_Tag::RemoveFromList (ID3_Elem *which, ID3_Elem **list)
  141. {
  142. ID3_Elem *cur = *list;
  143. if (cur == which)
  144. {
  145. *list = which->next;
  146. DeleteElem (which);
  147. }
  148. else
  149. {
  150. while (cur)
  151. {
  152. if (cur->next == which)
  153. {
  154. cur->next = which->next;
  155. DeleteElem (which);
  156. break;
  157. }
  158. else
  159. cur = cur->next;
  160. }
  161. }
  162. return ;
  163. }
  164. bool ID3_Tag::HasChanged (void)
  165. {
  166. bool changed = hasChanged;
  167. if (! changed)
  168. {
  169. ID3_Elem *cur = frameList;
  170. while (cur)
  171. {
  172. if (cur->frame)
  173. changed = cur->frame->HasChanged();
  174. if (changed)
  175. break;
  176. else
  177. cur = cur->next;
  178. }
  179. }
  180. return changed;
  181. }
  182. void ID3_Tag::SetVersion(uchar ver, uchar rev)
  183. {
  184. if (version != ver || revision != rev)
  185. hasChanged = true;
  186. version = ver;
  187. revision = rev;
  188. return ;
  189. }
  190. void ID3_Tag::SetUnsync(bool newSync)
  191. {
  192. if (syncOn != newSync)
  193. hasChanged = true;
  194. syncOn = newSync;
  195. return ;
  196. }
  197. void ID3_Tag::SetExtendedHeader(bool ext)
  198. {
  199. if (extendedHeader != ext)
  200. hasChanged = true;
  201. extendedHeader = ext;
  202. return ;
  203. }
  204. void ID3_Tag::SetCompression (bool comp)
  205. {
  206. if (compression != comp)
  207. hasChanged = true;
  208. compression = comp;
  209. return ;
  210. }
  211. void ID3_Tag::SetPadding (bool pad)
  212. {
  213. if (padding != pad)
  214. hasChanged = true;
  215. padding = pad;
  216. return ;
  217. }
  218. luint ID3_Tag::NumFrames (void)
  219. {
  220. luint numFrames = 0;
  221. ID3_Elem *cur = frameList;
  222. while (cur)
  223. {
  224. numFrames++;
  225. cur = cur->next;
  226. }
  227. return numFrames;
  228. }