4915 lines · 148.9 KB · C++
RawDownload
/*----------------------------------------------------------------------------  ChucK Concurrent, On-the-fly Audio Programming Language    Compiler and Virtual Machine   Copyright (c) 2004 Ge Wang and Perry R. Cook.  All rights reserved.    http://chuck.stanford.edu/    http://chuck.cs.princeton.edu/   This program 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 of the License, or  (at your option) any later version.   This program 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 program; if not, write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  U.S.A.-----------------------------------------------------------------------------*/ //-----------------------------------------------------------------------------// name: chuck_io.cpp// desc: chuck i/o//// authors: Spencer Salazar (spencer@ccrma.stanford.edu)//    date: Summer 2012//-----------------------------------------------------------------------------#include "chuck_io.h"#include "chuck_errmsg.h"#include "chuck_instr.h"#include "chuck_type.h"#include "chuck_vm.h" #ifndef __DISABLE_HID__#include "hidio_sdl.h"#endif #ifndef __DISABLE_MIDI__#include "midiio_rtmidi.h"#endif #ifndef __DISABLE_SERIAL__#include "util_serial.h"#endif #ifndef __PLATFORM_WIN32__#include <sys/select.h>#include <poll.h>#include <termios.h>#include <unistd.h>#else#include <io.h>#endif // __PLATFORM_WIN32__#include <fcntl.h>#include <math.h> #include <iomanip>#include <sstream>#include <iostream>#include <fstream>using namespace std;  #ifdef __PLATFORM_WIN32__typedef BYTE uint8_t;typedef WORD uint16_t;// ge: this needed in earlier/some versions of windows#ifndef __WINDOWS_MODERN__typedef __int32 int32_t; // 1.4.1.0 (ge) addedtypedef DWORD uint32_t;#endif#endif #ifdef __PLATFORM_LINUX__#include <stdint.h>#endif // globalstatic Chuck_String * g_newline = new Chuck_String();    //-----------------------------------------------------------------------------// name: init_class_io()// desc: ...//-----------------------------------------------------------------------------t_CKBOOL init_class_io( Chuck_Env * env, Chuck_Type * type ){    // init as base class    Chuck_DL_Func * func = NULL;    std::string doc = "Base class for other IO classes such as FileIO, StdOut and StdErr. Besides IO.newline(), it’s unlikely you need to use this class directly.";     // log    EM_log( CK_LOG_SEVERE, "class 'IO'" );        // init as base class    // TODO: ctor/dtor?    // TODO: replace dummy with pure function    if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc.c_str() ) )        return FALSE;     // add good()    func = make_new_mfun( "int", "good", io_dummy_int );    func->doc = "Returns whether IO is ready for reading.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add close()    func = make_new_mfun( "void", "close", io_dummy_void );    func->doc = "Close the currently open IO.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add flush()    func = make_new_mfun( "void", "flush", io_dummy_void );    func->doc = "Write any buffered output.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode(int)    func = make_new_mfun( "int", "mode", io_dummy_int );    func->add_arg( "int", "flag" );    func->doc = "Set the current IO mode; either IO.MODE_ASYNC or IO.MODE_SYNC.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode()    func = make_new_mfun( "int", "mode", io_dummy_int );    func->doc = "Get the current IO mode; either IO.MODE_ASYNC or IO.MODE_SYNC.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // TODO: add this later?    // add read()    // func = make_new_mfun( "string", "read", io_dummy_string );    // func->add_arg( "int", "length" );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add readLine()    func = make_new_mfun( "string", "readLine", io_dummy_string );    func->doc = "Read until an end-of-line character.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readInt()    func = make_new_mfun( "int", "readInt", io_dummy_int );    func->add_arg( "int", "flags" );    func->doc = "Read and return the next integer; binary mode: 'flags' denotes the bit-size of int (IO.INT8, IO.INT16, or IO.INT32).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readFloat()    func = make_new_mfun( "float", "readFloat", io_dummy_float );    func->add_arg( "int", "flags" );    func->doc = "Read and return the next floating point value; binary mode: 'flags' denotes the size of float to read (IO.FLOAT32 or IO.FLOAT64).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add eof()    func = make_new_mfun( "int", "eof", io_dummy_int );    func->doc = "Return whether end-of-file has been reached; the opposite of .more().";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", io_dummy_int );    func->doc = "Return whether there is more to read; the opposite of .eof().";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(string)    func = make_new_mfun( "void", "write", io_dummy_void );    func->add_arg( "string", "val" );    func->doc = "Write string 'val'.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(int)    func = make_new_mfun( "void", "write", io_dummy_void );    func->add_arg( "int", "val" );    func->doc = "Write integer 'val'.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(float)    func = make_new_mfun( "void", "write", io_dummy_void );    func->add_arg( "float", "val" );    func->doc = "Write floating point number 'val'.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add newline    func = make_new_sfun( "string", "newline", io_newline );    func->doc = "newline character; same as IO.nl().";    if( !type_engine_import_sfun( env, func ) ) goto error;    // add nl    func = make_new_sfun( "string", "nl", io_newline );    func->doc = "newline character; same as IO.newline().";    if( !type_engine_import_sfun( env, func ) ) goto error;    // hmm    // func = make_new_sfun( "string", "newlineEx2VistaHWNDVisualFoxProA", io_newline );    // func->doc = "_";    // if( !type_engine_import_sfun( env, func ) ) goto error;    // new line string    initialize_object( g_newline, env->t_string );    g_newline->set( "\n" );     // add TYPE_ASCII    if( !type_engine_import_svar( env, "int", "ASCII",                                 TRUE, (t_CKUINT)&Chuck_IO::TYPE_ASCII, "Flag denoting ASCII IO mode." ) ) goto error;    // add TYPE_BINARY    if( !type_engine_import_svar( env, "int", "BINARY",                                 TRUE, (t_CKUINT)&Chuck_IO::TYPE_BINARY, "Flag denoting binary IO mode." ) ) goto error;    // add INT8    if( !type_engine_import_svar( env, "int", "INT8",                                 TRUE, (t_CKUINT)&Chuck_IO::INT8, "Flag denoting 8-bit integer type." ) ) goto error;    // add INT16    if( !type_engine_import_svar( env, "int", "INT16",                                 TRUE, (t_CKUINT)&Chuck_IO::INT16, "Flag denoting 16-bit integer type." ) ) goto error;    // add INT24 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "INT24",                                 TRUE, (t_CKUINT)&Chuck_IO::INT24, "Flag denoting 24-bit integer type." ) ) goto error;    // add INT32    if( !type_engine_import_svar( env, "int", "INT32",                                 TRUE, (t_CKUINT)&Chuck_IO::INT32, "Flag denoting 32-bit integer type." ) ) goto error;    // add INT64 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "INT64",                                 TRUE, (t_CKUINT)&Chuck_IO::INT64, "Flag denoting 64-bit integer type." ) ) goto error;    // add SINT8 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "SINT8",                                 TRUE, (t_CKUINT)&Chuck_IO::SINT8, "Flag denoting 8-bit signed integer type." ) ) goto error;    // add SINT16 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "SINT16",                                 TRUE, (t_CKUINT)&Chuck_IO::SINT16, "Flag denoting 16-bit signed integer type." ) ) goto error;    // add SINT24 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "SINT24",                                 TRUE, (t_CKUINT)&Chuck_IO::SINT24, "Flag denoting 24-bit signed integer type." ) ) goto error;    // add SINT32 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "SINT32",                                 TRUE, (t_CKUINT)&Chuck_IO::SINT32, "Flag denoting 32-bit signed integer type." ) ) goto error;    // add SINT64 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "SINT64",                                 TRUE, (t_CKUINT)&Chuck_IO::SINT64, "Flag denoting 64-bit signed integer type." ) ) goto error;    // add UINT8 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "UINT8",                                 TRUE, (t_CKUINT)&Chuck_IO::UINT8, "Flag denoting 8-bit unsigned integer type." ) ) goto error;    // add UINT16 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "UINT16",                                 TRUE, (t_CKUINT)&Chuck_IO::UINT16, "Flag denoting 16-bit unsigned integer type." ) ) goto error;    // add UINT24 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "UINT24",                                 TRUE, (t_CKUINT)&Chuck_IO::UINT24, "Flag denoting 24-bit unsigned integer type." ) ) goto error;    // add UINT32 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "UINT32",                                 TRUE, (t_CKUINT)&Chuck_IO::UINT32, "Flag denoting 32-bit unsigned integer type." ) ) goto error;    // add UINT64 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "UINT64",                                 TRUE, (t_CKUINT)&Chuck_IO::UINT64, "Flag denoting 64-bit unsigned integer type." ) ) goto error;    // add FLOAT32 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "FLOAT32",                                 TRUE, (t_CKUINT)&Chuck_IO::FLOAT32, "Flag denoting 32-bit floating point type." ) ) goto error;    // add FLOAT64 | 1.5.0.1 (ge) added    if( !type_engine_import_svar( env, "int", "FLOAT64",                                 TRUE, (t_CKUINT)&Chuck_IO::FLOAT64, "Flag denoting 64-bit floating point type." ) ) goto error;    // add MODE_SYNC    if( !type_engine_import_svar( env, "int", "MODE_SYNC",                                 TRUE, (t_CKUINT)&Chuck_IO::MODE_SYNC, "Flag denoting synchronous IO." ) ) goto error;#ifndef __DISABLE_FILEIO__    // add MODE_ASYNC    if( !type_engine_import_svar( env, "int", "MODE_ASYNC",                                 TRUE, (t_CKUINT)&Chuck_IO::MODE_ASYNC, "Flag denoting asychronous IO." ) ) goto error;#endif    // add FLAG_READONLY    if( !type_engine_import_svar( env, "int", "READ",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_READONLY, "Flag denoting read mode." ) ) goto error;    // add FLAG_WRITEONLY    if( !type_engine_import_svar( env, "int", "WRITE",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_WRITEONLY, "Flag denoting write mode." ) ) goto error;    // add FLAG_APPEND    if( !type_engine_import_svar( env, "int", "APPEND",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_APPEND, "Flag denoting append mode." ) ) goto error;    // add FLAG_READ_WRITE    if( !type_engine_import_svar( env, "int", "READ_WRITE",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_READ_WRITE, "Flag denoting read/write mode." ) ) goto error;     // end the class import    type_engine_import_class_end( env );     return TRUE; error:     // end the class import    type_engine_import_class_end( env );     return FALSE;}    // #ifndef __DISABLE_FILEIO__//-----------------------------------------------------------------------------// name: init_class_fileio()// desc: ...//-----------------------------------------------------------------------------t_CKBOOL init_class_fileio( Chuck_Env * env, Chuck_Type * type ){    // init as base class    Chuck_DL_Func * func = NULL;     // log    EM_log( CK_LOG_SEVERE, "class 'FileIO'" );     // init as base class    // TODO: ctor/dtor?    // TODO: replace dummy with pure function    if( !type_engine_import_class_begin( env, type, env->global(), fileio_ctor, fileio_dtor,        "File input and output utilities for reading, writing, seeking, etc. See examples for usage." ) )        return FALSE;     // add open(string)    func = make_new_mfun( "int", "open", fileio_open );    func->add_arg( "string", "path" );    func->doc = "Open a file by name (and by default in ASCII mode).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add open(string, flags)    func = make_new_mfun( "int", "open", fileio_openflags );    func->add_arg( "string", "path" );    func->add_arg( "int", "flags" );    func->doc = "Open a file by name with flags (bitwise combinations of IO.READ, IO.WRITE, IO.READ_WRITE, IO_APPEND, IO.ASCII, IO.BINARY).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add good()    func = make_new_mfun( "int", "good", fileio_good );    func->doc = "Returns whether the file is ready for reading.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add close()    func = make_new_mfun( "void", "close", fileio_close );    func->doc = "Close (and flush) the currently open file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add flush()    func = make_new_mfun( "void", "flush", fileio_flush );    func->doc = "Write any buffered output to file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode()    func = make_new_mfun( "int", "mode", fileio_getmode );    func->doc = "Get file IO mode (IO.MODE_SYNC or IO.MODE_ASYNC).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode(int)    func = make_new_mfun( "int", "mode", fileio_setmode );    func->add_arg( "int", "flag" );    func->doc = "Set file IO mode (IO.MODE_SYNC or IO.MODE_ASYNC).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add size()    func = make_new_mfun( "int", "size", fileio_size );    func->doc = "Return the size of the file in bytes, or -1 if no file is opened or if a directory is opened.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add seek(int)    func = make_new_mfun( "void", "seek", fileio_seek );    func->add_arg( "int", "pos" );    func->doc = "Seek to a specified byte offset in file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add tell()    func = make_new_mfun( "int", "tell", fileio_tell );    func->doc = "Return the byte read offset of the file, or -1 if no file is opened.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add isDir()    func = make_new_mfun( "int", "isDir", fileio_isdir );    func->doc = "Return if the open file is a directory.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add dirList()    func = make_new_mfun( "string[]", "dirList", fileio_dirlist );    func->doc = "Get an array of file names in an open directory.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add read()    // func = make_new_mfun( "string", "read", fileio_read );    // func->add_arg( "int", "length" );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add readLine()    func = make_new_mfun( "string", "readLine", fileio_readline );    func->doc = "Read and return the next line from file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readInt()    // func = make_new_mfun( "int", "readInt", fileio_readint );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add readInt(int)    func = make_new_mfun( "int", "readInt", fileio_readintflags );    func->add_arg( "int", "flags" );    func->doc = "Read and return an integer; binary mode: 'flags' specifies int size to read (IO.INT8, IO.INT16, IO.INT32 default to unsigned values; for signed integers use IO.SINT8, IO.SINT16, IO.SINT32).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readFloat()    func = make_new_mfun( "float", "readFloat", fileio_readfloat );    func->doc = "Read and return the next floating point value.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readFloat()    func = make_new_mfun( "float", "readFloat", fileio_readfloatflags );    func->add_arg( "int", "flags" );    func->doc = "Read and return the next floating point value; if binary mode: 'flags' denotes the size of float to read (IO.FLOAT32 or IO.FLOAT64).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add eof()    func = make_new_mfun( "int", "eof", fileio_eof );    func->doc = "Return whether end-of-file has been reached; the opposite of .more().";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", fileio_more );    func->doc = "Return whether there is more to read; the opposite of .eof().";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(string)    func = make_new_mfun( "void", "write", fileio_writestring );    func->add_arg( "string", "val" );    func->doc = "Write a string to file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(int)    func = make_new_mfun( "void", "write", fileio_writeint );    func->add_arg( "int", "val" );    func->doc = "Write an integer to file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(int,flags)    func = make_new_mfun( "void", "write", fileio_writeintflags );    func->add_arg( "int", "val" );    func->add_arg( "int", "flags" );    func->doc = "Write integer value to file; binary mode: int size specified by 'flags' (IO.INT8, IO.INT16, IO.INT32).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(float)    func = make_new_mfun( "void", "write", fileio_writefloat );    func->add_arg( "float", "val" );    func->doc = "Write floating point value to file.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(float)    func = make_new_mfun( "void", "write", fileio_writefloatflags );    func->add_arg( "float", "val" );    func->add_arg( "int", "flags" );    func->doc = "Write floating point value to file; binary mode: flags indicate float size (IO.FLOAT32 or IO.FLOAT64).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add examples    if( !type_engine_import_add_ex( env, "io/chout.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-float.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-int.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-line.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-str.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-tokens.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/seek.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/write.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/write2.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-byte.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/write-byte.ck" ) ) goto error;     // end the class import    type_engine_import_class_end( env );     return TRUE; error:     // end the class import    type_engine_import_class_end( env );     return FALSE;}// #endif // __DISABLE_FILEIO__    //-----------------------------------------------------------------------------// name: init_class_chout()// desc: added 1.3.0.0 as full class//-----------------------------------------------------------------------------t_CKBOOL init_class_chout( Chuck_Env * env, Chuck_Type * type ){    // init as base class    Chuck_DL_Func * func = NULL;     // log    EM_log( CK_LOG_SEVERE, "class 'chout'" );     // TODO: ctor/dtor?    if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, "Do not instantiate this class directly! Use 'chout' instead." ) )        return FALSE;     // add good()    func = make_new_mfun( "int", "good", chout_good );    if( !type_engine_import_mfun( env, func ) ) goto error;

Lines 1–500 of 4915.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t