const char *construct_label(struct rmonitor_file_watch_info *f) { struct rmonitor_file_watch_event *e; static buffer_t *b = NULL; if(!b) { b = malloc(sizeof(*b)); buffer_init(b); } buffer_rewind(b, 0); int event_count = 0; char *sep = ""; list_first_item(f->events); while((e = list_next_item(f->events))) { if(e->cycle_count > 0) { e->total_count += e->cycle_count; event_count += e->cycle_count; buffer_printf(b, "%s%s(%" PRId64 ")", sep, e->label, e->cycle_count); sep = ","; } } if(event_count) { return buffer_tostring(b); } else { return NULL; } }
/* ** self = buffer:rewind() ** ** discard all the data in the buffer ** ** allocated memory stays the same */ static int lbuffer_rewind(lua_State *L) { Buffer *buffer = buffer_lcheck(L, 1); buffer_rewind(buffer); lua_pushvalue(L, 1); return 1; }
void buffer_fread_realloc(buffer_type * buffer , const char * filename) { size_t file_size = util_file_size( filename ); FILE * stream = util_fopen( filename , "r"); buffer_clear( buffer ); /* Setting: content_size = 0; pos = 0; */ buffer_stream_fread( buffer , file_size , stream ); buffer_rewind( buffer ); /* Setting: pos = 0; */ fclose( stream ); }
void enkf_fs_fread_vector(enkf_fs_type * enkf_fs , buffer_type * buffer , const char * node_key , enkf_var_type var_type , int iens , state_enum state) { fs_driver_type * driver = enkf_fs_select_driver(enkf_fs , var_type , state , node_key ); buffer_rewind( buffer ); driver->load_vector(driver , node_key , iens , buffer); }
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; } } }
/** Updates the buffer inplace with all the string substitutions in the subst_list. This is the lowest level function, which does *NOT* consider the parent pointer. */ static void subst_list_replace_strings__(const subst_list_type * subst_list , buffer_type * buffer) { int index; for (index = 0; index < vector_get_size( subst_list->string_data ); index++) { const subst_list_string_type * node = vector_iget_const( subst_list->string_data , index ); if (node->value != NULL) { bool match; buffer_rewind( buffer ); do { match = buffer_search_replace( buffer , node->key , node->value); } while (match); } } }
void enkf_fs_fread_node(enkf_fs_type * enkf_fs , buffer_type * buffer , const char * node_key , enkf_var_type var_type , int report_step, int iens , state_enum state) { fs_driver_type * driver = enkf_fs_select_driver(enkf_fs , var_type , state , node_key ); if (var_type == PARAMETER) report_step = __get_parameter_report_step(driver , node_key , report_step , iens , state); buffer_rewind( buffer ); driver->load_node(driver , node_key , report_step , iens , buffer); }
static void subst_list_eval_funcs____(const subst_list_type * subst_list , const parser_type * parser , buffer_type * buffer) { { int index; for (index = 0; index < vector_get_size( subst_list->func_data); index++) { const subst_list_func_type * subst_func = vector_iget_const( subst_list->func_data , index ); const char * func_name = subst_func->name; bool match; buffer_rewind( buffer ); do { size_t match_pos; match = buffer_strstr( buffer , func_name ); match_pos = buffer_get_offset( buffer ); if (match) { bool update = false; char * arg_start = buffer_get_data( buffer ); arg_start += buffer_get_offset( buffer ) + strlen( func_name ); if (arg_start[0] == '(') { /* We require that an opening paren follows immediately behind the function name. */ char * arg_end = strchr( arg_start , ')'); if (arg_end != NULL) { /* OK - we found an enclosing () pair. */ char * arg_content = util_alloc_substring_copy( arg_start, 1 , arg_end - arg_start - 1); stringlist_type * arg_list = parser_tokenize_buffer( parser , arg_content , true); char * func_eval = subst_list_func_eval( subst_func , arg_list ); int old_len = strlen(func_name) + strlen( arg_content) + 2; if (func_eval != NULL) { buffer_memshift( buffer , match_pos + old_len , strlen( func_eval ) - old_len); buffer_fwrite( buffer , func_eval , strlen( func_eval ) , sizeof * func_eval ); free( func_eval ); update = true; } free( arg_content ); stringlist_free( arg_list ); } } if (!update) buffer_fseek( buffer , match_pos + strlen( func_name ) , SEEK_SET); } } while (match); } } if (subst_list->parent != NULL) subst_list_eval_funcs____( subst_list->parent , parser , buffer ); }
static void do_debug(INT64_T flags, const char *fmt, va_list args) { buffer_t B; char ubuf[1<<16]; buffer_init(&B); buffer_ubuf(&B, ubuf, sizeof(ubuf)); buffer_max(&B, sizeof(ubuf)); if (debug_write == debug_file_write || debug_write == debug_stderr_write || debug_write == debug_stdout_write) { struct timeval tv; struct tm *tm; gettimeofday(&tv, 0); tm = localtime(&tv.tv_sec); buffer_putfstring(&B, "%04d/%02d/%02d %02d:%02d:%02d.%02ld ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (long) tv.tv_usec / 10000); buffer_putfstring(&B, "%s[%d] ", debug_program_name, getpid()); } /* Parrot prints debug messages for children: */ if (getpid() != debug_getpid()) { buffer_putfstring(&B, "<child:%d> ", (int)debug_getpid()); } buffer_putfstring(&B, "%s: ", debug_flags_to_name(flags)); buffer_putvfstring(&B, fmt, args); while(isspace(buffer_tostring(&B)[buffer_pos(&B)-1])) buffer_rewind(&B, buffer_pos(&B)-1); /* chomp whitespace */ buffer_putliteral(&B, "\n"); debug_write(flags, buffer_tostring(&B)); if(terminal_available && (flags & (D_ERROR | D_NOTICE | D_FATAL))) { if(debug_write != debug_stderr_write || !isatty(STDERR_FILENO)) { if(!terminal_f) { if((terminal_f = fopen(terminal_path, "a")) == NULL) { /* print to wherever stderr is pointing that we could not open the terminal. */ terminal_available = 0; } } } if(terminal_f) fprintf(terminal_f, "%s", buffer_tostring(&B)); } buffer_free(&B); }
static int find (buffer_t *B, const size_t base, buffer_t *path, const char *pattern, int recursive) { int rc = 0; DIR *D = opendir(buffer_tostring(path)); if (D) { struct dirent *entry; size_t current = buffer_pos(path); while ((entry = readdir(D))) { struct stat buf; if (buffer_putstring(path, entry->d_name) == -1) goto failure; /* N.B. We don't use FNM_PATHNAME, so `*.c' matches `foo/bar.c' */ if (fnmatch(pattern, buffer_tostring(path)+base, 0) == 0) { if (buffer_printf(B, "%s%c", buffer_tostring(path), 0) == -1) goto failure; /* NUL padded */ rc += 1; } if (recursive && strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..") && stat(buffer_tostring(path), &buf) == 0 && S_ISDIR(buf.st_mode)) { if (buffer_putliteral(path, "/") == -1) goto failure; int found = find(B, base, path, pattern, recursive); if (found == -1) goto failure; else if (found > 0) rc += found; } buffer_rewind(path, current); } } /* else skip */ goto out; failure: rc = -1; goto out; out: if (D) closedir(D); return rc; }
void stat_header_received(HURLPath *path, int response_code, HURLHeader *headers, size_t header_len) { HURLHeader *h; ElementStat *stat = (ElementStat *)path->tag; struct tm date; Buffer *json = NULL; char *tmp; char *escaped; size_t tmp_len; /* Initialize HTTP statistics */ if (!stat->http) { stat->http = calloc(1, sizeof(HTTPStat)); } if (!buffer_init(&json, 1024, 256)) { return; } buffer_insert_strlen(json, "{"); stat->http->response_code = response_code; stat->http->header_size = (int)header_len; /* Check if header data should be ignored. */ h = headers; while (h != NULL) { if (strcasecmp("date", h->key) == 0) { strptime(h->value, "%a, %d %b %Y %T %z", &date); stat->http->date = mktime(&date); } else if (strcasecmp("expires", h->key) == 0) { strptime(h->value, "%a, %d %b %Y %T %z", &date); stat->http->expiry_date = mktime(&date); } else if (test->stats.http.all_headers || hurl_header_exists(test->stat_headers, h->key)) { escaped = json_escape(h->value); tmp_len = strlen(escaped) + strlen(h->key) + strlen("\"\":\"\",") + 1; tmp = malloc(sizeof(char) * tmp_len); snprintf(tmp, tmp_len, "\"%s\":\"%s\",", h->key, escaped); buffer_insert_strlen(json, tmp); free(escaped); free(tmp); } /* Specifically extract content type header */ if (strcasecmp("content-type", h->key) == 0) { stat->http->content_type = allocstrcpy(h->value, strlen(h->value), 1); } h = h->next; } /* Remove last comma */ if (json->data_len > 1) { buffer_rewind(json, 1); } buffer_insert_strlen(json, "}"); buffer_trim(json); stat->http->headers = json->head; free(json); }