Permalink
Browse files

Split libmpdclient: entity

Create 2 new files (entity.c and entity.h). They contain the declaration
of mpd_InfoEntity structre and methods used to manipulate it.
  • Loading branch information...
1 parent ab44866 commit bdd05335b564469c77e1490df3a1dba39f6de5b4 Marc Pavot committed with MaxKellermann Dec 1, 2008
Showing with 272 additions and 193 deletions.
  1. +2 −0 Makefile.am
  2. +0 −34 include/mpd/client.h
  3. +72 −0 include/mpd/entity.h
  4. +197 −0 src/entity.c
  5. +1 −0 src/example.c
  6. +0 −159 src/libmpdclient.c
View
2 Makefile.am
@@ -8,6 +8,7 @@ mpdinclude_HEADERS = \
include/mpd/client.h \
include/mpd/connection.h \
include/mpd/directory.h \
+ include/mpd/entity.h \
include/mpd/idle.h \
include/mpd/status.h \
include/mpd/stats.h \
@@ -26,6 +27,7 @@ src_libmpdclient_la_SOURCES = \
src/directory.c \
src/output.c \
src/return_element.c \
+ src/entity.c \
src/idle.c \
src/song.c \
src/status.c \
View
34 include/mpd/client.h
@@ -91,42 +91,8 @@ void mpd_freePlaylistFile(mpd_PlaylistFile * playlist);
*/
mpd_PlaylistFile * mpd_playlistFileDup(const mpd_PlaylistFile * playlist);
-/* INFO ENTITY STUFF */
-
-/* the type of entity returned from one of the commands that generates info
- * use in conjunction with mpd_InfoEntity.type
- */
-enum mpd_entity_type {
- MPD_INFO_ENTITY_TYPE_DIRECTORY,
- MPD_INFO_ENTITY_TYPE_SONG,
- MPD_INFO_ENTITY_TYPE_PLAYLISTFILE
-};
-
-/* mpd_InfoEntity
- * stores info on stuff returned info commands
- */
-typedef struct mpd_InfoEntity {
- /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine
- * what this entity is (song, directory, etc...)
- */
- enum mpd_entity_type type;
- /* the actual data you want, mpd_song, mpd_directory, etc */
- union {
- struct mpd_directory *directory;
- struct mpd_song *song;
- mpd_PlaylistFile * playlistFile;
- } info;
-} mpd_InfoEntity;
-
-mpd_InfoEntity * mpd_newInfoEntity(void);
-
-void mpd_freeInfoEntity(mpd_InfoEntity * entity);
-
/* INFO COMMANDS AND STUFF */
-/* use this function to loop over after calling Info/Listall functions */
-mpd_InfoEntity * mpd_getNextInfoEntity(struct mpd_connection *connection);
-
/* fetches the currently seeletect song (the song referenced by status->song
* and status->songid*/
void mpd_sendCurrentSongCommand(struct mpd_connection *connection);
View
72 include/mpd/entity.h
@@ -0,0 +1,72 @@
+/* libmpdclient
+ (c) 2003-2008 The Music Player Daemon Project
+ This project's homepage is: http://www.musicpd.org
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ - Neither the name of the Music Player Daemon nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef MPD_ENTITY_H
+#define MPD_ENTITY_H
+
+#include <mpd/song.h>
+#include <mpd/directory.h>
+#include <mpd/client.h>
+
+/* the type of entity returned from one of the commands that generates info
+ * use in conjunction with mpd_InfoEntity.type
+ */
+enum mpd_entity_type {
+ MPD_INFO_ENTITY_TYPE_DIRECTORY,
+ MPD_INFO_ENTITY_TYPE_SONG,
+ MPD_INFO_ENTITY_TYPE_PLAYLISTFILE
+};
+
+/* mpd_InfoEntity
+ * stores info on stuff returned info commands
+ */
+typedef struct mpd_InfoEntity {
+ /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine
+ * what this entity is (song, directory, etc...)
+ */
+ enum mpd_entity_type type;
+ /* the actual data you want, mpd_song, mpd_directory, etc */
+ union {
+ struct mpd_directory *directory;
+ struct mpd_song *song;
+ mpd_PlaylistFile * playlistFile;
+ } info;
+} mpd_InfoEntity;
+
+mpd_InfoEntity * mpd_newInfoEntity(void);
+
+void mpd_freeInfoEntity(mpd_InfoEntity * entity);
+
+/* use this function to loop over after calling Info/Listall functions */
+mpd_InfoEntity * mpd_getNextInfoEntity(struct mpd_connection *connection);
+
+#endif
View
197 src/entity.c
@@ -0,0 +1,197 @@
+/* libmpdclient
+ (c) 2003-2008 The Music Player Daemon Project
+ This project's homepage is: http://www.musicpd.org
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ - Neither the name of the Music Player Daemon nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <mpd/entity.h>
+
+#include "str_pool.h"
+#include <stdlib.h>
+#include <string.h>
+
+static void
+mpd_initInfoEntity(mpd_InfoEntity * entity) {
+ entity->info.directory = NULL;
+}
+
+static void
+mpd_finishInfoEntity(mpd_InfoEntity * entity) {
+ if (entity->info.directory) {
+ if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
+ mpd_freeDirectory(entity->info.directory);
+ else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
+ mpd_freeSong(entity->info.song);
+ else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
+ mpd_freePlaylistFile(entity->info.playlistFile);
+ }
+}
+
+mpd_InfoEntity *
+mpd_newInfoEntity(void) {
+ mpd_InfoEntity * entity = malloc(sizeof(mpd_InfoEntity));
+
+ mpd_initInfoEntity(entity);
+
+ return entity;
+}
+
+void
+mpd_freeInfoEntity(mpd_InfoEntity * entity) {
+ mpd_finishInfoEntity(entity);
+ free(entity);
+}
+
+mpd_InfoEntity *
+mpd_getNextInfoEntity(struct mpd_connection *connection)
+{
+ mpd_InfoEntity * entity = NULL;
+
+ if (connection->doneProcessing || (connection->listOks &&
+ connection->doneListOk)) {
+ return NULL;
+ }
+
+ if (!connection->returnElement) mpd_getNextReturnElement(connection);
+
+ if (connection->returnElement) {
+ if (strcmp(connection->returnElement->name,"file")==0) {
+ entity = mpd_newInfoEntity();
+ entity->type = MPD_INFO_ENTITY_TYPE_SONG;
+ entity->info.song = mpd_newSong();
+ entity->info.song->file =
+ str_pool_dup(connection->returnElement->value);
+ }
+ else if (strcmp(connection->returnElement->name,
+ "directory")==0) {
+ entity = mpd_newInfoEntity();
+ entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
+ entity->info.directory = mpd_newDirectory();
+ entity->info.directory->path =
+ str_pool_dup(connection->returnElement->value);
+ }
+ else if (strcmp(connection->returnElement->name,"playlist")==0) {
+ entity = mpd_newInfoEntity();
+ entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
+ entity->info.playlistFile = mpd_newPlaylistFile();
+ entity->info.playlistFile->path =
+ str_pool_dup(connection->returnElement->value);
+ }
+ else if (strcmp(connection->returnElement->name, "cpos") == 0){
+ entity = mpd_newInfoEntity();
+ entity->type = MPD_INFO_ENTITY_TYPE_SONG;
+ entity->info.song = mpd_newSong();
+ entity->info.song->pos = atoi(connection->returnElement->value);
+ }
+ else {
+ connection->error = 1;
+ strcpy(connection->errorStr,"problem parsing song info");
+ return NULL;
+ }
+ }
+ else return NULL;
+
+ mpd_getNextReturnElement(connection);
+ while (connection->returnElement) {
+ struct mpd_return_element *re = connection->returnElement;
+
+ if (strcmp(re->name,"file")==0) return entity;
+ else if (strcmp(re->name,"directory")==0) return entity;
+ else if (strcmp(re->name,"playlist")==0) return entity;
+ else if (strcmp(re->name,"cpos")==0) return entity;
+
+ if (entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
+ strlen(re->value)) {
+ if (!entity->info.song->artist &&
+ strcmp(re->name,"Artist")==0) {
+ entity->info.song->artist = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->album &&
+ strcmp(re->name,"Album")==0) {
+ entity->info.song->album = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->title &&
+ strcmp(re->name,"Title")==0) {
+ entity->info.song->title = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->track &&
+ strcmp(re->name,"Track")==0) {
+ entity->info.song->track = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->name &&
+ strcmp(re->name,"Name")==0) {
+ entity->info.song->name = str_pool_dup(re->value);
+ }
+ else if (entity->info.song->time==MPD_SONG_NO_TIME &&
+ strcmp(re->name,"Time")==0) {
+ entity->info.song->time = atoi(re->value);
+ }
+ else if (entity->info.song->pos==MPD_SONG_NO_NUM &&
+ strcmp(re->name,"Pos")==0) {
+ entity->info.song->pos = atoi(re->value);
+ }
+ else if (entity->info.song->id==MPD_SONG_NO_ID &&
+ strcmp(re->name,"Id")==0) {
+ entity->info.song->id = atoi(re->value);
+ }
+ else if (!entity->info.song->date &&
+ strcmp(re->name, "Date") == 0) {
+ entity->info.song->date = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->genre &&
+ strcmp(re->name, "Genre") == 0) {
+ entity->info.song->genre = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->composer &&
+ strcmp(re->name, "Composer") == 0) {
+ entity->info.song->composer = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->performer &&
+ strcmp(re->name, "Performer") == 0) {
+ entity->info.song->performer = strdup(re->value);
+ }
+ else if (!entity->info.song->disc &&
+ strcmp(re->name, "Disc") == 0) {
+ entity->info.song->disc = str_pool_dup(re->value);
+ }
+ else if (!entity->info.song->comment &&
+ strcmp(re->name, "Comment") == 0) {
+ entity->info.song->comment = str_pool_dup(re->value);
+ }
+ }
+ else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
+ }
+ else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
+ }
+
+ mpd_getNextReturnElement(connection);
+ }
+
+ return entity;
+}
View
1 src/example.c
@@ -32,6 +32,7 @@
#include <mpd/client.h>
#include <mpd/status.h>
+#include <mpd/entity.h>
#include <stdio.h>
#include <string.h>
View
159 src/libmpdclient.c
@@ -146,171 +146,12 @@ mpd_PlaylistFile * mpd_playlistFileDup(const mpd_PlaylistFile * playlist) {
return ret;
}
-static void mpd_initInfoEntity(mpd_InfoEntity * entity) {
- entity->info.directory = NULL;
-}
-
-static void mpd_finishInfoEntity(mpd_InfoEntity * entity) {
- if (entity->info.directory) {
- if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
- mpd_freeDirectory(entity->info.directory);
- }
- else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
- mpd_freeSong(entity->info.song);
- }
- else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
- mpd_freePlaylistFile(entity->info.playlistFile);
- }
- }
-}
-
-mpd_InfoEntity * mpd_newInfoEntity(void) {
- mpd_InfoEntity * entity = malloc(sizeof(mpd_InfoEntity));
-
- mpd_initInfoEntity(entity);
-
- return entity;
-}
-
-void mpd_freeInfoEntity(mpd_InfoEntity * entity) {
- mpd_finishInfoEntity(entity);
- free(entity);
-}
-
static void
mpd_sendInfoCommand(struct mpd_connection *connection, char *command)
{
mpd_executeCommand(connection,command);
}
-mpd_InfoEntity *
-mpd_getNextInfoEntity(struct mpd_connection *connection)
-{
- mpd_InfoEntity * entity = NULL;
-
- if (connection->doneProcessing || (connection->listOks &&
- connection->doneListOk)) {
- return NULL;
- }
-
- if (!connection->returnElement) mpd_getNextReturnElement(connection);
-
- if (connection->returnElement) {
- if (strcmp(connection->returnElement->name,"file")==0) {
- entity = mpd_newInfoEntity();
- entity->type = MPD_INFO_ENTITY_TYPE_SONG;
- entity->info.song = mpd_newSong();
- entity->info.song->file =
- str_pool_dup(connection->returnElement->value);
- }
- else if (strcmp(connection->returnElement->name,
- "directory")==0) {
- entity = mpd_newInfoEntity();
- entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
- entity->info.directory = mpd_newDirectory();
- entity->info.directory->path =
- str_pool_dup(connection->returnElement->value);
- }
- else if (strcmp(connection->returnElement->name,"playlist")==0) {
- entity = mpd_newInfoEntity();
- entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
- entity->info.playlistFile = mpd_newPlaylistFile();
- entity->info.playlistFile->path =
- str_pool_dup(connection->returnElement->value);
- }
- else if (strcmp(connection->returnElement->name, "cpos") == 0){
- entity = mpd_newInfoEntity();
- entity->type = MPD_INFO_ENTITY_TYPE_SONG;
- entity->info.song = mpd_newSong();
- entity->info.song->pos = atoi(connection->returnElement->value);
- }
- else {
- connection->error = 1;
- strcpy(connection->errorStr,"problem parsing song info");
- return NULL;
- }
- }
- else return NULL;
-
- mpd_getNextReturnElement(connection);
- while (connection->returnElement) {
- struct mpd_return_element *re = connection->returnElement;
-
- if (strcmp(re->name,"file")==0) return entity;
- else if (strcmp(re->name,"directory")==0) return entity;
- else if (strcmp(re->name,"playlist")==0) return entity;
- else if (strcmp(re->name,"cpos")==0) return entity;
-
- if (entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
- strlen(re->value)) {
- if (!entity->info.song->artist &&
- strcmp(re->name,"Artist")==0) {
- entity->info.song->artist = str_pool_dup(re->value);
- }
- else if (!entity->info.song->album &&
- strcmp(re->name,"Album")==0) {
- entity->info.song->album = str_pool_dup(re->value);
- }
- else if (!entity->info.song->title &&
- strcmp(re->name,"Title")==0) {
- entity->info.song->title = str_pool_dup(re->value);
- }
- else if (!entity->info.song->track &&
- strcmp(re->name,"Track")==0) {
- entity->info.song->track = str_pool_dup(re->value);
- }
- else if (!entity->info.song->name &&
- strcmp(re->name,"Name")==0) {
- entity->info.song->name = str_pool_dup(re->value);
- }
- else if (entity->info.song->time==MPD_SONG_NO_TIME &&
- strcmp(re->name,"Time")==0) {
- entity->info.song->time = atoi(re->value);
- }
- else if (entity->info.song->pos==MPD_SONG_NO_NUM &&
- strcmp(re->name,"Pos")==0) {
- entity->info.song->pos = atoi(re->value);
- }
- else if (entity->info.song->id==MPD_SONG_NO_ID &&
- strcmp(re->name,"Id")==0) {
- entity->info.song->id = atoi(re->value);
- }
- else if (!entity->info.song->date &&
- strcmp(re->name, "Date") == 0) {
- entity->info.song->date = str_pool_dup(re->value);
- }
- else if (!entity->info.song->genre &&
- strcmp(re->name, "Genre") == 0) {
- entity->info.song->genre = str_pool_dup(re->value);
- }
- else if (!entity->info.song->composer &&
- strcmp(re->name, "Composer") == 0) {
- entity->info.song->composer = str_pool_dup(re->value);
- }
- else if (!entity->info.song->performer &&
- strcmp(re->name, "Performer") == 0) {
- entity->info.song->performer = strdup(re->value);
- }
- else if (!entity->info.song->disc &&
- strcmp(re->name, "Disc") == 0) {
- entity->info.song->disc = str_pool_dup(re->value);
- }
- else if (!entity->info.song->comment &&
- strcmp(re->name, "Comment") == 0) {
- entity->info.song->comment = str_pool_dup(re->value);
- }
- }
- else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
- }
- else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
- }
-
- mpd_getNextReturnElement(connection);
- }
-
- return entity;
-}
-
static char *
mpd_getNextReturnElementNamed(struct mpd_connection *connection,
const char *name)

0 comments on commit bdd0533

Please sign in to comment.