Annotation of GNUtools/libg++/libio/tests/putbackdog.cc, revision 1.1

1.1     ! root        1: /* 
        !             2: Copyright (C) 1993 Free Software Foundation
        !             3: 
        !             4: This file is part of the GNU IO Library.  This library is free
        !             5: software; you can redistribute it and/or modify it under the
        !             6: terms of the GNU General Public License as published by the
        !             7: Free Software Foundation; either version 2, or (at your option)
        !             8: any later version.
        !             9: 
        !            10: This library is distributed in the hope that it will be useful,
        !            11: but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            13: GNU General Public License for more details.
        !            14: 
        !            15: You should have received a copy of the GNU General Public License
        !            16: along with GNU CC; see the file COPYING.  If not, write to
        !            17: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
        !            18: 
        !            19: As a special exception, if you link this library with files
        !            20: compiled with a GNU compiler to produce an executable, this does not cause
        !            21: the resulting executable to be covered by the GNU General Public License.
        !            22: This exception does not however invalidate any other reasons why
        !            23: the executable file might be covered by the GNU General Public License. */
        !            24: 
        !            25: // Test streambuf::sputbackc
        !            26: 
        !            27: #include <iostream.h>
        !            28: #include <stdlib.h>
        !            29: #include <string.h>
        !            30: 
        !            31: // Read either "dog", "hound", or "hounddog".
        !            32: // If "dog" is found, return 1.
        !            33: // If "hound" is found, return 2.
        !            34: // If "hounddog" is found, return 3.
        !            35: // If non of these are found, return -1.
        !            36: 
        !            37: void unget_string(streambuf *sb, char *str, int count)
        !            38: {
        !            39:     for (str += count; -- count >= 0; )
        !            40:        sb->sputbackc(*--str);
        !            41: }
        !            42: 
        !            43: int my_scan(streambuf* sb)
        !            44: {
        !            45:     char buffer[20];
        !            46:     // Try reading "hounddog":
        !            47:     int count;
        !            48:     count = sb->sgetn(buffer, 8);
        !            49:     if (count == 8 && strncmp(buffer, "hounddog", 8) == 0)
        !            50:       return 3;
        !            51:     // No, no "hounddog":  Backup to 'fence' ...
        !            52:     unget_string(sb, buffer, count);
        !            53:     // ... and try reading "dog":
        !            54:     count = sb->sgetn(buffer, 3);
        !            55:     if (count == 3 && strncmp(buffer, "dog", 3) == 0)
        !            56:       return 1;
        !            57:     // No, no "dog" either:  Backup to 'fence' ...
        !            58:     unget_string(sb, buffer, count);
        !            59:     // ... and try reading "hound":
        !            60:     count = sb->sgetn(buffer, 5);
        !            61:     if (count == 5 && strncmp(buffer, "hound", 5) == 0)
        !            62:       return 2;
        !            63:     // No, no "hound" either:  Backup to 'fence' and signal failure.
        !            64:     unget_string(sb, buffer, count);
        !            65:     return -1;
        !            66: }
        !            67: 
        !            68: int main(int argc, char **argv)
        !            69: {
        !            70:     streambuf *sb = cin.rdbuf();
        !            71:     if (argc > 1 && strncmp(argv[1], "-b", 2) == 0) {
        !            72:        streambuf *ret;
        !            73:        int buffer_size = atoi(&argv[1][2]);
        !            74:        if (buffer_size == 0)
        !            75:            ret = sb->setbuf(NULL, 0);
        !            76:        else
        !            77:            ret = sb->setbuf(new char[buffer_size], buffer_size);
        !            78:        if (ret != sb)
        !            79:            cerr << "Warning: cin.rdbuf()->setbuf failed!\n";
        !            80:     }
        !            81:     for (;;) {
        !            82:        int code = my_scan(sb);
        !            83:        int ch = sb->sbumpc();
        !            84:        if (code == -1 && ch == EOF)
        !            85:            break;
        !            86:        int n = 0;
        !            87:        while (ch != EOF && ch != '\n') {
        !            88:            n++;
        !            89:            ch = sb->sbumpc();
        !            90:        };
        !            91:        if (ch == EOF) {
        !            92:            cout << "[Unexpected EOF]\n";
        !            93:            break;
        !            94:        }
        !            95:        cout << "Code: " << code << " followed by " << n << " chars\n";
        !            96:     }
        !            97: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.