Subject:
|
Re: how does a line ends?
|
Newsgroups:
|
lugnet.cad
|
Date:
|
Fri, 6 Apr 2007 23:34:28 GMT
|
Viewed:
|
1510 times
|
| |
| |
In lugnet.cad, Michael Heidemann wrote:
|
In the LDraw File Format (http://www.ldraw.org/Article45.html) it is
written that LDraw files are plain text. and Each line in the file is
a single command..
Also in the LDraw.org File Format Version 1.0.0
(http://www.ldraw.org/Article218.html) there is no more specification.
I found some parts that do not have a carriage return and line feed
to determine the end of the line.
They have instead only a line feed.
So it is not easy for a programmer to deal all possiblities. It would be
great if we can agree to define a line as text with carriage return
and line feed at the end.
Any suggestion or comments on this?
I like to have a statement about this also in the File Format (see above).
|
There was some discussion about this issue recently. Unfortunately, this is not
the kind of problem that can be solved by tightening up the spec. The problem
is that people use different text editors on different operating systems to
create LDRAW files, resulting in files that do not have consistent line
termination. Changing the specification will not fix all the files that are
already wrong, and it wont fix all the possible text editors, either.
A better solution, in my opinion, is for all tools to be able to handle any
combination of LF or CR characters as a proper line feed. (And the spec should
spell this out.) This is not all that hard to do, and it makes life easier for
your users. The amount of programming effort to code defensively is probably
less than the effort just one of your users will go through if they have to work
around this problem.
When reading a file, either a CR or a LF character should be recognized as the
end of a line. At that point, the program can peek at the next byte in the
input stream to see if it is a paired LF/CR, and discard the character if it is.
The following is not the most elegant code, and Im typing it from memory
without testing to see if it works, but this is how I handle linefeeds in a
completely platform-independent manner:
// Read a line of text from a file, return count of characters in buffer:
int readLine(FILE* f, char* lineBuffer, int nBufferSize)
{
char c = 0x00;
int n = 0;
bool eol = false;
while ((!feof(f)) && (!eol))
{
c = getc(f);
if (c == 0x0D) // We have a CR
{
c = getc(f); // Is the next character a LF?
if (c != 0x0A)
ungetc(f, c); // No, so put it back.
eol = true;
}
else if (c == 0x0A) // We have a LF
{
c = getc(f) // Is the next char a CR?
if (c != 0x0D)
ungetc(f, c);
eol = true;
}
else if (n < (nBufferSize - 1))
{
line[n++] = c;
}
}
lineBuffer[n] = '\0';
return n;
}
In the case of LDRAW files, you could even simplify this code further, since
there is no harm in discarding empty lines. (Unless, of course, you need to
count lines in order to print error messages.) In that case, you could simply
discard any extra CRs or LFs that follow the first one encountered.
Hope this helps!.
|
|
Message has 1 Reply: | | Re: how does a line ends?
|
| (...) I don't fully agree with this. In the nearly seven years since I released LDView 0.1, I've never once run into an LDraw file that LDView couldn't handle due to line terminations, and it requires the newline character to be present at the end (...) (18 years ago, 7-Apr-07, to lugnet.cad, FTX)
|
Message is in Reply To:
| | how does a line ends?
|
| In the LDraw File Format ((URL) it is written that "LDraw files are plain text." and "Each line in the file is a single command.". Also in the LDraw.org File Format Version 1.0.0 ((URL) there is no more specification. I found some parts that do not (...) (18 years ago, 6-Apr-07, to lugnet.cad)
|
24 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|