It is currently Sun Jun 25, 2017 6:21 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Ascii-Strings in Data
PostPosted: Mon Jan 02, 2017 5:35 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Fri Apr 25, 2003 6:41 pm
Posts: 1315
Code:
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:
Data.i @a"one",@a"two",@a"three",0

would be nice :)

I need something like this to access lua:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Jan 02, 2017 6:06 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3099
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.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Jan 02, 2017 6:30 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Fri Apr 25, 2003 6:41 pm
Posts: 1315
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:
    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


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Jan 02, 2017 6:41 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3099
This fits your use case but what if someone else wants to do the same thing but with Unicode?

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Jan 02, 2017 7:07 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Fri Apr 25, 2003 6:41 pm
Posts: 1315
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:
        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...


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Jan 09, 2017 3:00 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 3784
Location: Lyon - France
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:
viewtopic.php?p=499404#p499404

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Jan 09, 2017 3:31 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 12947
Location: France
Why not using a static structure instead ? Using a datasection for this doesn't seems to be the good fit

Code:
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()


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Mon Mar 20, 2017 8:44 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Fri Apr 25, 2003 6:41 pm
Posts: 1315
In my opinion, there are static datas. I don't want to change them. And it is much more to write :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 11:12 am 
Offline
User
User

Joined: Fri Jan 08, 2010 11:04 pm
Posts: 98
Location: Hampshire, UK
@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:
; 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


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 12:45 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1415
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 3:37 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 2350
Location: Boston, MA
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:
[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]

_________________
First of all, you've got to unlearn everything you were never taught. ~ me
Any given program, when running, is obsolete. ~ Laws of Computer Programming, I


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 4:10 pm 
Offline
User
User

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 4:23 pm 
Offline
User
User

Joined: Fri Jan 08, 2010 11:04 pm
Posts: 98
Location: Hampshire, UK
Ok - stupid, stupid.

The assembler macro has the line
Code:
    dd label

For 64 bit this should (of course) be
Code:
    dq label

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 4:46 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 2350
Location: Boston, MA
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.

_________________
First of all, you've got to unlearn everything you were never taught. ~ me
Any given program, when running, is obsolete. ~ Laws of Computer Programming, I


Top
 Profile  
Reply with quote  
 Post subject: Re: Ascii-Strings in Data
PostPosted: Thu Mar 23, 2017 5:03 pm 
Offline
User
User

Joined: Fri Jan 08, 2010 11:04 pm
Posts: 98
Location: Hampshire, UK
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:
; 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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye