Unified · Split
| @@ -28,8 +28,11 @@ int val; · 27 unmodified lines | ||
| 28 | 28 | while( fio.more() ) |
| 29 | 29 | { |
| 30 | 30 | // read the next byte |
| 31 | − // (other options include IO.INT16 and IO.INT32) | |
| 32 | 31 | fio.readInt( IO.INT8 ) => val; |
| 32 | + // optionsss ^^^^^^^ | requires chuck-1.5.0.1 or higher | |
| 33 | + // IO.INT8, IO.INT16, IO.INT32 (default to unsigned) | |
| 34 | + // IO.UINT8, IO.UINT16, IO.UINT32 (explicitly unsigned) | |
| 35 | + // IO.SINT6, IO.SINT16, IO.SINT32 (explicitly signed) | |
| 33 | 36 | |
| 34 | 37 | // print the value |
| 35 | 38 | cherr <= val <= IO.newline(); |
| @@ -0,0 +1,149 @@ | ||
| 1 | +//--------------------------------------------------------------------- | |
| 2 | +// name: read-wav-raw.ck | |
| 3 | +// desc: example that illustrates manually parsing and rendering a | |
| 4 | +// mono WAV file. This demonstrates using FileIO in binary mode. | |
| 5 | +// FYI in practice, use SndBuf for easier audio file playback! | |
| 6 | +// requires: chuck-1.5.0.1 or higher | |
| 7 | +// | |
| 8 | +// author: Perry R. Cook | |
| 9 | +// date: Summer 2023 | |
| 10 | +// credit "saigon44.wav": opening words from /Apocalypse Now/ | |
| 11 | +//--------------------------------------------------------------------- | |
| 12 | + | |
| 13 | +// warn people what is about to happen | |
| 14 | +<<< "raw mono WAV file reader!!", "" >>>; | |
| 15 | + | |
| 16 | +// default file | |
| 17 | +me.dir() + "saigon44.wav" => string filename; | |
| 18 | +// look at command line | |
| 19 | +if( me.args() > 0 ) me.arg(0) => filename; | |
| 20 | + | |
| 21 | +// instantiate a file IO object | |
| 22 | +FileIO fio; | |
| 23 | + | |
| 24 | +// open a file for reading in BINARY mode | |
| 25 | +fio.open( filename, IO.READ | IO.BINARY ); | |
| 26 | +// ensure it's ok | |
| 27 | +if( !fio.good() ) | |
| 28 | +{ | |
| 29 | + // print error message | |
| 30 | + cherr <= "can't open file: " <= filename <= " for reading..." | |
| 31 | + <= IO.newline(); | |
| 32 | + // bail | |
| 33 | + me.exit(); | |
| 34 | +} | |
| 35 | + | |
| 36 | +// variable to read into | |
| 37 | +int val; | |
| 38 | +// header | |
| 39 | +"0123456789abcdefghijklmnopqrstuvwxyzABCD" @=> string header; | |
| 40 | + | |
| 41 | +// .wav header details: | |
| 42 | +// struct wavhdr { | |
| 43 | +// char riff[4]; /* "RIFF" */ | |
| 44 | +// int flength; /* file length in bytes */ | |
| 45 | +// char wave[4]; /* "WAVE" */ | |
| 46 | +// char fmt[4]; /* "fmt " */ | |
| 47 | +// int block_size; /* in bytes (generally 16) */ | |
| 48 | +// short format_tag; /* 1=PCM, 257=Mu-Law, 258=A-Law, 259=ADPCM */ | |
| 49 | +// short num_chans; /* 1=mono, 2=stereo */ | |
| 50 | +// int srate; /* Sampling rate in samples per second */ | |
| 51 | +// int bytes_per_sec; /* bytes per second */ | |
| 52 | +// short bytes_per_samp; /* 2=16-bit mono, 4=16-bit stereo */ | |
| 53 | +// short bits_per_samp; /* Number of bits per sample */ | |
| 54 | +// char data[4]; /* "data" */ | |
| 55 | +// int dlength; /* data length in bytes (filelength - 44) */ | |
| 56 | +// }; | |
| 57 | + | |
| 58 | +// read in the WAV file header, field by field | |
| 59 | +// FYI since chuck-1.5.0.1, in binary mode, readInt() for IO.INT8, | |
| 60 | +// IO.INT16, or IO.INT32 by default returns unsigned integers; to read | |
| 61 | +// signed integers, call readInt() with IO.SINT8, IO.SINT16, or IO.SINT32 | |
| 62 | +// e.g., IO.SINT16 is used below when reading signed 16-bits samples | |
| 63 | + | |
| 64 | +// "RIFF" | |
| 65 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(0,val); | |
| 66 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(1,val); | |
| 67 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(2,val); | |
| 68 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(3,val); | |
| 69 | +// file length | |
| 70 | +fio.readInt( IO.INT32 ) => int flength; | |
| 71 | +// "WAVE" | |
| 72 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(8,val); | |
| 73 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(9,val); | |
| 74 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(10,val); | |
| 75 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(11,val); | |
| 76 | +// "fmt " | |
| 77 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(12,val); | |
| 78 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(13,val); | |
| 79 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(14,val); | |
| 80 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(15,val); | |
| 81 | +// file info | |
| 82 | +fio.readInt( IO.INT32 ) => int blockSize; | |
| 83 | +fio.readInt( IO.INT16 ) => int format; | |
| 84 | +fio.readInt( IO.INT16 ) => int numChannels; | |
| 85 | +fio.readInt( IO.INT32 ) => int sRate; | |
| 86 | +fio.readInt( IO.INT32 ) => int bytesPerSecond; | |
| 87 | +fio.readInt( IO.INT16 ) => int bytesPerSample; | |
| 88 | +fio.readInt( IO.INT16 ) => int bitsPerSample; | |
| 89 | +// "data" | |
| 90 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(36,val); | |
| 91 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(37,val); | |
| 92 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(38,val); | |
| 93 | +fio.readInt( IO.INT8 ) => val; header.setCharAt(39,val); | |
| 94 | +// size of data region in bytes | |
| 95 | +fio.readInt( IO.INT32 ) => int dLength; | |
| 96 | + | |
| 97 | +<<< "***", header, "***" >>>; | |
| 98 | +<<< "---------------------", "" >>>; | |
| 99 | +<<< "File Length =", flength >>>; | |
| 100 | +<<< "Block Size =", blockSize >>>; | |
| 101 | +<<< "Format =", format >>>; | |
| 102 | +<<< "Num Chans =", numChannels >>>; | |
| 103 | +<<< "Sample Rate =", sRate >>>; | |
| 104 | +<<< "Bytes/Sec =", bytesPerSecond >>>; | |
| 105 | +<<< "Bytes/Sample =", bytesPerSample >>>; | |
| 106 | +<<< "Bits/Sample =", bitsPerSample >>>; | |
| 107 | +<<< "Data Length =", dLength >>>; | |
| 108 | +<<< "---------------------", "" >>>; | |
| 109 | + | |
| 110 | +// check | |
| 111 | +if( numChannels > 1 ) | |
| 112 | +{ | |
| 113 | + // print error message | |
| 114 | + <<< "WAV file contains", numChannels, "channels..." >>>; | |
| 115 | + <<< "this examples only supports mono WAV files...", "" >>>; | |
| 116 | + // bail | |
| 117 | + me.exit(); | |
| 118 | +} | |
| 119 | + | |
| 120 | +// render the waveform | |
| 121 | +Impulse imp => dac; | |
| 122 | +// number of samples | |
| 123 | +dLength / bytesPerSample => int numSamples; | |
| 124 | +// array | |
| 125 | +float samples[numSamples]; | |
| 126 | +// sample number | |
| 127 | +0 => int n; | |
| 128 | + | |
| 129 | +// loop until end | |
| 130 | +while( n < numSamples ) | |
| 131 | +// while( fio.more() ) | |
| 132 | +// (^ this may not always work; WAV files may contain debris beyond data region) | |
| 133 | +{ | |
| 134 | + // read the next sample: signed 16-bit integer | |
| 135 | + fio.readInt( IO.SINT16 ) => int sample; | |
| 136 | + // map sample value from [-32768,32767] to [-1,1] | |
| 137 | + Math.remap(sample, -32768, 32767, -1, 1) => samples[n++] => imp.next; | |
| 138 | + // print dot every 1000 samples | |
| 139 | + if( n % 1000 == 0 ) { | |
| 140 | + chout <= "."; chout.flush(); | |
| 141 | + } | |
| 142 | + // print marker every second | |
| 143 | + if( n % sRate == 0 ) { | |
| 144 | + chout <= "|"; chout.flush(); | |
| 145 | + } | |
| 146 | + | |
| 147 | + // advance one sample | |
| 148 | + samp => now; | |
| 149 | +} | |
| @@ -6,21 +6,35 @@ ChucK VERSIONS log · 5 unmodified lines | ||
| 6 | 6 | --- |
| 7 | 7 | * (added) WvOut support for multiple bit-depths: 16-bit signed int, |
| 8 | 8 | 24-bit signed int, 32-bit signed int, 32-bit float, 64-bit float |
| 9 | −* these can optionally specified in the newly overloaded WvOut methods: | |
| 9 | +* (added) the above can be optionally specified in new overloaded WvOut methods: | |
| 10 | 10 | string aifFilename( string value, int datatype ); |
| 11 | 11 | Open an AIFF file for writing, with datatype (e.g., IO.INT16, |
| 12 | 12 | IO.INT24, IO.INT32, IO.FLOAT32, IO.FLOAT64). |
| 13 | − string matFilename( string value, int datatype ); | |
| 14 | − Open MATLAB file for writing; datatype for MATLAB files can | |
| 15 | − only be IO.FLOAT64. | |
| 16 | 13 | string sndFilename( string value, int datatype ); |
| 17 | − Open SND file for writing, with datatype (e.g., IO.INT16, IO.INT24, IO.INT32, IO.FLOAT32, IO.FLOAT64). | |
| 14 | + Open SND file for writing, with datatype (e.g., IO.INT16, IO.INT24, IO.INT32, | |
| 15 | + IO.FLOAT32, IO.FLOAT64). | |
| 18 | 16 | string wavFilename( string value, int datatype ); |
| 19 | 17 | Open WAVE file for writing, with datatype (e.g., IO.INT16, IO.INT24, IO.INT32, IO.FLOAT32, IO.FLOAT64). |
| 18 | + string matFilename( string value, int datatype ); | |
| 19 | + Open MATLAB file for writing; NOTE: datatype for MATLAB files can | |
| 20 | + only be IO.FLOAT64. | |
| 20 | 21 | string rawFilename( string value, int datatype ); |
| 21 | − Open a RAW file for writing; datatype for raw files can | |
| 22 | − only be IO.INT16. | |
| 23 | −* (added) IO.INT24, IO.INT64, IO.FLOAT32, IO.FLOAT64 IO flags | |
| 22 | + Open a RAW file for writing; NOTE: datatype for raw files can | |
| 23 | + only be IO.INT16 | |
| 24 | +* (added) examples/stk/wvout-24bit.ck shows writing to a 24-bit audio file | |
| 25 | +* (added) new IO flags: IO.INT24, IO.INT64, IO.FLOAT32, IO.FLOAT64 | |
| 26 | + IO.UINT8, IO.SINT8, IO.UINT16, IO.SINT16, IO.UINT32, IO.SINT32, | |
| 27 | + IO.UINT64, IO.SINT64 | |
| 28 | +* (updated) BINARY mode for IO now able to read signed vs. unsigned integers, | |
| 29 | + specified using using .readtInt( flags ), where 'flags' is one of the above | |
| 30 | + IO flags; in BINARY mode, IO.INT8/16/32 now returns unsigned integers, same as | |
| 31 | + IO.UINT8/16/32; to read in signed two's complement integers, use the new | |
| 32 | + IO.SINT8/16/32/64 flags | |
| 33 | +* (added) FileIO.readFloat( int flags) and FileIO.write( float val, int flags ) | |
| 34 | + these overloaded versions of readFloat() and write() respectively have a 'flags' | |
| 35 | + argument to specify the kind of float (IO.FLOAT32 or IO.FLOAT64) | |
| 36 | +* (added) examples/io/read-wav-raw.ck -- illustrates directly parsing a WAV file | |
| 37 | + using FileIO in binary mode (author: Perry R. Cook) | |
| 24 | 38 | * (added) examples/special/scream-o-matic/ -- using LiSa + granular |
| 25 | 39 | synthesis for a scream texture generator. |
| 26 | 40 | * (updated) on Linux, better default runtime driver selection for |
| @@ -105,93 +105,78 @@ t_CKBOOL init_class_io( Chuck_Env * env, Chuck_Type * type ) · 104 unmodified lines | ||
| 105 | 105 | return FALSE; |
| 106 | 106 | |
| 107 | 107 | // add good() |
| 108 | − func = make_new_mfun( "int", "good", io_dummy ); | |
| 108 | + func = make_new_mfun( "int", "good", io_dummy_int ); | |
| 109 | 109 | func->doc = "Returns whether IO is ready for reading."; |
| 110 | 110 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 111 | 111 | |
| 112 | 112 | // add close() |
| 113 | − func = make_new_mfun( "void", "close", io_dummy ); | |
| 113 | + func = make_new_mfun( "void", "close", io_dummy_void ); | |
| 114 | 114 | func->doc = "Close the currently open IO."; |
| 115 | 115 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 116 | 116 | |
| 117 | 117 | // add flush() |
| 118 | − func = make_new_mfun( "void", "flush", io_dummy ); | |
| 118 | + func = make_new_mfun( "void", "flush", io_dummy_void ); | |
| 119 | 119 | func->doc = "Write any buffered output."; |
| 120 | 120 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 121 | 121 | |
| 122 | 122 | // add mode(int) |
| 123 | − func = make_new_mfun( "int", "mode", io_dummy ); | |
| 123 | + func = make_new_mfun( "int", "mode", io_dummy_int ); | |
| 124 | 124 | func->add_arg( "int", "flag" ); |
| 125 | 125 | func->doc = "Set the current IO mode; either IO.MODE_ASYNC or IO.MODE_SYNC."; |
| 126 | 126 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 127 | 127 | |
| 128 | 128 | // add mode() |
| 129 | − func = make_new_mfun( "int", "mode", io_dummy ); | |
| 129 | + func = make_new_mfun( "int", "mode", io_dummy_int ); | |
| 130 | 130 | func->doc = "Get the current IO mode; either IO.MODE_ASYNC or IO.MODE_SYNC."; |
| 131 | 131 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 132 | 132 | |
| 133 | 133 | // TODO: add this later? |
| 134 | 134 | // add read() |
| 135 | − // func = make_new_mfun( "string", "read", io_dummy ); | |
| 135 | + // func = make_new_mfun( "string", "read", io_dummy_string ); | |
| 136 | 136 | // func->add_arg( "int", "length" ); |
| 137 | 137 | // if( !type_engine_import_mfun( env, func ) ) goto error; |
| 138 | 138 | |
| 139 | 139 | // add readLine() |
| 140 | − func = make_new_mfun( "string", "readLine", io_dummy ); | |
| 140 | + func = make_new_mfun( "string", "readLine", io_dummy_string ); | |
| 141 | 141 | func->doc = "Read until an end-of-line character."; |
| 142 | 142 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 143 | 143 | |
| 144 | 144 | // add readInt() |
| 145 | − func = make_new_mfun( "int", "readInt", io_dummy ); | |
| 145 | + func = make_new_mfun( "int", "readInt", io_dummy_int ); | |
| 146 | 146 | func->add_arg( "int", "flags" ); |
| 147 | − func->doc = "Read and return the next integer (binary mode); 'flags' denotes the bit-size of int (IO.INT8, IO.INT16, or IO.INT32)."; | |
| 147 | + func->doc = "Read and return the next integer; binary mode: 'flags' denotes the bit-size of int (IO.INT8, IO.INT16, or IO.INT32)."; | |
| 148 | 148 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 149 | 149 | |
| 150 | − /* | |
| 151 | − // add readInt() | |
| 152 | − func = make_new_mfun( "int", "readInt8", io_dummy ); | |
| 153 | − func->add_arg( "int", "flags" ); | |
| 154 | − if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 155 | − | |
| 156 | − // add readInt() | |
| 157 | − func = make_new_mfun( "int", "readInt16", io_dummy ); | |
| 158 | − func->add_arg( "int", "flags" ); | |
| 159 | − if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 160 | − | |
| 161 | − // add readInt() | |
| 162 | − func = make_new_mfun( "int", "readInt32", io_dummy ); | |
| 150 | + // add readFloat() | |
| 151 | + func = make_new_mfun( "float", "readFloat", io_dummy_float ); | |
| 163 | 152 | func->add_arg( "int", "flags" ); |
| 153 | + 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)."; | |
| 164 | 154 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 165 | − */ | |
| 166 | − | |
| 167 | − // add readFloat() | |
| 168 | − // func = make_new_mfun( "float", "readFloat", io_dummy ); | |
| 169 | − // if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 170 | 155 | |
| 171 | 156 | // add eof() |
| 172 | − func = make_new_mfun( "int", "eof", io_dummy ); | |
| 157 | + func = make_new_mfun( "int", "eof", io_dummy_int ); | |
| 173 | 158 | func->doc = "Return whether end-of-file has been reached; the opposite of .more()."; |
| 174 | 159 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 175 | 160 | |
| 176 | 161 | // add more() |
| 177 | − func = make_new_mfun( "int", "more", io_dummy ); | |
| 162 | + func = make_new_mfun( "int", "more", io_dummy_int ); | |
| 178 | 163 | func->doc = "Return whether there is more to read; the opposite of .eof()."; |
| 179 | 164 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 180 | 165 | |
| 181 | 166 | // add write(string) |
| 182 | − func = make_new_mfun( "void", "write", io_dummy ); | |
| 167 | + func = make_new_mfun( "void", "write", io_dummy_void ); | |
| 183 | 168 | func->add_arg( "string", "val" ); |
| 184 | 169 | func->doc = "Write string 'val'."; |
| 185 | 170 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 186 | 171 | |
| 187 | 172 | // add write(int) |
| 188 | − func = make_new_mfun( "void", "write", io_dummy ); | |
| 173 | + func = make_new_mfun( "void", "write", io_dummy_void ); | |
| 189 | 174 | func->add_arg( "int", "val" ); |
| 190 | 175 | func->doc = "Write integer 'val'."; |
| 191 | 176 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 192 | 177 | |
| 193 | 178 | // add write(float) |
| 194 | − func = make_new_mfun( "void", "write", io_dummy ); | |
| 179 | + func = make_new_mfun( "void", "write", io_dummy_void ); | |
| 195 | 180 | func->add_arg( "float", "val" ); |
| 196 | 181 | func->doc = "Write floating point number 'val'."; |
| 197 | 182 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| @@ -233,21 +218,42 @@ t_CKBOOL init_class_io( Chuck_Env * env, Chuck_Type * type ) · 35 unmodified lines | ||
| 233 | 218 | // add INT64 | 1.5.0.1 (ge) added |
| 234 | 219 | if( !type_engine_import_svar( env, "int", "INT64", |
| 235 | 220 | TRUE, (t_CKUINT)&Chuck_IO::INT64, "Flag denoting 64-bit integer type." ) ) goto error; |
| 221 | + // add SINT8 | 1.5.0.1 (ge) added | |
| 222 | + if( !type_engine_import_svar( env, "int", "SINT8", | |
| 223 | + TRUE, (t_CKUINT)&Chuck_IO::SINT8, "Flag denoting 8-bit signed integer type." ) ) goto error; | |
| 224 | + // add SINT16 | 1.5.0.1 (ge) added | |
| 225 | + if( !type_engine_import_svar( env, "int", "SINT16", | |
| 226 | + TRUE, (t_CKUINT)&Chuck_IO::SINT16, "Flag denoting 16-bit signed integer type." ) ) goto error; | |
| 227 | + // add SINT24 | 1.5.0.1 (ge) added | |
| 228 | + if( !type_engine_import_svar( env, "int", "SINT24", | |
| 229 | + TRUE, (t_CKUINT)&Chuck_IO::SINT24, "Flag denoting 24-bit signed integer type." ) ) goto error; | |
| 230 | + // add SINT32 | 1.5.0.1 (ge) added | |
| 231 | + if( !type_engine_import_svar( env, "int", "SINT32", | |
| 232 | + TRUE, (t_CKUINT)&Chuck_IO::SINT32, "Flag denoting 32-bit signed integer type." ) ) goto error; | |
| 233 | + // add SINT64 | 1.5.0.1 (ge) added | |
| 234 | + if( !type_engine_import_svar( env, "int", "SINT64", | |
| 235 | + TRUE, (t_CKUINT)&Chuck_IO::SINT64, "Flag denoting 64-bit signed integer type." ) ) goto error; | |
| 236 | + // add UINT8 | 1.5.0.1 (ge) added | |
| 237 | + if( !type_engine_import_svar( env, "int", "UINT8", | |
| 238 | + TRUE, (t_CKUINT)&Chuck_IO::UINT8, "Flag denoting 8-bit unsigned integer type." ) ) goto error; | |
| 239 | + // add UINT16 | 1.5.0.1 (ge) added | |
| 240 | + if( !type_engine_import_svar( env, "int", "UINT16", | |
| 241 | + TRUE, (t_CKUINT)&Chuck_IO::UINT16, "Flag denoting 16-bit unsigned integer type." ) ) goto error; | |
| 242 | + // add UINT24 | 1.5.0.1 (ge) added | |
| 243 | + if( !type_engine_import_svar( env, "int", "UINT24", | |
| 244 | + TRUE, (t_CKUINT)&Chuck_IO::UINT24, "Flag denoting 24-bit unsigned integer type." ) ) goto error; | |
| 245 | + // add UINT32 | 1.5.0.1 (ge) added | |
| 246 | + if( !type_engine_import_svar( env, "int", "UINT32", | |
| 247 | + TRUE, (t_CKUINT)&Chuck_IO::UINT32, "Flag denoting 32-bit unsigned integer type." ) ) goto error; | |
| 248 | + // add UINT64 | 1.5.0.1 (ge) added | |
| 249 | + if( !type_engine_import_svar( env, "int", "UINT64", | |
| 250 | + TRUE, (t_CKUINT)&Chuck_IO::UINT64, "Flag denoting 64-bit unsigned integer type." ) ) goto error; | |
| 236 | 251 | // add FLOAT32 | 1.5.0.1 (ge) added |
| 237 | 252 | if( !type_engine_import_svar( env, "int", "FLOAT32", |
| 238 | 253 | TRUE, (t_CKUINT)&Chuck_IO::FLOAT32, "Flag denoting 32-bit floating point type." ) ) goto error; |
| 239 | 254 | // add FLOAT64 | 1.5.0.1 (ge) added |
| 240 | 255 | if( !type_engine_import_svar( env, "int", "FLOAT64", |
| 241 | 256 | TRUE, (t_CKUINT)&Chuck_IO::FLOAT64, "Flag denoting 64-bit floating point type." ) ) goto error; |
| 242 | −// // add READ_INT8 | |
| 243 | −// if( !type_engine_import_svar( env, "int", "READ_INT8", | |
| 244 | −// TRUE, (t_CKUINT)&Chuck_IO::INT8, "Flag denoting 8-bit integer type (same as INT8)." ) ) goto error; | |
| 245 | −// // add READ_INT16 | |
| 246 | −// if( !type_engine_import_svar( env, "int", "READ_INT16", | |
| 247 | −// TRUE, (t_CKUINT)&Chuck_IO::INT16, "Flag denoting 16-bit integer type (same as INT16)." ) ) goto error; | |
| 248 | −// // add READ_INT32 | |
| 249 | −// if( !type_engine_import_svar( env, "int", "READ_INT32", | |
| 250 | −// TRUE, (t_CKUINT)&Chuck_IO::INT32, "Flag denoting 32-bit integer type (same as INT32)." ) ) goto error; | |
| 251 | 257 | // add MODE_SYNC |
| 252 | 258 | if( !type_engine_import_svar( env, "int", "MODE_SYNC", |
| 253 | 259 | TRUE, (t_CKUINT)&Chuck_IO::MODE_SYNC, "Flag denoting synchronous IO." ) ) goto error; |
| @@ -387,34 +393,19 @@ t_CKBOOL init_class_fileio( Chuck_Env * env, Chuck_Type * type ) · 133 unmodified lines | ||
| 387 | 393 | // add readInt(int) |
| 388 | 394 | func = make_new_mfun( "int", "readInt", fileio_readintflags ); |
| 389 | 395 | func->add_arg( "int", "flags" ); |
| 390 | − func->doc = "Read and return an integer by size (binary mode) as specified in 'flags' (IO.INT8, IO.INT16, IO.INT32)."; | |
| 391 | − if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 392 | − | |
| 393 | − /* | |
| 394 | − // add readByte() | |
| 395 | − func = make_new_mfun( "int", "readByte", fileio_readint8 ); | |
| 396 | − func->add_arg( "int", "flags" ); | |
| 397 | − if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 398 | − | |
| 399 | − // add readInt8() | |
| 400 | − func = make_new_mfun( "int", "readInt8", fileio_readint8 ); | |
| 401 | − func->add_arg( "int", "flags" ); | |
| 396 | + 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)."; | |
| 402 | 397 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 403 | 398 | |
| 404 | − // add readInt16() | |
| 405 | − func = make_new_mfun( "int", "readInt16", fileio_readint16 ); | |
| 406 | − func->add_arg( "int", "flags" ); | |
| 399 | + // add readFloat() | |
| 400 | + func = make_new_mfun( "float", "readFloat", fileio_readfloat ); | |
| 401 | + func->doc = "Read and return the next floating point value."; | |
| 407 | 402 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 408 | 403 | |
| 409 | − // add readInt32() | |
| 410 | − func = make_new_mfun( "int", "readInt32", fileio_readint32 ); | |
| 404 | + // add readFloat() | |
| 405 | + func = make_new_mfun( "float", "readFloat", fileio_readfloatflags ); | |
| 411 | 406 | func->add_arg( "int", "flags" ); |
| 407 | + 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)."; | |
| 412 | 408 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 413 | − */ | |
| 414 | − | |
| 415 | − // add readFloat() | |
| 416 | − // func = make_new_mfun( "float", "readFloat", fileio_readfloat ); | |
| 417 | − // if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 418 | 409 | |
| 419 | 410 | // add eof() |
| 420 | 411 | func = make_new_mfun( "int", "eof", fileio_eof ); |
| @@ -442,13 +433,20 @@ t_CKBOOL init_class_fileio( Chuck_Env * env, Chuck_Type * type ) · 21 unmodified lines | ||
| 442 | 433 | func = make_new_mfun( "void", "write", fileio_writeintflags ); |
| 443 | 434 | func->add_arg( "int", "val" ); |
| 444 | 435 | func->add_arg( "int", "flags" ); |
| 445 | − func->doc = "Write integer to file (binary mode) by size as specified by 'flags' (IO.INT8, IO.INT16, IO.INT32)."; | |
| 436 | + func->doc = "Write integer value to file; binary mode: int size specified by 'flags' (IO.INT8, IO.INT16, IO.INT32)."; | |
| 446 | 437 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 447 | 438 | |
| 448 | 439 | // add write(float) |
| 449 | 440 | func = make_new_mfun( "void", "write", fileio_writefloat ); |
| 450 | 441 | func->add_arg( "float", "val" ); |
| 451 | − func->doc = "Write integer to file."; | |
| 442 | + func->doc = "Write floating point value to file."; | |
| 443 | + if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 444 | + | |
| 445 | + // add write(float) | |
| 446 | + func = make_new_mfun( "void", "write", fileio_writefloatflags ); | |
| 447 | + func->add_arg( "float", "val" ); | |
| 448 | + func->add_arg( "int", "flags" ); | |
| 449 | + func->doc = "Write floating point value to file; binary mode: flags indicate float size (IO.FLOAT32 or IO.FLOAT64)."; | |
| 452 | 450 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 453 | 451 | |
| 454 | 452 | // add examples |
| @@ -522,10 +520,6 @@ t_CKBOOL init_class_chout( Chuck_Env * env, Chuck_Type * type ) · 67 unmodified lines | ||
| 522 | 520 | func = make_new_mfun( "string", "readLine", chout_readline ); |
| 523 | 521 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 524 | 522 | |
| 525 | − // add readInt() | |
| 526 | − // func = make_new_mfun( "int", "readInt", chout_readint ); | |
| 527 | − // if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 528 | − | |
| 529 | 523 | // add readInt(int) |
| 530 | 524 | func = make_new_mfun( "int", "readInt", chout_readintflags ); |
| 531 | 525 | func->add_arg( "int", "flags" ); |
| @@ -533,6 +527,7 @@ t_CKBOOL init_class_chout( Chuck_Env * env, Chuck_Type * type ) · 1 unmodified line | ||
| 533 | 527 | |
| 534 | 528 | // add readFloat() |
| 535 | 529 | // func = make_new_mfun( "float", "readFloat", chout_readfloat ); |
| 530 | + // func->add_arg( "int", "flags" ); | |
| 536 | 531 | // if( !type_engine_import_mfun( env, func ) ) goto error; |
| 537 | 532 | |
| 538 | 533 | // add eof() |
| @@ -615,17 +610,14 @@ t_CKBOOL init_class_cherr( Chuck_Env * env, Chuck_Type * type ) · 76 unmodified lines | ||
| 615 | 610 | func = make_new_mfun( "string", "readLine", cherr_readline ); |
| 616 | 611 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 617 | 612 | |
| 618 | − // add readInt() | |
| 619 | − // func = make_new_mfun( "int", "readInt", cherr_readint ); | |
| 620 | − // if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 621 | − | |
| 622 | 613 | // add readInt(int) |
| 623 | − func = make_new_mfun( "int", "readInt", cherr_readintflags ); | |
| 624 | − func->add_arg( "int", "flags" ); | |
| 625 | − if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 614 | + // func = make_new_mfun( "int", "readInt", cherr_readintflags ); | |
| 615 | + // func->add_arg( "int", "flags" ); | |
| 616 | + // if( !type_engine_import_mfun( env, func ) ) goto error; | |
| 626 | 617 | |
| 627 | 618 | // add readFloat() |
| 628 | 619 | // func = make_new_mfun( "float", "readFloat", cherr_readfloat ); |
| 620 | + // func->add_arg( "int", "flags" ); | |
| 629 | 621 | // if( !type_engine_import_mfun( env, func ) ) goto error; |
| 630 | 622 | |
| 631 | 623 | // add eof() |
| @@ -1507,14 +1499,31 @@ error: · 875 unmodified lines | ||
| 1507 | 1499 | //----------------------------------------------------------------------------- |
| 1508 | 1500 | // IO API |
| 1509 | 1501 | //----------------------------------------------------------------------------- |
| 1510 | −CK_DLL_MFUN( io_dummy ) | |
| 1502 | +CK_DLL_MFUN( io_dummy_int ) | |
| 1511 | 1503 | { |
| 1512 | − EM_error3( "(IO): internal error! inside an abstract function! help!" ); | |
| 1513 | − EM_error3( " note: please hunt down someone (e.g., Ge) to fix this..." ); | |
| 1514 | − assert( FALSE ); | |
| 1504 | + // EM_error3( "(IO): internal error! inside an abstract function! help!" ); | |
| 1505 | + // EM_error3( " note: please hunt down someone (e.g., Ge) to fix this..." ); | |
| 1506 | + // assert( FALSE ); | |
| 1515 | 1507 | RETURN->v_int = 0; |
| 1516 | 1508 | } |
| 1517 | 1509 | |
| 1510 | +CK_DLL_MFUN( io_dummy_float ) | |
| 1511 | +{ | |
| 1512 | + // return 0.0 | |
| 1513 | + RETURN->v_float = 0; | |
| 1514 | +} | |
| 1515 | + | |
| 1516 | +CK_DLL_MFUN( io_dummy_string ) | |
| 1517 | +{ | |
| 1518 | + // return NULL | |
| 1519 | + RETURN->v_string = NULL; | |
| 1520 | +} | |
| 1521 | + | |
| 1522 | +CK_DLL_MFUN( io_dummy_void ) | |
| 1523 | +{ | |
| 1524 | + // do nothing | |
| 1525 | +} | |
| 1526 | + | |
| 1518 | 1527 | CK_DLL_SFUN( io_newline ) |
| 1519 | 1528 | { |
| 1520 | 1529 | RETURN->v_string = g_newline; |
| @@ -1681,21 +1690,18 @@ CK_DLL_MFUN( fileio_readintflags ) · 160 unmodified lines | ||
| 1681 | 1690 | |
| 1682 | 1691 | CK_DLL_MFUN( fileio_readint8 ) |
| 1683 | 1692 | { |
| 1684 | − t_CKINT flags = GET_NEXT_INT(ARGS); | |
| 1685 | 1693 | Chuck_IO_File * f = (Chuck_IO_File *)SELF; |
| 1686 | 1694 | RETURN->v_int = f->readInt( Chuck_IO::INT8 ); |
| 1687 | 1695 | } |
| 1688 | 1696 | |
| 1689 | 1697 | CK_DLL_MFUN( fileio_readint16 ) |
| 1690 | 1698 | { |
| 1691 | − t_CKINT flags = GET_NEXT_INT(ARGS); | |
| 1692 | 1699 | Chuck_IO_File * f = (Chuck_IO_File *)SELF; |
| 1693 | 1700 | RETURN->v_int = f->readInt( Chuck_IO::INT16 ); |
| 1694 | 1701 | } |
| 1695 | 1702 | |
| 1696 | 1703 | CK_DLL_MFUN( fileio_readint32 ) |
| 1697 | 1704 | { |
| 1698 | − t_CKINT flags = GET_NEXT_INT(ARGS); | |
| 1699 | 1705 | Chuck_IO_File * f = (Chuck_IO_File *)SELF; |
| 1700 | 1706 | RETURN->v_int = f->readInt( Chuck_IO::INT32 ); |
| 1701 | 1707 | } |
| @@ -1703,8 +1709,14 @@ CK_DLL_MFUN( fileio_readint32 ) · 1 unmodified line | ||
| 1703 | 1709 | CK_DLL_MFUN( fileio_readfloat ) |
| 1704 | 1710 | { |
| 1705 | 1711 | Chuck_IO_File * f = (Chuck_IO_File *)SELF; |
| 1706 | − t_CKFLOAT ret = f->readFloat(); | |
| 1707 | − RETURN->v_float = ret; | |
| 1712 | + RETURN->v_float = f->readFloat(); | |
| 1713 | +} | |
| 1714 | + | |
| 1715 | +CK_DLL_MFUN( fileio_readfloatflags ) | |
| 1716 | +{ | |
| 1717 | + Chuck_IO_File * f = (Chuck_IO_File *)SELF; | |
| 1718 | + t_CKINT flags = GET_NEXT_INT(ARGS); | |
| 1719 | + RETURN->v_float = f->readFloat( flags ); | |
| 1708 | 1720 | } |
| 1709 | 1721 | |
| 1710 | 1722 | CK_DLL_MFUN( fileio_eof ) |
| @@ -1835,6 +1847,7 @@ CK_DLL_MFUN( fileio_writeintflags ) · 124 unmodified lines | ||
| 1835 | 1847 | CK_DLL_MFUN( fileio_writefloat ) |
| 1836 | 1848 | { |
| 1837 | 1849 | t_CKFLOAT val = GET_NEXT_FLOAT(ARGS); |
| 1850 | + t_CKINT flagsDefault = Chuck_IO::FLOAT32; | |
| 1838 | 1851 | Chuck_IO_File * f = (Chuck_IO_File *)SELF; |
| 1839 | 1852 | |
| 1840 | 1853 | #ifndef __DISABLE_FILEIO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK) |
| @@ -1845,6 +1858,7 @@ CK_DLL_MFUN( fileio_writefloat ) · 4 unmodified lines | ||
| 1845 | 1858 | args->RETURN = (void *)RETURN; |
| 1846 | 1859 | args->fileio_obj = f; |
| 1847 | 1860 | args->floatArg = val; |
| 1861 | + args->intArg = flagsDefault; // 1.5.0.1 (ge) add default | |
| 1848 | 1862 | // set shred to wait for I/O completion |
| 1849 | 1863 | assert( SHRED != NULL ); |
| 1850 | 1864 | f->m_asyncEvent->wait( SHRED, SHRED->vm_ref ); |
| @@ -1861,10 +1875,48 @@ CK_DLL_MFUN( fileio_writefloat ) · 10 unmodified lines | ||
| 1861 | 1875 | } |
| 1862 | 1876 | } |
| 1863 | 1877 | } else { |
| 1864 | − f->write(val); | |
| 1878 | + f->write( val, flagsDefault ); | |
| 1865 | 1879 | } |
| 1866 | 1880 | #else |
| 1867 | − f->write(val); | |
| 1881 | + f->write( val, flagsDefault ); | |
| 1882 | +#endif //__DISABLE_FILEIO__ | |
| 1883 | +} | |
| 1884 | + | |
| 1885 | +CK_DLL_MFUN( fileio_writefloatflags ) | |
| 1886 | +{ | |
| 1887 | + t_CKFLOAT val = GET_NEXT_FLOAT(ARGS); | |
| 1888 | + t_CKINT flags = GET_NEXT_INT(ARGS); | |
| 1889 | + Chuck_IO_File * f = (Chuck_IO_File *)SELF; | |
| 1890 | + | |
| 1891 | +#ifndef __DISABLE_FILEIO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK) | |
| 1892 | + if (f->mode() == Chuck_IO::MODE_ASYNC) | |
| 1893 | + { | |
| 1894 | + // set up arguments | |
| 1895 | + Chuck_IO::async_args *args = new Chuck_IO::async_args; | |
| 1896 | + args->RETURN = (void *)RETURN; | |
| 1897 | + args->fileio_obj = f; | |
| 1898 | + args->floatArg = val; | |
| 1899 | + args->intArg = flags; // 1.5.0.1 (ge) added | |
| 1900 | + // set shred to wait for I/O completion | |
| 1901 | + assert( SHRED != NULL ); | |
| 1902 | + f->m_asyncEvent->wait( SHRED, SHRED->vm_ref ); | |
| 1903 | + // start thread | |
| 1904 | + bool ret = f->m_thread->start( f->writeFloat_thread, (void *)args ); | |
| 1905 | + if (!ret) { | |
| 1906 | + // for some reason, the XThread object needs to be | |
| 1907 | + // deleted and reconstructed every time after call #375 | |
| 1908 | + delete f->m_thread; | |
| 1909 | + f->m_thread = new XThread; | |
| 1910 | + ret = f->m_thread->start( f->writeFloat_thread, (void *)args ); | |
| 1911 | + if (!ret) { | |
| 1912 | + EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" ); | |
| 1913 | + } | |
| 1914 | + } | |
| 1915 | + } else { | |
| 1916 | + f->write( val,flags ); | |
| 1917 | + } | |
| 1918 | +#else | |
| 1919 | + f->write( val,flags ); | |
| 1868 | 1920 | #endif //__DISABLE_FILEIO__ |
| 1869 | 1921 | } |
| 1870 | 1922 | |
| @@ -3123,7 +3175,7 @@ t_CKINT Chuck_IO_Serial::readInt( t_CKINT flags ) · 1252 unmodified lines | ||
| 3123 | 3175 | |
| 3124 | 3176 | if( m_flags & Chuck_IO::TYPE_BINARY ) |
| 3125 | 3177 | { |
| 3126 | − if( flags & INT8 ) | |
| 3178 | + if( flags & Chuck_IO::INT8 || flags & Chuck_IO::UINT8 ) // 1.5.0.1 (ge) added UINT | |
| 3127 | 3179 | { |
| 3128 | 3180 | uint8_t byte = 0; |
| 3129 | 3181 | if(!fread(&byte, 1, 1, m_cfd)) |
| @@ -3131,7 +3183,7 @@ t_CKINT Chuck_IO_Serial::readInt( t_CKINT flags ) · 1 unmodified line | ||
| 3131 | 3183 | |
| 3132 | 3184 | i = byte; |
| 3133 | 3185 | } |
| 3134 | − else if( flags & INT16 ) | |
| 3186 | + else if( flags & Chuck_IO::INT16 || flags & Chuck_IO::UINT16 ) // 1.5.0.1 (ge) added UINT | |
| 3135 | 3187 | { |
| 3136 | 3188 | uint16_t word = 0; |
| 3137 | 3189 | if(!fread(&word, 2, 1, m_cfd)) |
| @@ -3139,7 +3191,7 @@ t_CKINT Chuck_IO_Serial::readInt( t_CKINT flags ) · 1 unmodified line | ||
| 3139 | 3191 | |
| 3140 | 3192 | i = word; |
| 3141 | 3193 | } |
| 3142 | − else if( flags & INT32 ) | |
| 3194 | + else if( flags & Chuck_IO::INT32 || flags & Chuck_IO::UINT32 ) // 1.5.0.1 (ge) added UINT | |
| 3143 | 3195 | { |
| 3144 | 3196 | uint32_t dword = 0; |
| 3145 | 3197 | if(!fread(&dword, 4, 1, m_cfd)) |
| @@ -3147,6 +3199,46 @@ t_CKINT Chuck_IO_Serial::readInt( t_CKINT flags ) · 1 unmodified line | ||
| 3147 | 3199 | |
| 3148 | 3200 | i = dword; |
| 3149 | 3201 | } |
| 3202 | + else if( flags & Chuck_IO::INT64 || flags & Chuck_IO::UINT64 ) // 1.5.0.1 (ge) added UINT and 64-bit | |
| 3203 | + { | |
| 3204 | + uint64_t val = 0; | |
| 3205 | + if(!fread(&val, 8, 1, m_cfd)) | |
| 3206 | + EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed"); | |
| 3207 | + | |
| 3208 | + i = val; | |
| 3209 | + } | |
| 3210 | + else if( flags & Chuck_IO::SINT8 ) // 1.5.0.1 (ge) added SINT | |
| 3211 | + { | |
| 3212 | + int8_t byte = 0; | |
| 3213 | + if(!fread(&byte, 1, 1, m_cfd)) | |
| 3214 | + EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed"); | |
| 3215 | + | |
| 3216 | + i = byte; | |
| 3217 | + } | |
| 3218 | + else if( flags & Chuck_IO::SINT16 ) // 1.5.0.1 (ge) added SINT | |
| 3219 | + { | |
| 3220 | + int16_t word = 0; | |
| 3221 | + if(!fread(&word, 2, 1, m_cfd)) | |
| 3222 | + EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed"); | |
| 3223 | + | |
| 3224 | + i = word; | |
| 3225 | + } | |
| 3226 | + else if( flags & Chuck_IO::SINT32 ) // 1.5.0.1 (ge) added SINT | |
| 3227 | + { | |
| 3228 | + int32_t dword = 0; | |
| 3229 | + if(!fread(&dword, 4, 1, m_cfd)) | |
| 3230 | + EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed"); | |
| 3231 | + | |
| 3232 | + i = dword; | |
| 3233 | + } | |
| 3234 | + else if( flags & Chuck_IO::SINT64 ) // 1.5.0.1 (ge) added SINT and 64-bit | |
| 3235 | + { | |
| 3236 | + int64_t val = 0; | |
| 3237 | + if(!fread(&val, 8, 1, m_cfd)) | |
| 3238 | + EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed"); | |
| 3239 | + | |
| 3240 | + i = val; | |
| 3241 | + } | |
| 3150 | 3242 | } |
| 3151 | 3243 | else |
| 3152 | 3244 | { |
| @@ -3157,7 +3249,13 @@ t_CKINT Chuck_IO_Serial::readInt( t_CKINT flags ) · 4 unmodified lines | ||
| 3157 | 3249 | return i; |
| 3158 | 3250 | } |
| 3159 | 3251 | |
| 3160 | −t_CKFLOAT Chuck_IO_Serial::readFloat() | |
| 3252 | +t_CKFLOAT Chuck_IO_Serial::readFloat( ) | |
| 3253 | +{ | |
| 3254 | + // 1.5.0.1 (ge) redirect | |
| 3255 | + return readFloat( Chuck_IO::FLOAT32 ); | |
| 3256 | +} | |
| 3257 | + | |
| 3258 | +t_CKFLOAT Chuck_IO_Serial::readFloat( t_CKINT flags ) | |
| 3161 | 3259 | { |
| 3162 | 3260 | if( !good() ) |
| 3163 | 3261 | { |
| @@ -3175,8 +3273,26 @@ t_CKFLOAT Chuck_IO_Serial::readFloat() · 11 unmodified lines | ||
| 3175 | 3273 | |
| 3176 | 3274 | if( m_flags & Chuck_IO::TYPE_BINARY ) |
| 3177 | 3275 | { |
| 3178 | − if(!fread(&f, 4, 1, m_cfd)) | |
| 3179 | − EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed"); | |
| 3276 | + // check size | |
| 3277 | + if( flags & Chuck_IO::FLOAT32 ) | |
| 3278 | + { | |
| 3279 | + float v = 0.0f; | |
| 3280 | + if(!fread(&v, 4, 1, m_cfd)) | |
| 3281 | + EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed"); | |
| 3282 | + f = v; | |
| 3283 | + } | |
| 3284 | + else if( flags & Chuck_IO::FLOAT64 ) | |
| 3285 | + { | |
| 3286 | + double v = 0.0; | |
| 3287 | + if( !fread(&v, 8, 1, m_cfd) ) | |
| 3288 | + EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed"); | |
| 3289 | + f = v; | |
| 3290 | + } | |
| 3291 | + else | |
| 3292 | + { | |
| 3293 | + // error message | |
| 3294 | + EM_log(CK_LOG_WARNING, "(Serial.readFloat): invalid or unsupported size flag"); | |
| 3295 | + } | |
| 3180 | 3296 | } |
| 3181 | 3297 | else |
| 3182 | 3298 | { |
| @@ -3213,7 +3329,7 @@ t_CKBOOL Chuck_IO_Serial::readString( std::string & str ) · 30 unmodified lines | ||
| 3213 | 3329 | return FALSE; |
| 3214 | 3330 | } |
| 3215 | 3331 | |
| 3216 | − str = (char *) m_tmp_buf; | |
| 3332 | + str = (char *) m_tmp_buf; | |
| 3217 | 3333 | |
| 3218 | 3334 | return TRUE; |
| 3219 | 3335 | } |
| @@ -3304,11 +3420,11 @@ void Chuck_IO_Serial::write( const std::string & val ) · 84 unmodified lines | ||
| 3304 | 3420 | |
| 3305 | 3421 | void Chuck_IO_Serial::write( t_CKINT val ) |
| 3306 | 3422 | { |
| 3307 | − write( val, 4 ); | |
| 3423 | + write( val, Chuck_IO::INT32 ); | |
| 3308 | 3424 | } |
| 3309 | 3425 | |
| 3310 | 3426 | |
| 3311 | −void Chuck_IO_Serial::write( t_CKINT val, t_CKINT size ) | |
| 3427 | +void Chuck_IO_Serial::write( t_CKINT val, t_CKINT flags ) | |
| 3312 | 3428 | { |
| 3313 | 3429 | if( !good() ) |
| 3314 | 3430 | { |
| @@ -3316,9 +3432,11 @@ void Chuck_IO_Serial::write( t_CKINT val, t_CKINT size ) · 1 unmodified line | ||
| 3316 | 3432 | return; |
| 3317 | 3433 | } |
| 3318 | 3434 | |
| 3319 | − if(!(size == 1 || size == 2 || size == 4 || size == 8)) | |
| 3435 | + // check flags | |
| 3436 | + if( (flags & Chuck_IO::INT8 ) || (flags & Chuck_IO::INT16) || | |
| 3437 | + (flags & Chuck_IO::INT32) || (flags & Chuck_IO::INT64) ) | |
| 3320 | 3438 | { |
| 3321 | − EM_log(CK_LOG_WARNING, "(Serial.write): warning: invalid int size %li, ignoring write request", size); | |
| 3439 | + EM_log( CK_LOG_WARNING, "(Serial.write): warning: invalid int size flag, ignoring write request" ); | |
| 3322 | 3440 | return; |
| 3323 | 3441 | } |
| 3324 | 3442 | |
| @@ -3350,11 +3468,23 @@ void Chuck_IO_Serial::write( t_CKINT val, t_CKINT size ) · 25 unmodified lines | ||
| 3350 | 3468 | } |
| 3351 | 3469 | else |
| 3352 | 3470 | { |
| 3353 | − char * buf = (char *)&val; | |
| 3354 | − | |
| 3355 | − // assume 4-byte int | |
| 3471 | + // 1.5.0.1 (ge) check and handle individually | |
| 3472 | + uint8_t v8 = (uint8_t)val; | |
| 3473 | + uint16_t v16 = (uint16_t)val; | |
| 3474 | + uint32_t v32 = (uint32_t)val; | |
| 3475 | + uint64_t v64 = (uint64_t)val; | |
| 3476 | + char * buf = NULL; | |
| 3477 | + t_CKINT size = 0; | |
| 3478 | + | |
| 3479 | + if( flags & Chuck_IO::INT8 ) { buf = (char *)&v8; size = 1; } | |
| 3480 | + else if( flags & Chuck_IO::INT16 ) { buf = (char *)&v16; size = 2; } | |
| 3481 | + else if( flags & Chuck_IO::INT32 ) { buf = (char *)&v32; size = 4; } | |
| 3482 | + else if( flags & Chuck_IO::INT64 ) { buf = (char *)&v64; size = 8; } | |
| 3483 | + // other size values should be caught above | |
| 3484 | + | |
| 3485 | + // put into write buffer | |
| 3356 | 3486 | for(int i = 0; i < size; i++) |
| 3357 | − m_writeBuffer.put(buf[i]); | |
| 3487 | + m_writeBuffer.put( buf[i] ); | |
| 3358 | 3488 | |
| 3359 | 3489 | Request r; |
| 3360 | 3490 | r.m_type = TYPE_WRITE; |
| @@ -3373,14 +3503,38 @@ void Chuck_IO_Serial::write( t_CKINT val, t_CKINT size ) · 12 unmodified lines | ||
| 3373 | 3503 | } |
| 3374 | 3504 | else |
| 3375 | 3505 | { |
| 3376 | − // assume 4-byte int | |
| 3377 | − char * buf = (char *) &val; | |
| 3378 | − fwrite(buf, 1, size, m_cfd); | |
| 3506 | + // 1.5.0.1 (ge) check and handle individually | |
| 3507 | + if( flags & Chuck_IO::INT8 ) | |
| 3508 | + { | |
| 3509 | + uint8_t v = (uint8_t)val; | |
| 3510 | + fwrite( (char*)&v, 1, 1, m_cfd ); | |
| 3511 | + } | |
| 3512 | + else if( flags & Chuck_IO::INT16 ) | |
| 3513 | + { | |
| 3514 | + uint16_t v = (uint16_t)val; | |
| 3515 | + fwrite( (char*)&v, 1, 2, m_cfd ); | |
| 3516 | + } | |
| 3517 | + else if( flags & Chuck_IO::INT32 ) | |
| 3518 | + { | |
| 3519 | + uint32_t v = (uint32_t)val; | |
| 3520 | + fwrite( (char*)&v, 1, 4, m_cfd ); | |
| 3521 | + } | |
| 3522 | + else if( flags & Chuck_IO::INT64 ) | |
| 3523 | + { | |
| 3524 | + uint64_t v = (uint64_t)val; | |
| 3525 | + fwrite( (char*)&v, 1, 8, m_cfd ); | |
| 3526 | + } | |
| 3527 | + // other size values should be caught above | |
| 3379 | 3528 | } |
| 3380 | 3529 | } |
| 3381 | 3530 | } |
| 3382 | 3531 | |
| 3383 | 3532 | void Chuck_IO_Serial::write( t_CKFLOAT val ) |
| 3533 | +{ | |
| 3534 | + this->write( val, Chuck_IO::FLOAT32 ); | |
| 3535 | +} | |
| 3536 | + | |
| 3537 | +void Chuck_IO_Serial::write( t_CKFLOAT val, t_CKINT flags ) | |
| 3384 | 3538 | { |
| 3385 | 3539 | if( !good() ) |
| 3386 | 3540 | { |
| @@ -3416,11 +3570,26 @@ void Chuck_IO_Serial::write( t_CKFLOAT val ) · 29 unmodified lines | ||
| 3416 | 3570 | } |
| 3417 | 3571 | else |
| 3418 | 3572 | { |
| 3419 | − char * buf = (char *) &val; | |
| 3420 | − | |
| 3421 | − // assume 4-byte float | |
| 3422 | − for(int i = 0; i < 4; i++) | |
| 3423 | − m_writeBuffer.put(buf[i]); | |
| 3573 | + // 1.5.0.1 (ge) check datatype size flag | |
| 3574 | + if( flags & Chuck_IO::FLOAT32 ) | |
| 3575 | + { | |
| 3576 | + float v = (float)val; | |
| 3577 | + char * buf = (char *)&v; | |
| 3578 | + for(int i = 0; i < sizeof(v); i++) | |
| 3579 | + m_writeBuffer.put(buf[i]); | |
| 3580 | + } | |
| 3581 | + else if( flags & Chuck_IO::FLOAT64 ) | |
| 3582 | + { | |
| 3583 | + double v = (double)val; | |
| 3584 | + char * buf = (char *)&v; | |
| 3585 | + for(int i = 0; i < sizeof(v); i++) | |
| 3586 | + m_writeBuffer.put(buf[i]); | |
| 3587 | + } | |
| 3588 | + else | |
| 3589 | + { | |
| 3590 | + // error message | |
| 3591 | + EM_log(CK_LOG_WARNING, "(Serial.write): invalid or unsupported size flag"); | |
| 3592 | + } | |
| 3424 | 3593 | |
| 3425 | 3594 | Request r; |
| 3426 | 3595 | r.m_type = TYPE_WRITE; |
| @@ -3437,12 +3606,28 @@ void Chuck_IO_Serial::write( t_CKFLOAT val ) · 10 unmodified lines | ||
| 3437 | 3606 | { |
| 3438 | 3607 | fprintf( m_cfd, "%f", val ); |
| 3439 | 3608 | } |
| 3440 | − else | |
| 3609 | + else // BINARY | |
| 3441 | 3610 | { |
| 3442 | − // assume 4-byte int | |
| 3443 | − char * buf = (char *) &val; | |
| 3444 | − // 1.5.0.0 (ge) | fixed from size to sizeof | |
| 3445 | − fwrite(buf, 1, sizeof(val), m_cfd); | |
| 3611 | + // 1.5.0.1 (ge) check datatype size flag | |
| 3612 | + if( flags & Chuck_IO::FLOAT32 ) | |
| 3613 | + { | |
| 3614 | + float v = (float)val; | |
| 3615 | + char * buf = (char *)&v; | |
| 3616 | + for(int i = 0; i < sizeof(v); i++) | |
| 3617 | + m_writeBuffer.put(buf[i]); | |
| 3618 | + } | |
| 3619 | + else if( flags & Chuck_IO::FLOAT64 ) | |
| 3620 | + { | |
| 3621 | + double v = (double)val; | |
| 3622 | + char * buf = (char *)&v; | |
| 3623 | + for(int i = 0; i < sizeof(v); i++) | |
| 3624 | + m_writeBuffer.put(buf[i]); | |
| 3625 | + } | |
| 3626 | + else | |
| 3627 | + { | |
| 3628 | + // error message | |
| 3629 | + EM_log(CK_LOG_WARNING, "(Serial.write): invalid or unsupported size flag"); | |
| 3630 | + } | |
| 3446 | 3631 | } |
| 3447 | 3632 | } |
| 3448 | 3633 | } |
| @@ -72,7 +72,10 @@ t_CKBOOL init_class_serialio( Chuck_Env * env ); · 71 unmodified lines | ||
| 72 | 72 | //----------------------------------------------------------------------------- |
| 73 | 73 | // io API |
| 74 | 74 | //----------------------------------------------------------------------------- |
| 75 | −CK_DLL_MFUN( io_dummy ); | |
| 75 | +CK_DLL_MFUN( io_dummy_int ); | |
| 76 | +CK_DLL_MFUN( io_dummy_float ); | |
| 77 | +CK_DLL_MFUN( io_dummy_string ); | |
| 78 | +CK_DLL_MFUN( io_dummy_void ); | |
| 76 | 79 | CK_DLL_SFUN( io_newline ); |
| 77 | 80 | CK_DLL_SFUN( io_openfile ); |
| 78 | 81 | |
| @@ -103,12 +106,14 @@ CK_DLL_MFUN( fileio_readint8 ); · 24 unmodified lines | ||
| 103 | 106 | CK_DLL_MFUN( fileio_readint16 ); |
| 104 | 107 | CK_DLL_MFUN( fileio_readint32 ); |
| 105 | 108 | CK_DLL_MFUN( fileio_readfloat ); |
| 109 | +CK_DLL_MFUN( fileio_readfloatflags ); | |
| 106 | 110 | CK_DLL_MFUN( fileio_eof ); |
| 107 | 111 | CK_DLL_MFUN( fileio_more ); |
| 108 | 112 | CK_DLL_MFUN( fileio_writestring ); |
| 109 | 113 | CK_DLL_MFUN( fileio_writeint ); |
| 110 | 114 | CK_DLL_MFUN( fileio_writeintflags ); |
| 111 | 115 | CK_DLL_MFUN( fileio_writefloat ); |
| 116 | +CK_DLL_MFUN( fileio_writefloatflags ); | |
| 112 | 117 | // #endif // __DISABLE_FILEIO__ |
| 113 | 118 | |
| 114 | 119 | |
| @@ -312,15 +317,17 @@ public: · 197 unmodified lines | ||
| 312 | 317 | virtual Chuck_String * readLine(); |
| 313 | 318 | virtual t_CKINT readInt( t_CKINT flags ); |
| 314 | 319 | virtual t_CKFLOAT readFloat(); |
| 320 | + virtual t_CKFLOAT readFloat( t_CKINT flags ); | |
| 315 | 321 | virtual t_CKBOOL readString( std::string & str ); |
| 316 | 322 | virtual t_CKBOOL eof(); |
| 317 | 323 | |
| 318 | 324 | // writing |
| 319 | 325 | virtual void write( const std::string & val ); |
| 320 | 326 | virtual void write( t_CKINT val ); |
| 321 | − virtual void write( t_CKINT val, t_CKINT size ); | |
| 327 | + virtual void write( t_CKINT val, t_CKINT flags ); | |
| 322 | 328 | virtual void writeBytes( Chuck_Array4 * arr ); |
| 323 | 329 | virtual void write( t_CKFLOAT val ); |
| 330 | + virtual void write( t_CKFLOAT val, t_CKINT flags ); | |
| 324 | 331 | |
| 325 | 332 | // serial stuff |
| 326 | 333 | virtual t_CKBOOL setBaudRate( t_CKUINT rate ); |
| @@ -55,18 +55,28 @@ using namespace std; · 54 unmodified lines | ||
| 55 | 55 | t_CKBOOL Chuck_VM_Object::our_locks_in_effect = TRUE; |
| 56 | 56 | |
| 57 | 57 | // constants |
| 58 | −const t_CKINT Chuck_IO::TYPE_ASCII = 0x1; | |
| 58 | +const t_CKINT Chuck_IO::TYPE_ASCII = 0x1; | |
| 59 | 59 | const t_CKINT Chuck_IO::TYPE_BINARY = 0x2; |
| 60 | −const t_CKINT Chuck_IO::INT8 = 0x10; | |
| 61 | −const t_CKINT Chuck_IO::INT16 = 0x20; | |
| 62 | −const t_CKINT Chuck_IO::INT24 = 0x40; | |
| 63 | −const t_CKINT Chuck_IO::INT32 = 0x80; | |
| 64 | −const t_CKINT Chuck_IO::INT64 = 0x100; | |
| 65 | −const t_CKINT Chuck_IO::FLOAT32 = 0x200; | |
| 66 | −const t_CKINT Chuck_IO::FLOAT64 = 0x400; | |
| 67 | −const t_CKINT Chuck_IO::FLAG_READ_WRITE = 0x100; | |
| 68 | −const t_CKINT Chuck_IO::FLAG_READONLY = 0x200; | |
| 69 | −const t_CKINT Chuck_IO::FLAG_WRITEONLY = 0x400; | |
| 60 | +const t_CKINT Chuck_IO::FLOAT32 = 0x10; | |
| 61 | +const t_CKINT Chuck_IO::FLOAT64 = 0x20; | |
| 62 | +const t_CKINT Chuck_IO::INT8 = 0x100; | |
| 63 | +const t_CKINT Chuck_IO::INT16 = 0x200; | |
| 64 | +const t_CKINT Chuck_IO::INT24 = 0x400; | |
| 65 | +const t_CKINT Chuck_IO::INT32 = 0x800; | |
| 66 | +const t_CKINT Chuck_IO::INT64 = 0x1000; | |
| 67 | +const t_CKINT Chuck_IO::SINT8 = 0x2000; | |
| 68 | +const t_CKINT Chuck_IO::SINT16 = 0x4000; | |
| 69 | +const t_CKINT Chuck_IO::SINT24 = 0x8000; | |
| 70 | +const t_CKINT Chuck_IO::SINT32 = 0x10000; | |
| 71 | +const t_CKINT Chuck_IO::SINT64 = 0x20000; | |
| 72 | +const t_CKINT Chuck_IO::UINT8 = 0x40000; | |
| 73 | +const t_CKINT Chuck_IO::UINT16 = 0x80000; | |
| 74 | +const t_CKINT Chuck_IO::UINT24 = 0x100000; | |
| 75 | +const t_CKINT Chuck_IO::UINT32 = 0x200000; | |
| 76 | +const t_CKINT Chuck_IO::UINT64 = 0x400000; | |
| 77 | +const t_CKINT Chuck_IO::FLAG_READONLY = 0x100; | |
| 78 | +const t_CKINT Chuck_IO::FLAG_WRITEONLY = 0x200; | |
| 79 | +const t_CKINT Chuck_IO::FLAG_READ_WRITE = 0x400; | |
| 70 | 80 | const t_CKINT Chuck_IO::FLAG_APPEND = 0x800; |
| 71 | 81 | |
| 72 | 82 | #ifndef __DISABLE_THREADS__ |
| @@ -3470,36 +3480,54 @@ t_CKINT Chuck_IO_File::readInt( t_CKINT flags ) · 3397 unmodified lines | ||
| 3470 | 3480 | else if( m_flags & TYPE_BINARY ) |
| 3471 | 3481 | { |
| 3472 | 3482 | // binary |
| 3473 | − if( flags & Chuck_IO::INT32 ) | |
| 3483 | + if( flags & Chuck_IO::INT8 || flags & Chuck_IO::UINT8 ) // 1.5.0.1 (ge) added UINT | |
| 3474 | 3484 | { |
| 3475 | − // 32-bit | |
| 3476 | − t_CKINT i; | |
| 3485 | + // unsigned 8-bit | |
| 3486 | + uint8_t i; // 1.5.0.1 (ge) changed to int8_t from unsigned char | |
| 3487 | + m_io.read( (char *)&i, 1 ); | |
| 3488 | + if( m_io.gcount() != 1 ) | |
| 3489 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 3490 | + else if( m_io.fail() ) | |
| 3491 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 3492 | + return (t_CKINT)i; | |
| 3493 | + } | |
| 3494 | + else if( flags & Chuck_IO::INT16 || flags & Chuck_IO::UINT16 ) // 1.5.0.1 (ge) added UINT | |
| 3495 | + { | |
| 3496 | + // unsigned 16-bit | |
| 3497 | + uint16_t i; // 1.5.0.1 (ge) changed to uint16_t from t_CKINT | |
| 3498 | + m_io.read( (char *)&i, 2 ); | |
| 3499 | + if( m_io.gcount() != 2 ) | |
| 3500 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 3501 | + else if (m_io.fail()) | |
| 3502 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 3503 | + return (t_CKINT)i; | |
| 3504 | + } | |
| 3505 | + else if( flags & Chuck_IO::INT32 || flags & Chuck_IO::UINT32 ) // 1.5.0.1 (ge) added UINT | |
| 3506 | + { | |
| 3507 | + // unsigned 32-bit | |
| 3508 | + uint32_t i; // 1.5.0.1 (ge) changed to uint32_t from t_CKINT | |
| 3477 | 3509 | m_io.read( (char *)&i, 4 ); |
| 3478 | 3510 | if (m_io.gcount() != 4) |
| 3479 | 3511 | EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); |
| 3480 | 3512 | else if (m_io.fail()) |
| 3481 | 3513 | EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); |
| 3482 | − return i; | |
| 3514 | + return (t_CKINT)i; | |
| 3483 | 3515 | } |
| 3484 | − else if( flags & Chuck_IO::INT16 ) | |
| 3516 | + else if( flags & Chuck_IO::INT64 || flags & Chuck_IO::UINT64 ) // 1.5.0.1 (ge) added 64-bit | |
| 3485 | 3517 | { |
| 3486 | − // 16-bit | |
| 3487 | − // int16_t i; | |
| 3488 | − // issue: 64-bit | |
| 3489 | − short i; | |
| 3490 | − m_io.read( (char *)&i, 2 ); | |
| 3491 | − if( m_io.gcount() != 2 ) | |
| 3518 | + // unsigned 64-bit | |
| 3519 | + uint64_t i; // 1.5.0.1 (ge) added | |
| 3520 | + m_io.read( (char *)&i, 8 ); | |
| 3521 | + if (m_io.gcount() != 8) | |
| 3492 | 3522 | EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); |
| 3493 | 3523 | else if (m_io.fail()) |
| 3494 | 3524 | EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); |
| 3495 | − return (t_CKINT) i; | |
| 3525 | + return (t_CKINT)i; // TODO: handle signed vs unsigned | |
| 3496 | 3526 | } |
| 3497 | − else if( flags & Chuck_IO::INT8 ) | |
| 3527 | + else if( flags & Chuck_IO::SINT8 ) // 1.5.0.1 (ge) added SINT | |
| 3498 | 3528 | { |
| 3499 | − // 8-bit | |
| 3500 | − // int8_t i; | |
| 3501 | − // issue: 64-bit | |
| 3502 | − unsigned char i; | |
| 3529 | + // signed 8-bit | |
| 3530 | + int8_t i; | |
| 3503 | 3531 | m_io.read( (char *)&i, 1 ); |
| 3504 | 3532 | if( m_io.gcount() != 1 ) |
| 3505 | 3533 | EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); |
| @@ -3507,9 +3535,42 @@ t_CKINT Chuck_IO_File::readInt( t_CKINT flags ) · 1 unmodified line | ||
| 3507 | 3535 | EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); |
| 3508 | 3536 | return (t_CKINT)i; |
| 3509 | 3537 | } |
| 3538 | + else if( flags & Chuck_IO::SINT16 ) // 1.5.0.1 (ge) added SINT | |
| 3539 | + { | |
| 3540 | + // signed 16-bit | |
| 3541 | + int16_t i; | |
| 3542 | + m_io.read( (char *)&i, 2 ); | |
| 3543 | + if( m_io.gcount() != 2 ) | |
| 3544 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 3545 | + else if (m_io.fail()) | |
| 3546 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 3547 | + return (t_CKINT)i; | |
| 3548 | + } | |
| 3549 | + else if( flags & Chuck_IO::SINT32 ) // 1.5.0.1 (ge) added SINT | |
| 3550 | + { | |
| 3551 | + // signed 32-bit | |
| 3552 | + int32_t i; | |
| 3553 | + m_io.read( (char *)&i, 4 ); | |
| 3554 | + if (m_io.gcount() != 4) | |
| 3555 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 3556 | + else if (m_io.fail()) | |
| 3557 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 3558 | + return (t_CKINT)i; | |
| 3559 | + } | |
| 3560 | + else if( flags & Chuck_IO::SINT64 ) // 1.5.0.1 (ge) added SINT | |
| 3561 | + { | |
| 3562 | + // signed 64-bit | |
| 3563 | + int64_t i; | |
| 3564 | + m_io.read( (char *)&i, 4 ); | |
| 3565 | + if (m_io.gcount() != 8) | |
| 3566 | + EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" ); | |
| 3567 | + else if (m_io.fail()) | |
| 3568 | + EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 3569 | + return (t_CKINT)i; | |
| 3570 | + } | |
| 3510 | 3571 | else |
| 3511 | 3572 | { |
| 3512 | − EM_error3( "[chuck](via FileIO): readInt error: invalid int size flag" ); | |
| 3573 | + EM_error3( "[chuck](via FileIO): readInt error: invalid/unsupported int size flag" ); | |
| 3513 | 3574 | return 0; |
| 3514 | 3575 | } |
| 3515 | 3576 | } |
| @@ -3525,33 +3586,43 @@ t_CKINT Chuck_IO_File::readInt( t_CKINT flags ) · 9 unmodified lines | ||
| 3525 | 3586 | |
| 3526 | 3587 | //----------------------------------------------------------------------------- |
| 3527 | 3588 | // name: readFloat() |
| 3528 | −// desc: read next as (ascii) floating point value | |
| 3589 | +// desc: read next as floating point value; could be ASCII or BINARY | |
| 3529 | 3590 | //----------------------------------------------------------------------------- |
| 3530 | 3591 | t_CKFLOAT Chuck_IO_File::readFloat() |
| 3592 | +{ | |
| 3593 | + return this->readFloat( Chuck_IO::FLOAT32 ); | |
| 3594 | +} | |
| 3595 | + | |
| 3596 | + | |
| 3597 | + | |
| 3598 | + | |
| 3599 | +//----------------------------------------------------------------------------- | |
| 3600 | +// name: readFloat() | |
| 3601 | +// desc: read next as floating point value; could be ASCII or BINARY | |
| 3602 | +//----------------------------------------------------------------------------- | |
| 3603 | +t_CKFLOAT Chuck_IO_File::readFloat( t_CKINT flags ) | |
| 3531 | 3604 | { |
| 3532 | 3605 | // sanity |
| 3533 | − if (!(m_io.is_open())) { | |
| 3606 | + if( !(m_io.is_open()) ) { | |
| 3534 | 3607 | EM_error3( "[chuck](via FileIO): cannot readFloat: no file open" ); |
| 3535 | 3608 | return 0; |
| 3536 | 3609 | } |
| 3537 | − | |
| 3538 | − if (m_io.eof()) { | |
| 3610 | + if( m_io.eof() ) { | |
| 3539 | 3611 | EM_error3( "[chuck](via FileIO): cannot readFloat: EOF reached" ); |
| 3540 | 3612 | return 0; |
| 3541 | 3613 | } |
| 3542 | − | |
| 3543 | − if (m_io.fail()) { | |
| 3614 | + if( m_io.fail() ) { | |
| 3544 | 3615 | EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); |
| 3545 | 3616 | return 0; |
| 3546 | 3617 | } |
| 3547 | − | |
| 3548 | − if ( m_dir ) | |
| 3549 | − { | |
| 3618 | + if( m_dir ) { | |
| 3550 | 3619 | EM_error3( "[chuck](via FileIO): cannot read a directory" ); |
| 3551 | 3620 | return 0; |
| 3552 | 3621 | } |
| 3553 | 3622 | |
| 3554 | − if (m_flags & TYPE_ASCII) { | |
| 3623 | + // check mode | |
| 3624 | + if( m_flags & TYPE_ASCII ) | |
| 3625 | + { | |
| 3555 | 3626 | // ASCII |
| 3556 | 3627 | t_CKFLOAT val = 0; |
| 3557 | 3628 | m_io >> val; |
| @@ -3559,17 +3630,38 @@ t_CKFLOAT Chuck_IO_File::readFloat() · 1 unmodified line | ||
| 3559 | 3630 | // EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); |
| 3560 | 3631 | return val; |
| 3561 | 3632 | |
| 3562 | − } else if (m_flags & TYPE_BINARY) { | |
| 3563 | − // binary | |
| 3564 | − t_CKFLOAT i; | |
| 3565 | − m_io.read( (char *)&i, sizeof(t_CKFLOAT) ); | |
| 3566 | − if (m_io.gcount() != sizeof(t_CKFLOAT) ) | |
| 3567 | − EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" ); | |
| 3568 | − else if (m_io.fail()) | |
| 3569 | − EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" ); | |
| 3570 | − return i; | |
| 3571 | − | |
| 3572 | − } else { | |
| 3633 | + } | |
| 3634 | + else if( m_flags & TYPE_BINARY ) | |
| 3635 | + { | |
| 3636 | + // 1.5.0.1 | |
| 3637 | + if( flags & Chuck_IO::FLOAT32 ) | |
| 3638 | + { | |
| 3639 | + float i; | |
| 3640 | + m_io.read( (char *)&i, sizeof(float) ); | |
| 3641 | + if (m_io.gcount() != sizeof(float) ) | |
| 3642 | + EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" ); | |
| 3643 | + else if (m_io.fail()) | |
| 3644 | + EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); | |
| 3645 | + return (t_CKFLOAT)i; | |
| 3646 | + } | |
| 3647 | + else if( flags & Chuck_IO::FLOAT64 ) | |
| 3648 | + { | |
| 3649 | + double i; | |
| 3650 | + m_io.read( (char *)&i, sizeof(double) ); | |
| 3651 | + if (m_io.gcount() != sizeof(double) ) | |
| 3652 | + EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" ); | |
| 3653 | + else if (m_io.fail()) | |
| 3654 | + EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" ); | |
| 3655 | + return (t_CKFLOAT)i; | |
| 3656 | + } | |
| 3657 | + else | |
| 3658 | + { | |
| 3659 | + EM_error3( "[chuck](via FileIO): readFloat error: invalid/unsupported datatype size flag" ); | |
| 3660 | + return 0; | |
| 3661 | + } | |
| 3662 | + } | |
| 3663 | + else | |
| 3664 | + { | |
| 3573 | 3665 | EM_error3( "[chuck](via FileIO): readFloat error: invalid ASCII/binary flag" ); |
| 3574 | 3666 | return 0; |
| 3575 | 3667 | } |
| @@ -3794,14 +3886,64 @@ void Chuck_IO_File::write( t_CKINT val, t_CKINT flags ) · 218 unmodified lines | ||
| 3794 | 3886 | return; |
| 3795 | 3887 | } |
| 3796 | 3888 | |
| 3797 | − if (m_flags & TYPE_ASCII) { | |
| 3889 | + // check file mode | |
| 3890 | + if( m_flags & TYPE_ASCII ) // ASCII mode | |
| 3891 | + { | |
| 3892 | + // insert into file stream | |
| 3798 | 3893 | m_io << val; |
| 3799 | − } else if (m_flags & TYPE_BINARY) { | |
| 3800 | − int nBytes = 4; | |
| 3801 | − if(flags & INT8) nBytes = 1; | |
| 3802 | − else if(flags & INT16) nBytes = 2; | |
| 3803 | − else if(flags & INT32) nBytes = 4; | |
| 3804 | − m_io.write( (char *)&val, nBytes ); | |
| 3894 | + } | |
| 3895 | + else if( m_flags & TYPE_BINARY ) // BINARY mode | |
| 3896 | + { | |
| 3897 | + // check datatype size flags | |
| 3898 | + // 1.5.0.1 (ge) modified to new signed/unsigned handling | |
| 3899 | + if( flags & Chuck_IO::INT8 || flags & Chuck_IO::UINT8 ) | |
| 3900 | + { | |
| 3901 | + // unsigned 8-bit | |
| 3902 | + uint8_t v = val; | |
| 3903 | + m_io.write( (char *)&v, 1 ); | |
| 3904 | + } | |
| 3905 | + else if( flags & Chuck_IO::INT16 || flags & Chuck_IO::UINT16 ) | |
| 3906 | + { | |
| 3907 | + // unsigned 16-bit | |
| 3908 | + uint16_t v = val; | |
| 3909 | + m_io.write( (char *)&v, 2 ); | |
| 3910 | + } | |
| 3911 | + else if( flags & Chuck_IO::INT32 || flags & Chuck_IO::UINT32 ) | |
| 3912 | + { | |
| 3913 | + // unsigned 32-bit | |
| 3914 | + uint32_t v = val; | |
| 3915 | + m_io.write( (char *)&v, 4 ); | |
| 3916 | + } | |
| 3917 | + else if( flags & Chuck_IO::INT64 || flags & Chuck_IO::UINT64 ) | |
| 3918 | + { | |
| 3919 | + // unsigned 64-bit | |
| 3920 | + uint64_t v = val; | |
| 3921 | + m_io.write( (char *)&v, 8 ); | |
| 3922 | + } | |
| 3923 | + else if( flags & Chuck_IO::SINT8 ) | |
| 3924 | + { | |
| 3925 | + // signed 8-bit | |
| 3926 | + int8_t v = val; | |
| 3927 | + m_io.write( (char *)&v, 1 ); | |
| 3928 | + } | |
| 3929 | + else if( flags & Chuck_IO::SINT16 ) | |
| 3930 | + { | |
| 3931 | + // signed 16-bit | |
| 3932 | + int16_t v = val; | |
| 3933 | + m_io.write( (char *)&v, 2 ); | |
| 3934 | + } | |
| 3935 | + else if( flags & Chuck_IO::SINT32 ) | |
| 3936 | + { | |
| 3937 | + // signed 32-bit | |
| 3938 | + int32_t v = val; | |
| 3939 | + m_io.write( (char *)&v, 4 ); | |
| 3940 | + } | |
| 3941 | + else if( flags & Chuck_IO::SINT64 ) | |
| 3942 | + { | |
| 3943 | + // signed 32-bit | |
| 3944 | + int64_t v = val; | |
| 3945 | + m_io.write( (char *)&v, 8 ); | |
| 3946 | + } | |
| 3805 | 3947 | } else { |
| 3806 | 3948 | EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); |
| 3807 | 3949 | } |
| @@ -3816,32 +3958,64 @@ void Chuck_IO_File::write( t_CKINT val, t_CKINT flags ) · 8 unmodified lines | ||
| 3816 | 3958 | |
| 3817 | 3959 | //----------------------------------------------------------------------------- |
| 3818 | 3960 | // name: write( t_CKFLOAT val ) |
| 3819 | −// desc: ... | |
| 3961 | +// desc: write a floating point value | |
| 3820 | 3962 | //----------------------------------------------------------------------------- |
| 3821 | 3963 | void Chuck_IO_File::write( t_CKFLOAT val ) |
| 3964 | +{ | |
| 3965 | + this->write( val, Chuck_IO::FLOAT32 ); | |
| 3966 | +} | |
| 3967 | + | |
| 3968 | + | |
| 3969 | + | |
| 3970 | + | |
| 3971 | +//----------------------------------------------------------------------------- | |
| 3972 | +// name: write( t_CKFLOAT val, t_CKINT flags ) | |
| 3973 | +// desc: write a floating point value; binary mode will heed flags for size | |
| 3974 | +//----------------------------------------------------------------------------- | |
| 3975 | +void Chuck_IO_File::write( t_CKFLOAT val, t_CKINT flags ) | |
| 3822 | 3976 | { |
| 3823 | 3977 | // sanity |
| 3824 | − if (!(m_io.is_open())) { | |
| 3978 | + if( !(m_io.is_open()) ) { | |
| 3825 | 3979 | EM_error3( "[chuck](via FileIO): cannot write: no file open" ); |
| 3826 | 3980 | return; |
| 3827 | 3981 | } |
| 3828 | − | |
| 3829 | − if (m_io.fail()) { | |
| 3982 | + if( m_io.fail() ) { | |
| 3830 | 3983 | EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" ); |
| 3831 | 3984 | return; |
| 3832 | 3985 | } |
| 3833 | − | |
| 3834 | − if ( m_dir ) | |
| 3835 | − { | |
| 3986 | + if( m_dir ) { | |
| 3836 | 3987 | EM_error3( "[chuck](via FileIO): cannot write to a directory" ); |
| 3837 | 3988 | return; |
| 3838 | 3989 | } |
| 3839 | 3990 | |
| 3840 | − if (m_flags & TYPE_ASCII) { | |
| 3991 | + // check ASCII or BINARY | |
| 3992 | + if( m_flags & TYPE_ASCII ) | |
| 3993 | + { | |
| 3994 | + // insert into stream | |
| 3841 | 3995 | m_io << val; |
| 3842 | − } else if (m_flags & TYPE_BINARY) { | |
| 3843 | − m_io.write( (char *)&val, sizeof(t_CKFLOAT) ); | |
| 3844 | − } else { | |
| 3996 | + } | |
| 3997 | + else if( m_flags & TYPE_BINARY ) | |
| 3998 | + { | |
| 3999 | + // 1.5.0.1 (ge) add distinction between different float sizes | |
| 4000 | + if( flags & Chuck_IO::FLOAT32 ) | |
| 4001 | + { | |
| 4002 | + // 32-bit | |
| 4003 | + float v = (float)val; | |
| 4004 | + m_io.write( (char *)&v, 4 ); | |
| 4005 | + } | |
| 4006 | + else if( flags & Chuck_IO::FLOAT64 ) | |
| 4007 | + { | |
| 4008 | + // 64-bit | |
| 4009 | + double v = (double)val; | |
| 4010 | + m_io.write( (char *)&v, 8 ); | |
| 4011 | + } | |
| 4012 | + else | |
| 4013 | + { | |
| 4014 | + EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" ); | |
| 4015 | + } | |
| 4016 | + } | |
| 4017 | + else | |
| 4018 | + { | |
| 3845 | 4019 | EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" ); |
| 3846 | 4020 | } |
| 3847 | 4021 | |
| @@ -3881,7 +4055,7 @@ THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeInt_thread ) ( void *data ) · 33 unmodified lines | ||
| 3881 | 4055 | THREAD_RETURN ( THREAD_TYPE Chuck_IO_File::writeFloat_thread ) ( void *data ) |
| 3882 | 4056 | { |
| 3883 | 4057 | async_args *args = (async_args *)data; |
| 3884 | − args->fileio_obj->write ( args->floatArg ); | |
| 4058 | + args->fileio_obj->write( args->floatArg, args->intArg ); | |
| 3885 | 4059 | args->fileio_obj->m_asyncEvent->broadcast_local(); // wake up |
| 3886 | 4060 | args->fileio_obj->m_asyncEvent->broadcast_global(); |
| 3887 | 4061 | delete args; |
| @@ -3960,7 +4134,10 @@ t_CKINT Chuck_IO_Chout::readInt( t_CKINT flags ) · 72 unmodified lines | ||
| 3960 | 4134 | { return 0; } |
| 3961 | 4135 | |
| 3962 | 4136 | t_CKFLOAT Chuck_IO_Chout::readFloat() |
| 3963 | −{ return 0.0f; } | |
| 4137 | +{ return 0; } | |
| 4138 | + | |
| 4139 | +t_CKFLOAT Chuck_IO_Chout::readFloat( t_CKINT flags ) | |
| 4140 | +{ return 0; } | |
| 3964 | 4141 | |
| 3965 | 4142 | t_CKBOOL Chuck_IO_Chout::readString( std::string & str ) |
| 3966 | 4143 | { |
| @@ -3993,6 +4170,11 @@ void Chuck_IO_Chout::write( t_CKFLOAT val ) · 26 unmodified lines | ||
| 3993 | 4170 | m_buffer << val; |
| 3994 | 4171 | } |
| 3995 | 4172 | |
| 4173 | +void Chuck_IO_Chout::write( t_CKFLOAT val, t_CKINT flags ) | |
| 4174 | +{ | |
| 4175 | + // ignore flags for chout | |
| 4176 | + m_buffer << val; | |
| 4177 | +} | |
| 3996 | 4178 | |
| 3997 | 4179 | |
| 3998 | 4180 | |
| @@ -4062,7 +4244,10 @@ t_CKINT Chuck_IO_Cherr::readInt( t_CKINT flags ) · 63 unmodified lines | ||
| 4062 | 4244 | { return 0; } |
| 4063 | 4245 | |
| 4064 | 4246 | t_CKFLOAT Chuck_IO_Cherr::readFloat() |
| 4065 | −{ return 0.0f; } | |
| 4247 | +{ return 0; } | |
| 4248 | + | |
| 4249 | +t_CKFLOAT Chuck_IO_Cherr::readFloat( t_CKINT flags ) | |
| 4250 | +{ return 0; } | |
| 4066 | 4251 | |
| 4067 | 4252 | t_CKBOOL Chuck_IO_Cherr::readString( std::string & str ) |
| 4068 | 4253 | { |
| @@ -4097,3 +4282,10 @@ void Chuck_IO_Cherr::write( t_CKFLOAT val ) · 28 unmodified lines | ||
| 4097 | 4282 | m_buffer << val; |
| 4098 | 4283 | flush(); // always flush for cerr | 1.5.0.0 (ge) added |
| 4099 | 4284 | } |
| 4285 | + | |
| 4286 | +void Chuck_IO_Cherr::write( t_CKFLOAT val, t_CKINT flags ) | |
| 4287 | +{ | |
| 4288 | + // ignore flags for cherr | |
| 4289 | + m_buffer << val; | |
| 4290 | + flush(); // always flush for cerr | 1.5.0.0 (ge) added | |
| 4291 | +} | |
| @@ -617,6 +617,7 @@ public: · 616 unmodified lines | ||
| 617 | 617 | virtual Chuck_String * readLine() = 0; |
| 618 | 618 | virtual t_CKINT readInt( t_CKINT flags ) = 0; |
| 619 | 619 | virtual t_CKFLOAT readFloat() = 0; |
| 620 | + virtual t_CKFLOAT readFloat( t_CKINT flags ) = 0; | |
| 620 | 621 | virtual t_CKBOOL readString( std::string & str ) = 0; |
| 621 | 622 | virtual t_CKBOOL eof() = 0; |
| 622 | 623 | |
| @@ -625,6 +626,7 @@ public: · 2 unmodified lines | ||
| 625 | 626 | virtual void write( t_CKINT val ) = 0; |
| 626 | 627 | virtual void write( t_CKINT val, t_CKINT flags ) = 0; |
| 627 | 628 | virtual void write( t_CKFLOAT val ) = 0; |
| 629 | + virtual void write( t_CKFLOAT val, t_CKINT flags ) = 0; | |
| 628 | 630 | |
| 629 | 631 | public: |
| 630 | 632 | // type | moved to IO in 1.5.0.0 (ge) |
| @@ -636,8 +638,20 @@ public: · 5 unmodified lines | ||
| 636 | 638 | static const t_CKINT INT24; // 1.5.0.1 (ge) added |
| 637 | 639 | static const t_CKINT INT32; |
| 638 | 640 | static const t_CKINT INT64; // 1.5.0.1 (ge) added |
| 639 | − static const t_CKINT FLOAT32; // 1.5.0.1 (ge) added | |
| 640 | − static const t_CKINT FLOAT64; // 1.5.0.1 (ge) added | |
| 641 | + // 1.5.0.1 (ge) added SINT and UINT type flags | |
| 642 | + static const t_CKINT SINT8; | |
| 643 | + static const t_CKINT SINT16; | |
| 644 | + static const t_CKINT SINT24; | |
| 645 | + static const t_CKINT SINT32; | |
| 646 | + static const t_CKINT SINT64; | |
| 647 | + static const t_CKINT UINT8; | |
| 648 | + static const t_CKINT UINT16; | |
| 649 | + static const t_CKINT UINT24; | |
| 650 | + static const t_CKINT UINT32; | |
| 651 | + static const t_CKINT UINT64; | |
| 652 | + // 1.5.0.1 (ge) added FLOAT type flags | |
| 653 | + static const t_CKINT FLOAT32; | |
| 654 | + static const t_CKINT FLOAT64; | |
| 641 | 655 | // flags | moved to IO in 1.5.0.0 (ge) |
| 642 | 656 | static const t_CKINT FLAG_READ_WRITE; |
| 643 | 657 | static const t_CKINT FLAG_READONLY; |
| @@ -698,6 +712,7 @@ public: · 54 unmodified lines | ||
| 698 | 712 | virtual Chuck_String * readLine(); |
| 699 | 713 | virtual t_CKINT readInt( t_CKINT flags ); |
| 700 | 714 | virtual t_CKFLOAT readFloat(); |
| 715 | + virtual t_CKFLOAT readFloat( t_CKINT flags ); | |
| 701 | 716 | virtual t_CKBOOL readString( std::string & str ); |
| 702 | 717 | virtual t_CKBOOL eof(); |
| 703 | 718 | |
| @@ -714,6 +729,7 @@ public: · 10 unmodified lines | ||
| 714 | 729 | virtual void write( t_CKINT val ); |
| 715 | 730 | virtual void write( t_CKINT val, t_CKINT flags ); |
| 716 | 731 | virtual void write( t_CKFLOAT val ); |
| 732 | + virtual void write( t_CKFLOAT val, t_CKINT flags ); | |
| 717 | 733 | |
| 718 | 734 | #ifndef __DISABLE_THREADS__ |
| 719 | 735 | // writing -- async |
| @@ -765,6 +781,7 @@ public: · 45 unmodified lines | ||
| 765 | 781 | virtual Chuck_String * readLine(); |
| 766 | 782 | virtual t_CKINT readInt( t_CKINT flags ); |
| 767 | 783 | virtual t_CKFLOAT readFloat(); |
| 784 | + virtual t_CKFLOAT readFloat( t_CKINT flags ); | |
| 768 | 785 | virtual t_CKBOOL readString( std::string & str ); |
| 769 | 786 | virtual t_CKBOOL eof(); |
| 770 | 787 | |
| @@ -773,6 +790,7 @@ public: · 2 unmodified lines | ||
| 773 | 790 | virtual void write( t_CKINT val ); |
| 774 | 791 | virtual void write( t_CKINT val, t_CKINT flags ); |
| 775 | 792 | virtual void write( t_CKFLOAT val ); |
| 793 | + virtual void write( t_CKFLOAT val, t_CKINT flags ); | |
| 776 | 794 | |
| 777 | 795 | public: // REFACTOR-2017 |
| 778 | 796 | // set callback |
| @@ -810,6 +828,7 @@ public: · 31 unmodified lines | ||
| 810 | 828 | virtual Chuck_String * readLine(); |
| 811 | 829 | virtual t_CKINT readInt( t_CKINT flags ); |
| 812 | 830 | virtual t_CKFLOAT readFloat(); |
| 831 | + virtual t_CKFLOAT readFloat( t_CKINT flags ); | |
| 813 | 832 | virtual t_CKBOOL readString( std::string & str ); |
| 814 | 833 | virtual t_CKBOOL eof(); |
| 815 | 834 | |
| @@ -818,6 +837,7 @@ public: · 2 unmodified lines | ||
| 818 | 837 | virtual void write( t_CKINT val ); |
| 819 | 838 | virtual void write( t_CKINT val, t_CKINT flags ); |
| 820 | 839 | virtual void write( t_CKFLOAT val ); |
| 840 | + virtual void write( t_CKFLOAT val, t_CKINT flags ); | |
| 821 | 841 | |
| 822 | 842 | public: |
| 823 | 843 | // set callback | REFACTOR-2017 |