2 (c) 2003-2017 The Music Player Daemon Project
3 This project's homepage is: http://www.musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of the Music Player Daemon nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <mpd/entity.h>
34 #include <mpd/playlist.h>
43 * The type of this entity.
45 enum mpd_entity_type type
;
48 * This union contains type-safe pointers to the real object.
49 * Check the entity type before attempting to obtain the
54 * Only valid if type==#MPD_ENTITY_TYPE_DIRECTORY.
56 struct mpd_directory
*directory
;
59 * Only valid if type==#MPD_ENTITY_TYPE_SONG.
61 struct mpd_song
*song
;
64 * Only valid if type==#MPD_ENTITY_TYPE_PLAYLIST.
66 struct mpd_playlist
*playlistFile
;
71 mpd_entity_free(struct mpd_entity
*entity
) {
72 assert(entity
!= NULL
);
74 switch (entity
->type
) {
75 case MPD_ENTITY_TYPE_UNKNOWN
:
79 case MPD_ENTITY_TYPE_DIRECTORY
:
80 mpd_directory_free(entity
->info
.directory
);
83 case MPD_ENTITY_TYPE_SONG
:
84 mpd_song_free(entity
->info
.song
);
87 case MPD_ENTITY_TYPE_PLAYLIST
:
88 mpd_playlist_free(entity
->info
.playlistFile
);
96 mpd_entity_get_type(const struct mpd_entity
*entity
)
98 assert(entity
!= NULL
);
103 const struct mpd_directory
*
104 mpd_entity_get_directory(const struct mpd_entity
*entity
)
106 assert(entity
!= NULL
);
107 assert(entity
->type
== MPD_ENTITY_TYPE_DIRECTORY
);
109 return entity
->info
.directory
;
112 const struct mpd_song
*
113 mpd_entity_get_song(const struct mpd_entity
*entity
)
115 assert(entity
!= NULL
);
116 assert(entity
->type
== MPD_ENTITY_TYPE_SONG
);
118 return entity
->info
.song
;
121 const struct mpd_playlist
*
122 mpd_entity_get_playlist(const struct mpd_entity
*entity
)
124 assert(entity
!= NULL
);
125 assert(entity
->type
== MPD_ENTITY_TYPE_PLAYLIST
);
127 return entity
->info
.playlistFile
;
131 mpd_entity_feed_first(struct mpd_entity
*entity
, const struct mpd_pair
*pair
)
133 if (strcmp(pair
->name
, "file") == 0) {
134 entity
->type
= MPD_ENTITY_TYPE_SONG
;
135 entity
->info
.song
= mpd_song_begin(pair
);
136 if (entity
->info
.song
== NULL
)
138 } else if (strcmp(pair
->name
, "directory") == 0) {
139 entity
->type
= MPD_ENTITY_TYPE_DIRECTORY
;
140 entity
->info
.directory
= mpd_directory_begin(pair
);
141 if (entity
->info
.directory
== NULL
)
143 } else if (strcmp(pair
->name
, "playlist") == 0) {
144 entity
->type
= MPD_ENTITY_TYPE_PLAYLIST
;
145 entity
->info
.playlistFile
= mpd_playlist_begin(pair
);
146 if (entity
->info
.playlistFile
== NULL
)
149 entity
->type
= MPD_ENTITY_TYPE_UNKNOWN
;
156 mpd_entity_begin(const struct mpd_pair
*pair
)
158 struct mpd_entity
*entity
;
161 entity
= malloc(sizeof(*entity
));
166 success
= mpd_entity_feed_first(entity
, pair
);
176 mpd_entity_feed(struct mpd_entity
*entity
, const struct mpd_pair
*pair
)
178 assert(pair
!= NULL
);
179 assert(pair
->name
!= NULL
);
180 assert(pair
->value
!= NULL
);
182 if (strcmp(pair
->name
, "file") == 0 ||
183 strcmp(pair
->name
, "directory") == 0 ||
184 strcmp(pair
->name
, "playlist") == 0)
187 switch (entity
->type
) {
188 case MPD_ENTITY_TYPE_UNKNOWN
:
191 case MPD_ENTITY_TYPE_DIRECTORY
:
192 mpd_directory_feed(entity
->info
.directory
, pair
);
195 case MPD_ENTITY_TYPE_SONG
:
196 mpd_song_feed(entity
->info
.song
, pair
);
199 case MPD_ENTITY_TYPE_PLAYLIST
:
200 mpd_playlist_feed(entity
->info
.playlistFile
, pair
);
208 mpd_recv_entity(struct mpd_connection
*connection
)
210 struct mpd_pair
*pair
;
211 struct mpd_entity
*entity
;
213 pair
= mpd_recv_pair(connection
);
217 entity
= mpd_entity_begin(pair
);
218 mpd_return_pair(connection
, pair
);
219 if (entity
== NULL
) {
220 mpd_error_entity(&connection
->error
);
224 while ((pair
= mpd_recv_pair(connection
)) != NULL
&&
225 mpd_entity_feed(entity
, pair
))
226 mpd_return_pair(connection
, pair
);
228 if (mpd_error_is_defined(&connection
->error
)) {
229 mpd_entity_free(entity
);
233 /* unread this pair for the next mpd_recv_entity() call */
234 mpd_enqueue_pair(connection
, pair
);