RE: Follow up: Are metainfo (.torrent) files text files ? ;)
"Marcel Popescu" <[email protected]>
| Newsgroups | gmane.network.bit-torrent.general |
|---|---|
| Message-ID | <[email protected]> |
> From: skybuckflying [mailto:[email protected]] > I think this is quite dangerous since some language like delphi and C > use char(0) to indicate the end of a string. Delphi does no such thing. On the other hand, you can be excused for believing this, given that even some of those at Borland who wrote the Delphi VCL made this mistake... Delphi appends a Char(0) at the end of the string so that you can pass a C-style string to Windows API functions easily, using PChar (s). But Delphi knows the end of the string because it prepends its length to it - it doesn't use the Char(0) to determine it. (Again, excluding the functions written by the idiot who confused the two.) > Fortunately delphi's native string type works with "length prefixes". > > But it could also be typecasted to a Pchar which works like a null > terminated character. Sorry, I should have read some more :) > Fortunately for me... I code in delphi and I will prevent to usage of > Pchars in my "soon to be" TmetaInfo class ;) > The funny thing is I actually found a bug in Delphi's TstringList > > It tries to turn the string that it read into multiple strings when > #0, #10 or #13 is encountered Not entirely a bug, wrt #10 and #13 those are the specs. Don't use TStringLists, use a simple string. > It starts with a while loop that checks for #0... if it encounters > this character it simply stops... apperently the borland coder didn't > expect the first character to be #0 and the rest to be text :) As I said, some Borland programmers didn't think straight when it comes to Char(0). > My final conclusion and advice is: don't try processing > torrent/metainfo files as text files with Delphi ;) and possibly > other languages... :D Wrong, Delphi's just fine, as long as you don't use the buggy parts. Use streams: This is what I'm using to read files of any kind - it has no problem with #0s: function LoadFile (const pFileName: string): string; var str : TFileStream; begin Result := ''; str := TFileStream.Create (pFileName, fmOpenRead or fmShareDenyNone); try SetLength (Result, str.Size); str.Read (Result [1], Length (Result)); finally str.Free; end; end; Marcel ------------------------ Yahoo! Groups Sponsor --------------------~--> $9.95 domain names from Yahoo!. Register anything. http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/dkFolB/TM --------------------------------------------------------------------~-> Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/BitTorrent/ <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/