Archive for May, 2008

UtopicMusic blog included me in their podcast

Tuesday May 13th, 2008 at 7:22 pm | music

Some of my music, from Quiet, hosted on Jamendo, has been included on the latest UtopicMusic podcast. Go over and check it out.

Thanks :-)

Save file dialog code – OSX/Carbon

Friday May 2nd, 2008 at 11:25 pm | code

Why is this so hard?

	short fRefNumOut;
	FSRef output_file;

	NavDialogCreationOptions options;
	NavGetDefaultDialogCreationOptions( &options );
	options.modality = kWindowModalityAppModal;

	NavDialogRef dialog;
	err = NavCreatePutFileDialog ( &options, "Midi", 'MIDI', NULL, NULL, &dialog);
	printf("NavCreatePutFileDialog returned %i\n", err );

	err = NavDialogRun( dialog );
	printf("NavDialogRun returned %i\n", err );

	NavUserAction action;
	action = NavDialogGetUserAction( dialog );
	printf("got action %i\n");
	if (action == kNavUserActionNone || action == kNavUserActionCancel) {
		return 2;
	}

	// get dialog reply
	NavReplyRecord reply;
	err = NavDialogGetReply(dialog, &reply);
	if ( err != noErr )
		return 2;

	if ( reply.replacing )
	{
		printf("need to replace\n");
	}

	AEKeyword keyword;
	DescType actual_type;
	Size actual_size;
	FSRef output_dir;
	err = AEGetNthPtr(&(reply.selection), 1, typeFSRef, &keyword, &actual_type,
						  &output_dir, sizeof(output_file), &actual_size);

	printf("AEGetNthPtr returned %i\n", err );

	UInt8 output_dir_name[1024];
	FSRefMakePath(&output_dir, output_dir_name, 1024 );
	printf("got %s\n", output_dir_name );

	// now get filename
	CFIndex len = CFStringGetLength(reply.saveFileName);
	if (len > 255)
		len = 255;
	UniChar output_filename[255];
	CFStringGetCharacters(reply.saveFileName, CFRangeMake(0, len), output_filename);

	// need to unlink the old file
	if ( reply.replacing )
	{
		FSRef oldfile;

		err = FSMakeFSRefUnicode(&output_dir, len, output_filename,
								kTextEncodingUnicodeDefault,
								&oldfile);
		if (err == noErr)
			err = FSDeleteObject(&oldfile);
		if (err != noErr)
		{
			printf( "error %d erasing old file\n", err );
			return 2;
		}
	}

	err = FSCreateFileUnicode( &output_dir, len, output_filename, kFSCatInfoNone,
							   NULL, &output_file, NULL );

	if ( err != noErr )
	{
		fprintf( stderr, "error %i creating file\n", err );
		return 2;
	}

	// cleanup dialog
	NavDialogDispose(dialog);

	HFSUniStr255 data_fork;
	FSGetDataForkName( &data_fork );
	err = FSOpenFork(&output_file,data_fork.length, data_fork.unicode,fsWrPerm,&fRefNumOut);
	if ( err != noErr )
	{
		fprintf( stderr, "error %i opening data fork for writing\n", err );
		return 2;
	}

	char* buf = "hello";
	long bytes_written;
	FSWriteFork( &fRefNumOut, fsAtMark, 0, strlen(buf), buf, &bytes_written );

	FSCloseFork( &fRefNumOut );