Article View: comp.sys.mac.programmer.codewarrior
Article #1050Re: CW "Map newlines to CR" chaos
From: meeroh
Date: Thu, 07 Mar 2002 04:25
Date: Thu, 07 Mar 2002 04:25
80 lines
3800 bytes
3800 bytes
In article <3C86BC4F.9930D425@vernier.com>, Christopher Corbell <ccorbell@vernier.com> wrote: >It seems that newlines-to-CR does odd things, >and even if unchecked some odd things can happen. well... no, not really odd... but possibly not the thing you might expect until it's explained to you :-) First, what does 'map newlines to cr' mean? It means that when that setting is on and you write '\n', the compiler generates a CR character, and when you write '\r', the compiler generates a LF character. Normally, Mac OS newlines are CR. Also, std::endl is the same as '\n'. Finally, when you are writing to a file which is opened in text mode using the standard C library, any LF characters are converted to CR, and vice versa, if you are using the default MSL. (See buffer_io.c) So, on to explain what you see. >With newlines-to-CR off, writing a std::endl >to an fstream writes '\r' std::endl is '\n', '\n' is LF, LF is converted to CR when written to the actual file. >but writing std::endl >to a stringstream writes '\n'. std::endl is '\n', '\n' is LF. >However writing >a std::string which contains carriage returns to >an fstream translates '\r' to '\n'!!! Writing to a text file (which is what an fstream is by default) converts LF to CR. >With newlines-to-CR on, I noticed a really bizarre >thing: writing a literal '\r' to an fstream produces >a '\n' in the file. That's because now your '\r' generates an LF, and LF is sent to the file >Which isn't map-newlines-to-CR, >it's map-CR-to-newline! > >Is all of this documented somewhere? I searched >the manuals for "newline" and nothing came up. > >What I'd really like is to have std::endl always >be the same thing regardless of whether it's >going to an fstream or a stringstream, and >also have '\r' in code always mean '\r' in >the byte and its subsequent i/o, and sim. for >'\n' to always mean '\n', with no translation >other than what the programmer does explicitly. >If this is possible in MSL C++, what does >it involve other than the newlines-to-CR setting? Why do you want that? The underlying principle here is simple: the representation of a newline in memory is a LF character, and on disk is a CR character. If you use text mode to open files (which fstream does by default), the conversion is done on the way from memory to disk and back. However, if you want to do it, all you have to do is use an MSL compiled with no CR/LF conversion, which would be a MSL C PPC (NL), and you can compile it from the MSL project. But, I really think that if you are trying to do that, you are probably doing something wrong, which is why I would like to know why you think it's crucial to you to make the two match. hth meeroh -- Hire me: <http://meeroh.org/hire.html>
Technical Details
Message-ID:
<macdev-7930E8.23250006032002@senator-bedfellow.mit.edu>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsswitch.lcs.mit.edu!bloom-beacon.mit.edu!senator-bedfellow.mit.edu!dreaderd!not-for-mailReferences:
<3C86BC4F.9930D425@vernier.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10bcce,1e3721ce480a395b X-Google-Attributes: gid10bcce,public X-Google-Thread: f635e,1e3721ce480a395b X-Google-Attributes: gidf635e,public X-Google-ArrivalTime: 2002-03-06 20:25:01 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsswitch.lcs.mit.edu!bloom-beacon.mit.edu!senator-bedfellow.mit.edu!dreaderd!not-for-mail From: meeroh <macdev@meeroh.org> Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac Mail-Copies-To: poster Subject: Re: CW "Map newlines to CR" chaos Organization: MIT References: <3C86BC4F.9930D425@vernier.com> User-Agent: MT-NewsWatcher/3.1 (PPC) Date: Wed, 06 Mar 2002 23:25:00 -0500 Message-ID: <macdev-7930E8.23250006032002@senator-bedfellow.mit.edu> Lines: 80 NNTP-Posting-Host: NEON-20.MIT.EDU X-Trace: 1015475098 senator-bedfellow.mit.edu 3929 18.101.0.201