r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Henrik
15-Oct-2007
[8772x3]
I see. :-/
hmm.. can't convert that to an integer.
to-integer trim to-binary bs

seems to work.
Terry
16-Oct-2007
[8775]
a: [11 22 33 44 55 66 77 88 99]

b: 44
foreach [ s p v] a [ if s = b [ remove s p v ???]
sqlab
16-Oct-2007
[8776]
while [pick set [s p v] a 1] [if s = b  [remove/part a 3  a: skip 
a -3] a: skip a 3]
btiffin
16-Oct-2007
[8777x2]
Another opinion piece;
While developing I've taken to starting each script with


;; if a symbol exists and is true, when will evaluate the block of 
code
;; when/not will evaluate the code if the symbol is not set
when: func ['condition [word!] code [block!] /not] [
    either not [
        unless value? condition code
    ][
        if all [value? condition  get condition] code
    ]
]

This allows for

; See if testing requested as script argument or it could be set 
by a caller
when/not testing [

    if all [system/script/args  find system/script/args "/test"] [
        testing: on
    ]
]
; If requested, try loading the test facility or stub it.
when testing [
    absent test [
        if error? try [do %../Test/test.r] [
            test: func ["test stub" drop [block!]] []
        ]
    ]
]


I had been using  given  and  absent (for when/not).  Anyone got 
a better way of handling conditional load sequences and command line 
argument passing that supports development cycles while not having 
to change the code for testing, autodocing  and production rollout?
Oops, an absent snuck in there from and older (untested...sad) cut'n'paste
   when testing [  when/not test ...
Terry
16-Oct-2007
[8779]
Thanks sq
Ladislav
17-Oct-2007
[8780]
foreach [ s p v] a [ if s = b [ remove s p v ???]
 - this is the Rebol way:
remove-each [s p v] a [s = b]
Terry
17-Oct-2007
[8781]
Cool, was wondering how remove-each worked, thanks Ladislav.
Sorry sq.
sqlab
17-Oct-2007
[8782]
I learned something too
Terry
21-Oct-2007
[8783x2]
With inline javascript in HTML, it's not uncommon to find a brace, 
apostrophe and a double quote in a single line  ie:


<a href="#"  onclick="$('test').function(){'some', 'options';}">Hrmm</a>

So, what's the best method for prepping this for output?

output: {}   ... then escape the all braces?
Braces are ok, if there's always a pair.. otherwise i guess the best 
method is braces, and escape any in the string with ^{   or ^}
Graham
21-Oct-2007
[8785x2]
it gets really messy though
what we need is some other way to enclose literal strings that are 
full of " ' and {}
Terry
21-Oct-2007
[8787]
aye
Gregg
21-Oct-2007
[8788]
I would avoid the "some other way", because you're always going to 
hit the issue with some char. At least escapes are consistent and 
easy to reason about. Of course, that doesn't help when they aren't 
used properly.
Graham
21-Oct-2007
[8789x2]
that other way would be the way it is done in embedded sql, you can 
often redefine the termination character.
So, I would want some way to redefine temporarily the {  or } pair. 
 Eg use a pipe character instead .. hardly ever see those used in 
 web pages
Oldes
25-Oct-2007
[8791]
Is there any script to detect JPG size without need to load the image?
btiffin
25-Oct-2007
[8792]
Rebol.org  exif-image.r  (uses exif-core.r) and has a jpeg-size function. 
 Didn't read enough to see if it loads the whole file before it looks 
for the size fieldsm but I don't think Piotr's routines requires 
a load.
Oldes
25-Oct-2007
[8793x2]
it would not be working if exif tag is not present.. never mind... 
I could probably enhance my jpg-analyse script.. decoding Start Of 
Frame marker, which may contain the real size info
yes.. it's bytes 5,6 for width and 7,8 for height after #{FFC0} marker
Graham
25-Oct-2007
[8795x2]
Is there any way for a rebol process to detect other versions of 
itself running and to kill them?
encapped
Terry
25-Oct-2007
[8797]
for windows you could use the winapi
Steeve
25-Oct-2007
[8798x9]
very strange behaviour when using skip command during parsing of 
a binary string
f: read/binary %15.jpg

get-len: [header: skip  (len: to integer! as-binary cp/part header 
2) ]
skip-len: [:header (header: skip header len) :header]
parse f [
        #{FFD8}   ; jpeg Header
        [
                #{FFE0}                         ;JFIF header

                        get-len                 ;get length of a header (2 octets)

                         #{4A46494600}          ;yeah it's a JFIF (confirmation)
                        skip-len
                        some [
                                #{FF}

                                        #{C0}           ;good ! i found the length properties

                                        (print ["height" to integer! as-binary cp/part at header 6 2])

                                        (print ["width" to integer! as-binary cp/part at header 8 2])
                                        break

                                | #{FF}                 ;don't know this header
                                        skip    
                                        get-len 
                                        skip-len
                                | 

                                        [end  skip]     ;error format
                        ]
                        
                        
                | #{FFE1}                       ;EXIF header

                        get-len                 ;get length of a header
                                                ;... to do
                        [end skip]
        ]
        to end
]
very strange behaviour of skip and copy, i should check what release 
i use
it's just a proof of concept, it's not usable as-is for an open/seek 
 file mode
made an async parser (not fully tested - some problems may occur 
when the parser go back in the stream )

but the concept works :  when the parser encouter a skip command, 
the data are not readed from the file but the offset is modified.
REBOL []

parse-async: func [
        file rules
        /local port buffer offset getf seek meta & && result
][
        port: open/seek/binary  file
        buffer: clear #{}
        offset: 1
        getf: func [len][
                offset: offset - length? buffer
                clear buffer 
                append buffer copy/part at port offset len 
                offset: offset + len 
        ]
        seek: [(offset: offset + 1)]
        ..: func [blk] [change/part & compose/deep blk && ]
        parse rules meta: [
                some [

                        &: binary! &&: (.. [buffer: (to-paren reduce ['getf length? &/1]) 
                        (&/1)]) :& 3 skip 
                        | &: 'skip &&: (.. [seek]) :& skip
                        | &: 'get word! integer! &&:

                                (.. [buffer: (to-paren compose/deep [getf (&/3) set [(&/2)] to integer! 
                                as-binary cp buffer]) to end]) :& 4 skip

                        | &: string! &&: (.. [(as-binary &/1)]) :& 
                        | 'end 'skip 
                        | into meta
                        | skip
                ]
        ]
        result: parse/all buffer rules
        close port
        result
 ]
 
if parse-async %15.jpg [
        #{FFD8}   ; jpeg Header
        [
                 #{FFE0}                        ;JFIF header

                        get len 2               ;get data length  for the current header 
                        (2 bytes)

                         "JFIF"                 ;yeah it's a JFIF (confirmation)

                        (len: len - 6) len skip ;skip data (len) times
                        some [

                                 #{FFC0}        ;good ! i found the length properties

                                        2 skip  ; skip length of this header

                                        skip    ; filler ??? always = #{08}
                                        get height 2
                                        get width 2
                                        break   ; finished

                                | #{FF} skip    ;skip this header
                                        get len 2 
                                        (len: len - 2) len skip
                                | 

                                        [end skip]     ;error format
                        ]

                | #{FFE1}                       ;EXIF header

                        get len 2               ;get length of a header
                                                ;... to do
                        [end skip]
        ]
        to end
][
   ?? height
   ?? width
]        
halt
perhaps it's not clear, but this parser do not load all the file 
in memory but  only the needed part to retrieve the width and the 
height.
you can probe the contain of the 'buffer'  variable to have a proof
the concept of async parsing could be extended at any type of serie
Gregg
26-Oct-2007
[8807]
Cool Steeve!
Steeve
26-Oct-2007
[8808]
yeah i'm a cool guy
Graham
27-Oct-2007
[8809x3]
>> c: make object! [ a: "test" ]
>> save/all c make binary! 1024

** Script Error: save expected where argument of type: file url binary
** Near: save/all c make binary! 1024
oops
wrong way round
Gabriele
27-Oct-2007
[8812]
why not mold/all ?
Graham
27-Oct-2007
[8813]
good point
Oldes
27-Oct-2007
[8814x8]
get-JPG-size: func[
	"Returns size on JPG image or none on invalid files"
	jpgfile [file! url!] "File to examine"
	/local stream byte1 byte2][
	stream: open/read/binary/direct jpgfile
	;seek to jpg start
	until [
		all [
			255 = first stream
			216 = first stream
		]
	]
	while[any[byte1: first stream]] [
		if 255 = byte1 [
			either 192 = byte2: first stream [
				copy/part stream 3
				height: to-integer copy/part stream 2
				width:  to-integer copy/part stream 2
				close stream
				return as-pair width height
			][
				copy/part stream ((to integer! copy/part stream 2) - 2)
			]
		]
	]
	close stream
	none
]
89 files with total size 158728227B done in 0:00:00.094
(instead of 'byte1 and 'byte2 there can be used just 'byte)
but it's not working on non jpeg files:-/ I should first better test 
it
get-JPG-size: func[
	"Returns size of JPG image or none on invalid files"
	jpgfile [file! url!] "File to examine"
	/local stream byte
][
	stream: open/read/binary/direct jpgfile
	;seek to jpg image start
	until [
		any [
			all [
				255 = first stream
				216 = byte: first stream
			]
			none? byte
		]
	]

 unless byte [close stream return none] ;no Start Of Image marker 
 found

	while[any[byte: first stream]] [
		if 255 = byte [
			either 192 = byte: first stream [
				copy/part stream 3
				height: to-integer copy/part stream 2
				width:  to-integer copy/part stream 2
				close stream
				return as-pair width height
			][
				copy/part stream ((to integer! copy/part stream 2) - 2)
			]
		]
	]
	close stream
	none
]
which is not correct as well. As it would throw an error if the martker 
192 is not found... I should rather use open/seek instead of /direct
here is correct version using the /seek method... sorry for so many 
posts:
get-JPG-size: func[
	"Returns size of JPG image or none on invalid files"
	jpgfile [file! url!] "File to examine"
	/local stream bytes height width
][
	stream: open/read/binary/seek jpgfile
	;seek to jpg image start
	while [#{FFD8} <> copy/part stream 2][
		if tail? stream: skip stream 2 [
			;no Start Of Image marker found
			close stream return none
		]
	]
	stream: skip stream 2
	while[not tail? stream][
		bytes:  copy/part stream 2
		stream: skip stream 2
		if 255 = bytes/1 [
			either 192 = bytes/2 [
				stream: skip stream 3
				height: to-integer copy/part stream 2
				width:  to-integer copy/part skip stream 2 2
				close stream
				return as-pair width height
			][
				stream: skip stream ((to integer! copy/part stream 2) - 2)
			]
		]
	]
	close stream
	none
]