value snow_assets_audio_load_info_wav( value _id, value _do_read, value _bytes, value _byteOffset, value _byteLength ) { bool from_bytes = !val_is_null(_bytes); bool do_read = val_bool(_do_read); std::string _asset_id(val_string(_id)); //the destination for the read, if any QuickVec<unsigned char> buffer; //the source information for the wav file snow::assets::audio::WAV_file_source* wav_source = new snow::assets::audio::WAV_file_source(); wav_source->source_name = _asset_id; if(!from_bytes) { wav_source->file_source = snow::io::iosrc_from_file(_asset_id.c_str(), "rb"); } else { int byteOffset = val_int(_byteOffset); int byteLength = val_int(_byteLength); const unsigned char* bytes = snow::bytes_from_hx(_bytes); wav_source->file_source = snow::io::iosrc_from_mem( (void*)(bytes + byteOffset), byteLength ); } bool success = snow::assets::audio::load_info_wav( buffer, _asset_id.c_str(), wav_source, do_read ); if(!success) { if(wav_source) { delete wav_source; wav_source = NULL; } return alloc_null(); } //!success value data = snow::bytes_to_hx( &buffer[0], buffer.size() ); value _object = alloc_empty_object(); alloc_field( _object, id_id, _id ); alloc_field( _object, id_format, alloc_int(2) ); //2 here is wav alloc_field( _object, id_handle, snow::to_hx<snow::assets::audio::WAV_file_source>( wav_source ) ); value _dataobject = alloc_empty_object(); alloc_field( _dataobject, id_channels, alloc_int(wav_source->channels) ); alloc_field( _dataobject, id_rate, alloc_int(wav_source->rate) ); alloc_field( _dataobject, id_bitrate, alloc_int(wav_source->bitrate) ); alloc_field( _dataobject, id_bits_per_sample, alloc_int(wav_source->bits_per_sample) ); alloc_field( _dataobject, id_bytes, data ); alloc_field( _dataobject, id_length, alloc_int(wav_source->length) ); alloc_field( _dataobject, id_length_pcm, alloc_int(wav_source->length_pcm) ); alloc_field( _object, id_data, _dataobject ); return _object; } DEFINE_PRIM(snow_assets_audio_load_info_wav, 5);
/** sys_stat : string -> { gid => int, uid => int, atime => 'int32, mtime => 'int32, ctime => 'int32, dev => int, ino => int, nlink => int, rdev => int, mode => int, size => int } <doc>Run the [stat] command on the given file or directory.</doc> **/ static value sys_stat( value path ) { #ifdef EPPC return alloc_null(); #else struct stat s; value o; val_check(path,string); gc_enter_blocking(); if( stat(val_string(path),&s) != 0 ) { gc_exit_blocking(); return alloc_null(); } gc_exit_blocking(); o = alloc_empty_object( ); STATF(gid); STATF(uid); STATF32(atime); STATF32(mtime); STATF32(ctime); STATF(dev); STATF(ino); STATF(mode); STATF(nlink); STATF(rdev); STATF(size); STATF(mode); return o; #endif }
void GamepadEvent::Dispatch (GamepadEvent* event) { if (GamepadEvent::callback) { if (!init) { id_axis = val_id ("axis"); id_button = val_id ("button"); id_id = val_id ("id"); id_type = val_id ("type"); id_value = val_id ("value"); init = true; } value object = (GamepadEvent::eventObject ? GamepadEvent::eventObject->get () : alloc_empty_object ()); alloc_field (object, id_axis, alloc_int (event->axis)); alloc_field (object, id_button, alloc_int (event->button)); alloc_field (object, id_id, alloc_int (event->id)); alloc_field (object, id_type, alloc_int (event->type)); alloc_field (object, id_value, alloc_float (event->axisValue)); val_call0 (GamepadEvent::callback->get ()); } }
value ImageBuffer::Value () { if (!init) { id_bitsPerPixel = val_id ("bitsPerPixel"); id_transparent = val_id ("transparent"); id_buffer = val_id ("buffer"); id_width = val_id ("width"); id_height = val_id ("height"); id_data = val_id ("data"); id_format = val_id ("format"); init = true; } mValue = alloc_empty_object (); alloc_field (mValue, id_width, alloc_int (width)); alloc_field (mValue, id_height, alloc_int (height)); alloc_field (mValue, id_bitsPerPixel, alloc_int (bitsPerPixel)); alloc_field (mValue, id_data, data ? data->Value () : alloc_null ()); alloc_field (mValue, id_transparent, alloc_bool (transparent)); alloc_field (mValue, id_format, alloc_int (format)); return mValue; }
void* DisplayMode::Value () { // if (_mode) { // _mode->height = height; // _mode->pixelFormat = pixelFormat; // _mode->refreshRate = refreshRate; // _mode->width = width; // return _mode; // } else { if (!init) { id_height = val_id ("height"); id_pixelFormat = val_id ("pixelFormat"); id_refreshRate = val_id ("refreshRate"); id_width = val_id ("width"); init = true; } value displayMode = alloc_empty_object (); alloc_field (displayMode, id_height, alloc_int (height)); alloc_field (displayMode, id_pixelFormat, alloc_int (pixelFormat)); alloc_field (displayMode, id_refreshRate, alloc_int (refreshRate)); alloc_field (displayMode, id_width, alloc_int (width)); return displayMode; // } }
value snow_assets_audio_read_bytes_pcm( value _info, value _start, value _len ) { QuickVec<unsigned char> buffer; value _handle = property_value(_info, id_handle); snow::assets::audio::PCM_file_source* pcm_source = snow::from_hx<snow::assets::audio::PCM_file_source>(_handle); if( !val_is_null(_handle) && pcm_source ) { bool complete = snow::assets::audio::read_bytes_pcm( pcm_source, buffer, val_int(_start), val_int(_len) ); ByteArray data(buffer); value _object = alloc_empty_object(); alloc_field( _object, id_bytes, data.mValue ); alloc_field( _object, id_complete, alloc_bool(complete) ); return _object; } else { return alloc_null(); } } DEFINE_PRIM(snow_assets_audio_read_bytes_pcm, 3);
value snow_assets_image_info_from_bytes( value _id, value _bytes, value _req_bpp ) { QuickVec<unsigned char> buffer; int w = 0, h = 0, bpp = 0, bpp_source = 0; int req_bpp = val_int(_req_bpp); bool success = snow::assets::image::info_from_bytes( buffer, ByteArray(_bytes), val_string(_id), &w, &h, &bpp, &bpp_source, req_bpp ); if(!success) { return alloc_null(); } value _object = alloc_empty_object(); alloc_field( _object, id_id, _id ); alloc_field( _object, id_width, alloc_int(w) ); alloc_field( _object, id_height, alloc_int(h) ); alloc_field( _object, id_bpp, alloc_int(bpp) ); alloc_field( _object, id_bpp_source, alloc_int(bpp_source) ); alloc_field( _object, id_data, ByteArray(buffer).mValue ); return _object; } DEFINE_PRIM(snow_assets_image_info_from_bytes, 3);
value snow_assets_image_load_info( value _id, value _req_bpp ) { QuickVec<unsigned char> buffer; int w = 0, h = 0, bpp = 0, bpp_source = 0; int req_bpp = val_int(_req_bpp); bool success = snow::assets::image::load_info( buffer, val_string(_id), &w, &h, &bpp, &bpp_source, req_bpp ); if(!success) { return alloc_null(); } value data = snow::bytes_to_hx( &buffer[0], buffer.size() ); value _object = alloc_empty_object(); alloc_field( _object, id_id, _id ); alloc_field( _object, id_width, alloc_int(w) ); alloc_field( _object, id_height, alloc_int(h) ); alloc_field( _object, id_bpp, alloc_int(bpp) ); alloc_field( _object, id_bpp_source, alloc_int(bpp_source) ); alloc_field( _object, id_data, data ); return _object; } DEFINE_PRIM(snow_assets_image_load_info, 2);
value hx_zmq_getint64sockopt(value socket_handle_,value option_) { val_check_kind(socket_handle_, k_zmq_socket_handle); if (!val_is_int(option_)) { val_throw(alloc_int(EINVAL)); return alloc_null(); } int rc = 0; int err = 0; uint64_t optval = 0; size_t optvallen = sizeof(optval); rc = zmq_getsockopt(val_data(socket_handle_),val_int(option_),&optval, &optvallen); err = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err)); return alloc_int(0); } value ret = alloc_empty_object(); alloc_field(ret, val_id("hi"),alloc_int(optval >> 32)); alloc_field(ret, val_id("lo"),alloc_int(optval & 0xFFFFFFFF)); return ret; }
value AudioBuffer::Value () { if (!init) { id_bitsPerSample = val_id ("bitsPerSample"); id_channels = val_id ("channels"); id_data = val_id ("data"); id_sampleRate = val_id ("sampleRate"); init = true; } if (val_is_null (mValue)) { mValue = alloc_empty_object (); } alloc_field (mValue, id_bitsPerSample, alloc_int (bitsPerSample)); alloc_field (mValue, id_channels, alloc_int (channels)); alloc_field (mValue, id_data, data ? data->Value () : alloc_null ()); alloc_field (mValue, id_sampleRate, alloc_int (sampleRate)); return mValue; }
void SensorEvent::Dispatch (SensorEvent* event) { if (SensorEvent::callback) { if (!init) { id_id = val_id ("id"); id_type = val_id ("type"); id_x = val_id ("x"); id_y = val_id ("y"); id_z = val_id ("z"); init = true; } value object = (SensorEvent::eventObject ? SensorEvent::eventObject->get () : alloc_empty_object ()); alloc_field (object, id_id, alloc_int (event->id)); alloc_field (object, id_type, alloc_int (event->type)); alloc_field (object, id_x, alloc_float (event->x)); alloc_field (object, id_y, alloc_float (event->y)); alloc_field (object, id_z, alloc_float (event->z)); val_call0 (SensorEvent::callback->get ()); } }
value snow_assets_audio_read_bytes_wav( value _info, value _start, value _len ) { QuickVec<unsigned char> buffer; value _handle = property_value(_info, id_handle); snow::assets::audio::WAV_file_source* wav_source = snow::from_hx<snow::assets::audio::WAV_file_source>(_handle); if( !val_is_null(_handle) && wav_source ) { bool complete = snow::assets::audio::read_bytes_wav( wav_source, buffer, val_int(_start), val_int(_len) ); value data = snow::bytes_to_hx( &buffer[0], buffer.size() ); value _object = alloc_empty_object(); alloc_field( _object, id_bytes, data ); alloc_field( _object, id_complete, alloc_bool(complete) ); return _object; } else { return alloc_null(); } } DEFINE_PRIM(snow_assets_audio_read_bytes_wav, 3);
value sdl2_window_event_to_hx( WindowEvent &new_event, SDL_Event &event ) { value _object = alloc_empty_object(); alloc_field( _object, id_type, alloc_int(event.window.event) ); alloc_field( _object, id_window_id, alloc_int(event.window.windowID) ); alloc_field( _object, id_timestamp, alloc_float(event.window.timestamp/1000.0) ); switch (event.window.event) { case SDL_WINDOWEVENT_MOVED: case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_SIZE_CHANGED: { alloc_field( _object, id_x, alloc_int(event.window.data1) ); alloc_field( _object, id_y, alloc_int(event.window.data2) ); break; } } //switch event.type return _object; } //sdl2_window_event_to_hx
void TouchEvent::Dispatch (TouchEvent* event) { if (TouchEvent::callback) { if (!init) { id_id = val_id ("id"); id_type = val_id ("type"); id_x = val_id ("x"); id_y = val_id ("y"); init = true; } value object = (TouchEvent::eventObject ? TouchEvent::eventObject->get () : alloc_empty_object ()); alloc_field (object, id_id, alloc_int (event->id)); alloc_field (object, id_type, alloc_int (event->type)); alloc_field (object, id_x, alloc_float (event->x)); alloc_field (object, id_y, alloc_float (event->y)); val_call1 (TouchEvent::callback->get (), object); } }
void Bytes::Resize (int size) { if (size != _length) { if (!_value) { _value = alloc_empty_object (); } if (val_is_null (val_field (_value, id_b))) { value dataValue; if (useBuffer) { buffer b = alloc_buffer_len (size); dataValue = buffer_val (b); _data = (unsigned char*)buffer_data (b); } else { dataValue = alloc_raw_string (size); _data = (unsigned char*)val_string (dataValue); } alloc_field (_value, id_b, dataValue); } else { if (useBuffer) { buffer b = val_to_buffer (val_field (_value, id_b)); buffer_set_size (b, size); _data = (unsigned char*)buffer_data (b); } else { value s = alloc_raw_string (size); memcpy ((char *)val_string (s), val_string (val_field (_value, id_b)), size); alloc_field (_value, id_b, s); _data = (unsigned char*)val_string (s); } } alloc_field (_value, id_length, alloc_int (size)); } _length = size; }
value display_mode_to_hx( display_mode mode ) { value _object = alloc_empty_object(); alloc_field( _object, id_width, alloc_int(mode.width) ); alloc_field( _object, id_height, alloc_int(mode.height) ); alloc_field( _object, id_refresh_rate, alloc_int(mode.refresh_rate) ); alloc_field( _object, id_format, alloc_int(mode.format) ); return _object; } //display_mode_to_hx
value display_bounds_to_hx( bounds_rect bounds ) { value _object = alloc_empty_object(); alloc_field( _object, id_x, alloc_int(bounds.x) ); alloc_field( _object, id_y, alloc_int(bounds.y) ); alloc_field( _object, id_width, alloc_int(bounds.width) ); alloc_field( _object, id_height, alloc_int(bounds.height) ); return _object; } //display_bounds_to_hx
value snow_assets_audio_load_info_ogg( value _id, value _do_read ) { bool do_read = val_bool(_do_read); //the destination for the read, if any QuickVec<unsigned char> buffer; //the source data ogg info snow::assets::audio::OGG_file_source* ogg_source = NULL; bool success = snow::assets::audio::load_info_ogg( buffer, val_string(_id), ogg_source, do_read ); if(!success) { return alloc_null(); } //!success ByteArray data(buffer); value _object = alloc_empty_object(); alloc_field( _object, id_id, _id ); alloc_field( _object, id_format, alloc_int( 1 )); //1 here is ogg alloc_field( _object, id_handle, snow::to_hx<snow::assets::audio::OGG_file_source>( ogg_source ) ); value _dataobject = alloc_empty_object(); alloc_field( _dataobject, id_channels, alloc_int( ogg_source->info->channels )); alloc_field( _dataobject, id_rate, alloc_int( ogg_source->info->rate )); alloc_field( _dataobject, id_bitrate, alloc_int( ogg_source->info->bitrate_nominal )); alloc_field( _dataobject, id_bits_per_sample, alloc_int( 16 ) ); //:todo: optionize? alloc_field( _dataobject, id_bytes, data.mValue ); alloc_field( _dataobject, id_length, alloc_int(ogg_source->length) ); alloc_field( _dataobject, id_length_pcm, alloc_int(ogg_source->length_pcm) ); alloc_field( _object, id_data, _dataobject ); return _object; } DEFINE_PRIM(snow_assets_audio_load_info_ogg, 2);
value snow_assets_audio_load_info_pcm( value _id, value _do_read ) { bool do_read = val_bool(_do_read); //the destination for the read, if any QuickVec<unsigned char> buffer; //the source information for the pcm file snow::assets::audio::PCM_file_source* pcm_source = NULL; bool success = snow::assets::audio::load_info_pcm( buffer, val_string(_id), pcm_source, do_read ); if(!success) { return alloc_null(); } //!success ByteArray data(buffer); value _object = alloc_empty_object(); alloc_field( _object, id_id, _id ); alloc_field( _object, id_format, alloc_int(3) ); //3 here is pcm alloc_field( _object, id_handle, snow::to_hx<snow::assets::audio::PCM_file_source>( pcm_source ) ); value _dataobject = alloc_empty_object(); alloc_field( _dataobject, id_channels, alloc_int(pcm_source->channels) ); alloc_field( _dataobject, id_rate, alloc_int(pcm_source->rate) ); alloc_field( _dataobject, id_bitrate, alloc_int(pcm_source->bitrate) ); alloc_field( _dataobject, id_bits_per_sample, alloc_int(pcm_source->bits_per_sample) ); alloc_field( _dataobject, id_bytes, data.mValue ); alloc_field( _dataobject, id_length, alloc_int(pcm_source->length) ); alloc_field( _dataobject, id_length_pcm, alloc_int(pcm_source->length_pcm) ); alloc_field( _object, id_data, _dataobject ); return _object; } DEFINE_PRIM(snow_assets_audio_load_info_pcm, 2);
/** regexp_matched_pos : 'regexp -> n:int -> { pos => int, len => int } <doc>Return the [n]th matched block position by the regexp. If [n] is 0 then return the whole matched substring position</doc> **/ static value regexp_matched_pos( value o, value n ) { pcredata *d; int m; val_check_kind(o,k_regexp); d = PCRE(o); val_check(n,int); m = val_int(n); if( m < 0 || m >= d->nmatchs || val_is_null(d->str) ) return alloc_null(); { int start = d->matchs[m*2]; int len = d->matchs[m*2+1] - start; value o = alloc_empty_object(); alloc_field(o,id_pos,alloc_int(start)); alloc_field(o,id_len,alloc_int(len)); return o; } }
// Display specific code static value display_get_screen_size() { #ifndef HX_LINUX dimensions dim; value w; value h; value o = alloc_empty_object(); systools_display_get_screen_size(&dim); w=alloc_int(dim.width); h=alloc_int(dim.height); alloc_field( o, val_id("w"), w ); alloc_field( o, val_id("h"), h ); return o; #else val_throw(alloc_string("function not available for this platform")); return val_null; #endif }
void RenderEvent::Dispatch (RenderEvent* event) { if (RenderEvent::callback) { //if (!init) { //id_type = val_id ("type"); //} value object = (RenderEvent::eventObject ? RenderEvent::eventObject->get () : alloc_empty_object ()); //alloc_field (object, id_type, alloc_int (event->type)); val_call0 (RenderEvent::callback->get ()); } }
/** inflate_buffer : 'istream -> src:string -> srcpos:int -> dst:string -> dstpos:int -> { done => bool, read => int, write => int } **/ static value inflate_buffer( value s, value src, value srcpos, value dst, value dstpos ) { z_stream *z; int err; value o; val_check_kind(s,k_stream_inf); val_check(srcpos,int); buffer src_buf = val_to_buffer(src); if (!src_buf) hx_failure("invalid source buffer"); buffer dst_buf = val_to_buffer(dst); if (!dst_buf) hx_failure("invalid destination buffer"); int slen = buffer_size(src_buf); int dlen = buffer_size(dst_buf); val_check(dstpos,int); z = val_stream(s); if( val_int(srcpos) < 0 || val_int(dstpos) < 0 ) return alloc_null(); slen -= val_int(srcpos); dlen -= val_int(dstpos); if( slen < 0 || dlen < 0 ) return alloc_null(); z->next_in = (Bytef*)buffer_data(src_buf) + val_int(srcpos); z->next_out = (Bytef*)buffer_data(dst_buf) + val_int(dstpos); z->avail_in = slen; z->avail_out = dlen; if( (err = inflate(z,val_flush(z))) < 0 ) zlib_error(z,err); z->next_in = NULL; z->next_out = NULL; o = alloc_empty_object(); alloc_field(o,id_done,alloc_bool(err == Z_STREAM_END)); alloc_field(o,id_read,alloc_int((int)(slen - z->avail_in))); alloc_field(o,id_write,alloc_int((int)(dlen - z->avail_out))); return o; }
void DropEvent::Dispatch (DropEvent* event) { if (DropEvent::callback) { if (!init) { id_file = val_id("file"); id_type = val_id("type"); init = true; } value object = (DropEvent::eventObject ? DropEvent::eventObject->get() : alloc_empty_object()); alloc_field(object, id_file, alloc_string(event->file)); alloc_field(object, id_type, alloc_int (event->type)); val_call0 (DropEvent::callback->get ()); } }
value snow_assets_image_info_from_bytes( value _id, value _bytes, value _byteOffset, value _byteLength, value _req_bpp ) { QuickVec<unsigned char> buffer; int w = 0, h = 0, bpp = 0, bpp_source = 0; int req_bpp = val_int(_req_bpp); int byteOffset = val_int(_byteOffset); int byteLength = val_int(_byteLength); bool success = snow::assets::image::info_from_bytes( buffer, snow::bytes_from_hx(_bytes), byteOffset, byteLength, val_string(_id), &w, &h, &bpp, &bpp_source, req_bpp ); if(!success) { return alloc_null(); } value result_bytes = snow::bytes_to_hx( &buffer[0], buffer.size() ); value _object = alloc_empty_object(); alloc_field( _object, id_id, _id ); alloc_field( _object, id_width, alloc_int(w) ); alloc_field( _object, id_height, alloc_int(h) ); alloc_field( _object, id_bpp, alloc_int(bpp) ); alloc_field( _object, id_bpp_source, alloc_int(bpp_source) ); alloc_field( _object, id_data, result_bytes ); return _object; } DEFINE_PRIM(snow_assets_image_info_from_bytes, 5);
value System::GetDisplay (int id) { if (!init) { id_bounds = val_id ("bounds"); id_currentMode = val_id ("currentMode"); id_dpi = val_id ("dpi"); id_height = val_id ("height"); id_name = val_id ("name"); id_pixelFormat = val_id ("pixelFormat"); id_refreshRate = val_id ("refreshRate"); id_supportedModes = val_id ("supportedModes"); id_width = val_id ("width"); init = true; } int numDisplays = GetNumDisplays (); if (id < 0 || id >= numDisplays) { return alloc_null (); } value display = alloc_empty_object (); alloc_field (display, id_name, alloc_string (SDL_GetDisplayName (id))); SDL_Rect bounds = { 0, 0, 0, 0 }; SDL_GetDisplayBounds (id, &bounds); alloc_field (display, id_bounds, Rectangle (bounds.x, bounds.y, bounds.w, bounds.h).Value ()); float dpi = 72.0; #ifndef EMSCRIPTEN SDL_GetDisplayDPI (id, &dpi, NULL, NULL); #endif alloc_field (display, id_dpi, alloc_float (dpi)); SDL_DisplayMode currentDisplayMode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 }; SDL_DisplayMode displayMode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 }; SDL_GetCurrentDisplayMode (id, ¤tDisplayMode); int numDisplayModes = SDL_GetNumDisplayModes (id); value supportedModes = alloc_array (numDisplayModes); value mode; for (int i = 0; i < numDisplayModes; i++) { SDL_GetDisplayMode (id, i, &displayMode); if (displayMode.format == currentDisplayMode.format && displayMode.w == currentDisplayMode.w && displayMode.h == currentDisplayMode.h && displayMode.refresh_rate == currentDisplayMode.refresh_rate) { alloc_field (display, id_currentMode, alloc_int (i)); } mode = alloc_empty_object (); alloc_field (mode, id_height, alloc_int (displayMode.h)); switch (displayMode.format) { case SDL_PIXELFORMAT_ARGB8888: alloc_field (mode, id_pixelFormat, alloc_int (ARGB32)); break; case SDL_PIXELFORMAT_BGRA8888: case SDL_PIXELFORMAT_BGRX8888: alloc_field (mode, id_pixelFormat, alloc_int (BGRA32)); break; default: alloc_field (mode, id_pixelFormat, alloc_int (RGBA32)); } alloc_field (mode, id_refreshRate, alloc_int (displayMode.refresh_rate)); alloc_field (mode, id_width, alloc_int (displayMode.w)); val_array_set_i (supportedModes, i, mode); } alloc_field (display, id_supportedModes, supportedModes); return display; }
static void do_parse_xml( const char *xml, const char **lp, int *line, value callb, const char *parentname ) { STATE state = BEGIN; STATE next = BEGIN; field aname = (field)0; value attribs = NULL_VAL; value nodename = NULL_VAL; const char *start = NULL; const char *p = *lp; char c = *p; int nsubs = 0, nbrackets = 0; while( c ) { switch( state ) { case IGNORE_SPACES: switch( c ) { case '\n': case '\r': case '\t': case ' ': break; default: state = next; continue; } break; case BEGIN: switch( c ) { case '<': state = IGNORE_SPACES; next = BEGIN_NODE; break; default: start = p; state = PCDATA; continue; } break; case PCDATA: if( c == '<' ) { val_ocall1(callb,id_pcdata,copy_string(start,p-start)); nsubs++; state = IGNORE_SPACES; next = BEGIN_NODE; } break; case CDATA: if( c == ']' && p[1] == ']' && p[2] == '>' ) { val_ocall1(callb,id_cdata,copy_string(start,p-start)); nsubs++; p += 2; state = BEGIN; } break; case BEGIN_NODE: switch( c ) { case '!': if( p[1] == '[' ) { p += 2; if( (p[0] != 'C' && p[0] != 'c') || (p[1] != 'D' && p[1] != 'd') || (p[2] != 'A' && p[2] != 'a') || (p[3] != 'T' && p[3] != 't') || (p[4] != 'A' && p[4] != 'a') || (p[5] != '[') ) ERROR("Expected <![CDATA["); p += 5; state = CDATA; start = p + 1; break; } if( p[1] == 'D' || p[1] == 'd' ) { if( (p[2] != 'O' && p[2] != 'o') || (p[3] != 'C' && p[3] != 'c') || (p[4] != 'T' && p[4] != 't') || (p[5] != 'Y' && p[5] != 'y') || (p[6] != 'P' && p[6] != 'p') || (p[7] != 'E' && p[7] != 'e') ) ERROR("Expected <!DOCTYPE"); p += 7; state = DOCTYPE; start = p + 1; break; } if( p[1] != '-' || p[2] != '-' ) ERROR("Expected <!--"); p += 2; state = COMMENT; start = p + 1; break; case '?': state = HEADER; start = p; break; case '/': if( parentname == NULL ) ERROR("Expected node name"); start = p + 1; state = IGNORE_SPACES; next = CLOSE; break; default: state = TAG_NAME; start = p; continue; } break; case TAG_NAME: if( !is_valid_char(c) ) { if( p == start ) ERROR("Expected node name"); nodename = copy_string(start,p-start); attribs = alloc_empty_object(); state = IGNORE_SPACES; next = BODY; continue; } break; case BODY: switch( c ) { case '/': state = WAIT_END; nsubs++; val_ocall2(callb,id_xml,nodename,attribs); break; case '>': state = CHILDS; nsubs++; val_ocall2(callb,id_xml,nodename,attribs); break; default: state = ATTRIB_NAME; start = p; continue; } break; case ATTRIB_NAME: if( !is_valid_char(c) ) { value tmp; if( start == p ) ERROR("Expected attribute name"); tmp = copy_string(start,p-start); aname = val_id(val_string(tmp)); if( !val_is_null(val_field(attribs,aname)) ) ERROR("Duplicate attribute"); state = IGNORE_SPACES; next = EQUALS; continue; } break; case EQUALS: switch( c ) { case '=': state = IGNORE_SPACES; next = ATTVAL_BEGIN; break; default: ERROR("Expected ="); } break; case ATTVAL_BEGIN: switch( c ) { case '"': case '\'': state = ATTRIB_VAL; start = p; break; default: ERROR("Expected \""); } break; case ATTRIB_VAL: if( c == *start ) { value aval = copy_string(start+1,p-start-1); alloc_field(attribs,aname,aval); state = IGNORE_SPACES; next = BODY; } break; case CHILDS: *lp = p; do_parse_xml(xml,lp,line,callb,val_string(nodename)); p = *lp; start = p; state = BEGIN; break; case WAIT_END: switch( c ) { case '>': val_ocall0(callb,id_done); state = BEGIN; break; default : ERROR("Expected >"); } break; case WAIT_END_RET: switch( c ) { case '>': if( nsubs == 0 ) val_ocall1(callb,id_pcdata,alloc_string("")); val_ocall0(callb,id_done); *lp = p; return; default : ERROR("Expected >"); } break; case CLOSE: if( !is_valid_char(c) ) { if( start == p ) ERROR("Expected node name"); { value v = copy_string(start,p - start); if( _strcmpi(parentname,val_string(v)) != 0 ) { buffer b = alloc_buffer("Expected </"); buffer_append(b,parentname); buffer_append(b,">"); ERROR(buffer_data(b)); } } state = IGNORE_SPACES; next = WAIT_END_RET; continue; } break; case COMMENT: if( c == '-' && p[1] == '-' && p[2] == '>' ) { val_ocall1(callb,id_comment,copy_string(start,p-start)); p += 2; state = BEGIN; } break; case DOCTYPE: if( c == '[' ) nbrackets++; else if( c == ']' ) nbrackets--; else if( c == '>' && nbrackets == 0 ) { val_ocall1(callb,id_doctype,copy_string(start,p-start)); state = BEGIN; } break; case HEADER: if( c == '?' && p[1] == '>' ) { p++; val_ocall1(callb,id_comment,copy_string(start,p-start)); state = BEGIN; } break; } c = *++p; if( c == '\n' ) (*line)++; } if( state == BEGIN ) { start = p; state = PCDATA; } if( parentname == NULL && state == PCDATA ) { if( p != start || nsubs == 0 ) val_ocall1(callb,id_pcdata,copy_string(start,p-start)); return; } ERROR("Unexpected end"); }
/** sys_stat : string -> { gid => int, uid => int, atime => 'int32, mtime => 'int32, ctime => 'int32, dev => int, ino => int, nlink => int, rdev => int, mode => int, size => int } <doc>Run the [stat] command on the given file or directory.</doc> **/ static value sys_stat( value path ) { #if defined(EPPC) || defined(KORE_CONSOLE) return alloc_null(); #else value o; val_check(path,string); #if defined(NEKO_WINDOWS) && !defined(KORE_WINDOWSAPP) && !defined(KORE_XBOX_ONE) const wchar_t* _path = val_wstring(path); gc_enter_blocking(); WIN32_FILE_ATTRIBUTE_DATA data; if( !GetFileAttributesExW(_path,GetFileExInfoStandard,&data) ) { gc_exit_blocking(); return alloc_null(); } gc_exit_blocking(); wchar_t fullPath[MAX_PATH+1]; GetFullPathNameW(_path,MAX_PATH+1,fullPath,NULL); int dev = PathGetDriveNumberW(fullPath); #define EPOCH_DIFF (134774*24*60*60.0) ULARGE_INTEGER ui; o = alloc_empty_object( ); alloc_field(o,val_id("gid"),alloc_int(0)); alloc_field(o,val_id("uid"),alloc_int(0)); ui.LowPart = data.ftLastAccessTime.dwLowDateTime; ui.HighPart = data.ftLastAccessTime.dwHighDateTime; alloc_field(o,val_id("atime"),alloc_int32((int)(((double)ui.QuadPart) / 10000000.0 - EPOCH_DIFF))); ui.LowPart = data.ftLastWriteTime.dwLowDateTime; ui.HighPart = data.ftLastWriteTime.dwHighDateTime; alloc_field(o,val_id("mtime"),alloc_int32((int)(((double)ui.QuadPart) / 10000000.0 - EPOCH_DIFF))); ui.LowPart = data.ftCreationTime.dwLowDateTime; ui.HighPart = data.ftCreationTime.dwHighDateTime; alloc_field(o,val_id("ctime"),alloc_int32((int)(((double)ui.QuadPart) / 10000000.0 - EPOCH_DIFF))); alloc_field(o,val_id("dev"),alloc_int(dev)); alloc_field(o,val_id("ino"),alloc_int(0)); int mode = 0; if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) mode |= _S_IFDIR; if ((data.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) == 0) mode |= _S_IFREG; mode |= _S_IREAD; if ((data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0) mode |= _S_IWRITE; if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) mode |= _S_IEXEC; alloc_field(o,val_id("mode"),alloc_int(mode)); alloc_field(o,val_id("nlink"),alloc_int(1)); alloc_field(o,val_id("rdev"),alloc_int(dev)); alloc_field(o,val_id("size"),alloc_int32(data.nFileSizeLow)); #else gc_enter_blocking(); struct stat s; if( stat(val_string(path),&s) != 0 ) { gc_exit_blocking(); return alloc_null(); } gc_exit_blocking(); o = alloc_empty_object( ); STATF(gid); STATF(uid); STATF32(atime); STATF32(mtime); STATF32(ctime); STATF(dev); STATF(ino); STATF(mode); STATF(nlink); STATF(rdev); STATF(size); #endif return o; #endif }
value AudioBuffer::Value () { return Value (alloc_empty_object ()); }