-
Quick Newbie Help
Sorry to bother you with a newbie question (I have no idea of C programming...): I'm running Litestream, an alternative lightweight Shoutcast streaming utility on my server, but I would like to modify it, so that it doesn't only write the file name that is currently played to regular server logs, but also to a single text file, so I can proccess it with PHP and display a "Now Playing" window on my website.
Here's the file that needs to be modified (playlist.c)
Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "stream_std.h"
#include "playlist.h"
#include "textutils.h"
extern int last_played;
FILE* open_next_file(char* _files[], int _nfiles, int* _curfile) {
int startfile = *_curfile;
FILE* fp;
char message[1024];
if (last_played)
{
stream_msg("finished playlist");
return(NULL);
}
do {
fp = fopen(_files[*_curfile], "r");
if (!fp) {
sprintf(message, "could not open %s", _files[*_curfile]);
stream_err(message);
}
else {
sprintf(message, "opened %s", _files[*_curfile]);
stream_msg(message);
}
*_curfile = (*_curfile + 1) % _nfiles;
/* check for playlist wrap-around */
if (0 == *_curfile)
last_played = 1;
if (fp)
break;
if (*_curfile == startfile) {
stream_msg("no reasonable files found");
return(NULL);
}
} while (!fp);
return(fp);
}
playlist* read_playlist(const char* _filename) {
FILE* fp;
playlist* list;
char filename[1024];
char comparison[1024];
list = (playlist*) malloc(sizeof(playlist));
list->used = 0;
list->size = 16;
list->names = (char**) malloc(sizeof(char*) * list->size);
/* test for filename of *.mp3 */
strncpy(filename, _filename, 1024);
strncpy(comparison, stripspaces(filename, LEADING | TRAILING), 1024);
if ((strlen(comparison) > 4) &&
!strncmp(comparison + (strlen(comparison)-4), ".mp3", 1020)) {
/* found a simple mp3 file */
if (list->used == list->size) {
list->size *= 2;
list->names = (char**) realloc(list->names, sizeof(char*) * list->size);
}
list->names[list->used++] = strdup(stripspaces(filename, LEADING | TRAILING));
} else {
if (!(fp = fopen(_filename, "r")))
return(NULL);
while (!feof(fp) && !ferror(fp)) {
if (!(fgets(filename, sizeof(filename), fp)))
break;
if (list->used == list->size) {
list->size *= 2;
list->names = (char**) realloc(list->names, sizeof(char*) * list->size);
}
list->names[list->used++] = strdup(stripspaces(filename, LEADING | TRAILING));
}
fclose(fp);
}
return(list);
}
Thanks a lot for you help in advance!
Litestream Source Download
-
If you want all of the messages to go to the secondary log (the text file you're going to be scanning with PHP), modify stream_msg() to write to that file. If you're just interested in the "opened" message, and not the others, you must either create a new function, let's call it "now_playing", and call it after calling stream_msg() in the location you have highlighted.
I personally don't like to use global variables or fixed file names within logging functions like this, but for our example, I'm going to assume that there is a global variable, "tracking_file_name", that contains a pointer to a char array containing the tracking file name.
Code:
extern char *tracking_file_name; // the tracking file
void now_playing(const char *name)
{
FILE *fp = NULL;
if (tracking_file_name != NULL)
{
fp = fopen(tracking_file_name, "a"); // open file for append
}
if (fp != NULL)
{ // succeeded in opening the tracking file
fprintf(fp, "%s\n", name);
fclose(fp);
}
}
This function will append whatever string you use in a call to the end of the file. If you want to have only one file name in the file at a time (so you don't have to find the last line of the tracking file), replace the "a" with "w" as the second parameter in the call to fopen().
So, after the call to stream_msg() in the code you posted, insert a call to now_playing that looks like
Code:
now_playing(_files[*_curfile]);
and you'll be all set.
Of course, you'll still have to set up tracking_file_name somehow, but I leave that as an exercise for the reader.
-
Actually, this is a board for learning, not a board to dump random bits of work onto for others to fix.
Someone might be interested enough as a project, so I've moved it there.
-
Thanks a lot, filker0!!! :) You saved me many time of trial and error work. It's working perfectly now. :)
And sorry about abusing this forum for my own selfish purposes, Salem...;)