strtok() is recursive by default on win32.
[mpd-mk.git] / src / song_save.c
blob1a3faa02a5e2c6df5266e4386c7d1cd814983457
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "song_save.h"
22 #include "song.h"
23 #include "tag_save.h"
24 #include "directory.h"
25 #include "tag.h"
26 #include "text_file.h"
28 #include <glib.h>
30 #include <stdlib.h>
32 #undef G_LOG_DOMAIN
33 #define G_LOG_DOMAIN "song"
35 #define SONG_MTIME "mtime"
36 #define SONG_END "song_end"
38 static GQuark
39 song_save_quark(void)
41 return g_quark_from_static_string("song_save");
44 static int
45 song_save(struct song *song, void *data)
47 FILE *fp = data;
49 fprintf(fp, SONG_BEGIN "%s\n", song->uri);
51 if (song->tag != NULL)
52 tag_save(fp, song->tag);
54 fprintf(fp, SONG_MTIME ": %li\n", (long)song->mtime);
55 fprintf(fp, SONG_END "\n");
57 return 0;
60 void songvec_save(FILE *fp, struct songvec *sv)
62 songvec_for_each(sv, song_save, fp);
65 struct song *
66 song_load(FILE *fp, struct directory *parent, const char *uri,
67 GString *buffer, GError **error_r)
69 struct song *song = song_file_new(uri, parent);
70 char *line, *colon;
71 enum tag_type type;
72 const char *value;
74 while ((line = read_text_line(fp, buffer)) != NULL &&
75 strcmp(line, SONG_END) != 0) {
76 colon = strchr(line, ':');
77 if (colon == NULL || colon == line) {
78 if (song->tag != NULL)
79 tag_end_add(song->tag);
80 song_free(song);
82 g_set_error(error_r, song_save_quark(), 0,
83 "unknown line in db: %s", line);
84 return false;
87 *colon++ = 0;
88 value = g_strchug(colon);
90 if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) {
91 if (!song->tag) {
92 song->tag = tag_new();
93 tag_begin_add(song->tag);
96 tag_add_item(song->tag, type, value);
97 } else if (strcmp(line, "Time") == 0) {
98 if (!song->tag) {
99 song->tag = tag_new();
100 tag_begin_add(song->tag);
103 song->tag->time = atoi(value);
104 } else if (strcmp(line, SONG_MTIME) == 0) {
105 song->mtime = atoi(value);
106 } else {
107 if (song->tag != NULL)
108 tag_end_add(song->tag);
109 song_free(song);
111 g_set_error(error_r, song_save_quark(), 0,
112 "unknown line in db: %s", line);
113 return false;
117 if (song->tag != NULL)
118 tag_end_add(song->tag);
120 return song;