1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
/*
FALCON - The Falcon Programming Language.
FILE: input.cpp
Read a line from the input stream
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: dom set 19 2004
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
Read a line from the input stream.
*/
/*#
@beginmodule core
*/
#include <falcon/setup.h>
#include <falcon/module.h>
#include <falcon/item.h>
#include <falcon/vm.h>
#include <falcon/string.h>
#include <falcon/memory.h>
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#else
#include <io.h>
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#endif
namespace Falcon {
namespace core {
/*#
@function input
@inset core_basic_io
@brief Get some text from the user (standard input stream).
Reads a line from the standard input stream and returns a string
containing the read data. This is mainly meant as a test/debugging
function to provide the scripts with minimal console based user input
support. When in need of reading lines from the standard input, prefer the
readLine() method of the input stream object.
This function may also be overloaded by embedders to provide the scripts
with a common general purpose input function, that returns a string that
the user is queried for.
*/
FALCON_FUNC input ( ::Falcon::VMachine *vm )
{
char mem[512];
int size = 0;
while( size < 511 )
{
if ( ::read( STDIN_FILENO, mem + size, 1 ) != 1 )
{
throw new IoError( ErrorParam( e_io_error )
.origin( e_orig_runtime )
.sysError( errno ) );
}
if( mem[size] == '\n' ) {
mem[ size ] = 0;
break;
}
else if( mem[size] != '\r' ) {
size++;
}
}
mem[size] = 0;
CoreString *str = new CoreString;
str->bufferize( mem );
vm->retval( str );
}
}}
/* end of input.cpp */
|