revCopyFile - What am I doing wrong?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: revCopyFile - What am I doing wrong?

Post by AxWald » Sat Apr 01, 2017 11:05 am

Hi,
Klaus wrote:Nowadays OS X uses file suffixes to determine the filetype and
one or two OS versions later Resource forks on Mac OS will be history!
I remember that, back in the good old days when Macs was still Macs etc., we had much fun poking the Windows people for having to rely on such crude mechanisms like file suffixes ;-)
Maybe the iJail inmates will soon have to learn about the benefits of drive letters, too? ;-)))
rumplestiltskin wrote:[...] check chars 5 to 8 for the presence of "jfif" [...]
Does this seem reasonable?
Caution - for some strange reasons valid JPEGs on my Win machine have the "JFIF" in bytes 7 to 10!
In Hex:
FF D8 FF E0 00 10 4A 46 49 46 00 [...]
In ASCII:
ÿ Ø ÿ à · · J F I F · [...]
(Spaces between Bytes added)

After playing with it a bit, I'd use such code:

Code: Select all

on mouseUp
   answer file "Show me the candidate, plz!"
   if it is empty then exit mouseUp
   put it into myFile 
   open file myFile for read
   read from file myFile for 42 chars
   close file myFile
   if ("ÿØÿà" is in it) AND ("JFIF" is in it) then
      answer "It seems to be a valid JPEG!" titled "Congraz!"
   else
      answer "Oh my god - it's full of chars! " & CR & CR & it titled "Bummer!"
   end if
 end mouseUp
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Sun Apr 02, 2017 12:35 am

Well, I thought I was in the clear but discovered that when jpegs come from places other than my Mac, sh-t happens. JPEGS coming from Lightroom on my PC look like this when dropped into TextEdit:

ˇÿˇ·;ÍExif

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by MaxV » Mon Apr 03, 2017 1:32 pm

Hi,
just read the magic number of the file: https://en.wikipedia.org/wiki/File_format#Magic_number and https://en.wikipedia.org/wiki/Magic_num ... ramming%29

So if the first 4 chars of a file are: byteTonum() = 255 216 255 224 , then is a JPEG image.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply