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.
21 #include "song_save.h"
24 #include "directory.h"
26 #include "text_file.h"
33 #define G_LOG_DOMAIN "song"
35 #define SONG_MTIME "mtime"
36 #define SONG_END "song_end"
41 return g_quark_from_static_string("song_save");
45 song_save(struct song
*song
, void *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");
60 void songvec_save(FILE *fp
, struct songvec
*sv
)
62 songvec_for_each(sv
, song_save
, fp
);
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
);
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
);
82 g_set_error(error_r
, song_save_quark(), 0,
83 "unknown line in db: %s", line
);
88 value
= g_strchug(colon
);
90 if ((type
= tag_name_parse(line
)) != TAG_NUM_OF_ITEM_TYPES
) {
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) {
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
);
107 if (song
->tag
!= NULL
)
108 tag_end_add(song
->tag
);
111 g_set_error(error_r
, song_save_quark(), 0,
112 "unknown line in db: %s", line
);
117 if (song
->tag
!= NULL
)
118 tag_end_add(song
->tag
);