Ascii-Strings in Data

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Ascii-Strings in Data

Post 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
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Ascii-Strings in Data

Post 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.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Ascii-Strings in Data

Post 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
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Ascii-Strings in Data

Post by Mistrel »

This fits your use case but what if someone else wants to do the same thing but with Unicode?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Ascii-Strings in Data

Post 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...
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Ascii-Strings in Data

Post 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
ImageThe happiness is a road...
Not a destination
Fred
Administrator
Administrator
Posts: 16618
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Ascii-Strings in Data

Post 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()
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Ascii-Strings in Data

Post by GPI »

In my opinion, there are static datas. I don't want to change them. And it is much more to write :)
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Ascii-Strings in Data

Post 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
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Ascii-Strings in Data

Post 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
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Ascii-Strings in Data

Post 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]
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Ascii-Strings in Data

Post 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.
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Ascii-Strings in Data

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Ascii-Strings in Data

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Ascii-Strings in Data

Post 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
Post Reply