Qucik Newbie Help

This is a discussion on Qucik Newbie Help within the Projects and Job Recruitment forums, part of the Community Boards category; Sorry to bother you with a newbie question (I have no idea of C programming...): I'm running Litestream, an alternative ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    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
    Last edited by floid; 12-06-2005 at 05:50 AM.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    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.
    Insert obnoxious but pithy remark here

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21