Author Topic: Dimension an array of zstrings.  (Read 3510 times)

0 Members and 2 Guests are viewing this topic.

Brian Alvarez

  • Guest
Dimension an array of zstrings.
« on: May 01, 2018, 12:48:21 PM »
 Hello Charles, i will need to ask everything. Is that ok?

 How would you dimension an array of ASCIIZ with a length of 255 characters?
« Last Edit: May 01, 2018, 08:47:35 PM by Brian Alvarez »

Charles Pegge

  • Guest
Re: Dimension an array of zstrings.
« Reply #1 on: May 01, 2018, 03:01:34 PM »
Hi Brian,

I would say primitively :)

PS: I may have misunderstood your question but here is an array of 100 C-style strings, each with a length of 255 chars and null terminator char.

Code: [Select]
'2018-05-01 T 23:46:36
'ARRAY OF ZSTRING PTR / char*
indexbase 0
typedef char *pchar
char s[25600] 'static char buffer
pchar ps[100] 'array of char pointers
int i,j
for i=0 to 99
  @ps[i]=@s[j] 'map positions in char buffer
  j+=256 'stride
next
'
'TEST:
copy @ps[2],"Helo",4
copy @ps[3],"Wrld",4
copy @ps[4]," !",2
print ps[2] " " ps[3] ps[4] 'Helo Wrld !
« Last Edit: May 01, 2018, 03:11:21 PM by Charles Pegge »

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #2 on: May 01, 2018, 03:30:19 PM »
I see. It is just a little lower level. Thats going to be just a little harder to convert.

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #3 on: May 01, 2018, 03:34:11 PM »

 Before going ahead to do it, Is there a way to do it with its BASIC mode?
Also, i am making searches for all this before but i dont get any matches,
so, please bear with me:

 Is there a way to have several statements per line? That would make conversions a little easier (tab positions).


Charles Pegge

  • Guest
Re: Dimension an array of zstrings.
« Reply #4 on: May 01, 2018, 08:28:00 PM »
Yes Brian, the Basic syntax is much the same as in PB and FB. You can have multiple statements with colon separators on one line. Underscore line continuations are also supported, but usually unnecessary. Lines ending with an operator or comma are continued onto the next line, ignoring comments.

Simple zstring (char / asciiz) variables are usually straightforward and integrate well with the native (OLE) strings. Only char-pointer assignments are a bit tricky.

All these declarations are supported
Code: [Select]
'zstrings
dim a as asciiz*80
dim as asciiz a[80]="Hello World !"
'asciiz a[80]="Hello World !"
'asciiz a[]="Hello World !"
'asciiz a="Hello World !"
print a



Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #5 on: May 01, 2018, 08:45:58 PM »
Can i do

Code: [Select]
dim a(100) as asciiz * 256
?

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #6 on: May 01, 2018, 08:55:36 PM »
PluriBASIC supports this syntax:

Code: [Select]
DIM Varname[Symbol][(Bound [TO Bound])][, VarName[Symbol][(Bound [TO Bound])]][...] [AS [SCOPE] DataType] [PTR] [AT Address&] [* Length&] [= DefaultValue]
 Im going to have to know which ones are supported natively by Oxygen so that i can just copy/paste (faster) or convert (normal).

Aurel

  • Guest
Re: Dimension an array of zstrings.
« Reply #7 on: May 01, 2018, 09:00:10 PM »
I use zstring only in filedialog like this:

Dim filename[256] As zstring

but what you need looks to me like
string array element which can hold 256 chars
and this can be created with ordinary string array
BUT maybe is better with asciiz... i am not sure.

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #8 on: May 01, 2018, 09:05:21 PM »
... maybe is better with asciiz... i am not sure...

 Hi Aurel, thanks. The thing is that in this case, better or worse is irrelevant... i need to add support for it, as efficient as possible but not trying to force efficiency. Just allowing the programmer to do it. :)

 When writing parts of a compiler, i usually look for help in StackOverflow, and many people give alternative solutions based in what i
am trying to do... but that doesnt apply while writing a compiler, because i need to do *everything*. Allow to do everything, that is.

 

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #9 on: May 01, 2018, 09:27:41 PM »

 I am guessing that while declaring a zstring the dimensions apply as the length of the null terminated string, but for anything else it applies as the number of elements of the array? Am i right? If so, that may be a little inconsistent...

Charles Pegge

  • Guest
Re: Dimension an array of zstrings.
« Reply #10 on: May 01, 2018, 10:21:46 PM »
Yes, it only dims an array of chars. O2 arrays are very primitive by comparison to PB. Do you use a lot of fixed length strings, Brian?

I rarely use fixed length strings but they could be defined as a type, then you could create arrays of them easily:

Code: [Select]
'2018-05-02 T 07:30:23
'fixed length strings
type zstr256 char s[256]
zstr256 z[100]
'
z[1].s="Hello"
z[2].s=" "
z[3].s="World"
print z[1].s + z[2].s + z[3].s
« Last Edit: May 01, 2018, 10:35:22 PM by Charles Pegge »

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #11 on: May 01, 2018, 11:03:54 PM »
I rarely use them. But i need to add them for those who do.

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #12 on: May 01, 2018, 11:07:41 PM »
 Thanks for the solution Charles, the problem with it is that i would need to define a different TYPE for every different ZSTRING length.... perhaps a class with the string length specified in the constructor?

Charles Pegge

  • Guest
Re: Dimension an array of zstrings.
« Reply #13 on: May 01, 2018, 11:49:54 PM »
No problem.

types can be defined locally and are quite disposable. And we can use a macro the abstract out the dot-s.

Code: [Select]
macro dim_zstring(z,m,n    zz,zu)
  type zz char s[n]
  zz zu[m]
  def z zu[%1].s
end macro

dim_zstring z,100,256
'
z[1]="Hello"
z[2]=" "
z[3]="World"
print z[1] + z[2] + z[3]

Brian Alvarez

  • Guest
Re: Dimension an array of zstrings.
« Reply #14 on: May 02, 2018, 12:00:09 AM »
Thanks again Charles... the problem with this would be that the DIM statement would vary depending on the Datatype of the variable being declared.

Edit:
I think i understand your code, and it is quite elegant. Let me see if i can modify it a bit to make it easier to implement. :) The problem here is, how would i go around using the declared array with REDIM, RESET, ARRAY SORT, ARRAY SCAN, being passed as a parameter of type STRINGZ... et cetera? Thats what is making it hard for me, trying to adapt to every other situation. There are simply too many things to consider... The array cannot be a type, i think i will need to create a mix of your last and your first... but it cannot be a PTR array... Maybe this will have to be fixed at lowest level... reimplementation of the DIM statement? More ideas?
« Last Edit: May 02, 2018, 12:47:30 AM by Brian Alvarez »