Array with no/open size in structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Array with no/open size in structures

Post by sverson »

It would be great to have a special kind of 'sizeless' arrays to address (imported) arrays of unknown*/variable size.
*unknown on structure definition time

For example MYSQL_ROW:
The C definition is »typedef char **MYSQL_ROW; /* return data as array of strings */«

The PB structure could look like this:

Code: Select all

Structure MYSQL_ROW
  Array field.s()
EndStructure
Structure MYSQL_LENGTHS
  Array length.l()
EndStructure
- OR -

Code: Select all

Structure MYSQL_ROW
  OpenArray field.s
EndStructure
Structure MYSQL_LENGTHS
  OpenArray length.l
EndStructure
In this case PureBasic will not take control of the array. It just makes it possible to access the MYSQL_ROW data.
mySQL still controls, fills and frees the memory - AND - off course knows the size of the array. (FieldCount = mysql_num_fields(*mySQL_Result))

This would be possible:

Code: Select all

*myRow.MYSQL_ROW = mysql_fetch_row(*mySQL_Result)
debug *myRow\field(3)
Direct access to the field string...

Or if there is no zero at the end:

Code: Select all

*myRow.MYSQL_ROW = mysql_fetch_row(*mySQL_Result)
*myLengths.MYSQL_LENGTHS  = mysql_fetch_lengths(*mySQL_Result)
debug PeekS(*myRow\field(3), *myLengths\length(3))
instead of:

Code: Select all

*mySQL_Row = mysql_fetch_row(*mySQL_Result)
*mySQL_Lengths = mysql_fetch_lengths(*mySQL_Result)
 mySQL_FieldLength.l = PeekL(*mySQL_Lengths+4*3)
*mySQL_FieldPtr = PeekL(*mySQL_Row+4*3)
debug PeekS(*mySQL_FieldPtr, mySQL_FieldLength)
This one works:

Code: Select all

Structure MYSQL_ROW
  field.s[255]
EndStructure
..but what happens if there are more than 255 fields?

I hope I was clear enough.
Any chance to implement this? - Would be great!
Thanks - sverson
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Array with no/open size in structures

Post by Trond »

Try this:

Code: Select all

Structure MYSQL_ROW
  *field.String[0]
EndStructure
I would strongly advice against using the type .s as this is strings managed automatically by PB. If your strings are managed by MySQL you could end up with some big trouble. Better make it an array of pointers to the structure String. Since they are pointers, PB won't reallocate or free the strings implicitly. Access the strings by reading variable\field[index]\s. Note: You can't assign anything to this string, as PB will try to free the original string, which should be managed by MySQL.
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Re: Array with no/open size in structures

Post by sverson »

Trond wrote:Try this:

Code: Select all

Structure MYSQL_ROW
  *field.String[0]
EndStructure
@Trond
I'm sure I tried this before - no success!
Now it works - on any string array size - strange :?

May be there was a little bug in my code :oops:

Thanks for making me try it again - it's exactly what I've been looking for.

EDIT (add):
It even works this way as I need to call peeks(*myRow\field[x]). [*myRow.MYSQL_ROW]

Code: Select all

Structure MYSQL_ROW
  *field[0]
EndStructure
Post Reply