Annotation of GNUtools/libg++/libio/tests/hounddog.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: #include <iostream.h>
        !            26: #include <stdlib.h>
        !            27: #include <string.h>
        !            28: 
        !            29: // Read either "dog", "hound", or "hounddog".
        !            30: // If "dog" is found, return 1.
        !            31: // If "hound" is found, return 2.
        !            32: // If "hounddog" is found, return 3.
        !            33: // If non of these are found, return -1.
        !            34: int my_scan(streambuf* sb)
        !            35: {
        !            36:     streammarker fence(sb);
        !            37:     char buffer[20];
        !            38:     // Try reading "hounddog":
        !            39:     if (sb->sgetn(buffer, 8) == 8 && strncmp(buffer, "hounddog", 8) == 0)
        !            40:       return 3;
        !            41:     // No, no "hounddog":  Backup to 'fence' ...
        !            42:     sb->seekmark(fence);
        !            43:     // ... and try reading "dog":
        !            44:     if (sb->sgetn(buffer, 3) == 3 && strncmp(buffer, "dog", 3) == 0)
        !            45:       return 1;
        !            46:     // No, no "dog" either:  Backup to 'fence' ...
        !            47:     sb->seekmark(fence);
        !            48:     // ... and try reading "hound":
        !            49:     if (sb->sgetn(buffer, 5) == 5 && strncmp(buffer, "hound", 5) == 0)
        !            50:       return 2;
        !            51:     // No, no "hound" either:  Backup to 'fence' and signal failure.
        !            52:     sb->seekmark(fence); // Backup to 'fence'..
        !            53:     return -1;
        !            54: }
        !            55: 
        !            56: int main(int argc, char **argv)
        !            57: {
        !            58:     streambuf *sb = cin.rdbuf();
        !            59:     if (argc > 1 && strncmp(argv[1], "-b", 2) == 0) {
        !            60:        streambuf *ret;
        !            61:        int buffer_size = atoi(&argv[1][2]);
        !            62:        if (buffer_size == 0)
        !            63:            ret = sb->setbuf(NULL, 0);
        !            64:        else
        !            65:            ret = sb->setbuf(new char[buffer_size], buffer_size);
        !            66:        if (ret != sb)
        !            67:            cerr << "Warning: cin.rdbuf()->setbuf failed!\n";
        !            68:     }
        !            69:     for (;;) {
        !            70:        int code = my_scan(sb);
        !            71:        int ch = sb->sbumpc();
        !            72:        if (code == -1 && ch == EOF)
        !            73:            break;
        !            74:        int n = 0;
        !            75:        while (ch != EOF && ch != '\n') {
        !            76:            n++;
        !            77:            ch = sb->sbumpc();
        !            78:        };
        !            79:        if (ch == EOF) {
        !            80:            cout << "[Unexpected EOF]\n";
        !            81:            break;
        !            82:        }
        !            83:        cout << "Code: " << code << " followed by " << n << " chars\n";
        !            84:     }
        !            85: }

unix.superglobalmegacorp.com

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