ID:171231
 
How do you put it so it says if it is a wav or midi file?
I tried.....
if(istype(".wav"))

But nothing....any idea's?
CodingSkillz2 wrote:
How do you put it so it says if it is a wav or midi file?

If you're always going to use .wav and .mid as extension, use findtext().


/Gazoot
In response to Gazoot
myfile=file("mysong.wav")
if(findtext(myfile,".mid"))world<<sound(myfile,0,1) //play the song, do not loop it, wait for the current song to stop first
else return 0 //do not play anything


Just as Gazoot said. Here's an example with a neat feature to make the song wait for the current song to end!

NOTE: This *can* still be circumvented by any player. All the person has to do is rename the .WAV file to a .MID file. Heck, even myfile.mid.wav would work! I'd suggest using dd_hassuffix(), in the Deadron TextHandling library to get some more additional security, but that even doesn't stop them.
In response to Phoenix Man
Phoenix Man wrote:
NOTE: This *can* still be circumvented by any player. All the person has to do is rename the .WAV file to a .MID file. Heck, even myfile.mid.wav would work! I'd suggest using dd_hassuffix(), in the Deadron TextHandling library to get some more additional security, but that even doesn't stop them.

Ah, he's using it for a file sharing/upload utility! Ok, that sure calls for some extra security. :) Here's a couple of functions I wrote that tests if the file is really a Midi or WAV, and also a filesize tester:

#define MIDI_SIZE_LIMIT 200    // Max 200k Midi files
#define WAV_SIZE_LIMIT 3000 // Max 3Mb Wav files

proc
IsMIDI(F)
var/binary = file2text(F) // Copy the file to a string...
var/header = copytext(binary, 1, 5) // and extract the header

if(header == "MThd") return 1 // "MThd" is the MIDI format header
return 0

IsWAV(F)
var/binary = file2text(F)
var/header = copytext(binary, 1, 5)

if(header == "RIFF") return 1 // "RIFF" is the WAV format header
return 0

SizeOK(mob/M, F, size_limit)
// Remember that size is in Kb and needs to be multiplied with 1024.
if(length(F) <= size_limit * 1024)
return 1
else
M << "Sorry, your file is to big. (Maximum size [size_limit]k)"
return 0


And how to use it? Here's a very basic file upload system verb. It's using fcopy() to prevent cluttering up the .rsc file:

mob
verb
uploadfile(F as file)

var/filename = "temp_soundfile"
fcopy(F, filename)

if(IsMIDI(F) && SizeOK(src, F, MIDI_SIZE_LIMIT))
world << sound(file(filename))

else if(IsWAV(F) && SizeOK(src, F, WAV_SIZE_LIMIT))
world << sound(file(filename))

else
src << "Illegal file."
fdel(filename)


This must certainly be expanded to having different file areas for each user, etc... Well, hope it can be useful to anyone.

[edit] Note that the #defines must be above the mob declaration for the code to work.


/Gazoot
In response to Gazoot
On a sidenote, do you know a website that has more of the file type headers?