4482 lines · 126.3 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 "util_serial.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 __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;     // 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 ) )        return FALSE;     // add good()    func = make_new_mfun( "int", "good", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add close()    func = make_new_mfun( "void", "close", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add flush()    func = make_new_mfun( "void", "flush", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode(int)    func = make_new_mfun( "int", "mode", io_dummy );    func->add_arg( "int", "flag" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode()    func = make_new_mfun( "int", "mode", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // TODO: add this later?    // add read()    // func = make_new_mfun( "string", "read", io_dummy );    // func->add_arg( "int", "length" );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add readLine()    func = make_new_mfun( "string", "readLine", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readInt()    func = make_new_mfun( "int", "readInt", io_dummy );    func->add_arg( "int", "flags" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readFloat()    // func = make_new_mfun( "float", "readFloat", io_dummy );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add eof()    func = make_new_mfun( "int", "eof", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", io_dummy );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(string)    func = make_new_mfun( "void", "write", io_dummy );    func->add_arg( "string", "val" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(int)    func = make_new_mfun( "void", "write", io_dummy );    func->add_arg( "int", "val" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(float)    func = make_new_mfun( "void", "write", io_dummy );    func->add_arg( "float", "val" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add newline    func = make_new_sfun( "string", "newline", io_newline );    if( !type_engine_import_sfun( env, func ) ) goto error;    func = make_new_sfun( "string", "nl", io_newline );    if( !type_engine_import_sfun( env, func ) ) goto error;    func = make_new_sfun( "string", "newlineEx2VistaHWNDVisualFoxProA", io_newline );    if( !type_engine_import_sfun( env, func ) ) goto error;    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 ) ) goto error;    // add TYPE_BINARY    if( !type_engine_import_svar( env, "int", "BINARY",                                 TRUE, (t_CKUINT)&Chuck_IO::TYPE_BINARY ) ) goto error;    // add INT8    if( !type_engine_import_svar( env, "int", "INT8",                                 TRUE, (t_CKUINT)&Chuck_IO::INT8 ) ) goto error;    // add INT16    if( !type_engine_import_svar( env, "int", "INT16",                                 TRUE, (t_CKUINT)&Chuck_IO::INT16 ) ) goto error;    // add INT32    if( !type_engine_import_svar( env, "int", "INT32",                                 TRUE, (t_CKUINT)&Chuck_IO::INT32 ) ) goto error;    // add READ_INT8    if( !type_engine_import_svar( env, "int", "READ_INT8",                                 TRUE, (t_CKUINT)&Chuck_IO::INT8 ) ) goto error;    // add READ_INT16    if( !type_engine_import_svar( env, "int", "READ_INT16",                                 TRUE, (t_CKUINT)&Chuck_IO::INT16 ) ) goto error;    // add READ_INT32    if( !type_engine_import_svar( env, "int", "READ_INT32",                                 TRUE, (t_CKUINT)&Chuck_IO::INT32 ) ) goto error;    // add MODE_SYNC    if( !type_engine_import_svar( env, "int", "MODE_SYNC",                                 TRUE, (t_CKUINT)&Chuck_IO::MODE_SYNC ) ) goto error;#ifndef __DISABLE_FILEIO__    // add MODE_ASYNC    if( !type_engine_import_svar( env, "int", "MODE_ASYNC",                                 TRUE, (t_CKUINT)&Chuck_IO::MODE_ASYNC ) ) goto error;#endif    // add FLAG_READONLY    if( !type_engine_import_svar( env, "int", "READ",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_READONLY ) ) goto error;    // add FLAG_WRITEONLY    if( !type_engine_import_svar( env, "int", "WRITE",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_WRITEONLY ) ) goto error;    // add FLAG_APPEND    if( !type_engine_import_svar( env, "int", "APPEND",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_APPEND ) ) goto error;    // add FLAG_READ_WRITE    if( !type_engine_import_svar( env, "int", "READ_WRITE",                                  TRUE, (t_CKUINT)&Chuck_IO::FLAG_READ_WRITE ) ) 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 ) )        return FALSE;     // add open(string)    func = make_new_mfun( "int", "open", fileio_open );    func->add_arg( "string", "path" );    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" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add good()    func = make_new_mfun( "int", "good", fileio_good );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add close()    func = make_new_mfun( "void", "close", fileio_close );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add flush()    func = make_new_mfun( "void", "flush", fileio_flush );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode()    func = make_new_mfun( "int", "mode", fileio_getmode );    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" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add size()    func = make_new_mfun( "int", "size", fileio_size );    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" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add tell()    func = make_new_mfun( "int", "tell", fileio_tell );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add isDir()    func = make_new_mfun( "int", "isDir", fileio_isdir );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add dirList()    func = make_new_mfun( "string[]", "dirList", fileio_dirlist );    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 );    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" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readFloat()    // func = make_new_mfun( "float", "readFloat", fileio_readfloat );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add eof()    func = make_new_mfun( "int", "eof", fileio_eof );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", fileio_more );    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" );    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" );    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" );    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" );    if( !type_engine_import_mfun( env, func ) ) goto error;     if( !type_engine_import_add_ex( env, "io/chout.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "io/read-byte.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-byte.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;    // 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 ) )        return FALSE;     // add good()    func = make_new_mfun( "int", "good", chout_good );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add close()    func = make_new_mfun( "void", "close", chout_close );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add flush()    func = make_new_mfun( "void", "flush", chout_flush );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode()    func = make_new_mfun( "int", "mode", chout_getmode );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add mode(int)    func = make_new_mfun( "int", "mode", chout_setmode );    func->add_arg( "int", "flag" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readLine()    func = make_new_mfun( "string", "readLine", chout_readline );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readInt()    // func = make_new_mfun( "int", "readInt", chout_readint );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add readInt(int)    func = make_new_mfun( "int", "readInt", chout_readintflags );    func->add_arg( "int", "flags" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add readFloat()    // func = make_new_mfun( "float", "readFloat", chout_readfloat );    // if( !type_engine_import_mfun( env, func ) ) goto error;     // add eof()    func = make_new_mfun( "int", "eof", chout_eof );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", chout_more );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(string)    func = make_new_mfun( "void", "write", chout_writestring );    func->add_arg( "string", "val" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(int)    func = make_new_mfun( "void", "write", chout_writeint );    func->add_arg( "int", "val" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add write(float)    func = make_new_mfun( "void", "write", chout_writefloat );    func->add_arg( "float", "val" );    if( !type_engine_import_mfun( env, func ) ) 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;}    //-----------------------------------------------------------------------------// name: init_class_cherr()// desc: added 1.3.0.0 -- as full class//-----------------------------------------------------------------------------t_CKBOOL init_class_cherr( Chuck_Env * env, Chuck_Type * type ){    // init as base class    Chuck_DL_Func * func = NULL;     // log    EM_log( CK_LOG_SEVERE, "class 'cherr'" );     // TODO: ctor/dtor?    if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL ) )        return FALSE;     // add good()    func = make_new_mfun( "int", "good", cherr_good );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add close()

Lines 1–500 of 4482.

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