Raised This Month: $51 Target: $400
 12% 

Solved ReadLine returns LF


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 03-09-2019 , 08:51   ReadLine returns LF
Reply With Quote #1

Hi,

PHP Code:
void UpdateList()
{
    
char sPath0[PLATFORM_MAX_PATH] = "valentine/file.mp3";
    
char sPath1[PLATFORM_MAX_PATH];
    
File hFile OpenFile("addons/sourcemod/data/music_mapstart.txt""r");

    while( !
hFile.EndOfFile() && hFile.ReadLine(sPath1sizeof(sPath1)) )
    {
        break; 
// read first line only
    
}
    
    
PrintToServer("item 0: %s"sPath0);
    
PrintToServer("item 1: %s"sPath1);
    
    
PrintToServer("Str equal? %b"StrEqual(sPath0sPath1));
    
PrintToServer("len[0]: %i, len[1]: %i"strlen(sPath0), strlen(sPath1));
    
    
// print codes of the last characters of each buffer
    
PrintToServer("asc[15]: %i, asc[16]: %i, asc[17]: %i, asc[18]: %i"sPath0[15], sPath0[16], sPath0[17], sPath0[18] );
    
PrintToServer("asc[17]: %i, asc[18]: %i, asc[19]: %i, asc[20]: %i"sPath1[17], sPath1[18], sPath1[19], sPath1[20] );

Quote:
item 0: valentine/file.mp3
item 1: valentine/file.mp3

Str equal? 0
len[0]: 18, len[1]: 20
asc[15]: 3371117, asc[16]: 13168, asc[17]: 51, asc[18]: 0
asc[17]: 658739, asc[18]: 2573, asc[19]: 10, asc[20]: 0
How is it possible non-binary file function return LF ?
Is 2573 mean CR? Why so strange code?

What kind of encoding is it - "13168" (0x3370) ? "p" character has no 2-byte analogue, afaik.

sm v.1.9
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 04-09-2019 at 06:57.
Dragokas is offline
adma
Senior Member
Join Date: Oct 2015
Old 03-10-2019 , 22:15   Re: ReadLine returns LF
Reply With Quote #2

I don't quite understand the problem, but when I have needed to read lines from a file I have always used TrimString before doing anything with the string retrieved by File.ReadLine(). e.g.

PHP Code:
while (!hFile.EndOfFile() && hFile.ReadLine(sBuffersizeof(sBuffer))) {
  
TrimString(sBuffer);
  
hArray.PushString(sBuffer);

adma is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 03-11-2019 , 03:30   Re: ReadLine returns LF
Reply With Quote #3

adma, problem is: ReadLine read to buffer line break character. So such line can't be used by e.g. EmitSound.
TrimString can't remove line breaks. It is intended for white space character only.
Non-binary functions should not return such character at all. For that kind of stuff Read() function is intended.

Second problem: why ASCII code of individual character of string have such strange code number.
51 - is a correct code of "3" character (last character of "valentine/file.mp3" string). 13168 - don't know, what is it.

UPD. I simplified code in 1-st post to reflect the problem as simple as possible.

I found this walkaround in "sm downloader" plugin:
PHP Code:
        len strlen(buffer);
        if (
buffer[len-1] == '\n')
            
buffer[--len] = '\0'
But, actually I don't need such check. It is a bug in ReadLine() function.
sm v.1.10.6361 is also affected.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 03-11-2019 at 04:00.
Dragokas is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 03-11-2019 , 12:52   Re: ReadLine returns LF
Reply With Quote #4

PHP Code:
ReplaceStringEx(buffersizeof(buffer), "\n""\0"); 
yes, file.ReadLine reads new line characters.
__________________
Ilusion9 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-11-2019 , 16:41   Re: ReadLine returns LF
Reply With Quote #5

Line breaks are whitespace.
__________________
asherkin is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 03-12-2019 , 10:29   Re: ReadLine returns LF
Reply With Quote #6

asherkin, thanks, I see. I checked. It's definitely trim them.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 03-12-2019 at 10:29.
Dragokas is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 04-09-2019 , 06:57   Re: ReadLine returns LF
Reply With Quote #7

Quote:
Originally Posted by Dragokas
Code:
PrintToServer("asc[15]: %i, asc[16]: %i, asc[17]: %i, asc[18]: %i", sPath0[15], sPath0[16], sPath0[17], sPath0[18] );
Is 2573 mean CR? Why so strange code?

What kind of encoding is it - "13168" (0x3370) ? "p" character has no 2-byte analogue, afaik.
Ok, solved. Instead, it should be:

Code:
int c1, c2, c3, c4;
c1 = sPath0[15];
c2 = sPath0[16];
c3 = sPath0[17];
c4 = sPath0[18];

PrintToServer("asc[15]: %i, asc[16]: %i, asc[17]: %i, asc[18]: %i", c1, c2, c3, c4 );
Now, codes are printed correctly.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 04-09-2019 , 07:18   Re: ReadLine returns LF
Reply With Quote #8

P.S. Possibly some problem or specific of sourcemod.
C++ handles both cases identically.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:30.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode