1 /* libmpdclient
2    (c) 2003-2019 The Music Player Daemon Project
3    This project's homepage is: http://www.musicpd.org
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    - Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 
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.
15 
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.
19 
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.
31 */
32 
33 #include <mpd/stats.h>
34 #include <mpd/pair.h>
35 
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 struct mpd_stats {
41 	unsigned number_of_artists;
42 	unsigned number_of_albums;
43 	unsigned number_of_songs;
44 	unsigned long uptime;
45 	unsigned long db_update_time;
46 	unsigned long play_time;
47 	unsigned long db_play_time;
48 };
49 
50 struct mpd_stats *
mpd_stats_begin(void)51 mpd_stats_begin(void)
52 {
53 	struct mpd_stats *stats = malloc(sizeof(struct mpd_stats));
54 	if (stats == NULL)
55 		return NULL;
56 
57 	stats->number_of_artists = 0;
58 	stats->number_of_albums = 0;
59 	stats->number_of_songs = 0;
60 	stats->uptime = 0;
61 	stats->db_update_time = 0;
62 	stats->play_time = 0;
63 	stats->db_play_time = 0;
64 
65 	return stats;
66 }
67 
68 void
mpd_stats_feed(struct mpd_stats * stats,const struct mpd_pair * pair)69 mpd_stats_feed(struct mpd_stats *stats, const struct mpd_pair *pair)
70 {
71 	if (strcmp(pair->name, "artists") == 0)
72 		stats->number_of_artists = atoi(pair->value);
73 	else if (strcmp(pair->name, "albums") == 0)
74 		stats->number_of_albums = atoi(pair->value);
75 	else if (strcmp(pair->name, "songs") == 0)
76 		stats->number_of_songs = atoi(pair->value);
77 	else if (strcmp(pair->name, "uptime") == 0)
78 		stats->uptime = strtoul(pair->value,NULL,10);
79 	else if (strcmp(pair->name, "db_update") == 0)
80 		stats->db_update_time = strtoul(pair->value,NULL,10);
81 	else if (strcmp(pair->name, "playtime") == 0)
82 		stats->play_time = strtoul(pair->value,NULL,10);
83 	else if (strcmp(pair->name, "db_playtime") == 0)
84 		stats->db_play_time = strtoul(pair->value,NULL,10);
85 }
86 
mpd_stats_free(struct mpd_stats * stats)87 void mpd_stats_free(struct mpd_stats * stats) {
88 	assert(stats != NULL);
89 
90 	free(stats);
91 }
92 
93 unsigned
mpd_stats_get_number_of_artists(const struct mpd_stats * stats)94 mpd_stats_get_number_of_artists(const struct mpd_stats * stats)
95 {
96 	assert(stats != NULL);
97 
98 	return stats->number_of_artists;
99 }
100 
101 unsigned
mpd_stats_get_number_of_albums(const struct mpd_stats * stats)102 mpd_stats_get_number_of_albums(const struct mpd_stats * stats)
103 {
104 	assert(stats != NULL);
105 
106 	return stats->number_of_albums;
107 }
108 
109 unsigned
mpd_stats_get_number_of_songs(const struct mpd_stats * stats)110 mpd_stats_get_number_of_songs(const struct mpd_stats * stats)
111 {
112 	assert(stats != NULL);
113 
114 	return stats->number_of_songs;
115 }
116 
mpd_stats_get_uptime(const struct mpd_stats * stats)117 unsigned long mpd_stats_get_uptime(const struct mpd_stats * stats)
118 {
119 	assert(stats != NULL);
120 
121 	return stats->uptime;
122 }
123 
mpd_stats_get_db_update_time(const struct mpd_stats * stats)124 unsigned long mpd_stats_get_db_update_time(const struct mpd_stats * stats)
125 {
126 	assert(stats != NULL);
127 
128 	return stats->db_update_time;
129 }
130 
mpd_stats_get_play_time(const struct mpd_stats * stats)131 unsigned long mpd_stats_get_play_time(const struct mpd_stats * stats)
132 {
133 	assert(stats != NULL);
134 
135 	return stats->play_time;
136 }
137 
mpd_stats_get_db_play_time(const struct mpd_stats * stats)138 unsigned long mpd_stats_get_db_play_time(const struct mpd_stats * stats)
139 {
140 	assert(stats != NULL);
141 
142 	return stats->db_play_time;
143 }
144