Home » Tag » External Text File Source - Version 2.0.40b4

Tag

External Text File Source - Version 2.0.40b4

The code below is that used to add the -f switch functionality, which allows users to pass the path to a text file, the contents of which are read as the tag value.

I would be very interested to hear about improvements to this code.

NB: This code is an improvement to that used in version 2.0.40b2. Thanks to Niko Korhonen for his suggestions for improvement. I feel a lot happier with it now it has been scrutinised by a competent C++ programmer. The code now opens the file in binary mode, negating the need for me to replace carriage returns, and also correctly stores the array filearg on the heap, and not the stack.

Obviously the variable new_argv[i] contains the parameter, e.g.: "cuesheet=C:\CDImage.ape.cue"


char *t;
size_t itemlen;
FILE* stream;
int ch;
int j;
char * filearg;

// do other stuff

// get filename from parameter (part after '=')
t = strchr ( new_argv[i], '=' ) + 1;

// get length of tag name
itemlen = t - new_argv[i] - 1;

// check file exists
if ( (stream = fopen ( t, "rb" ) ) == NULL ) {
	fprintf ( stderr, "%s: failed to open file for for reading.\n", t );
	exit (1);
}

//set filearg to "<tag>="
filearg = (char *)malloc(10240);
strncpy(filearg, new_argv[i], itemlen + 1);

// start filling char array after "="
j = itemlen + 1;

// read text from file (up to 10239 chars)
while ( ((ch = fgetc (stream)) != EOF ) && ( j < 10239 ) ) {
	
	// add read character to array
	filearg[j] = (char)ch;
	j++;
}

// terminate will null char
filearg[j] = '\0';

// close stream
fclose (stream);

// do other stuff

//free the array
free ( filearg );

Created using C++2HTML

Top


Visit the WavPack Website foobar2000 audio player

Home  |  Tag  |  Wapet  |  Contact


This document has been printed from http://www.synthetic-soul.co.uk/