Playlist.cpp
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:3k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include "Playlist.h"
  2. Playlist::Playlist()
  3. {
  4. this->current   = -1;
  5. this->itemCount = 0;
  6. this->playlist = NULL;
  7. }
  8. Playlist::~Playlist()
  9. {
  10. this->Reset();
  11. }
  12. void             Playlist::Reset()
  13. {
  14. playlist_node_t *node, *temp;
  15. this->current   = -1;
  16. this->itemCount = 0;
  17. if(this->playlist != NULL) {
  18. node = playlist;
  19. while(node->next != NULL) {
  20. temp = node;
  21. node = node->next;
  22. free(temp);
  23. }
  24. free(node);
  25. this->playlist = NULL;
  26. }
  27. }
  28. void             Playlist::AddItem(char *filename)
  29. {
  30. playlist_node_t *node;
  31. playlist_node_t *iterator;
  32. if(filename) {
  33. node = (playlist_node_t *) new playlist_node_t;
  34. node->item.filename = (char *) new char[strlen(filename)];
  35. strcpy(node->item.filename, filename);
  36. node->next = NULL;
  37. if(this->itemCount == 0) {
  38. this->playlist  = node;
  39. this->itemCount = 1;
  40. this->current   = 0;
  41. }
  42. else {
  43. DWORD i;
  44. iterator = this->playlist;
  45. for(i=0; i<this->itemCount-1; i++) {
  46. if(iterator->next != NULL)
  47. iterator = iterator->next;
  48. }
  49. iterator->next = node;
  50. this->itemCount++;
  51. }
  52. }
  53. }
  54. void             Playlist::RemoveItemAt(DWORD i)
  55. {
  56. DWORD           count;
  57. playlist_node_t *node;
  58. if(this->itemCount == 0)
  59. return;
  60. if(this->itemCount == 1) {
  61. this->Reset();
  62. }
  63. else {
  64. if( i>=1 && i<this->itemCount) {
  65. if(i == 0) {
  66. node = this->playlist;
  67. this->playlist = this->playlist->next;
  68. free(node);
  69. this->itemCount--;
  70. }
  71. else {
  72. node = this->playlist;
  73. for(count = 0; count < i-1; count++) {
  74. if(node->next != NULL)
  75. node = node->next;
  76. }
  77. if(node->next->next == NULL) {
  78. free(node->next);
  79. node->next = NULL;
  80. this->itemCount--;
  81. }
  82. else {
  83. playlist_node_t *temp;
  84. temp = node->next;
  85. node->next = temp->next;
  86. free(temp);
  87. this->itemCount--;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. void             Playlist::NextItem()
  94. {
  95. if(this->current < this->itemCount - 1)
  96. this->current++;
  97. }
  98. void             Playlist::PreviousItem()
  99. {
  100. if(this->current > 0)
  101. this->current--;
  102. }
  103. DWORD            Playlist::GetItemCount()
  104. {
  105. return this->itemCount;
  106. }
  107. DWORD            Playlist::GetCurrentPosition()
  108. {
  109. return this->current;
  110. }
  111. playlist_item_t *Playlist::GetCurrentItem()
  112. {
  113. if(current == -1 || this->itemCount <= 0) {
  114. return NULL;
  115. }
  116. else {
  117. playlist_node_t *node;
  118. DWORD i;
  119. node = playlist;
  120. for(i=0; i < this->current; i++) {
  121. node = node->next;
  122. }
  123. return &node->item;
  124. }
  125. }
  126. playlist_item_t *Playlist::GetItemAt(DWORD pos)
  127. {
  128. if(current == -1 || this->itemCount <= 0) {
  129. return NULL;
  130. }
  131. else {
  132. playlist_node_t *node;
  133. DWORD i;
  134. node = playlist;
  135. for(i=0; i < pos; i++) {
  136. node = node->next;
  137. }
  138. return &node->item;
  139. }
  140. }
  141. void             Playlist::SetCurrentPosition(DWORD pos)
  142. {
  143. if(pos >= 0 && pos < this->itemCount) {
  144. this->current = pos;
  145. }
  146. }