MemoryBlock to string

I finally found the reason why so many people use MemoryBlock: HTTPSocket doesn’t return a String.
I don’t use the web. I only Desktop.
I am learning from "Designing a Recent Items Submenu, By Charles Yeomans " because it is also a great teaching tool for classes.
I don’t enough about memoryBlock or BinaryStream to translate this into String.
What this method does is translate a String from a text file ( recentItems folderitems as …???) into memoryBlock and also out.
I understand nothing from the assigns to why it is doing.
Actually I understand one thing. It is an effective replacement for a computed property. I am including the “Public Function Data() as String” for reference and I have successfully changed just that piece.

Please give me some clues about how to turn this into String code.

[code]Public Sub Data(Assigns data as String)
dim start as Integer
dim m as MemoryBlock
dim aliasData as String
dim f as FolderItem

m = NewMemoryBlock(4)
start = 1
While start < LenB(data)
m.StringValue(0, 4) = MidB(data, start, 4)
aliasData = MidB(data, start + 4, m.Long(0))
f = GetFolderItem(aliasData)
If f <> nil and f.Exists then
me.Add f
//file is gone
End if
start = start + 4 + LenB(aliasData)
End Sub

Public Function Data() as String
dim output as String
dim m as MemoryBlock
dim i as Integer
dim aliasData as String

m = NewMemoryBlock(4)
For i = UBound(me.RecentItemsQueue) DownTo 0
aliasData = me.RecentItemsQueue(i).File.GetSaveInfo(nil, 0)
m.Long(0) = LenB(aliasData)
output = output + m.StringValue(0, 4) + aliasData
Return output
End Function

you can use binary stream to read a file, get all data in a string.
Than assign string to memory block for easy conversion.

Thanks. I neglected or avoided adding another piece of information.
Somewhere in the code it writes recentItems.txt file, as part of the close operation, with some I don’t know stuff.
I suspect it’s Apple’s code for a SpecialFolder etc, but I don’t know
The only piece mentioned in that code is data. Yes, it’s on a Mac and the last part is NativePath.
This is the output of the file.


җsMacintosh HD
?!H+/RecentItemsTexth^vz??? Documents
a{./ge=Macintosh HD:Users:arthurgabhart:Documents:RecentItemsText RecentItemsTextMacintosh HD-Users/arthurgabhart/Documents/RecentItemsText/??/Users/arthurgabhart/Documents/>/Users/arthurgabhart/Documents/Korean Language/4 Guardians.txt[/quote]

I just realized that this method(s) could be the cause of me getting weird characters. I cannot figure out how to change the “File” method to nativePath.

[code]Public Sub Constructor(f as FolderItem)
me.Alias = f.NativePath//GetSaveInfo(nil, 0)
End Sub

Public Function File() as FolderItem
Return GetFolderItem(me.Alias)
End Function

MacOS has the concept of SaveInfo, which describes the location of a file independent of its fixed path. It allows you to move the file and still find it again using the stored SaveInfo information, which would not be possible if you just saved the path alone. What Charles is doing is saving that information in a binary file by storing the length of the SaveInfo followed by the info itself, followed by the length of the next info, followed by that info. So to read it back, you read in the length and then read that many bytes, then read the next length, etc. MemoryBlock has accessor functions that facilitate this. That’s the only reason to choose it over String. Using a string in this context will just be harder. Are you sure you want to do that?

Actually yes. The files I use are all verified and must in certain folders, so I CAN use a partial path. SaveInfo would be the cause, and I thought I’d gotten rid of them. Thanks.
Also thanks for explaining why MemoryBlock is used. Kind of figured that when I saw HTTPSocket. I’m not closing it in case I have more questions.
Do you know of good explanations of MemoryBlock and Binary stuff?

What does MemoryBlock have to do with HTTPSocket? Are you thinking of the new framework? In the Classic Desktop framework, HTTPSocket uses String.

That’s really a case by case thing. There’s a lot of ways you can use them. The common theme is either you know how many bytes to expect, as Charles is doing, or you use some kind of delimiter that is guaranteed to not be part of the data to mark the end of the data. PString does the former, CString does the latter.

Actually I’m looking for anything. The code I have is a good starter. I remember articles on XDEV mag but when I did a search it was in practically every issue. No surprise, just harder to find.

If you want to use String in this case, use EndOfLine as a delimiter.

Public Sub Data(Assigns data as String)
   dim arr() as String
   dim i as Integer
   dim f as FolderItem

   arr = split(data, EndOfLine)
   for i = 0 to ubound(arr)
         f = GetFolderItem(arr(i))
         if f.Exists then
            me.Add f
            // file is gone
      Catch e as NilObjectException
         // path is gone
      End Try
End Sub

Public Function Data() as String
   dim output as String
   dim i as Integer

   For i = UBound(me.RecentItemsQueue) DownTo 0
      output = output + me.RecentItemsQueue(i).File.NativePath + EndOfLine
   return Trim(output)
End Function

Thanks. I didn’t realize “path is gone” would be NilObject. Makes sense