1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
* Common routines for tag and audio information retrieval.
*
* Copyright (c) 2005 Nyaochi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
* http://www.gnu.org/copyleft/gpl.html .
*
*/
/* $Id: getmediainfo.c,v 1.16 2005/08/25 17:08:59 nyaochi Exp $ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif/*HAVE_CONFIG_H*/
#include <os.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <ucs2char.h>
#include <h10db.h>
#include <getmediainfo.h>
static const ucs2_char_t ucs2cs_unknown_title[] = {'U','n','k','n','o','w','n',' ','T','i','t','l','e',0};
static const ucs2_char_t ucs2cs_unknown_artist[] = {'U','n','k','n','o','w','n',' ','A','r','t','i','s','t',0};
static const ucs2_char_t ucs2cs_unknown_album[] = {'U','n','k','n','o','w','n',' ','A','l','b','u','m',0};
static const ucs2_char_t ucs2cs_unknown_genre[] = {'U','n','k','n','o','w','n',' ','G','e','n','r','e',0};
/* I do want to rewite this function to use gettag_insert_ordinalnumber(), but
* am taking a safety first until we release 1.0.
*/
int getinfo_set_title(
h10db_t* h10db,
int index,
const ucs2_char_t* title,
uint32_t tn,
int tn_flags)
{
int retval = 0;
static const ucs2_char_t ucs2_empty[1] = {0};
if (tn_flags == 0) {
h10db_set_title(h10db, index, title ? title : ucs2_empty);
} else if (tn_flags == -1) {
size_t title_length = title ? ucs2len(title) : 0;
ucs2_char_t *new_title = NULL, *track_number = NULL;
char tracknumber[32];
snprintf(tracknumber, sizeof(tracknumber)-1, "%02d ", tn);
track_number = mbsdupucs2(tracknumber);
if (!track_number) {
return -1;
}
new_title = malloc(sizeof(ucs2_char_t) * (title_length+ucs2len(track_number)+1));
if (new_title) {
ucs2cpy(new_title, track_number);
ucs2cat(new_title, title ? title : ucs2_empty);
h10db_set_title(h10db, index, new_title);
free(new_title);
} else {
h10db_set_title(h10db, index, ucs2_empty);
retval = -1;
}
ucs2free(track_number);
} else if (tn_flags == 1) {
size_t title_length = title ? ucs2len(title) : 0;
ucs2_char_t *new_title = malloc(sizeof(ucs2_char_t) * (title_length+1+1));
if (new_title) {
new_title[0] = (ucs2_char_t)0xD800 + tn;
ucs2cpy(new_title+1, title ? title : ucs2_empty);
h10db_set_title(h10db, index, new_title);
free(new_title);
} else {
h10db_set_title(h10db, index, ucs2_empty);
retval = -1;
}
} else if (tn_flags == 2) {
size_t title_length = title ? ucs2len(title) : 0;
ucs2_char_t *new_title = malloc(sizeof(ucs2_char_t) * (title_length+2+1));
if (new_title) {
new_title[0] = (ucs2_char_t)0xD800 + (tn & 0x07FF);
new_title[1] = (ucs2_char_t)0xD800 + ((tn >> 11) & 0x07FF);
ucs2cpy(new_title+2, title ? title : ucs2_empty);
h10db_set_title(h10db, index, new_title);
free(new_title);
} else {
h10db_set_title(h10db, index, ucs2_empty);
retval = -1;
}
}
return retval;
}
void getmediainfo_init(media_info* info)
{
memset(info, 0, sizeof(*info));
}
void getmediainfo_finish(media_info* info)
{
free(info->pathname);
free(info->filename);
free(info->title);
free(info->artist);
free(info->album);
free(info->genre);
getmediainfo_init(info);
}
int gettag_set_info(h10db_t* h10db, int index, const media_info* info)
{
static const ucs2_char_t ucs2_empty[1] = {0};
if (h10db && info) {
ucs2strip(info->title);
if (info->title && ucs2icmp(info->title, ucs2cs_unknown_title) != 0) {
h10db_set_title(h10db, index, info->title);
} else {
h10db_set_title(h10db, index, info->filename);
}
ucs2strip(info->artist);
if (info->artist && ucs2icmp(info->artist, ucs2cs_unknown_artist) != 0) {
h10db_set_artist(h10db, index, info->artist);
} else {
h10db_set_artist(h10db, index, ucs2_empty);
}
ucs2strip(info->album);
if (info->album && ucs2icmp(info->album, ucs2cs_unknown_album) != 0) {
h10db_set_album(h10db, index, info->album);
} else {
h10db_set_album(h10db, index, ucs2_empty);
}
ucs2strip(info->genre);
if (info->genre && ucs2icmp(info->genre, ucs2cs_unknown_genre) != 0) {
h10db_set_genre(h10db, index, info->genre);
} else {
h10db_set_genre(h10db, index, ucs2_empty);
}
h10db_set_tracknumber(h10db, index, info->tracknumer); /* This track number may be faked. */
h10db_set_year(h10db, index, info->year);
h10db_set_filesize(h10db, index, info->filesize);
h10db_set_duration(h10db, index, info->duration);
h10db_set_samplerate(h10db, index, info->samplerate);
h10db_set_bitrate(h10db, index, info->bitrate);
h10db_set_unknown5(h10db, index, info->tracknumer); /* Set unknown5 to be the track number. */
h10db_set_unknown6(h10db, index, ucs2_empty);
h10db_set_unknown8(h10db, index, ucs2_empty);
}
return 0;
}
static ucs2_char_t* dupucs2(const ucs2_char_t* val)
{
static const ucs2_char_t ucs2_empty[1] = {0};
return ucs2dup(val ? val : ucs2_empty);
}
static ucs2_char_t* dupucs2strip(const ucs2_char_t* val)
{
static const ucs2_char_t ucs2_empty[1] = {0};
if (val) {
while (0xD800 <= *val && *val < 0xDFFF) {
val++;
}
return ucs2dup(val);
} else {
return ucs2dup(ucs2_empty);
}
}
int gettag_get_info(h10db_t* h10db, int index, media_info* info)
{
if (h10db && info) {
info->pathname = dupucs2(h10db_get_filepath(h10db, index));
info->filename = dupucs2(h10db_get_filename(h10db, index));
info->title = dupucs2strip(h10db_get_title(h10db, index)); /* Strip track number. */
info->artist = dupucs2strip(h10db_get_artist(h10db, index));
info->album = dupucs2strip(h10db_get_album(h10db, index));
info->genre = dupucs2strip(h10db_get_genre(h10db, index));
info->tracknumer = h10db_get_tracknumber(h10db, index); /* This will be overwritten by unknown5 below. */
info->year = h10db_get_year(h10db, index);
info->filesize = h10db_get_filesize(h10db, index);
info->duration = h10db_get_duration(h10db, index);
info->samplerate = h10db_get_samplerate(h10db, index);
info->bitrate = h10db_get_bitrate(h10db, index);
info->tracknumer = h10db_get_unknown5(h10db, index);
/*h10db_get_unknown6(h10db, index, ucs2_empty);*/
/*h10db_get_unknown8(h10db, index, ucs2_empty);*/
}
return 0;
}
int gettag_force_order(h10db_t* h10db, int index, uint32_t order, uint32_t max_order)
{
ucs2_char_t* title = NULL;
int flags = 0;
if (max_order < 0) {
flags = -1;
} else if (max_order == 0) {
flags = 0;
} else if (max_order <= 2048) {
flags = 1;
} else {
flags = 2;
}
h10db_set_tracknumber(h10db, index, order);
title = dupucs2(h10db_get_title(h10db, index));
getinfo_set_title(h10db, index, title, order, flags);
free(title);
return 0;
}
ucs2_char_t* gettag_insert_ordinalnumber(const ucs2_char_t* str, uint32_t number, uint32_t max_number)
{
int flags = 0;
static const ucs2_char_t ucs2_empty[1] = {0};
if (max_number < 0) {
flags = -1;
} else if (max_number == 0) {
flags = 0;
} else if (max_number <= 2048) {
flags = 1;
} else {
flags = 2;
}
if (flags == 0) {
return ucs2dup(str ? str : ucs2_empty);
} else if (flags == -1) {
size_t length = str ? ucs2len(str) : 0;
ucs2_char_t *new_str = NULL, *ordinal_number = NULL;
char ordinalnumber[32];
snprintf(ordinalnumber, sizeof(ordinalnumber)-1, "%02d ", number);
ordinal_number = mbsdupucs2(ordinalnumber);
if (!ordinal_number) {
return ucs2dup(ucs2_empty);
}
new_str = ucs2malloc(sizeof(ucs2_char_t) * (length+ucs2len(ordinal_number)+1));
if (new_str) {
ucs2cpy(new_str, ordinal_number);
ucs2cat(new_str, str ? str : ucs2_empty);
ucs2free(ordinal_number);
return new_str;
} else {
ucs2free(ordinal_number);
return ucs2dup(ucs2_empty);
}
} else if (flags == 1) {
size_t length = str ? ucs2len(str) : 0;
ucs2_char_t *new_str = ucs2malloc(sizeof(ucs2_char_t) * (length+1+1));
if (new_str) {
new_str[0] = (ucs2_char_t)0xD800 + number;
ucs2cpy(new_str+1, str ? str : ucs2_empty);
return new_str;
} else {
return ucs2dup(ucs2_empty);
}
} else if (flags == 2) {
size_t length = str ? ucs2len(str) : 0;
ucs2_char_t *new_str = malloc(sizeof(ucs2_char_t) * (length+2+1));
if (new_str) {
new_str[0] = (ucs2_char_t)0xD800 + (number & 0x07FF);
new_str[1] = (ucs2_char_t)0xD800 + ((number >> 11) & 0x07FF);
ucs2cpy(new_str+2, str ? str : ucs2_empty);
return new_str;
} else {
return ucs2dup(ucs2_empty);
}
}
return ucs2dup(ucs2_empty);
}
ucs2_char_t* gettag_setfilename(const ucs2_char_t* file)
{
ucs2_char_t *ucs2 = NULL;
ucs2_char_t* filename = ucs2dup(file);
if (filename) {
ucs2_char_t *p = ucs2rchr(filename, '.');
if (*p) {
*p = 0;
}
ucs2 = ucs2dup(filename);
free(filename);
}
return ucs2;
}
|