common/readline.c

///////////////////////////////////////////////////////////////////////////////
// Filename: readline.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: provide a function that reads a line from cin and recognizes
//          EOF immediately
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/06 00:44:30 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <stdlib.h>



// Local headers ///////////////////////////////////////////// Local headers //

#include "../common.h"



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //

/*
 * readline: reads a line from istream in, with recognizing EOF
 *           immediately. 
 * returns:  -1 for error, 0 for ok
 */

int
readline(istream &in, char *buf, int max_len)
{
    int ok = -1;

    if(buf && max_len)
    {
        char *t=buf;
        bool no_return = true;

        while(max_len && in && no_return)
        {
            in.read(t,1);

            if(*t == '\n')
                no_return=false;

            t++;
            max_len--;

        }

        *t=0;

        if(in) ok=0;
    }
    return ok;
}



// Main /////////////////////////////////////////////////////////////// Main //
    /* NONE */