output: add _get_plugin()
[libmpdclient.git] / src / directory.c
bloba3578b8f67f3d164beb9010e6aa08d3c7ad4b5e6
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/directory.h>
34 #include <mpd/pair.h>
35 #include "uri.h"
36 #include "iso8601.h"
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
43 struct mpd_directory {
44 /**
45 * The full path of this directory. It does not begin with a
46 * slash.
48 char *path;
50 /**
51 * The POSIX UTC time stamp of the last modification, or 0 if
52 * that is unknown.
54 time_t last_modified;
57 mpd_malloc
58 static struct mpd_directory *
59 mpd_directory_new(const char *path)
61 struct mpd_directory *directory;
63 assert(path != NULL);
64 assert(mpd_verify_local_uri(path));
66 directory = malloc(sizeof(*directory));
67 if (directory == NULL)
68 /* out of memory */
69 return NULL;
71 directory->path = strdup(path);
72 if (directory->path == NULL) {
73 /* out of memory */
74 free(directory);
75 return NULL;
78 directory->last_modified = 0;
80 return directory;
83 void mpd_directory_free(struct mpd_directory *directory)
85 assert(directory != NULL);
86 assert(directory->path != NULL);
88 free(directory->path);
89 free(directory);
92 struct mpd_directory *
93 mpd_directory_dup(const struct mpd_directory *directory)
95 assert(directory != NULL);
96 assert(directory->path != NULL);
98 struct mpd_directory *copy = mpd_directory_new(directory->path);
99 copy->last_modified = directory->last_modified;
100 return copy;
103 const char *
104 mpd_directory_get_path(const struct mpd_directory *directory)
106 assert(directory != NULL);
107 assert(directory->path != NULL);
109 return directory->path;
112 time_t
113 mpd_directory_get_last_modified(const struct mpd_directory *directory)
115 assert(directory != NULL);
117 return directory->last_modified;
120 struct mpd_directory *
121 mpd_directory_begin(const struct mpd_pair *pair)
123 assert(pair != NULL);
124 assert(pair->name != NULL);
125 assert(pair->value != NULL);
127 if (strcmp(pair->name, "directory") != 0 ||
128 !mpd_verify_local_uri(pair->value)) {
129 errno = EINVAL;
130 return NULL;
133 return mpd_directory_new(pair->value);
136 bool
137 mpd_directory_feed(struct mpd_directory *directory,
138 const struct mpd_pair *pair)
140 assert(pair != NULL);
141 assert(pair->name != NULL);
142 assert(pair->value != NULL);
144 if (strcmp(pair->name, "directory") == 0)
145 return false;
147 if (strcmp(pair->name, "Last-Modified") == 0)
148 directory->last_modified =
149 iso8601_datetime_parse(pair->value);
151 return true;