Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: New line in sf::String  (Read 6755 times)

0 Members and 1 Guest are viewing this topic.

Kian

  • Newbie
  • *
  • Posts: 44
    • View Profile
New line in sf::String
« on: May 06, 2012, 11:36:11 pm »
I'm sure this has been answered a hundred times, but the search function couldn't help me. Terms are too generic I guess.

I'm wondering how to stick the "new line" code in sf::String. I'm currently creating strings from ANSI literals; sf::String string ("The text"); but if I use the new line character '\n' it doesn't seem to be converted or stored. What's the proper way to do this?

minirop

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • http://dev.peyj.com
Re: New line in sf::String
« Reply #1 on: May 07, 2012, 05:35:40 am »
\n and \t work perfectly, you have to put the right code, 0x0a not just "\n" won't work. I need it too, so I made this function :
Code: [Select]
sf::String evaluateSpecialChars(sf::String string)
{
sf::String tmp;

std::size_t i = 0;
while(i < string.getSize())
{
sf::Uint32 c = string[i];

if(c == '\\')
{
i++;
sf::Uint32 n = string[i];

switch(n)
{
case '\\':
tmp += n;
break;
case 'n':
tmp += '\n';
break;
case 't':
tmp += '\t';
break;
default:
std::cerr << "Error: invalid special char found : " << c << n << std::endl;
}
}
else
tmp += c;

i++;
}

return tmp;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: New line in sf::String
« Reply #2 on: May 07, 2012, 08:13:55 am »
\n works, how do you see that it doesn't work?

Quote
I need it too, so I made this function
This function is useful if you want to parse a text that contains escaped characters, but I don't see its usefulness:
- if the text is in the code, the compiler already interprets escaped characters
- if the text is in a text file or whatever, you can just use a true line break, there's no need to write C++ escaped sequences
Laurent Gomila - SFML developer

minirop

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • http://dev.peyj.com
Re: New line in sf::String
« Reply #3 on: May 07, 2012, 09:19:14 am »
\n works, how do you see that it doesn't work?
I just mixed up everything :-X . I'm on a project where the text is in a file and must be on 1 line.

Kian

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: New line in sf::String
« Reply #4 on: May 07, 2012, 06:06:48 pm »
Let me check something, be right back.

...

Hmm. The problem is apparently with Notepad, everything else shows it correctly.

What I was doing was creating an sf::String with the constructor that takes a string literal, then using the fstream write method to write the string to a text file. The relevant code:

Code: [Select]
void Log::write(sf::String const& line)
{
    logfile.write( line.toAnsiString().data(), line.getSize() );
    // logfile is an fstream object pointed to a file opened with the trunc, output and binary flags
}

Log log;

log.write(sf::String("1st line\n\n2nd Line");

The problem I was seeing was that notepad put squares where the new lines should be. Copying to the browser's textbox however added new lines. And opening in a more versatile text editor (TextPad) displayed the new lines as well.

I should just delete notepad.exe .

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: New line in sf::String
« Reply #5 on: May 07, 2012, 07:45:12 pm »
Quote
I should just delete notepad.exe
Don't blame the tools (*) ;)

You're writing to a binary file, which means that your \n is written directly instead of being converted correctly to your OS line endings. On Windows, line endings are supposed to be \r\n so many tools won't understand a \n alone.
Fortunately, tools tend to get better and interpret other line endings correctly.

(*) ... but yeah, get a better text editor anyway
Laurent Gomila - SFML developer

Kian

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: New line in sf::String
« Reply #6 on: May 07, 2012, 08:29:57 pm »
I keep forgetting about that whenever I work with files. Thanks.