Unsure what this syntax is doing
BlitzMax Forums/BlitzMax Beginners Area/Unsure what this syntax is doing
| ||
In my quest to find a way to create a "stringstream" I popped open stream.bmx and came across the following function. Function OpenStream:TStream(url:Object, readable = True, writeable = True) Local stream:TStream=TStream( url ) If stream Return TStreamStream.Create( stream ) EndIf Local str$=String( url ),proto$,path$ If str Local i=str.Find( "::",0 ) If i=-1 Return TCStream.OpenFile( str,readable,writeable ) proto$=str[..i].ToLower() path$=str[i+2..] EndIf Local factory:TStreamFactory=stream_factories While factory Local stream:TStream=factory.CreateStream( url,proto,path,readable,writeable ) If stream Return stream factory=factory._succ Wend End Function The line where the tstream is being declared Local stream:TStream=TStream( url ) In any other language I would say this is a constructor, but I was under the impression BlitzMax didn't have constructors. What is this doing? Also, is there a clean way of creating a string stream? I'm reading all over the documentation and for some reason I'm not putting it together. http://en.wikibooks.org/wiki/BlitzMax/Modules/Streams/Text_streams seems to imply it can be done, but I need a stream first and I don't know how to create one out of thin air. What I'm wanting to do is the following: - create a MemoryStream - copy a string to that MemoryStream so I can peak/poke at the individual characters (ascii charset only) - copy byte ranges out to another MemoryStream - When all is said and done, load the resulting MemoryStream into a String I'm about to just create multiple byte arrays and then just downsize at the end as needed, but that seems inefficient. Any ideas? |
| ||
I just found http://en.wikibooks.org/wiki/BlitzMax/Modules/Brl#Streams I'm unsure how I managed to miss it. I cracked open the source for it and it apparently uses the byte ptr directly, so it expects you to allocate it beforehand. I'm guessing this means I have to allocate a byte array and then call CreateRamStream, which implies to me that the only real benefit from doing so is just getting the convenience methods of a stream. I was kind of hoping for a dynamically re-allocated stream ala C++ type containers, but I suppose what's there is good enough. Is there anything I'm missing or is this most likely the best way to accomplish this? |
| ||
About the syntax... Function OpenStream:TStream(url:Object, readable = True, writeable = True) Local stream:TStream=TStream( url ) From the first line you can see that OpenStream is a function which returns a TStream. So TStream must be a user defined type. A look at the code shows it is in the stream module. You can also see that url is an Object. See http://www.blitzbasic.com/bmdocs/command.php?name=object&ref=goto for a brief description of Object. In the second line stream is a variable of type TStream and TStream( url ) is a typecast from Object to TStream. Notice that user defined types have names beginning with T. That's just a convention, not enforced in any way. |
| ||
ah, typecast, got it. |
| ||
If your just gonna read a single string, TRamStream and the pointer returned from String.ToCString() or String.ToWString() should work. If you need a dynamic one heres one i whipped up in a few minutes, so you should probably debug it somewhat ;) |
| ||
Thanks for the example code. I ended up just using Byte arrays and pointers directly, but this gives me something more to look into. |