[Implemented] Enhance Command: ReadString()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

[Implemented] Enhance Command: ReadString()

Post by Akuma no Houkon »

Coming from a PowerBasic / VisualBasic background, I have *hundreds* of places where I need to read a string from a file that is NOT null terminated. I need to read a specfic amount of bytes into the string. In VB and PowerBasic (and almost every dialect of basic I have ever seen) you would simply go:

MyString = Space(LengthOfString)
Get #1, , MyString

and MyString will be filled with bytes from the file upto its current size. If its 5 characters in length, the Get command will read 5 characters into the string keeping it its original size.

Now in purebasic as far as I can tell there isnt a simple way to do this

MyString.s = Space(LengthOfString)
MyString = ReadString()

Doesnt work. It gets until the next null character no matter what.

Please either add an enhancement (an optional paramater) to specify the number of bytes that ReadString() will get, or add a new function that is like ReadString() but with properties like ReadData() .

many years of basic programing experiance and that is my two cents.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Sounds like ReadData is exactly what you want...

Code: Select all

*ptr_my_str = globalalloc_(LengthOfString) ; Could use AllocateMemory here too

ReadData(*ptr_my_str,LengthOfString)

MyString.s = peeks(*ptr_my_str)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

That would require keeping pointers and the length of everystring, thats alot of extra work, 10 lines to the 2 it would be otherwise.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

That is what you have to do if you want to manipulate a file in a string... it's not that hard.. Why don't you make a function of it, and use that in you program... It's not hard at all..

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

10 lines? I only see 3 :-)

Yes you have to keep track of string lengths but you were doing that in your example anyway. You could reuse the same pointer as long as you copy the memory out to a string variable (as in the example with peeks()).. So your 2 lines becomes 3.. It is a *little* harder and a generic read() function that takes a string as an argument instead of a pointer isn't a bad request at all.. I wouldn't want readstring() modified though, it does exactly what it's supposed to now.

Have fun!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

10 lines? I only see 3
Dont play that game, you know better than that. He obviously needs to set and store the pointers, the length of the string etc... (with a function like he is posting) and error trap the eventuality of a zero length string being asked for (if its going to be a function), set return values, the function paramaters to set it all up. ALOT more work that simply using an OPTIONAL paramater in ReadString() or even a separate command ReadStringEX()

Since when did this part of the forum become an argument about why you think its a good idea or not? I thought it was for us to post what we think will be good additions (10 years of programing in the basic dialect tell me this will be a good addition)

I didnt say it was hard, I have a simple function that does that right now. I was saying, a simple optional paramater in the ReadString() function, nothing to difficult, will make this much easier. PureBasic seems to have alot of the more advanced functions built in that most languages require you to write (one of its better features), but even the simplest of function that most languages alow without using pointers and peek and malloc, etc...(such as reading a specified amount of characters from a file into a string) have to be written manually, its kind of contridictory.
Fred wrote: I could add a parameter to readstring, it seems ok to me.

Good bye,

--
Fred.
I like PureBasic and want it to grow more as a programing language this is my Feature Request that I think will help it grow and become a better, more rounded programing language.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Hell man, I'm not arguing with you - this is a DISCUSSION forum. I was only trying to help you out with an example of how to do what you wanted with the currently available PB commands.. The 10 line comment was just me joking around - no need to get all snappy!

I agreed with you (and your 10 years of experience) that it was a good addition, I didn't think it was a good thing to modify the behavior of ReadString. Just my opinion, it's not like it matters anymore than yours does :-)

No offense intended by anything here, I was just discussing and trying to provide you a way to accomplish what you wanted to accomplish...
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

I wasnt snappy, it may have "sounded" so, but its hard to convey the proper emotion online.

I wouldnt ever ask to modify the existing behavior of a function, that would break or ruin many existing projects. Instead I asked for either an optional paramater or failing that a separate function to fullfill this. So as to add, without changing. :)
No offense intended by anything here, I was just discussing and trying to provide you a way to accomplish what you wanted to accomplish...
And I appreciate that, although I already have a function that I call ReadStringEX() which does the very same thing I am asking for, so it wasnt really a matter of trying to do something, as a "it would be a good addition to an already great langauge"
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

it would be a good addition to an already great langauge
Indeed! :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

Also a nice addition to the FindString(), Allow for a NEGATIVE position, which will search the string backwards. VB has this in the form of InstrRev() and PowerBasic have it in the form of a negative position on their Instr() function.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Post by Doobrey »

Karbon wrote:Sounds like ReadData is exactly what you want...

Code: Select all

*ptr_my_str = globalalloc_(LengthOfString) ; Could use AllocateMemory here too
ReadData(*ptr_my_str,LengthOfString)
MyString.s = peeks(*ptr_my_str)
What about this?
It cuts down on having to track allocated memory etc..
I think this is what the original poster wanted(copying data to an existing string)

Code: Select all


 CopyMemory(someaddress.l,@mystring,len(mystring))

 
If only PB had macros :cry:
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

I wanted to read from a file Into an existing string.

I can do it, its not a problem. I didnt post to ask for help, I have a function that does that all for me. I was suggesting a simple optional paramater which will make it to where others (and even me down the road) do not have to do such simple things manually.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Akuma: Could you perhaps post your procedure that does this?
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

Sure

This assumes you already have the file open

Code: Select all

Procedure.s ReadStringEX(length.l)
  If length > 0
    AllocateMemory(0, length+1) 
    ReadData(MemoryID(), length)
    ProcedureReturn = PeekS(MemoryID())
  Else
    ProcedureReturn = ""
  EndIf
EndProcedure
Use it like

MyString.s = ReadStringEX(5)
;where 5 is the length of the string.

It checks length to make sure you are not trying to read 0 bytes, but doesnt check to make sure you dont read past the EOF, that might be a nice check to add, but for my purposes checking the 0 byte boundry is enough for me.
Last edited by Akuma no Houkon on Wed Nov 05, 2003 2:13 am, edited 1 time in total.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Thank you :)
Post Reply