view src/xs_length.c @ 4:4bb09e405eab

Added new files for 0.8
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Jun 2003 11:03:04 +0000
parents
children f5d82424b0ed
line wrap: on
line source

/*  
   XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS)

   Get song length (from database or by other means)
   
   Written by Matti "ccr" Hamalainen <ccr@tnsp.org>

   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "xmms-sid.h"
#include "xs_length.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

/*
 * Pointer to database in memory
 */
static t_xs_dbentry	*xs_database = NULL;


/*
 * Parses a time-entry in SLDB format
 */
long int xs_gettime(char *pcStr, int *piPos)
{
 long int iResult;
 int iTemp;
 char chTempBuf[16];

 /* Check if it starts with a digit */ 
 if (isdigit(pcStr[*piPos]))
	{
	/* Get minutes-field */
	iTemp = *piPos;
	xs_findnum(pcStr, piPos);
 
	strncpy(chTempBuf, &pcStr[iTemp], (*piPos - iTemp));
	chTempBuf[*piPos - iTemp] = 0;
 
	iResult = (atol(chTempBuf) * 60);

	/* Check the field separator char */
	if (pcStr[*piPos] == ':')
		{
		/* Get seconds-field */
		(*piPos)++;
		iTemp = *piPos;
		xs_findnum(pcStr, piPos);

		strncpy(chTempBuf, &pcStr[iTemp], (*piPos - iTemp));
		chTempBuf[*piPos - iTemp] = 0;
 
		iResult += atol(chTempBuf);
		
		} else
		iResult = -2;
	} else
	iResult = -1;

 /* Ignore and skip the possible attributes */
 while (!isspace(pcStr[*piPos])) (*piPos)++;
 
 return iResult;
}


/*
 * Initialize, read database to memory
 */
gint xs_db_initialize(gchar *pcFilename)
{
 FILE *inFile;
 char inLine[256];
 int lineNum, linePos, i, j, k;
 t_xs_hash tmpHash;

   
 /* Try to open the file */
 if ((inFile = fopen(pcFilename, "r")) == NULL)
 	{
 	fprintf(stderr, "hv!\n");
 	return -1;
 	}
 

 /* Read and parse the data */
 lineNum = 0;

 while (!feof(inFile))
 {
 fgets(inLine, sizeof(inLine), inFile);
 linePos = 0;
  
 if (strlen(inLine) > 1)
 {
 /* Find first character */
 xs_findnext(inLine, &linePos);

 /* Check if it is a hash-line */ 
 if (isxdigit(inLine[linePos]))
 	{
 	i = linePos;
 	while (isxdigit(inLine[linePos])) linePos++;

	if ((linePos - i) != XS_HASH_LENGTH_CH)
		{
		XSERR("Invalid hash/syntax error in SongLengthDB file '%s' line #%d!\n",
		pcFilename, lineNum);
		} else {
		/* Get hashcode value */
		for (j = 0; j < XS_HASH_LENGTH; j++)
			{
			
			}

		/* Get playtimes */
		xs_findnext(line, &linePos);

		if (line[linePos] != '=')
			{
			XSERR("Warning: '=' expected in line #%d in SongLengthDB file '%s'\n",
			lineNum, pcFilename);
			} else {
			linePos++;

			while (linePos < strlen(inLine))
				{
				xs_findnext(line, &linePos);
				printf("[%lis]", xs_gettime(inLine, &linePos));
				}
		

			/* Add an entry to db in memory */
			}
		}
 	}
 	else
 if ((line[linePos] != ';') && (line[linePos] != '['))
 	{
	XSERR("Invalid line #%d in SongLengthDB file '%s'\n", lineNum, pcFilename);
	}
 
 } /* strlen(line) > 1 */
 
 lineNum++;
 }
  
 fclose(inFile);
 
 return 0;
}


/*
 *
 */
gint xs_db_delete(void)
{
 /* Free the memory allocated for database */

 return 0;
}


/*
 *
 */
int xs_comparehashes(t_xs_hash *testHash1, t_xs_hash *testHash2)
{
 int iIndex, iOK;

 iOK = 1;
 iIndex = 0;
 while ((iIndex < 16) && (iOK))
 	{
 	if (testHash1[iIndex] != testHash2[iIndex])
 		iOK = 0;
 		else
 		iIndex++;
	}

 return iOK;
}




/*
 * Get song length from database
 */
t_xs_dbentry * xs_db_get(gchar *pcFilename)
{

 return NULL;
}


gint32 xs_get_length(gchar *pcFilename, gint iSubTune)
{
 gint32 iResult;
 t_xs_dbentry *dbEntry;
 t_xs_hash dbHash;

 iResult = -1;

 switch (xs_cfg.playMethod) {

  case XMMS_SID_PMETHOD_DATABASE:
  		
	iResult = xs_db_get(pcFilename, &dbHash);
	if (iResult >= 0)
		{
		}
		
	if (dbEntry)
		{
		if ((iSubTune >= 0) && (iSubTune < dbEntry->nlengths))
			iResult = dbEntry->lengths[iSubTune];
			else
			iResult = -1;
  		}
  	break;

  case XMMS_SID_PMETHOD_MAXSILENCE:
  case XMMS_SID_PMETHOD_NONE:
	iResult = -1;
	break;

  default:
	XSERR("Invalid play-method! (%d, unknown)\n", xs_cfg.playMethod);
	break;
 }

 /* Check for max playtime */
 if (iResult == -1)
 	{
	if (xs_cfg.playUseMaxTime)
		iResult = xs_cfg.playMaxTime;
		else
		iResult = -1;
 	}
 
 XSDEBUG("fname='%s', sub=%i, res=%li\n", pcFilename, iSubTune, iResult);
  
 return iResult;
}