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
|
/*
* MOC - music on console
* Copyright (C) 2008-2009 Geraud Le Falher and John Fitzgerald
*
* 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.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "common.h"
#include "files.h"
#include "log.h"
#include "options.h"
#include "lists.h"
#include "lyrics.h"
static lists_t_strs *raw_lyrics = NULL;
static const char *lyrics_message = NULL;
/* Return the list of lyrics lines, or NULL if none are loaded. */
lists_t_strs *lyrics_lines_get (void)
{
return raw_lyrics;
}
/* Store new lyrics lines as supplied. */
void lyrics_lines_set (lists_t_strs *lines)
{
assert (!raw_lyrics);
assert (lines);
raw_lyrics = lines;
lyrics_message = NULL;
}
/* Return a list of lyrics lines loaded from a file, or NULL on error. */
lists_t_strs *lyrics_load_file (const char *filename)
{
FILE *lyrics_file = NULL;
const char *mime;
char *line;
lists_t_strs *result;
assert (filename);
lyrics_message = "[No lyrics file!]";
if (!file_exists (filename))
return NULL;
mime = file_mime_type (filename);
if (mime && strncmp (mime, "text/plain", 10))
return NULL;
lyrics_file = fopen (filename, "r");
if (lyrics_file == NULL) {
lyrics_message = "[Lyrics file cannot be read!]";
logit ("Error reading '%s': %s", filename, strerror (errno));
return NULL;
}
result = lists_strs_new (0);
while ((line = read_line (lyrics_file)) != NULL)
lists_strs_push (result, line);
fclose (lyrics_file);
lyrics_message = NULL;
return result;
}
/* Given an audio's file name, load lyrics from the default lyrics file name. */
void lyrics_autoload (const char *filename)
{
char *lyrics_filename, *extn;
assert (!raw_lyrics);
assert (lyrics_message);
if (filename == NULL) {
lyrics_message = "[No file playing!]";
return;
}
if (!options_get_bool ("AutoLoadLyrics")) {
lyrics_message = "[Lyrics not autoloaded!]";
return;
}
if (is_url (filename)) {
lyrics_message = "[Lyrics from URL is not supported!]";
return;
}
lyrics_filename = xstrdup (filename);
extn = ext_pos (lyrics_filename);
if (extn) {
*--extn = '\0';
raw_lyrics = lyrics_load_file (lyrics_filename);
}
else
lyrics_message = "[No lyrics file!]";
free (lyrics_filename);
}
/* Given a line, return a centred copy of it. */
static char *centre_line (const char* line, int max)
{
char *result;
int len;
len = strlen (line);
if (len < (max - 1)) {
int space;
space = (max - len) / 2;
result = (char *) xmalloc (space + len + 2);
memset (result, ' ', space);
strcpy (&result[space], line);
len += space;
}
else {
result = (char *) xmalloc (max + 2);
strncpy (result, line, max);
len = max;
}
strcpy (&result[len], "\n");
return result;
}
/* Centre all the lines in the lyrics. */
static lists_t_strs *centre_style (lists_t_strs *lines, int height ATTR_UNUSED,
int width, void *data ATTR_UNUSED)
{
lists_t_strs *result;
int ix, size;
size = lists_strs_size (lines);
result = lists_strs_new (size);
for (ix = 0; ix < size; ix += 1) {
char *old_line, *new_line;
old_line = lists_strs_at (lines, ix);
new_line = centre_line (old_line, width);
lists_strs_push (result, new_line);
}
return result;
}
/* Formatting function information. */
static lyrics_t_formatter *lyrics_formatter = centre_style;
static lyrics_t_reaper *formatter_reaper = NULL;
static void *formatter_data = NULL;
/* Register a new function to be used for formatting. A NULL formatter
* resets formatting to the default centred style. */
void lyrics_use_formatter (lyrics_t_formatter formatter,
lyrics_t_reaper reaper, void *data)
{
if (formatter_reaper)
formatter_reaper (formatter_data);
if (formatter) {
lyrics_formatter = formatter;
formatter_reaper = reaper;
formatter_data = data;
}
else {
lyrics_formatter = centre_style;
formatter_reaper = NULL;
formatter_data = NULL;
}
}
/* Return a list of either the formatted lyrics if any are loaded or
* a centred message. */
lists_t_strs *lyrics_format (int height, int width)
{
int ix;
lists_t_strs *result;
assert (raw_lyrics || lyrics_message);
result = NULL;
if (raw_lyrics) {
result = lyrics_formatter (raw_lyrics, height, width - 1,
formatter_data);
if (!result)
lyrics_message = "[Error formatting lyrics!]";
}
if (!result) {
char *line;
result = lists_strs_new (1);
line = centre_line (lyrics_message, width - 1);
lists_strs_push (result, line);
}
for (ix = 0; ix < lists_strs_size (result); ix += 1) {
int len;
char *this_line;
this_line = lists_strs_at (result, ix);
len = strlen (this_line);
if (len > width - 1)
strcpy (&this_line[width - 1], "\n");
else if (this_line[len - 1] != '\n') {
char *new_line;
new_line = xmalloc (len + 2);
strcpy (new_line, this_line);
strcat (new_line, "\n");
lists_strs_swap (result, ix, new_line);
free (this_line);
}
}
return result;
}
/* Dispose of raw lyrics lines. */
void lyrics_cleanup (void)
{
if (raw_lyrics) {
lists_strs_free (raw_lyrics);
raw_lyrics = NULL;
}
lyrics_message = "[No lyrics loaded!]";
}
|