Page 1 of 2

Ascii-Strings in Data

Posted: Mon Jan 02, 2017 5:35 pm
by GPI

Code: Select all

Debug "unicode"
*adr.integer=?uni
While *adr\i
  Debug PeekS(*adr\i,-1,#PB_Unicode)
  *adr+SizeOf(integer)
Wend

Debug ~"\nascii"
*adr=?asci
While *adr\i
  Debug PeekS(*adr\i,-1,#PB_Ascii)
  *adr+SizeOf(integer)
Wend

DataSection
  uni:
  Data.i @"one",@"two",@"three",0
  
  s_one:   :Data.b "one"
  s_two:   :Data.b "two"
  s_three: :Data.b "three"
  asci:
  Data.i ?s_one,?s_two,?s_three,0
  
EndDataSection
something like this:

Code: Select all

Data.i @a"one",@a"two",@a"three",0
would be nice :)

I need something like this to access lua:

Code: Select all

Procedure OpenSpriteLib(lua)
  Register_lib(lua,"sprite",#spr_meta,?sprite_meta,?sprite_fuc)
  
  DataSection
    s__newindex: :Data.b "__newindex"
    s__index:    :Data.b "__index"
    s__gc:       :Data.b "__gc"
    s_new:       :Data.b "new"
    s_dispose:   :Data.b "dispose"
    sprite_fuc:   
    Data.i ?s_new, @sprite_new(),?s_dispose,@sprite_dispose(),0,0
    sprite_meta:  
    Data.i ?s__gc,@sprite_dispose(),?s__index,@sprite_index(),?s__newindex,@sprite_newindex(),0,0
  EndDataSection
  
EndProcedure

Re: Ascii-Strings in Data

Posted: Mon Jan 02, 2017 6:06 pm
by Mistrel
You have to encode the data as explicit bytes for your data section. Otherwise a "string" in PureBasic might be Ascii or Unicode depending on the compiler setting.

Re: Ascii-Strings in Data

Posted: Mon Jan 02, 2017 6:30 pm
by GPI
Mistrel wrote:You have to encode the data as explicit bytes for your data section. Otherwise a "string" in PureBasic might be Ascii or Unicode depending on the compiler setting.
That is the problem. With 5.51 there is no ascii/unicode compiler setting, it is always unicode. But it would be nice to have something like @"string" for ascii.
it would reduce my example to this:

Code: Select all

    sprite_fuc:   
    Data.i @a"new", @sprite_new(),  @a"dispose",@sprite_dispose(),  0,0
    sprite_meta: 
    Data.i @a"__gc",@sprite_dispose(),  @a"__index",@sprite_index(),  @a"__newindex",@sprite_newindex(),  0,0

Re: Ascii-Strings in Data

Posted: Mon Jan 02, 2017 6:41 pm
by Mistrel
This fits your use case but what if someone else wants to do the same thing but with Unicode?

Re: Ascii-Strings in Data

Posted: Mon Jan 02, 2017 7:07 pm
by GPI
Mistrel wrote:This fits your use case but what if someone else wants to do the same thing but with Unicode?
he simple writes:

Code: Select all

        sprite_fuc:   
        Data.i @"new", @sprite_new(),  @"dispose",@sprite_dispose(),  0,0
        sprite_meta:
        Data.i @"__gc",@sprite_dispose(),  @"__index",@sprite_index(),  @"__newindex",@sprite_newindex(),  0,0 
unicode is the default behaviour, because it is not possible to switch to ascii-mode in the compiler settings...

Re: Ascii-Strings in Data

Posted: Mon Jan 09, 2017 3:00 pm
by Kwai chang caine
At my advice.... my little finger say to me, you cannot have this option before several decennium :wink:

I took a spanking, they are no much time, for have talking nearly about the same subject :lol:
http://www.purebasic.fr/english/viewtop ... 04#p499404

Re: Ascii-Strings in Data

Posted: Mon Jan 09, 2017 3:31 pm
by Fred
Why not using a static structure instead ? Using a datasection for this doesn't seems to be the good fit

Code: Select all

Structure LuaSprite
  *new
  *newFunc
  *dispose
  *disposeFunc
EndStructure

Static luaSprite.LuaSprite\new = Ascii("new")
luaSprite\newFunc = @sprite_new()
luaSprite\dispose = Ascii("dispose")
luaSprite\newFunc = @sprite_dispose()

Re: Ascii-Strings in Data

Posted: Mon Mar 20, 2017 8:44 pm
by GPI
In my opinion, there are static datas. I don't want to change them. And it is much more to write :)

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 11:12 am
by bosker
@GPI - you can declare Ascii strings regardless of PB compiler mode if you are prepared to use assembler.
My example below shows the use of an assembler macro which creates an Ascii string table in the DataSection.
The asm macro creates a table of string addresses followed by the string data (similar to using @"string" in PB). I have it in my residents file ;-)

Most of the code is just proving that the string data IS in Ascii even though I used 5.50 to build this.
I know this doesn't exactly fit your data structures but I had the code to hand and the basics are there.

Code: Select all

; ASCII strings in data
EnableExplicit

; asm macro to create a string table
!macro strtbl [st]
!{
!  forward
!    local label
!    dd label
!  forward
!    label db st,0
!}


DataSection
LAscData:
!   strtbl "ascii string","another","and one more"
EndDataSection

Structure TPtr
    p.i[0]
EndStructure

Define.l i, j
Define.s s
Define.TPtr *Stable = ?LascData

OpenConsole()

; get the strings, convert and print
For i = 0 To 2
    PrintN(PeekS(*Stable\p[i], -1, #PB_ASCII))
Next

; get as bytes to prove they are ASCII
For i = 0 To 2
    j = *Stable\p[i]

    While PeekA(j) <> 0
        Print(Hex(PeekA(j), #PB_BYTE)+" ")
        j+1
    WEnd

    PrintN("")
Next

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 12:45 pm
by kenmo
I would like this too, ASCII (or UTF-8) strings in a Unicode program's DataSection.
When dealing with a lot of text, I end up placing it in a separate UTF-8 file and using IncludeBinary.

The best idea I've come up with, keeping PB-style compile syntax, is an "Enable" command something like this:

Code: Select all

DataSection
  Text:
  Data.s "These are"
  Data.i @" normal strings"

  AText:
  EnableASCIIData
  Data.s "These are now"
  Data.i @" ASCII encoded strings!"
  EnableUnicodeData ; or DisableASCIIData ?  

  ; maybe EnableUTF8Data too ?
EndDataSection

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 3:37 pm
by skywalk
bosker wrote:My example below shows the use of an assembler macro which creates an Ascii string table in the DataSection.
The asm macro creates a table of string addresses followed by the string data (similar to using @"string" in PB). I have it in my residents file ;-)

Most of the code is just proving that the string data IS in Ascii even though I used 5.50 to build this.
I know this doesn't exactly fit your data structures but I had the code to hand and the basics are there.
I have errors on Windows 10 pbv56 x64?
Without a linker file before running, I get a Polink error:
"Relocation type ADDR32 is invalid without /LARGEADDRESSAWARE:NO, for symbol '.data'."

With a linker file containing /LARGEADDRESSAWARE:NO
I get IMA...
Memory Viewer [?LAscData + 64] during IMA...

Code: Select all

[FS]åC[NULL]
)åC[NULL]
1åC[NULL]
ascii string[NULL]
another[NULL]
and one more[NULL]
À[DLE]D[NULL]
[NULL]
[NULL]
[NULL]
[NULL]
à[DLE]D[NULL]
[NULL]
[NULL]
[NULL]
[NULL]
è[DLE]

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 4:10 pm
by bosker
Dammit!
There should be a comment line at the top that says.. ; NOTE: 32 bit only
It's in my source file but I guess I chopped it off pasting. I haven't tried 64-bit.

My sincere apologies for that.

I'll try 64-bit and see what's up.

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 4:23 pm
by bosker
Ok - stupid, stupid.

The assembler macro has the line

Code: Select all

    dd label
For 64 bit this should (of course) be

Code: Select all

    dq label
Because addresses are quads. With that change it works for 64 bit.
I'll go fall on my coding pencil.

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 4:46 pm
by skywalk
Doh! I looked for the x64 equivalent but not hard enough.
The strings read fine now, but there is still an IMA in the byte retrieval if I omit the linker file containing /LARGEADDRESSAWARE:NO.

Re: Ascii-Strings in Data

Posted: Thu Mar 23, 2017 5:03 pm
by bosker
I'm really having a bad day.
The byte retrieval fails because the version I posted declares i and j as Long.
My source (naturally) already has i and j untyped (integer).

That should be Integer or defaulted.
I owe you several gallons of e-beer to make up for the cr?p.
To make sure we have the same source, here's the whole thing again for 32 and 64 bit (I hope).

Code: Select all

; ASCII strings in data
EnableExplicit

; asm macro to create a string table
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
!macro strtbl [st]
!{
!  forward
!    local label
!    dq label
!  forward
!    label db st,0
!}
CompilerElse
!macro strtbl [st]
!{
!  forward
!    local label
!    dd label
!  forward
!    label db st,0
!}
CompilerEndif

DataSection
LAscData:
!   strtbl "ascii string","another","and one more"
EndDataSection

Structure TPtr
    p.i[0]
EndStructure

Define i, j
Define.s s
Define.TPtr *Stable = ?LascData

OpenConsole()

; get the strings, convert and print
For i = 0 To 2
    PrintN(PeekS(*Stable\p[i], -1, #PB_ASCII))
Next

; get as bytes to prove they are ASCII
For i = 0 To 2
    j = *Stable\p[i]

    While PeekA(j) <> 0
        Print(Hex(PeekA(j), #PB_BYTE)+" ")
        j+1
    WEnd

    PrintN("")
Next