• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[#Red] Red language group

Arnold
2-Jul-2013
[9120]
Will the code be reusable in a pure Red solution if one would do 
that? 

To be able to do all kinds of possible things using Red bindings 
to C-libraries is great, beats not being able to do things by infinite 
factors. Still having the functionality coded in Red feels better. 
Open sourcing R3 has reminded everyone that it is made using C. HostileFork 
expresses the lack of development by the community as being caused 
by the difficulty and abstraction level of this C codebase.
Kaj
2-Jul-2013
[9121x2]
There is no pure Red solution. When Doc will make an I/O framework, 
file I/O will still use a binding to the operating system underneath, 
which almost always means the standard C library
In a framework with scheme handlers, code will have to be refactored, 
but pieces from the current code could still be used. They're already 
small pieces, anyway
Arnold
2-Jul-2013
[9123]
ok. The same solution was made for adding time or now you provided 
earlier. Somehow the timer has to be read using a standard C library.
Kaj
2-Jul-2013
[9124]
Yes, if you don't want to write a lot of different code for different 
operating system kernels
Arnold
2-Jul-2013
[9125]
One day I will understand and make the video ;-)
Kaj
2-Jul-2013
[9126]
Koel :-)
Arnold
2-Jul-2013
[9127]
Having to add all #include for many of these kind of common functionality 
(time random i/o etc) is less friendly than having these integrated.
Kaj
2-Jul-2013
[9128x2]
Sure, that's why they're in separate repositories. When Red takes 
the pieces it needs, it won't be bothered by the full bindings, but 
they can still be used in Red/System programs
In the meantime, if you want everything included, use the interpreter 
builds
Bo
2-Jul-2013
[9130]
'make-c-string is my friend. My Red/System scripts are so much happier 
now that I'm not assigning c-strings to arbitrary locations in memory 
like I was doing with 'as-c-string. :-)
Kaj
2-Jul-2013
[9131]
:-) Note that most of your string initialisations were unneeded. 
Several allocations are done by the functions you're calling
Bo
2-Jul-2013
[9132x2]
How do I know when to initialize a string or not?  I was just going 
to ask that question.  Take this function from ANSI.reds for example:

	append-string: "strcat" [				"Append string."
		target			[c-string!]
		source			[c-string!]
		return:			[c-string!]
	]

Can I just do this?

file1: make-c-string 128
file2: make-c-string 128
file1: "to-process/"
file2: "dir/"
append-string file1 file2
append-string file1 "file.txt"
I'm trying to track down an access violation.
Kaj
2-Jul-2013
[9134]
You're allocating both file1 and file2 double there. The two 128 
byte strings immediately leak away when you assign the literals
Bo
2-Jul-2013
[9135x2]
OK.  So 'make-c-string isn't really needed?  Isn't it like Rebol 
where if you don't do:

	copy "to-process/"


that you will be linking to a static memory location with "to-process/" 
in it?
linking = pointing
Kaj
2-Jul-2013
[9137]
Then when you append to file1, you're overwriting the string literal 
inside the executable, so you're destroying your program in memory
Bo
2-Jul-2013
[9138]
So how would you write the above?
Kaj
2-Jul-2013
[9139x2]
Allocations are very much needed, but once made, you have to guard 
them like your most precious posession :-)
The problem is exactly that you're not doing the COPY of the literal. 
Very much like REBOL
Bo
2-Jul-2013
[9141]
Is this the right way?

file1: make-c-string 128
file2: make-c-string 128
copy-string file1 "to-process/"
copy-string file2 "dir/"
append-string file1 file2
append-string file1 "file.txt"
Kaj
2-Jul-2013
[9142]
Yep
Bo
2-Jul-2013
[9143]
I'm assuming that 'append-string copies the source parameter.  Right?
Kaj
2-Jul-2013
[9144]
Yes, it needs to for appending. It's not a linked list of strings 
or something like that
Bo
2-Jul-2013
[9145]
OK.  The mental picture grows clearer.
Kaj
2-Jul-2013
[9146x2]
So file2 is unneeded in there, because appending the literal copies 
it, anyway
It's a contiguous memory area, just like a block in REBOL
Bo
2-Jul-2013
[9148]
OK.
Kaj
2-Jul-2013
[9149x2]
append-string returns the result, so you can chain them just like 
in REBOL
append-string append-string file1 "dir/" "file.txt"
Bo
2-Jul-2013
[9151]
If 'file1 is initialized but hasn't been assigned a value, can append-string 
use it as the target?
Kaj
2-Jul-2013
[9152]
How do you mean? Initialising means assigning a value
Bo
2-Jul-2013
[9153]
What I mean is this:

file1: make-c-string 128
append-string append-string file1 "dir/" "file.txt"
Kaj
2-Jul-2013
[9154x2]
But you did assign a value there: a memory block of 128 bytes
The only problem with using it as a target is that it may not be 
initialised as an empty string. For that, it needs to hold a null 
byte marker
Bo
2-Jul-2013
[9156]
In my tests, it did not work.  How do I initialize it with a null 
byte marker?  Like this?

file1: make-c-string 128
file1: #"^(00)"
append-string append-string file1 "dir/" "file.txt"
Kaj
2-Jul-2013
[9157]
file1/1: #"^(00)"
PeterWood
2-Jul-2013
[9158]
There is also a pre-defined constant "null-byte" available:

file1/1: null-byte
Bo
2-Jul-2013
[9159]
Oh no!  Just found a bug in the Linux-ARM version of Red/System. 
 It has to do with string handling, but I haven't isolated yet.  
Will post here once I do.
Kaj
2-Jul-2013
[9160]
If you're lucky, it may be fixed in the dyn-lib-emitter branch
Bo
2-Jul-2013
[9161x2]
It appears to have something to do with pointers and 'append-string.
Consider the following code snippet:

	print-line file1
	append-string file1 "/img00.bin"
	print-line file1

On Windows, it outputs:

	to-process/2013-06-29-20-18-55.h264
	to-process/2013-06-29-20-18-55.h264/img00.bin

The exact same code on Linux-ARM outputs:

	to-process/2013-06-29-20-19-06.h264
	/img00.bin/2013-06-29-20-19-06.h264
Kaj
2-Jul-2013
[9163]
Odd. The append somehow considers the string empty on ARM, but it 
doesn't close the appended part with a null byte, either
Bo
2-Jul-2013
[9164]
That's what I was thinking.
Kaj
2-Jul-2013
[9165]
Could you print the string lengths, as well?
Bo
2-Jul-2013
[9166]
How does one do that?
Kaj
2-Jul-2013
[9167]
length? file1
Bo
2-Jul-2013
[9168]
That's too much like Rebol.
Kaj
2-Jul-2013
[9169]
We could distort it :-)