Exemple #1
0
/**
 * call-seq:
 *  put(first_track, sectors, offsets)
 * 
 * Set the TOC information directly instead of reading it from a device.
 * 
 * Use this instead of read if the TOC information was already read elsewhere
 * and you want to recalculate the ID.
 * Throws an _Exception_ if the CD's TOC can not be read.
 * 
 * <b>Parameters:</b>
 * [first_track] The number of the first track on the disc (usually 1).
 * [sectors] The total number of sectors on the disc.
 * [offsets] Array of all track offsets. The number of tracks must not exceed 99.
 *
 * Raises:: Exception
 */
static VALUE mb_discid_put(VALUE self, VALUE first_track, VALUE sectors,
                           VALUE offsets)
{
	DiscId *disc;                       /* Pointer to the disc struct */
	long length = RARRAY_LEN(offsets);  /* length of the offsets array */
	int cfirst  = NUM2INT(first_track); /* number of the first track */
	int clast   = length + 1 - cfirst;  /* number of the last track */
	int coffsets[100];                  /* C array to hold the offsets */
	int i = 1;                          /* Counter for iterating over coffsets*/
	
	Data_Get_Struct(self, DiscId, disc);
	
	/* Convert the Ruby array to an C array of integers. discid_puts expects
	   always an offsets array with exactly 100 elements. */
	coffsets[0] = NUM2INT(sectors); /* 0 is always the leadout track */
	while (i <= length && i < 100)
	{
		coffsets[i] = NUM2INT(rb_ary_entry(offsets, i - 1));
		i++;
	}
	
	/* Mark the disc id as unread in case something goes wrong. */
	rb_iv_set(self, "@read", Qfalse);
	
	/* Read the discid */
	if (discid_put(disc, cfirst, clast, coffsets) == 0)
		rb_raise(rb_eException, discid_get_error_msg(disc));
	else /* Remember that we already read the ID. */
		rb_iv_set(self, "@read", Qtrue);
	
	return Qnil;
}
Exemple #2
0
int main(int argc, char *argv[]) {
	DiscId *d;
	char *tmp, *expected;
	int ret;
	int ntests = 0, nok = 0;
	int offsets[] = {
		303602,
		150, 9700, 25887, 39297, 53795, 63735, 77517, 94877, 107270,
		123552, 135522, 148422, 161197, 174790, 192022, 205545,
		218010, 228700, 239590, 255470, 266932, 288750,
	};

	d = discid_new();

	/* Setting TOC */
	printf("Testing discid_put ... ");
	ret = discid_put(d, 1, 22, offsets);
	if ( ret ) {
		printf("OK\n");
		nok++;
	}
	else
		printf("Failed\n");
	ntests++;

	/* MusicBrainz DiscID */
	printf("Testing discid_get_id ... ");
	tmp = discid_get_id(d);
	expected = "xUp1F2NkfP8s8jaeFn_Av3jNEI4-";
	if ( strcmp(tmp, expected) == 0 ) {
		printf("OK\n");
		nok++;
	}
	else {
		printf("Failed\n");
		printf("  Expected : %s\n", expected);
		printf("  Actual   : %s\n", tmp);
	}
	ntests++;

	/* FreeDB DiscID */
	printf("Testing discid_get_freedb_id ... ");
	tmp = discid_get_freedb_id(d);
	expected = "370fce16";
	if ( strcmp(tmp, expected) == 0 ) {
		printf("OK\n");
		nok++;
	}
	else {
		printf("Failed\n");
		printf("  Expected : %s\n", expected);
		printf("  Actual   : %s\n", tmp);
	}
	ntests++;

	/* MusicBrainz web submit URL */
	printf("Testing discid_get_submission_url ... ");
	tmp = discid_get_submission_url(d);
	expected = "http://mm.musicbrainz.org/bare/cdlookup.html?id=xUp1F2NkfP8s8jaeFn_Av3jNEI4-&tracks=22&toc=1+22+303602+150+9700+25887+39297+53795+63735+77517+94877+107270+123552+135522+148422+161197+174790+192022+205545+218010+228700+239590+255470+266932+288750";
	if ( strcmp(tmp, expected) == 0 ) {
		printf("OK\n");
		nok++;
	}
	else {
		printf("Failed\n");
		printf("  Expected : %s\n", expected);
		printf("  Actual   : %s\n", tmp);
	}
	ntests++;

	printf("\n%d tests, %d passed, %d failed\n", ntests, nok, ntests - nok);
	
	discid_free(d);
	
	return ntests != nok;
}