2 * Copyright 2004 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
43 struct track_db
*track_db_new(const char *filename_base
)
48 db
= xnew(struct track_db
, 1);
49 db
->db
= db_new(filename_base
);
52 d_print("error: %s\n", rc
== -1 ? strerror(errno
) : "-2");
57 int track_db_close(struct track_db
*db
)
61 rc
= db_close(db
->db
);
66 void track_db_insert(struct track_db
*db
, const char *filename
, struct track_info
*ti
)
68 char *key
, *data
, *ptr
;
72 for (i
= 0; ti
->comments
[i
].key
; i
++) {
73 data_size
+= strlen(ti
->comments
[i
].key
) + 1;
74 data_size
+= strlen(ti
->comments
[i
].val
) + 1;
76 data
= xmalloc(data_size
);
77 *(uint32_t *)(data
+ 0) = ti
->mtime
;
78 *(uint32_t *)(data
+ 4) = ti
->duration
;
80 for (i
= 0; ti
->comments
[i
].key
; i
++) {
84 s
= ti
->comments
[i
].key
;
89 s
= ti
->comments
[i
].val
;
94 key
= xstrdup(filename
);
95 rc
= db_insert(db
->db
, key
, strlen(key
) + 1, data
, data_size
);
97 d_print("error: %s\n", strerror(errno
));
102 static struct track_info
*data_to_track_info(const void *data
, unsigned int data_size
)
104 struct track_info
*ti
;
110 ti
= xnew(struct track_info
, 1);
115 ti
->mtime
= *(uint32_t *)str
; str
+= 4;
116 ti
->duration
= *(uint32_t *)str
; str
+= 4;
122 while (pos
< data_size
- 8) {
127 if (str
[data_size
- 9] != 0 || count
% 2) {
133 ti
->comments
= xnew(struct keyval
, count
+ 1);
134 for (i
= 0; i
< count
; i
++) {
139 s
= xmalloc(len
+ 1);
140 memcpy(s
, str
, len
+ 1);
142 ti
->comments
[i
].key
= s
;
145 s
= xmalloc(len
+ 1);
146 memcpy(s
, str
, len
+ 1);
148 ti
->comments
[i
].val
= s
;
150 ti
->comments
[i
].key
= NULL
;
151 ti
->comments
[i
].val
= NULL
;
155 struct track_info
*track_db_get_track(struct track_db
*db
, const char *filename
)
157 struct track_info
*ti
;
158 struct keyval
*comments
;
161 unsigned int data_size
;
163 time_t mtime
= file_get_mtime(filename
);
165 rc
= db_query(db
->db
, filename
, &data
, &data_size
);
168 ti
= data_to_track_info(data
, data_size
);
170 if (mtime
!= -1 && ti
->mtime
== mtime
) {
171 /* mtime not changed, return data */
172 ti
->filename
= xstrdup(filename
);
176 /* db data not up to date, remove data */
177 db_remove(db
->db
, filename
, strlen(filename
) + 1);
178 track_info_unref(ti
);
179 } else if (rc
== 0) {
183 d_print("error: %s\n", rc
== -1 ? strerror(errno
) : "-2");
187 if (player_get_fileinfo(filename
, &duration
, &comments
)) {
188 d_print("INVALID: '%s'\n", filename
);
191 ti
= xnew(struct track_info
, 1);
193 ti
->filename
= xstrdup(filename
);
194 ti
->comments
= comments
;
195 ti
->duration
= duration
;
197 track_db_insert(db
, filename
, ti
);