Value * Bundle::getBytesContentV(const char * format,va_list va){ Value * path = getFilePathV(format,va); if(_parent){ return _parent->getBytesContent(path->stringValue()); } Value * v = NULL; InvokeTickBegin hbuffer_t content = buffer_alloc(512, 512); FILE * f = fopen(path->stringValue(), "r"); char buffer[1024]; int len; if(f){ while((len = fread(buffer, 1, sizeof(buffer), f)) >0){ buffer_append(content, buffer, len); } fclose(f); } if(buffer_length(content) >0){ v = Value::newValue((void *)buffer_data(content),buffer_length(content)); } buffer_dealloc(content); return v; }
void list_split_str(hlist_t hlist, hcchar * str,hcchar *split_chars,InvokeTickDeclare){ if(hlist && str && split_chars){ hchar * p = (hchar *)str; hchar *s = NULL; hbuffer_t buffer = buffer_alloc(1024, 1024); while(*p !='\0'){ if(list_split_str_exist_char(split_chars,p,InvokeTickArg) ){ if(buffer_length(buffer) > 0){ s = NULL; str_cpy(&s, buffer_to_str(buffer)); list_add(hlist, s,InvokeTickArg); buffer_clear(buffer); } } else{ buffer_append(buffer, p, 1); } p++; } if(buffer_length(buffer) >0){ s = NULL; str_cpy(&s, buffer_to_str(buffer)); list_add(hlist, s,InvokeTickArg); } buffer_dealloc(buffer); } }
Buffer *rope_flatten(const Rope *r) { Buffer *buf = buffer_empty(r->length); size_t counter = 0; void visitor(List *l, void *ctx, void *item, bool *keep_going) { Buffer *buf_item = (Buffer *)item; memcpy(buffer_data(buf) + counter, buffer_data(buf_item), buffer_length(buf_item)); counter += buffer_length(buf_item); }
void generate_code(java_file file, size_t method_index, FILE* source) { struct java_method* method = java_get_class(file)->methods + method_index; buffer bytecode_buffer = buffer_create(1024); bool success; write_method_prologue(bytecode_buffer); write_section(bytecode_buffer, source, file); write_method_epilogue(bytecode_buffer); success = (peek_token(source) == EOF); /* Otherwise it's an unmatched ']'. */ if (success) { method->max_stack = 6; method->max_locals = 1; method->bytecode_length = buffer_length(bytecode_buffer); method->bytecode = buffer_publish(bytecode_buffer); } buffer_free(bytecode_buffer); if (!success) { printf("Error: Unmatched \"]\".\n"); abort(); } }
int main( int argc, char* argv[] ) { struct buffer* buf = buffer_new(); buffer_insert_from_array( buf, 0, "Hello World!", 0, 12 ); printf( "%s\n", buffer_pointer( buf ) ); buffer_insert( buf, buffer_length( buf ), '\n' ); buffer_insert_from_array( buf, buffer_length( buf ), "Hello World!", 0, 12 ); printf( "%s\n", buffer_pointer( buf ) ); buffer_del( &buf ); return 0; }
// 会话停止(删除网络事件以及关闭描述符) void _stop( struct session * self ) { evsets_t sets = self->evsets; // 删除网络事件 if ( self->status&SESSION_READING ) { evsets_del( sets, self->evread ); self->status &= ~SESSION_READING; } if ( self->status&SESSION_WRITING ) { evsets_del( sets, self->evwrite ); self->status &= ~SESSION_WRITING; } if ( self->status&SESSION_KEEPALIVING ) { evsets_del( sets, self->evkeepalive ); self->status &= ~SESSION_KEEPALIVING; } // 清空接收缓冲区 buffer_erase( &self->inbuffer, buffer_length(&self->inbuffer) ); // 关闭描述符 if ( self->fd > 0 ) { close( self->fd ); self->fd = -1; } }
/** @brief Add a message to an output buffer. @param outbuf Pointer to the output buffer. @param msg Pointer to the message to be added, in the form of a JSON string. Since the output buffer is in the form of a JSON array, prepend a left bracket to the first message, and a comma to subsequent ones. Used only by servers to respond to clients. */ static inline void append_msg( growing_buffer* outbuf, const char* msg ) { if( outbuf && msg ) { char prefix = buffer_length( outbuf ) > 0 ? ',' : '['; buffer_add_char( outbuf, prefix ); buffer_add( outbuf, msg ); } }
static vmVariant vmFileClassInitCallback(vmRuntimeContext context,vmClass * clazz,vmObject * object,vmVariantList args,InvokeTickDeclare){ vmFile * file = (vmFile *) object; vmVariant rs= {vmVariantTypeVoid,0}; hbuffer_t bPath = buffer_alloc(128, 128); hbuffer_t bMode = buffer_alloc(32, 32); vmVariant path = vmVariantListGet(args, 0); vmVariant mode = vmVariantListGet(args, 1); vmVariantToString(context, path, bPath); vmVariantToString(context, mode, bMode); if(buffer_length(bPath) ==0){ rs = vmRuntimeContextException(context, 0, "not found argument 1 file path"); } if(rs.type == vmVariantTypeVoid){ file->file = fopen(buffer_to_str(bPath), buffer_to_str(bMode)); } if(!file->file){ rs = vmRuntimeContextException(context, 0, "not found argument 2 file mode"); } buffer_dealloc(bPath); buffer_dealloc(bMode); file->uniqueKeys.flush = vmRuntimeContextGetUniqueKey(context, "flush"); file->uniqueKeys.read = vmRuntimeContextGetUniqueKey(context, "read"); file->uniqueKeys.write = vmRuntimeContextGetUniqueKey(context, "write"); file->uniqueKeys.seek = vmRuntimeContextGetUniqueKey(context, "seek"); file->uniqueKeys.pos = vmRuntimeContextGetUniqueKey(context, "pos"); return rs; }
void buffer_trim_right(buffer_t *self) { int c; size_t i = buffer_length(self) - 1; while ((c = self->data[i]) && isspace(c)) { self->data[i--] = 0; } }
void bufreverse_process_internal(t_bufreverse *x, t_symbol *sym, short argc, t_atom *argv) { t_symbol *target = atom_getsym(argv++); t_symbol *source = atom_getsym(argv++); float *temp1; double *temp2; t_buffer_write_error error; AH_SIntPtr full_length = buffer_length(source); AH_SIntPtr i; double sample_rate = 0; t_atom_long read_chan = x->read_chan - 1; // Check source buffer if (buffer_check((t_object *) x, source, read_chan)) return; sample_rate = buffer_sample_rate(source); // Allocate Memory temp1 = (float *) ALIGNED_MALLOC(full_length * (sizeof(double) + sizeof(float))); temp2 = (double *) (temp1 + full_length); // Check momory allocation if (!temp1) { object_error((t_object *)x, "could not allocate temporary memory for processing"); free(temp1); return; } // Read from buffer buffer_read(source, read_chan, (float *) temp1, full_length); // Copy to double precision version for (i = 0; i < full_length; i++) temp2[i] = temp1[full_length - i - 1]; // Copy out to buffer error = buffer_write(target, temp2, full_length, x->write_chan - 1, x->resize, sample_rate, 1.); buffer_write_error((t_object *)x, target, error); // Free Resources ALIGNED_FREE(temp1); if (!error) outlet_bang(x->process_done); }
Value * Bundle::getFilePathV(const char * format,va_list va){ InvokeTickBegin hbuffer_t path = buffer_alloc(MAX_PATH, 128); Value * v = NULL; buffer_append_str(path,_bundlePath.c_str()); if(buffer_length(path) >0 && * (buffer_data(path) + buffer_length(path) -1) != PATH_SPLIT){ buffer_append_str(path, "/"); } buffer_append_format_va_list(path, format, va); if(_parent){ v = _parent->getFilePath(buffer_to_str(path)); } else{ v = Value::newValue(buffer_to_str(path)); } buffer_dealloc(path); return v; }
void recv (MatrixViewType& R, const int srcProc) { const typename MatrixViewType::ordinal_type ncols = R.ncols(); const Ordinal buflen = buffer_length (ncols); buffer_.resize (buflen); messenger_->recv (&buffer_[0], buflen, srcProc, 0); unpack (R); }
int write_file(char *path, buffer b) { descriptor d = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); if(d >= 0) { write(d, bref(b, 0), buffer_length(b)); close(d); return 1; } return 0; }
static inline int next (json_tokenizer_t *self) { if (self->offset >= self->src_length) { return 0; } self->offset += 1; self->buf = buffer_slice(self->buf, self->offset, buffer_length(self->buf)); return 1; }
void broadcast (MatrixViewType& R, const int rootProc) { const int myRank = messenger_->rank(); if (myRank == rootProc) pack (R); messenger_->broadcast (&buffer_[0], buffer_length (R.ncols()), rootProc); if (myRank != rootProc) unpack (R); }
variable flux_buffer_length(opts o,interpreter *i) { variable v=init_variable(); int b=0; if (i->cast_int(&b,o,0)) { return i->int_variable(buffer_length(b)); } return v; }
/* This should be called with a unique decoder instance as the seeking * it does triggers an FAAD bug which results in distorted audio due to * retained state being corrupted. (One suspects NeAACDecPostSeekReset() * should resolve the problem but experimentation suggests not and no * documentation exists describing its use.) */ static int aac_count_time (struct aac_data *data) { NeAACDecFrameInfo frame_info; int samples = 0, bytes = 0, frames = 0; off_t file_size; int16_t *sample_buf; file_size = io_file_size (data->stream); if (file_size == -1) return -1; if (io_seek(data->stream, file_size / 2, SEEK_SET) == -1) return -1; buffer_flush (data); /* Guess track length by decoding the middle 50 frames which have * more than 25% of samples having absolute values greater than 16. */ while (frames < 50) { if (buffer_fill_frame (data) <= 0) break; sample_buf = NeAACDecDecode (data->decoder, &frame_info, buffer_data (data), buffer_length (data)); if (frame_info.error == 0 && frame_info.samples > 0) { unsigned int ix, zeroes = 0; for (ix = 0; ix < frame_info.samples; ix += 1) { if (RANGE(-16, sample_buf[ix], 16)) zeroes += 1; } if (zeroes * 4 < frame_info.samples) { samples += frame_info.samples; bytes += frame_info.bytesconsumed; frames += 1; } } if (frame_info.bytesconsumed == 0) break; buffer_consume (data, frame_info.bytesconsumed); } if (frames == 0) return -1; samples /= frames; samples /= data->channels; bytes /= frames; return ((file_size / bytes) * samples) / data->sample_rate; }
// --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // core_frame_ethernet_c* core_frame_ethernet_c::instance( u16_t data_length, const u8_t* data, bool_t is_copied ) { DEBUG( "core_frame_ethernet_c::instance()" ); DEBUG1( "core_frame_ethernet_c::instance() - is_copied %u", is_copied ); if ( data_length < CORE_ETHERNET_MIN_LENGTH ) { DEBUG( "core_frame_ethernet_c::instance() - not a valid Ethernet frame, frame is too short" ); return NULL; } u8_t* buffer = const_cast<u8_t*>( data ); u16_t buffer_length( 0 ); if ( is_copied ) { buffer_length = data_length; buffer = new u8_t[buffer_length]; if ( !buffer ) { DEBUG( "core_frame_ethernet_c::instance() - not able to allocate buffer for copying" ); return NULL; } core_tools_c::copy( buffer, data, buffer_length ); } core_frame_ethernet_c* instance = new core_frame_ethernet_c( data_length, buffer, buffer_length ); if ( !instance ) { DEBUG( "core_frame_ethernet_c::instance() - unable to create an instance" ); if ( is_copied ) { delete[] buffer; } return NULL; } return instance; }
void prf(char *format, ...) { string b = allocate_string(prf_heap); va_list ap; string f = alloca_string(format); va_start(ap, format); vbprintf(b, f, ap); va_end(ap); write(1, bref(b, 0), buffer_length(b)); // deallocate_buffer(b); }
ssize_t buffer_compact(buffer_t *self) { size_t len = buffer_length(self); size_t rem = self->len - len; char *buf = calloc(len, 1); if (!buf) return -1; memcpy(buf, self->data, len); free(self->alloc); self->len = len; self->data = self->alloc = buf; return rem; }
static struct pdu * pdu_create_from_gfp(gfp_t flags, const struct sdu * sdu) { struct pdu * tmp_pdu; const struct buffer * tmp_buff; const uint8_t * ptr; size_t pci_size; /* FIXME: This implementation is pure crap, please fix it soon */ if (!sdu_is_ok(sdu)) return NULL; tmp_buff = sdu_buffer_ro(sdu); ASSERT(tmp_buff); if (buffer_length(tmp_buff) < pci_length_min()) return NULL; /* FIXME: We should compute the real PCI length */ pci_size = pci_length_min(); tmp_pdu = pdu_create_gfp(flags); if (!tmp_pdu) return NULL; ptr = (const uint8_t *) buffer_data_ro(tmp_buff); ASSERT(ptr); tmp_pdu->pci = pci_create_from_gfp(flags, ptr); tmp_pdu->buffer = buffer_create_from_gfp(flags, ptr + pci_size, (buffer_length(sdu->buffer) - pci_size)); ASSERT(pdu_is_ok(tmp_pdu)); return tmp_pdu; }
void put(CharT ch) { if (p_ < end_buffer_) { *p_++ = ch; } else { os_.write(begin_buffer_, buffer_length()); p_ = begin_buffer_; put(ch); } }
static char *parse_cmdname(const char **s) { skip_spaces(s); Buffer buf; buffer_init(&buf); while (isalpha((unsigned char)**s) || **s == '-') buffer_append(&buf, (*s)++, 1); if (buffer_length(&buf)) buffer_append(&buf, "\0", 1); return buf.data; }
static char *parse_until(const char **s, const char *until) { Buffer buf; buffer_init(&buf); size_t len = strlen(until); while (**s && !memchr(until, **s, len)) buffer_append(&buf, (*s)++, 1); if (buffer_length(&buf)) buffer_append(&buf, "\0", 1); return buf.data; }
/* Consumes and generates bytecode for all characters up to and including the next ']' or EOF. This function is similar to write_section(), except that this function assumes the next token is the beginning of a loop body. */ static void write_loop(buffer destination, FILE* source, java_file file) { bool success; buffer loop_body = buffer_create(64); write_section(loop_body, source, file); success = (get_token(source) == ']'); /* Otherwise it's EOF. */ if (success) { write_loop_prologue(destination, buffer_length(loop_body)); buffer_write_buffer(destination, loop_body); write_loop_epilogue(destination, buffer_length(loop_body)); } buffer_free(loop_body); if (!success) { printf("Error: Unmatched \"[\".\n"); abort(); } }
static int buffer_fill_min (struct aac_data *data, int len) { int rc; assert (len < BUFFER_SIZE); while (buffer_length(data) < len) { rc = buffer_fill (data); if (rc <= 0) return rc; } return 1; }
hint32 vmBinaryLength(vmBinary * binary,InvokeTickDeclare){ hint32 length = sizeof(vmRuntimeClassLibraryBytes); length += sizeof(vmRuntimeClassMetaBytes) * vmCompileObjectArrayCount(binary->classMetas); length += sizeof(vmClassMetaOffset) * binary->uniqueKeyCount; length += binary->classOffset; length += binary->operatorOffset; length += buffer_length(binary->uniqueKeys); return length; }
void write(const CharT* s, size_t length) { size_t diff = end_buffer_ - p_; if (diff >= length) { std::memcpy(p_, s, length*sizeof(CharT)); p_ += length; } else { os_.write(begin_buffer_, buffer_length()); os_.write(s,length); p_ = begin_buffer_; } }
/* scans forward to the next aac frame and makes sure * the entire frame is in the buffer. */ static int buffer_fill_frame(struct aac_data *data) { unsigned char *datap; int rc, n, len; int max = 32768; while (1) { /* need at least 6 bytes of data */ rc = buffer_fill_min(data, 6); if (rc <= 0) break; len = buffer_length(data); datap = buffer_data(data); /* scan for a frame */ for (n = 0; n < len - 5; n++) { /* give up after 32KB */ if (max-- == 0) { logit ("no frame found!"); /* FIXME: set errno? */ return -1; } /* see if there's a frame at this location */ rc = parse_frame(datap + n); if (rc == 0) continue; /* found a frame, consume all data up to the frame */ buffer_consume (data, n); /* rc == frame length */ rc = buffer_fill_min (data, rc); if (rc <= 0) goto end; return 1; } /* consume what we used */ buffer_consume (data, n); } end: return rc; }
/** @brief Send any response messages that have accumulated in the output buffer. @param ses Pointer to the current application session. @param outbuf Pointer to the output buffer. @return Zero if successful, or -1 if not. Used only by servers to respond to clients. */ static int flush_responses( osrfAppSession* ses, growing_buffer* outbuf ) { // Collect any inbound traffic on the socket(s). This doesn't accomplish anything for the // immediate task at hand, but it may help to keep TCP from getting clogged in some cases. osrf_app_session_queue_wait( ses, 0, NULL ); int rc = 0; if( buffer_length( outbuf ) > 0 ) { // If there's anything to send... buffer_add_char( outbuf, ']' ); // Close the JSON array if( osrfSendTransportPayload( ses, OSRF_BUFFER_C_STR( ses->outbuf ))) { osrfLogError( OSRF_LOG_MARK, "Unable to flush response buffer" ); rc = -1; } } buffer_reset( ses->outbuf ); return rc; }