On (27 Jul 97) Cameron Clark wrote to Darin Mcbride...
CC> You've got it down to 3(logical) check per character.
CC> Here's what I eventually came up with:
CC> void Token::getVariable() {
CC> char c;
CC> do {
CC> inf->get(c);
CC> if (inf->gcount() != 1) break;
CC> if (cur>=30) break;
CC> if (!isMyChar(c)) {inf->putback(c);break;};
CC> token[cur++] = c;
CC> } while (1);
CC> }
CC> It's heavily unstructured, yet more readable than any structured
CC> rule of thumb I could come up with. My goals are usually "easily
CC> readable" over most others.
I can't say as I like it very well. How 'bout:
char *get_token(ifstream &inf, char *token, unsigned max) {
char c;
unsigned cur;
while ( inf.get(c), !isspace(c)) {
if ( cur) >= max || inf->gcount != 1) {
inf.putback(c);
break;
}
token[cur++] = c;
}
return token;
}
However, the real soul of OO design is to pass the buck whenever
possible:
class token_stream : public ifstream {
public:
char get() {
char ch;
get(ch);
if (gcount != 1)
throw input_problem;
return ch;
}
};
vector get_token(token_stream &inf, vector &token) {
try {
char c;
while (!isspace(c = inf.get())
token.pushback(c);
inf.putback(c);
return token;
}
catch(input_problem &g) {
return token;
}
}
This divides the responsibility for the three different types of checks
into three different areas. The stream "knows" the stuff about the
stream, and the vector code handles the length of the output token. The
token code basically just defines what a token consists of - a run of
contiguous non-white space characters.
Later,
Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)
|