summaryrefslogtreecommitdiffstats
path: root/kmid/songlist.cpp
blob: 2d12b952f4ba47a5a7d05360fc6561f10ec6d4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**************************************************************************

    songlist.cc  - class SongList, which holds a list of songs (collection)
    Copyright (C) 1997,98  Antonio Larrosa Jimenez

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1307, USA.

    Send comments and bug fixes to larrosa@kde.org
    or to Antonio Larrosa, Rio Arnoya, 10 5B, 29006 Malaga, Spain

***************************************************************************/
#include "songlist.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

SongList::SongList(void)
{
    list=NULL;
    last=NULL;
    active=NULL;
    ntotal=0;
}

SongList::SongList(SongList &src)
{
    list=last=active=NULL;
    ntotal=0;
    src.iteratorStart();
    while (!src.iteratorAtEnd())
    {
        AddSong(src.getIteratorName());
        src.iteratorNext();
    }
    if (src.active!=NULL) active=getSongid(src.active->id);
}

SongList::~SongList()
{
    clean();
    Song *ptr=list;
    active=last=NULL;
    ntotal=0;
    
    while (ptr!=NULL)
    {
        list=ptr->next;
        delete ptr->name;
        delete ptr;
        ptr=list;
    }
    
}

SongList::Song *SongList::getSongid(int id)
{
    Song *ptr=list;
    while ((ptr!=NULL)&&(ptr->id!=id))
        ptr=ptr->next;
    
    return ptr;
}

int SongList::AddSong(const char *song)
{
    if (!song) return 0;

    if (last==NULL)
    {
        last=new Song;
        list=last;
    }
    else
    {
        last->next=new Song;
        last=last->next;
    }
    last->name=new char[strlen(song)+1];
    strcpy(last->name,song);
    last->id= ++ntotal;
    last->next=NULL;
    if (active==NULL) active=last;
    return last->id;
}

void SongList::DelSong(int id)
{
    Song *ptr;
    
    if (list==NULL) return;
    if (id==1)
    {
        if (last->id==1)
        {
            list=last=active=NULL;
            ntotal=0;
        }
        else
        {
            ptr=list;
            if (active->id==1) active=list->next;
            list=list->next;
            delete ptr->name;
            delete ptr;
            ntotal--;
            
            regenerateid(list,1);
        }
        return;
    }
    Song *ptr_prev=getSongid(id-1);
    ptr=ptr_prev->next;
    if (last->id==id) last=ptr_prev;
    if (active->id==id) 
        if (active->next!=NULL) active=active->next;
        else active=ptr_prev;
    
    ntotal--;
    ptr_prev->next=ptr->next;
    delete ptr->name;
    delete ptr;
    regenerateid(ptr_prev->next,id);
    
}

void SongList::regenerateid(Song *song,int id)
{
    Song *tmp=song;
    int i=id;
    while (tmp!=NULL)
    {
        tmp->id=i++;
        tmp=tmp->next;
    }
    ntotal=i-1;
}

void SongList::setActiveSong(int id)
{
    Song *tmp=getSongid(id);
    if (tmp!=NULL) active=tmp;
}

char *SongList::getName(int id)
{
    Song *tmp=getSongid(id);
    if (tmp!=NULL) return tmp->name;
    return NULL;
}

/*
void SongList::saveList(FILE *fh)
{
    Song *ptr=list;
    while (ptr!=NULL)
    {
        fputs(fh,ptr->name);
        ptr=ptr->next;
    }
}
*/

void SongList::iteratorStart(void)
{
    it=list;
}

void SongList::iteratorNext(void)
{
    if (it!=NULL) it=it->next;
}

int SongList::getIteratorID(void)
{
    if (it==NULL) return -1;
    return it->id;
}

char *SongList::getIteratorName(void)
{
    if (it==NULL) return NULL;
    return it->name;
}


void SongList::clean(void)
{
    Song *tmp=list;
    active=last=NULL;
    ntotal=0;
    
    while (tmp!=NULL)
    {
        list=tmp->next;
        delete [] tmp->name;
        delete tmp;
        tmp=list;
    }
}

void SongList::copy(SongList &src)
{
    clean();
    src.iteratorStart();
    while (!src.iteratorAtEnd())
    {
        AddSong(src.getIteratorName());
        src.iteratorNext();
    }
    if (src.active!=NULL) active=getSongid(src.active->id);
}

int SongList::next(void)
{
    if (list==NULL) {active=NULL;return 0;};
    if (active!=NULL) active=active->next;
    if (active==NULL) 
    {
        Song *tmp=list;
        while (tmp->next!=NULL) tmp=tmp->next;
        active=tmp;
        return 0;
    }
    return 1;
}


void SongList::previous(void)
{
    if (list==NULL) {active=NULL;return;};
    Song *tmp=list;
    while ((tmp->next!=NULL)&&(tmp->next->id!=active->id)) tmp=tmp->next;
    if (tmp->next==NULL) {active=list;return;};
    active=tmp;
}