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 put_namerecord_w( BUFFER *rec_buf, BUFFER *stringbuf, char *name, uint16_t platformID, uint16_t encodingID, uint16_t languageID, uint16_t nameID ) { char *name_victim = name; size_t size_org = strlen( name ); size_t inbytesleft = size_org; size_t outbytes_org = 2*size_org + 2; size_t outbytesleft = outbytes_org; char *converted = malloc( outbytes_org ); char *converted_victim = converted; /* Convert from ISO8859-15 to UCS2 big-endian ( specifying endianness removes the BOM ), replacing untranslateable characters with similar ones ( but there shouldn't be any ), */ iconv_t iconv_settings = iconv_open( "UCS-2BE//TRANSLIT", "ISO8859-15" ); if ( iconv_settings == ( iconv_t ) -1 ) perror( "iconv_open failure : " ); /* second argument should be (const char**) on FreeBSD but (char**) is expected on Linuses and OS X. FreeBSD must live with the compile warning. */ size_t iconv_res = iconv( iconv_settings, ( char** ) &name_victim, &inbytesleft, &converted_victim, &outbytesleft ); iconv_close( iconv_settings ); size_t converted_bytes = outbytes_org - outbytesleft; if ( iconv_res != 0 ) perror( "iconv error : " ); NAME_RECORD *nr = buffer_alloc( rec_buf, sizeof( NAME_RECORD ) ); nr->length = htons( converted_bytes ); nr->offset = htons( stringbuf->position ); nr->platformID = htons( platformID ); nr->encodingID = htons( encodingID ); nr->languageID = htons( languageID ); nr->nameID = htons( nameID ); char *str = buffer_alloc( stringbuf, converted_bytes + 2 ); memcpy( str, converted, converted_bytes ); free( converted ); }
BUFFER *set_table_cmap( void ) { BUFFER *cmap_buf = makebuffer( sizeof( OTF_TABLE_CMAP ) + sizeof( OTF_CMAP_FORMAT4_ENCTABLE ) + sizeof( OTF_CMAP_FORMAT6_ENCTABLE ) ); OTF_TABLE_CMAP *t_cmaptable = buffer_alloc( cmap_buf, sizeof( OTF_TABLE_CMAP ) ); t_cmaptable->version = 0; t_cmaptable->numTables = htons( 3 ); t_cmaptable->record1.platformID = 0; t_cmaptable->record1.encodingID = htons( 3 ); t_cmaptable->record2.platformID = htons( 1 ); t_cmaptable->record2.encodingID = 0; t_cmaptable->record3.platformID = htons( 3 ); t_cmaptable->record3.encodingID = htons( 1 ); OTF_CMAP_FORMAT4_ENCTABLE *t_cmap_encode4 = buffer_alloc( cmap_buf, sizeof( OTF_CMAP_FORMAT4_ENCTABLE ) ); t_cmap_encode4->format = htons( 4 ); t_cmap_encode4->length = htons( 40 ); t_cmap_encode4->segCountX2 = htons( 6 ); t_cmap_encode4->searchRange = htons( 4 ); t_cmap_encode4->entrySelector = htons( 1 ); t_cmap_encode4->rangeShift = htons( 2 ); t_cmap_encode4->endCount1 = htons( 32 ); t_cmap_encode4->endCount2 = htons( 46 ); t_cmap_encode4->endCount3 = htons( 65535 ); t_cmap_encode4->startCount1 = htons( 32 ); t_cmap_encode4->startCount2 = htons( 46 ); t_cmap_encode4->startCount3 = htons( 65535 ); t_cmap_encode4->idDelta1 = htons( -31 ); t_cmap_encode4->idDelta2 = htons( -44 ); t_cmap_encode4->idDelta3 = htons( 1 ); OTF_CMAP_FORMAT6_ENCTABLE *t_cmap_encode6 = buffer_alloc( cmap_buf, sizeof( OTF_CMAP_FORMAT6_ENCTABLE ) ); t_cmap_encode6->format = htons( 6 ); t_cmap_encode6->length = htons( 86 ); t_cmap_encode6->firstCode = htons( 9 ); t_cmap_encode6->entryCount = htons( 38 ); t_cmap_encode6->entry[0] = htons( 1 ); t_cmap_encode6->entry[4] = htons( 1 ); t_cmap_encode6->entry[23] = htons( 1 ); t_cmap_encode6->entry[37] = htons( 2 ); /* Record one and three point at the same character. Record two is offset * immediatly following record one. */ t_cmaptable->record1.offset = htonl( sizeof( OTF_TABLE_CMAP ) ); t_cmaptable->record2.offset = htonl( sizeof( OTF_TABLE_CMAP ) + sizeof( OTF_CMAP_FORMAT4_ENCTABLE ) ); t_cmaptable->record3.offset = htonl( sizeof( OTF_TABLE_CMAP ) ); return cmap_buf; }
//Scans the full the Cog and returns iterator. iterator scan_full_array( struct cog *cog) { if( cog->type == COG_BTREE ) { struct cog *a = cog->data.btree.lhs; struct cog *b = cog->data.btree.rhs; return iter_concat( scan_full_array(a), scan_full_array(b) ); } if( cog->type == COG_CONCAT ) { struct cog *a = cog->data.concat.lhs; struct cog *b = cog->data.concat.rhs; return iter_concat( scan_full_array(a), scan_full_array(b) ); } if( cog->type == COG_SORTEDARRAY ) { int start = cog->data.array.start; int len = cog->data.array.len; struct record *in = buffer_start(cog->data.array.records, start); struct buffer *out = buffer_alloc(cog->data.sortedarray.len + 1); int i, tgt; iterator ret; for(i = 0, tgt = 0; i < len; i++){ record_copy(&(in[i]), &(out->data[tgt])); tgt++; } ret = array_iter_alloc(out, 0, tgt); return ret; } if( cog->type == COG_ARRAY ) { int start = cog->data.array.start; int len = cog->data.array.len; struct record *in = buffer_start(cog->data.array.records, start); struct buffer *out = buffer_alloc(cog->data.array.len ); int i, tgt; iterator ret; for(i = 0, tgt = 0; i < len; i++){ record_copy(&(in[i]), &(out->data[tgt])); tgt++; } ret = array_iter_alloc(out, 0, tgt); return ret; } fprintf(stderr, "Invalid Cog Type %d\n", cog->type); exit(-1); }
void put_namerecord( BUFFER *rec_buf, BUFFER *stringbuf, char *name, uint16_t platformID, uint16_t encodingID, uint16_t languageID, uint16_t nameID ) { int length = strlen( name ); char *str; NAME_RECORD *nr = buffer_alloc( rec_buf, sizeof( NAME_RECORD ) ); nr->length = htons( length ); nr->offset = htons( stringbuf->position ); nr->platformID = htons( platformID ); nr->encodingID = htons( encodingID ); nr->languageID = htons( languageID ); nr->nameID = htons( nameID ); str = buffer_alloc( stringbuf, length + 1 ); memcpy( str, name, length ); }
Value * CDExpNotEqual::sql(const char * prefix,const char * suffix){ InvokeTickBegin hbuffer_t buf = buffer_alloc(128, 128); Value * v = NULL; buffer_append_str(buf, "("); v = _left->sql(prefix, suffix); if(v){ buffer_append_str(buf, v->stringValue()); } buffer_append_str(buf, " <= "); v = _right->sql(prefix, suffix); if(v){ buffer_append_str(buf, v->stringValue()); } buffer_append_str(buf, ")"); v = (Value *)(new Value(buffer_to_str(buf)))->autorelease(); buffer_dealloc(buf); return v; }
Value * Bundle::getStringContentV(const char * format,va_list va){ Value * path = getFilePathV(format,va); if(_parent){ return _parent->getStringContent(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); } v = Value::newValue(buffer_to_str(content)); buffer_dealloc(content); return v; }
void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count) { if (UNLIKELY(new_count > buffer->alloc_count)) { if (buffer->flag & BLI_BUFFER_USE_STATIC) { void *orig = buffer->data; buffer->data = buffer_alloc(buffer, new_count); memcpy(buffer->data, orig, buffer->elem_size * buffer->count); buffer->alloc_count = new_count; buffer->flag &= ~BLI_BUFFER_USE_STATIC; } else { if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) { buffer->alloc_count *= 2; } else { buffer->alloc_count = new_count; } buffer->data = buffer_realloc(buffer, buffer->alloc_count); } } buffer->count = new_count; }
/** This function writes a filtered version of string to a file. */ void subst_list_filtered_fprintf(const subst_list_type * subst_list , const char * string , FILE * stream) { buffer_type * buffer = buffer_alloc( strlen(string) + 1 ); buffer_fwrite( buffer , string , 1 , strlen( string ) + 1); subst_list_update_buffer(subst_list , buffer); buffer_stream_fwrite_n( buffer , 0 , -1 , stream ); /* -1: The trailing \0 is not explicitly written to file. */ buffer_free(buffer); }
bool register_put(Register *reg, Text *txt, Filerange *range) { size_t len = range->end - range->start; if (!buffer_alloc((Buffer*)reg, len)) return false; reg->len = text_bytes_get(txt, range->start, len, reg->data); return true; }
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); } }
/** * Allocate a buffer for a dot-matrix page. * This is only done once per page at initialization. */ static struct buffer *asciidmd_alloc (void) { struct buffer *buf = buffer_alloc (128 * 32 / 8); buf->width = 128 / 8; buf->height = 32; return buf; }
static vmVariant vmSqliteClassInitCallback(vmRuntimeContext context,vmClass * clazz,vmObject * object,vmVariantList args,InvokeTickDeclare){ vmVariant rs= {vmVariantTypeVoid,0}; vmSqlite * sqlite = (vmSqlite *) object; hbuffer_t file = buffer_alloc(128, 128); vmVariantToString(context, vmVariantListGet(args, 0), file); if(sqlite3_open(buffer_to_str(file), & sqlite->_sqlite)){ rs = vmRuntimeContextException(context, sqlite3_errcode(sqlite->_sqlite), sqlite3_errmsg(sqlite->_sqlite)); sqlite3_close(sqlite->_sqlite); sqlite->_sqlite = NULL; } buffer_dealloc(file); sqlite->uniqueKeys.exec = vmRuntimeContextGetUniqueKey(context, "exec"); sqlite->uniqueKeys.query = vmRuntimeContextGetUniqueKey(context, "query"); sqlite->uniqueKeys.lastRowId = vmRuntimeContextGetUniqueKey(context, "lastRowId"); sqlite->uniqueKeys.encode = vmRuntimeContextGetUniqueKey(context, "encode"); sqlite->uniqueKeys.sql = vmRuntimeContextGetUniqueKey(context, "sql"); return rs; }
void hdata_print_json(const hdata_class_t * data_class,hdata_t data,InvokeTickDeclare){ if(data_class && DATA_STRUCT_ASSET(data)){ hbuffer_t buffer = buffer_alloc(1024, 1024); hdata_write_json(data_class, data, buffer, hbool_true,InvokeTickArg); hlog("\n\n%s\n\n",buffer_to_str(buffer)); buffer_dealloc(buffer); } }
bool register_append(Register *reg, Text *txt, Filerange *range) { size_t rem = reg->size - reg->len; size_t len = range->end - range->start; if (len > rem && !buffer_alloc((Buffer*)reg, reg->size + len - rem)) return false; reg->len += text_bytes_get(txt, range->start, len, reg->data + reg->len); return true; }
hbool vmBinaryInit(vmBinary * binary,InvokeTickDeclare){ mem_memset(binary, 0, sizeof(vmBinary)); binary->uniqueKeys = buffer_alloc(1024, 1024); binary->classMetas = vmCompileObjectArrayCreate(); binary->operatorMetas = vmCompileObjectArrayCreate(); return hbool_true; }
void enkf_fs_fwrite_restart_kw_list(enkf_fs_type * enkf_fs , int report_step , int iens, const stringlist_type * kw_list) { buffer_type * buffer = buffer_alloc(1024); stringlist_buffer_fwrite( kw_list , buffer ); { fs_driver_type * driver = enkf_fs->index; driver->save_node(driver , "kw_list" , report_step , iens , buffer); } buffer_free( buffer ); }
void enkf_fs_fread_restart_kw_list(enkf_fs_type * enkf_fs , int report_step , int iens, stringlist_type * kw_list) { buffer_type * buffer = buffer_alloc(1024); { fs_driver_type * driver = enkf_fs->index; driver->load_node(driver , "kw_list" , report_step , iens , buffer); } stringlist_buffer_fread( kw_list , buffer ); buffer_free( buffer ); }
BUFFER *set_table_maxp( int CFFnumGlyphs ) { BUFFER *maxp_buf = makebuffer( sizeof( OTF_TABLE_MAXP ) ); OTF_TABLE_MAXP *t_maxptable = buffer_alloc( maxp_buf, sizeof( OTF_TABLE_MAXP ) ); t_maxptable->version.minor = htons( 0x5000 ); t_maxptable->numGlyphs = htons( ntohs( CFFnumGlyphs ) ); return maxp_buf; }
void tree_mem_init(void) { /* We copy the settings value in case it is changed by the user. We can't use it until the next reboot. */ max_files = global_settings.max_files_in_dir; /* initialize tree context struct */ memset(&tc, 0, sizeof(tc)); tc.dirfilter = &global_settings.dirfilter; tc.sort_dir = global_settings.sort_dir; tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files; tc.name_buffer = buffer_alloc(tc.name_buffer_size); tc.dircache_size = max_files * sizeof(struct entry); tc.dircache = buffer_alloc(tc.dircache_size); tree_get_filetypes(&filetypes, &filetypes_count); }
static inline void buffer_check_limits(struct real_buffer *buf, size_t pos, size_t data_size) { unsigned int extra; size_t new_size; if (unlikely((size_t)-1 - pos < data_size)) { i_panic("Buffer write out of range (%"PRIuSIZE_T " + %"PRIuSIZE_T")", pos, data_size); } new_size = pos + data_size; if (new_size > buf->used && buf->used < buf->dirty) { /* clear used..dirty area */ size_t max = I_MIN(I_MIN(buf->alloc, buf->dirty), new_size); memset(buf->w_buffer + buf->used, 0, max - buf->used); } /* always keep +1 byte allocated available in case str_c() is called for this buffer. this is mainly for cases where the buffer is allocated from data stack, and str_c() is called in a separate stack frame. */ extra = buf->dynamic ? 1 : 0; if (new_size + extra > buf->alloc) { if (unlikely(!buf->dynamic)) { i_panic("Buffer full (%"PRIuSIZE_T" > %"PRIuSIZE_T", " "pool %s)", pos + data_size, buf->alloc, buf->pool == NULL ? "<none>" : pool_get_name(buf->pool)); } buffer_alloc(buf, pool_get_exp_grown_size(buf->pool, buf->alloc, new_size + extra)); } #if 0 else if (new_size > buf->used && buf->alloced && !buf->pool->alloconly_pool && !buf->pool->datastack_pool) { void *new_buf; /* buffer's size increased: move the buffer's memory elsewhere. this should help catch bugs where old pointers are tried to be used to access the buffer's memory */ new_buf = p_malloc(buf->pool, buf->alloc); memcpy(new_buf, buf->w_buffer, buf->alloc); p_free(buf->pool, buf->w_buffer); buf->w_buffer = new_buf; buf->r_buffer = new_buf; } #endif if (new_size > buf->used) buf->used = new_size; i_assert(buf->used <= buf->alloc); }
int main(int argc, char **argv) { if(argc != 2) exit(EXIT_FAILURE); buffer_t *in = buffer_alloc(10); buffer_t *buf = buffer_alloc(10); // gzFile f = gzopen(argv[1],"r"); FILE *f = fopen(argv[1],"r"); if(f == NULL) exit(EXIT_FAILURE); int i; for(i = 0; freadline_buf(f,in,buf) > 0; i++) { printf("line %i: '%s'\n", i, buf->b); } fclose(f); buffer_destroy(buf); }
buffer_t *buffer_create_dynamic(pool_t pool, size_t init_size) { struct real_buffer *buf; buf = p_new(pool, struct real_buffer, 1); buf->pool = pool; buf->dynamic = TRUE; buffer_alloc(buf, init_size); return (buffer_t *)buf; }
/** * Acquire a random array cog. * * @param size - size of the array * @param range - key range * @return a random array cog */ cog *getRandomArray(int size, int range) { buffer buffer = buffer_alloc(size); for(int i = 0; i < size; i++){ buffer->data[i].key = rand() % range; buffer->data[i].value = rand(); } return make_array(0, size, buffer);; }
void *insert_full_table( BUFFER *complete_font, OTF_TABLE_RECORD *head_entry, BUFFER *insert_me ) { buffer_alloc( complete_font, 4 - ( complete_font->position % 4 ) ); /* Pad to a multiple of 4 */ void *table = &complete_font->data[complete_font->position]; head_entry->offset = complete_font->position; head_entry->length = insert_me->position; insert_buffer( complete_font, insert_me ); free_buffer( insert_me ); return table; }
/* * Initialize the trail. */ extern void solver_init_trail(void) { __solver_trail_len = 0; size_t size = sizeof(struct trailentry_s)*0x7FFFFFFF; __solver_trail = (trailentry_t)buffer_alloc(size); if (!gc_dynamic_root((void **)&__solver_trail, &__solver_trail_len, sizeof(struct trailentry_s))) panic("failed to set GC dynamic root for trail: %s", strerror(errno)); }
jstring Java_euphoria_psycho_comic_util_Utilities_request(JNIEnv *env, jstring host, jstring path) { const char *n_host = (*env)->GetStringUTFChars(env, host, 0); const char *n_path = (*env)->GetStringUTFChars(env, path, 0); LOGW("%s",n_host); Buffer *response = buffer_alloc(255); int status = 0; int sockfd = 0; struct addrinfo *res = NULL; status = init_connection(n_host, "80", &res); error_unless(status == 0, "Could not resolve host: %s\n", gai_strerror(status)); jstring result1 = (*env)->NewStringUTF(env, n_host); return result1; sockfd = make_connection(res); error_unless(sockfd > 0, "Could not make connection to '"); status = make_request(sockfd, n_host, n_path); error_unless(status > 0, "Sending request failed"); status = fetch_response(sockfd, &response, RECV_SIZE); error_unless(status >= 0, "Fetching response failed"); printf("%s\n", response->contents); close(sockfd); freeaddrinfo(res); jstring result = (*env)->NewStringUTF(env, response->contents); buffer_free(response); (*env)->ReleaseStringUTFChars(env, host, n_host); (*env)->ReleaseStringUTFChars(env, path, n_path); return result; error: if (sockfd > 0) { close(sockfd); } if (res != NULL) { freeaddrinfo(res); } (*env)->ReleaseStringUTFChars(env, host, n_host); (*env)->ReleaseStringUTFChars(env, path, n_path); return (*env)->NewStringUTF(env, "Error"); buffer_free(response); // return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " // ABI // "."); }
// Allocates and returns a new Buffer with a copy of a range of another Buffer. Buffer buffer_slice(const Buffer *buffer, size_t size, size_t offset) { //FIXME: Range check problem. Need to cover if size and/or offset are too large (three states). if ((size + offset) > malloc_size(buffer->data)) { size = malloc_size(buffer->data) - offset; } Buffer new_buffer = buffer_alloc(size); buffer_set(&new_buffer, buffer->data+offset, size); return new_buffer; }
char* buffer_string_prepare_copy(buffer *b, size_t size) { force_assert(NULL != b); force_assert(size + 1 > size); buffer_alloc(b, size + 1); b->used = 1; b->ptr[0] = '\0'; return b->ptr; }
char * util_alloc_envvar( const char * value ) { if (value == NULL) return NULL; else { buffer_type * buffer = buffer_alloc( 1024 ); /* Start by filling up a buffer instance with the current content of @value. */ buffer_fwrite_char_ptr( buffer , value ); buffer_fwrite_char( buffer , '\0' ); buffer_rewind( buffer ); while (true) { if (buffer_strchr( buffer , '$')) { const char * data = buffer_get_data( buffer ); int offset = buffer_get_offset( buffer ) + 1; /* Points at the first character following the '$' */ int var_length = 0; /* Find the length of the variable name */ while (true) { char c; c = data[offset + var_length]; if (!(isalnum( c ) || c == '_')) /* Any character which is NOT in the set [a-Z,0-9_] marks the end of the variable. */ break; if (c == '\0') /* The end of the string. */ break; var_length += 1; } { char * var_name = util_alloc_substring_copy( data , offset - 1 , var_length + 1); /* Include the leading $ */ const char * var_value = getenv( &var_name[1] ); if (var_value != NULL) buffer_search_replace( buffer , var_name , var_value); /* The actual string replacement. */ else buffer_fseek( buffer , var_length , SEEK_CUR ); /* The variable is not defined, and we leave the $name. */ free( var_name ); } } else break; /* No more $ to replace */ } buffer_shrink_to_fit( buffer ); { char * expanded_value = buffer_get_data( buffer ); buffer_free_container( buffer ); return expanded_value; } } }