Subject:
|
Re: how does a line ends?
|
Newsgroups:
|
lugnet.cad
|
Date:
|
Sat, 7 Apr 2007 21:55:11 GMT
|
Viewed:
|
1757 times
|
| |
| |
In lugnet.cad, Travis Cobbs wrote:
|
OK, as promised, here it my fgets replacement:
|
That was quick! Thanks for taking the time to indulge me...
|
char *myFgets(char *buf, int bufSize, FILE *file)
{
int i;
for (i = 0; i < bufSize - 1; i++)
{
int char1 = fgetc(file);
if (feof(file))
{
buf[i] = 0;
if (i > 0)
{
return buf;
}
else
{
return NULL;
}
}
if (char1 == '\r' || char1 == '\n')
{
int char2 = fgetc(file);
buf[i] = '\n';
buf[i + 1] = 0;
if (!feof(file))
{
if (!(char1 == '\r' && char2 == '\n' ||
char1 == '\n' && char2 == '\r'))
{
ungetc(char2, file);
}
}
return buf;
}
buf[i] = (char)char1;
}
buf[bufSize - 1] = 0;
return buf;
}
It seems to work, but Im not 100% confident in its lack of bugs. Timing
with 8464.mpd results in about 550ms for loading using fgets and 750ms using
myFgets from above. On the one hand, thats a 50% slowdown based purely on
that one change. On the other hand, 750ms isnt very long, and while
8464.mpd isnt exactly a huge file, its big enough to prove your point that
the performance is fine.
|
It is interesting that it was that much time difference. Are these the total
load times, or just profiling of the time spent inside the fgets() routines? I
see one or two things you might tighten up in this code, but nothing thats
going to make it 50% faster.
I do wonder how this code compares to the stdlib fgets() implementation that you
are using. Did you modify the original fgets, or did you write this one from
scratch? Any chance you could post the original fgets code for comparison?
|
At this point, Im much more concerned about any possible bugs in the code,
though. I have a fairly high confidence level that fgets does what its
supposed to do in all possible cases. I dont have the same confidence level
for the myFgets function above.
|
This code seems to work for me, and passes a quick visual read-through, but I
know how you feel.
|
|
Message has 2 Replies: | | Re: how does a line ends?
|
| (...) I timed the file reading/parsing portion of the model load. It would have been more difficult to time the actual amount of time spent inside fgets/myFgets, so realistically it's likely that myFgets is even slower in comparison to fgets. The (...) (18 years ago, 7-Apr-07, to lugnet.cad, FTX)
| | | Re: how does a line ends?
|
| (...) Quick question. I was in a hurry, and wanted to play it safe, so I call feof instead of checking to see if char1 == EOF. Any idea what gets returned for extended ASCII characters (>= 128)? Does the int contain positive numbers greater than (...) (18 years ago, 7-Apr-07, to lugnet.cad, FTX)
|
Message is in Reply To:
| | Re: how does a line ends?
|
| (...) OK, as promised, here it my fgets replacement: char *myFgets(char *buf, int bufSize, FILE *file) int i; for (i = 0; i < bufSize - 1; i++) int char1 = fgetc(file); if (feof(file)) bufi = 0; if (i > 0) return buf; else return NULL; if (char1 == (...) (18 years ago, 7-Apr-07, to lugnet.cad, FTX)
|
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
|
|
|
|