diff options
author | Marc Espie <espie@cvs.openbsd.org> | 1999-05-26 13:38:57 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 1999-05-26 13:38:57 +0000 |
commit | 0126e157b87f137fc08dc7f46f6c291b9d06ac5d (patch) | |
tree | f8555e3e504eb82b4cd3cba5cec20ae4ce8124ff /gnu/egcs/libio/tests/putbackdog.cc | |
parent | ff8e9a4356e55ed142306c3a375fa280800abc86 (diff) |
egcs projects compiler system
Exact copy of the snapshot, except for the removal of
texinfo/
gcc/ch/
libchill/
Diffstat (limited to 'gnu/egcs/libio/tests/putbackdog.cc')
-rw-r--r-- | gnu/egcs/libio/tests/putbackdog.cc | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/gnu/egcs/libio/tests/putbackdog.cc b/gnu/egcs/libio/tests/putbackdog.cc new file mode 100644 index 00000000000..0e1ed61f559 --- /dev/null +++ b/gnu/egcs/libio/tests/putbackdog.cc @@ -0,0 +1,97 @@ +/* +Copyright (C) 1993 Free Software Foundation + +This file is part of the GNU IO Library. This library is free +software; you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) +any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this library; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +As a special exception, if you link this library with files +compiled with a GNU compiler to produce an executable, this does not cause +the resulting executable to be covered by the GNU General Public License. +This exception does not however invalidate any other reasons why +the executable file might be covered by the GNU General Public License. */ + +// Test streambuf::sputbackc + +#include <iostream.h> +#include <stdlib.h> +#include <string.h> + +// Read either "dog", "hound", or "hounddog". +// If "dog" is found, return 1. +// If "hound" is found, return 2. +// If "hounddog" is found, return 3. +// If non of these are found, return -1. + +void unget_string(streambuf *sb, char *str, int count) +{ + for (str += count; -- count >= 0; ) + sb->sputbackc(*--str); +} + +int my_scan(streambuf* sb) +{ + char buffer[20]; + // Try reading "hounddog": + int count; + count = sb->sgetn(buffer, 8); + if (count == 8 && strncmp(buffer, "hounddog", 8) == 0) + return 3; + // No, no "hounddog": Backup to 'fence' ... + unget_string(sb, buffer, count); + // ... and try reading "dog": + count = sb->sgetn(buffer, 3); + if (count == 3 && strncmp(buffer, "dog", 3) == 0) + return 1; + // No, no "dog" either: Backup to 'fence' ... + unget_string(sb, buffer, count); + // ... and try reading "hound": + count = sb->sgetn(buffer, 5); + if (count == 5 && strncmp(buffer, "hound", 5) == 0) + return 2; + // No, no "hound" either: Backup to 'fence' and signal failure. + unget_string(sb, buffer, count); + return -1; +} + +int main(int argc, char **argv) +{ + streambuf *sb = cin.rdbuf(); + if (argc > 1 && strncmp(argv[1], "-b", 2) == 0) { + streambuf *ret; + int buffer_size = atoi(&argv[1][2]); + if (buffer_size == 0) + ret = sb->setbuf(NULL, 0); + else + ret = sb->setbuf(new char[buffer_size], buffer_size); + if (ret != sb) + cerr << "Warning: cin.rdbuf()->setbuf failed!\n"; + } + for (;;) { + int code = my_scan(sb); + int ch = sb->sbumpc(); + if (code == -1 && ch == EOF) + break; + int n = 0; + while (ch != EOF && ch != '\n') { + n++; + ch = sb->sbumpc(); + }; + if (ch == EOF) { + cout << "[Unexpected EOF]\n"; + break; + } + cout << "Code: " << code << " followed by " << n << " chars\n"; + } +} |