output: add _get_plugin()
[libmpdclient.git] / src / entity.c
blob7eb3ab33a07949cf933914d37165d226970e5844
1 /* libmpdclient
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
7 are met:
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>
35 #include "internal.h"
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
41 struct mpd_entity {
42 /**
43 * The type of this entity.
45 enum mpd_entity_type type;
47 /**
48 * This union contains type-safe pointers to the real object.
49 * Check the entity type before attempting to obtain the
50 * object!
52 union {
53 /**
54 * Only valid if type==#MPD_ENTITY_TYPE_DIRECTORY.
56 struct mpd_directory *directory;
58 /**
59 * Only valid if type==#MPD_ENTITY_TYPE_SONG.
61 struct mpd_song *song;
63 /**
64 * Only valid if type==#MPD_ENTITY_TYPE_PLAYLIST.
66 struct mpd_playlist *playlistFile;
67 } info;
70 void
71 mpd_entity_free(struct mpd_entity *entity) {
72 assert(entity != NULL);
74 switch (entity->type) {
75 case MPD_ENTITY_TYPE_UNKNOWN:
76 /* nothing to free */
77 break;
79 case MPD_ENTITY_TYPE_DIRECTORY:
80 mpd_directory_free(entity->info.directory);
81 break;
83 case MPD_ENTITY_TYPE_SONG:
84 mpd_song_free(entity->info.song);
85 break;
87 case MPD_ENTITY_TYPE_PLAYLIST:
88 mpd_playlist_free(entity->info.playlistFile);
89 break;
92 free(entity);
95 enum mpd_entity_type
96 mpd_entity_get_type(const struct mpd_entity *entity)
98 assert(entity != NULL);
100 return entity->type;
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;
130 static bool
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)
137 return false;
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)
142 return false;
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)
147 return false;
148 } else {
149 entity->type = MPD_ENTITY_TYPE_UNKNOWN;
152 return true;
155 struct mpd_entity *
156 mpd_entity_begin(const struct mpd_pair *pair)
158 struct mpd_entity *entity;
159 bool success;
161 entity = malloc(sizeof(*entity));
162 if (entity == NULL)
163 /* out of memory */
164 return NULL;
166 success = mpd_entity_feed_first(entity, pair);
167 if (!success) {
168 free(entity);
169 return NULL;
172 return entity;
175 bool
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)
185 return false;
187 switch (entity->type) {
188 case MPD_ENTITY_TYPE_UNKNOWN:
189 break;
191 case MPD_ENTITY_TYPE_DIRECTORY:
192 mpd_directory_feed(entity->info.directory, pair);
193 break;
195 case MPD_ENTITY_TYPE_SONG:
196 mpd_song_feed(entity->info.song, pair);
197 break;
199 case MPD_ENTITY_TYPE_PLAYLIST:
200 mpd_playlist_feed(entity->info.playlistFile, pair);
201 break;
204 return true;
207 struct mpd_entity *
208 mpd_recv_entity(struct mpd_connection *connection)
210 struct mpd_pair *pair;
211 struct mpd_entity *entity;
213 pair = mpd_recv_pair(connection);
214 if (pair == NULL)
215 return NULL;
217 entity = mpd_entity_begin(pair);
218 mpd_return_pair(connection, pair);
219 if (entity == NULL) {
220 mpd_error_entity(&connection->error);
221 return NULL;
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);
230 return NULL;
233 /* unread this pair for the next mpd_recv_entity() call */
234 mpd_enqueue_pair(connection, pair);
236 return entity;