int Level::get_total_coins() const { int total_coins = 0; for(auto const& sector : m_sectors) { for(const auto& o: sector->get_objects()) { auto coin = dynamic_cast<Coin*>(o.get()); if(coin) { total_coins++; continue; } auto block = dynamic_cast<BonusBlock*>(o.get()); if(block) { if (block->get_contents() == BonusBlock::CONTENT_COIN) { total_coins += block->get_hit_counter(); continue; } else if (block->get_contents() == BonusBlock::CONTENT_RAIN || block->get_contents() == BonusBlock::CONTENT_EXPLODE) { total_coins += 10; continue; } } auto goldbomb = dynamic_cast<GoldBomb*>(o.get()); if(goldbomb) total_coins += 10; } } return total_coins; }
static void parse_parameter(rapidxml::xml_node<>* node, parameter& p) { // #define: <param><defname>Point</defname></param> // template: <param><type>typename</type><declname>CoordinateType</declname><defname>CoordinateType</defname></param> // template with default: <param><type>typename</type><declname>CoordinateSystem</declname><defname>CoordinateSystem</defname><defval><ref ....>cs::cartesian</ref></defval></param> // with enum: <type><ref refid="group__enum_1ga7d33eca9a5389952bdf719972eb802b6" kindref="member">closure_selector</ref></type> if (node != NULL) { std::string name = node->name(); if (name == "type") { get_contents(node->first_node(), p.fulltype); p.type = p.fulltype; boost::replace_all(p.type, " const", ""); boost::trim(p.type); boost::replace_all(p.type, "&", ""); boost::replace_all(p.type, "*", ""); boost::trim(p.type); } else if (name == "declname") p.name = node->value(); else if (name == "parametername") p.name = node->value(); else if (name == "defname") p.name = node->value(); else if (name == "defval") { parse_para(node, p.default_value, p.skip); } else if (name == "para") { parse_para(node, p.brief_description, p.skip); } parse_parameter(node->first_node(), p); parse_parameter(node->next_sibling(), p); } }
file_contents start_compile(char *file) { build current; current.declarations = hm_new_eqfunc(&slice_eq_voidptr); current.composite = get_contents(file); continue_build(¤t, file, current.composite); return current.composite; }
/** * Fetches the contents of a block. * Returns true on success and false on failure. */ bool fetch_block(char* output, size_t block_number, size_t startpos, size_t length) { auto& bc = block_cache::get_instance(); std::string key = get_key_name(block_number); int64_t ret = bc.read(key, output, startpos, startpos + length); if (ret == length) return true; logstream(LOG_INFO) << "Fetching " << sanitize_url(m_filename) << " Block " << block_number << std::endl; // ok. failure... no such block or block is bad. We read it ourselves. // read the whole block auto block_start = block_number * READ_CACHING_BLOCK_SIZE; auto block_end = std::min(block_start + READ_CACHING_BLOCK_SIZE, m_file_size); // seek to the block and read the whole block at once auto& contents = get_contents(); contents->seek(block_start, std::ios_base::beg, std::ios_base::in); std::string block_contents(block_end - block_start, 0); auto bytes_read = contents->read(&(block_contents[0]), block_end - block_start); // read failed. if (bytes_read < block_end - block_start) return false; // write the block bool write_block_ok = bc.write(key, block_contents); if (write_block_ok == false) { logstream(LOG_ERROR) << "Unable to write block " << key << std::endl; // still ok. we can continue. but too many of these are bad. } // since we just read the block, lets fill the output const char* src = block_contents.c_str(); memcpy(output, src + startpos, length); return true; }
static int db_truncate(backend_store_interface* i, const char* rel_path, off_t newsize) { int j; logger_logf(LOG(i), "rel_path = %s", rel_path); ERR_IF_DOESNT_EXIST(i, rel_path); ERR_IF_CANT_WRITE_TO_FILE(i, rel_path); char* contents = get_contents(rel_path); size_t current_size = get_size(rel_path); char buf[newsize]; for (j = 0; j < newsize; ++j) { if (j < current_size) { buf[j] = contents[j]; } else { buf[j] = '\0'; } } update_contents(rel_path, buf, newsize); update_size(rel_path, newsize); free(contents); return 0; }
static int db_write(backend_store_interface* i, const char* rel_path, const char *buf, size_t size, off_t offset, uint64_t* file_handle) { int j; logger_logf(LOG(i), "rel_path = %s", rel_path); ERR_IF_DOESNT_EXIST(i, rel_path); ERR_IF_CANT_WRITE_TO_FILE(i, rel_path); char* contents = get_contents(rel_path); size_t old_size = get_size(rel_path); size_t new_size = (offset + size > old_size ? offset + size : old_size); logger_logf(LOG(i), "old_size = %ld, new_size = %ld, offset = %ld", old_size, new_size, offset); char new_contents[new_size]; for (j = 0; j < offset; ++j) { new_contents[j] = contents[j]; } for (j = offset; j < offset + size; ++j) { new_contents[j] = buf[j - offset]; } for (j = offset; j < old_size; ++j) { new_contents[offset + size + offset - j] = buf[j]; } update_contents(rel_path, new_contents, new_size); update_size(rel_path, new_size); free(contents); return size; }
JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef* exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { JSStringRef path_str_ref = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str_ref) < 100); char tmp[100]; tmp[0] = '\0'; JSStringGetUTF8CString(path_str_ref, tmp, 100); JSStringRelease(path_str_ref); char *path = tmp; if (str_has_prefix(path, "goog/../") == 0) { path = path + 8; } char *source = NULL; if (out_path == NULL) { source = bundle_get_contents(path); } else { char *full_path = str_concat(out_path, path); source = get_contents(full_path, NULL); free(full_path); } if (source != NULL) { evaluate_script(ctx, source, path); free(source); } } return JSValueMakeUndefined(ctx); }
/* prints the contents of a cell. */ void print_cell(CELL_CONTENTS board[][BOARD_WIDTH], int row, int col) { switch (get_contents(board, row, col)) { case PEG : printf(COLOR_LINES); putchar('|'); putchar(' '); printf(COLOR_PEG); putchar('o'); putchar(' '); printf(COLOR_RESET); break; case HOLE : printf(COLOR_LINES); putchar('|'); putchar(' '); printf(COLOR_HOLE); putchar('.'); putchar(' '); printf(COLOR_RESET); break; case INVALID : print_spaces(board, row, col, CELL_LINE); break; } }
JSValueRef function_read_file(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { // TODO: implement fully if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char path[100]; JSStringRef path_str = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str) < 100); JSStringGetUTF8CString(path_str, path, 100); JSStringRelease(path_str); // debug_print_value("read_file", ctx, args[0]); time_t last_modified = 0; char *contents = get_contents(path, &last_modified); if (contents != NULL) { JSStringRef contents_str = JSStringCreateWithUTF8CString(contents); free(contents); JSValueRef res[2]; res[0] = JSValueMakeString(ctx, contents_str); res[1] = JSValueMakeNumber(ctx, last_modified); return JSObjectMakeArray(ctx, 2, res, NULL); } } return JSValueMakeNull(ctx); }
static void parse_function(rapidxml::xml_node<>* node, configuration const& config, std::string const& parent, function& f) { if (node != NULL) { std::string name = node->name(); std::string full = parent + "." + name; if (full == ".name") f.name = node->value(); else if (full == ".argsstring") f.argsstring = node->value(); else if (full == ".definition") { f.definition = node->value(); if (! config.skip_namespace.empty()) { boost::replace_all(f.definition, config.skip_namespace, ""); } } else if (full == ".param") { parameter p; parse_parameter(node->first_node(), p); add_or_set(f.parameters, p); } else if (full == ".type") { get_contents(node->first_node(), f.return_type); } else if (full == ".detaileddescription.para.simplesect") { std::string kind = get_attribute(node, "kind"); if (kind == "return") { get_contents(node->first_node(), f.return_description); } /*else if (kind == "param") { get_contents(node->first_node(), f.paragraphs); }*/ } else if (full == ".detaileddescription.para.image") { } parse_function(node->first_node(), config, full, f); parse_function(node->next_sibling(), config, parent, f); } }
bool attachment::is_binary() { const unsigned char* contents = (unsigned char*)get_contents(); for (uint i=0; i<m_size; i++) { if (contents[i]!='\t' && contents[i]!='\r' && contents[i]!='\n' && (contents[i]<0x20 || contents[i]>=0x7f)) return true; } return false; }
inline void get_contents(rapidxml::xml_node<>* node, std::string& contents) { if (node != NULL) { if (node->type() == rapidxml::node_element) { //std::cout << "ELEMENT: " << node->name() << "=" << node->value() << std::endl; } else if (node->type() == rapidxml::node_data) { contents += node->value(); //std::cout << "DATA: " << node->name() << "=" << node->value() << std::endl; } else { //std::cout << "OTHER: " << node->name() << "=" << node->value() << std::endl; } get_contents(node->first_node(), contents); get_contents(node->next_sibling(), contents); } }
/* * prints the border line. we either need to print spaces or lines. * we then need to print an extra character on the end of each row to complete * the line. */ void print_boarder_line (CELL_CONTENTS board[][BOARD_WIDTH], int row, int col) { if ((get_contents(board, row, col) == INVALID) && get_contents(board, row - 1, col) == INVALID) { print_spaces(board, row, col, BORDER_LINE); } else { printf(COLOR_LINES); printf("+---"); printf(COLOR_RESET); } if (col == BOARD_DISPLAY_WIDTH - 1) { if ((get_contents(board, row, col) == INVALID) && (get_contents(board, row - 1, col) == INVALID)) { putchar(' '); } else { printf(COLOR_LINES); putchar('+'); printf(COLOR_RESET); } } }
ErrorCode ReadDamsel::get_contents(damsel_model m, damsel_container c, EntityHandle *ents) { EntityHandle eh; int ind = 0; if (DMSLcontainer_get_type(c) == DAMSEL_HANDLE_CONTAINER_TYPE_SEQUENCE) { damsel_handle start; size_t count, stride; damsel_err_t err = DMSLcontainer_sequence_get_contents(m, c, &start, &count, &stride); CHK_DMSL_ERR(err, " "); if (stride == 1) { while (count) { // get start in rangemap RangeMap<damsel_handle, EntityHandle, 0>::iterator beg = dmHandleRMap.lower_bound(start); if (beg == dmHandleRMap.end()) return MB_SUCCESS; unsigned int diff = std::max((*beg).begin-start, (damsel_handle)0); unsigned int num = std::min(count-diff, (size_t)(*beg).count); for (EntityHandle hdl = (*beg).begin+diff; hdl <= (int)(*beg).begin+diff+num-1; hdl++) ents[ind++] = hdl; count -= (diff + num); beg++; } } else { for (int i = count-1; i >= 0; i--) { if (dmHandleRMap.find(start+i, eh)) ents[i] = eh; } } } else if (DMSLcontainer_get_type(c) == DAMSEL_HANDLE_CONTAINER_TYPE_VECTOR) { damsel_handle_ptr handle_ptr; size_t count; damsel_err_t err = DMSLcontainer_vector_get_contents(m, c, &handle_ptr, &count); CHK_DMSL_ERR(err, "Failed to get vector contents."); for (int i = count-1; i >= 0; i--) { if (dmHandleRMap.find(handle_ptr[i], eh)) ents[i] = eh; } } else if (DMSLcontainer_get_type(c) == DAMSEL_HANDLE_CONTAINER_TYPE_TREE) { damsel_handle_ptr node_ptr = NULL; damsel_container cont = NULL; while (DMSLcontainer_tree_get_contents(m, c, &node_ptr, &cont) == DMSL_OK && cont) { ErrorCode rval = get_contents(m, cont, ents); if (MB_SUCCESS != rval) return rval; unsigned int num = DMSLcontainer_count(cont); ents += num; } } return MB_SUCCESS; }
/* * prints spaces. this is either for a border line or a cell line. we either * need to print all spaces or subsitute the first space for the closing * character of the previous cell. */ void print_spaces(CELL_CONTENTS board[][BOARD_WIDTH], int row, int col, int line) { if (line == BORDER_LINE) { if ((get_contents(board, row - 1, col - 1) != INVALID) || (get_contents(board, row, col - 1) != INVALID)) { printf(COLOR_LINES); printf("+ "); printf(COLOR_RESET); } else { printf(" "); } } else { if (get_contents(board, row, col -1) == INVALID) { printf(" "); } else { printf(COLOR_LINES); printf("| "); printf(COLOR_RESET); } } }
/* * get_string_from_file() * @path: file to read contents of. * * Returns: dynamically-allocated copy of the contents of @path, * or NULL on error. */ static char *get_string_from_file(const char *path) { char *str; ssize_t len; str = get_contents(path); if (!str) return NULL; len = strlen(str) - 1; if ((len >= 0) && (str[len] == '\n')) str[len] = '\0'; return str; }
static int db_read(backend_store_interface* i, const char* rel_path, char *buf, size_t size, off_t offset, uint64_t* file_handle) { size_t j; logger_logf(LOG(i), "rel_path = %s", rel_path); ERR_IF_DOESNT_EXIST(i, rel_path); ERR_IF_CANT_READ(i, rel_path); char* contents = get_contents(rel_path); for (j = 0; j < size; ++j) { buf[j] = contents[j + offset]; } free(contents); return size; }
/** * Seeks to a different location. */ std::streampos seek(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode openmode) { if (openmode == std::ios_base::in) { if (way == std::ios_base::beg) { m_file_pos = std::min<std::streamoff>(off, m_file_size); } else if (way == std::ios_base::cur) { m_file_pos = std::min<std::streamoff>(m_file_pos + off, m_file_size); m_file_pos = std::max<std::streamoff>(m_file_pos, 0); } else if (way == std::ios_base::end) { m_file_pos = std::min<std::streamoff>(m_file_size + off - 1, m_file_size); m_file_pos = std::max<std::streamoff>(m_file_pos, 0); } return m_file_pos; } else { get_contents()->seek(off, way, openmode); } }
JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { JSStringRef path_str_ref = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str_ref) < 100); char tmp[100]; tmp[0] = '\0'; JSStringGetUTF8CString(path_str_ref, tmp, 100); JSStringRelease(path_str_ref); bool can_skip_load = false; char *path = tmp; if (str_has_prefix(path, "goog/../") == 0) { path = path + 8; } else { unsigned long h = hash((unsigned char *) path); if (is_loaded(h)) { can_skip_load = true; } else { add_loaded_hash(h); } } if (!can_skip_load) { char *source = NULL; if (config.out_path == NULL) { source = bundle_get_contents(path); } else { char *full_path = str_concat(config.out_path, path); source = get_contents(full_path, NULL); free(full_path); } if (source != NULL) { evaluate_script(ctx, source, path); display_launch_timing(path); free(source); } } } return JSValueMakeUndefined(ctx); }
int main(int argc, char *argv[]) { char *input = get_contents(stdin); pmh_style_collection *styles = pmh_parse_styles(input, &parsing_error_callback, NULL); printf("------\n"); if (styles->editor_styles != NULL) { printf("Editor styles:\n"); print_styles(styles->editor_styles); printf("\n"); } if (styles->editor_current_line_styles != NULL) { printf("Current line styles:\n"); print_styles(styles->editor_current_line_styles); printf("\n"); } if (styles->editor_selection_styles != NULL) { printf("Selection styles:\n"); print_styles(styles->editor_selection_styles); printf("\n"); } int i; for (i = 0; i < pmh_NUM_LANG_TYPES; i++) { pmh_style_attribute *cur = styles->element_styles[i]; if (cur == NULL) continue; printf("%s:\n", pmh_element_name_from_type(cur->lang_element_type)); print_styles(cur); } pmh_free_style_collection(styles); free(input); return 0; }
static void continue_build(build *current, char *filename, file_contents newest) { slice name_slice = new_slice(filename); slice *boxed_slice = new(boxed_slice); *boxed_slice = name_slice; hm_put(current->declarations, slice_hash(name_slice), boxed_slice, filename); linked_iter iterator = ll_iter_head(newest.imports); while(ll_iter_has_next(&iterator)) { import_declaration *dec = ll_iter_next(&iterator); char *filename = resolve_name(dec->name); slice filename_slice = new_slice(filename); slice *boxed_filename = new(boxed_filename); *boxed_filename = filename_slice; if(!hm_has(current->declarations, slice_hash(filename_slice), &(filename_slice))) { file_contents contents = get_contents(filename); ll_concat(current->composite.structs, contents.structs); ll_concat(current->composite.functions, contents.functions); hm_put(current->declarations, slice_hash(filename_slice), boxed_filename, filename); continue_build(current, filename, contents); } } }
static HashNode getpmmapfile(UNUSED(HashTable ht), char *name) { char *contents; Param pm = NULL; pm = (Param) hcalloc(sizeof(struct param)); pm->nam = dupstring(name); pm->flags = PM_SCALAR; pm->gsu.s = &mapfile_gsu; pm->flags |= (mapfile_pm->flags & PM_READONLY); /* Set u.str to contents of file given by name */ if ((contents = get_contents(pm->nam))) pm->u.str = contents; else { pm->u.str = ""; pm->flags |= PM_UNSET; } return (HashNode) pm; }
/* * prints the cell line. we either need to print yaxis labels, xaxis labels, * pegs, holes, or spaces. we also need to print an extra character on the end * of each row to complete the line. */ void print_cell_line (CELL_CONTENTS board[][BOARD_WIDTH], int row, int col) { if ((col == 0) && (row != BOARD_DISPLAY_HEIGHT - 1)) { printf(" %d ", row + Y_OFFSET); } else if (row == BOARD_DISPLAY_HEIGHT - 1) { if (col == 0) print_spaces(board, row, col, CELL_LINE); else printf(" %c ", (char)(col - LABEL_LEN) + X_OFFSET); } else if ((col != 0) && (row != BOARD_DISPLAY_HEIGHT - 1)) { print_cell(board, row, col); } if (col == BOARD_DISPLAY_WIDTH - 1) { if (get_contents(board, row, col) != INVALID) { printf(COLOR_LINES); putchar('|'); printf(COLOR_RESET); } else { putchar(' '); } } }
JSValueRef function_load(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { // TODO: implement fully if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char path[PATH_MAX]; JSStringRef path_str = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str) < PATH_MAX); JSStringGetUTF8CString(path_str, path, PATH_MAX); JSStringRelease(path_str); // debug_print_value("load", ctx, args[0]); time_t last_modified = 0; char *contents = NULL; char *loaded_path = strdup(path); bool developing = (config.num_src_paths == 1 && strcmp(config.src_paths[0].type, "src") == 0 && str_has_suffix(config.src_paths[0].path, "/planck-cljs/src/") == 0); if (!developing) { contents = bundle_get_contents(path); last_modified = 0; } // load from classpath if (contents == NULL) { for (int i = 0; i < config.num_src_paths; i++) { if (config.src_paths[i].blacklisted) { continue; } char *type = config.src_paths[i].type; char *location = config.src_paths[i].path; if (strcmp(type, "src") == 0) { char *full_path = str_concat(location, path); contents = get_contents(full_path, &last_modified); if (contents != NULL) { free(loaded_path); loaded_path = strdup(full_path); } free(full_path); } else if (strcmp(type, "jar") == 0) { struct stat file_stat; if (stat(location, &file_stat) == 0) { contents = get_contents_zip(location, path, &last_modified); } else { cljs_perror(location); config.src_paths[i].blacklisted = true; } } if (contents != NULL) { break; } } } // load from out/ if (contents == NULL) { if (config.out_path != NULL) { char *full_path = str_concat(config.out_path, path); contents = get_contents(full_path, &last_modified); free(full_path); } } if (developing && contents == NULL) { contents = bundle_get_contents(path); last_modified = 0; } if (contents != NULL) { JSStringRef contents_str = JSStringCreateWithUTF8CString(contents); free(contents); JSStringRef loaded_path_str = JSStringCreateWithUTF8CString(loaded_path); free(loaded_path); JSValueRef res[3]; res[0] = JSValueMakeString(ctx, contents_str); res[1] = JSValueMakeNumber(ctx, last_modified); res[2] = JSValueMakeString(ctx, loaded_path_str); return JSObjectMakeArray(ctx, 3, res, NULL); } } return JSValueMakeNull(ctx); }
ErrorCode ReadDamsel::process_ent_info(const damsel_entity_buf_type &einfo) { // create this chunk of entities EntityHandle *connect, start_handle; ErrorCode rval; damsel_err_t err; damsel_container app_cont; Range these_ents; // check that there's only one contiguous run of file-side handles, fail if there isn't #ifndef NDEBUG Range fside_handles; rval = DamselUtil::container_to_range(dU.dmslModel, const_cast<damsel_container&>(einfo.entity_container), fside_handles); if (MB_SUCCESS != rval || fside_handles.size() != einfo.count || fside_handles.psize() != 1) return MB_FAILURE; #endif if (einfo.entity_type != DAMSEL_ENTITY_TYPE_VERTEX) { // create the moab entities rval = readMeshIface->get_element_connect(einfo.count, einfo.vertices_per_entity, DamselUtil::dtom_entity_type[einfo.entity_type], 0, start_handle, connect); CHK_MB_ERR(rval, " "); these_ents.insert(start_handle, start_handle+einfo.count-1); // create an app-side sequence and map to file-side container app_cont = DMSLcontainer_create_sequence(dU.dmslModel, einfo.count, start_handle, 1); err = DMSLmodel_map_handles(app_cont, einfo.entity_container); CHK_DMSL_ERR(err, "Error returned mapping entity handles."); // map connectivity assert(DMSLcontainer_count(einfo.vertex_container) == (int)(einfo.vertices_per_entity*einfo.count)); rval = get_contents(dU.dmslModel, einfo.vertex_container, connect); CHK_MB_ERR(rval, "Error returned mapping connectivity."); } else { // get the number of coordinate arrays int num_ctags = 0; damsel_handle xcoord_dtag = DMSLselect_tag_by_name(dU.dmslModel, "mbdmsl_XCOORDS"); if (xcoord_dtag) num_ctags++; damsel_handle ycoord_dtag = DMSLselect_tag_by_name(dU.dmslModel, "mbdmsl_YCOORDS"); if (ycoord_dtag) num_ctags++; damsel_handle zcoord_dtag = DMSLselect_tag_by_name(dU.dmslModel, "mbdmsl_ZCOORDS"); if (zcoord_dtag) num_ctags++; // should have one vertex per entity assert(einfo.vertices_per_entity == 1); std::vector<double*> coord_arrays; rval = readMeshIface->get_node_coords(num_ctags, einfo.count, 0, start_handle, coord_arrays); CHK_MB_ERR(rval, " "); these_ents.insert(start_handle, start_handle+einfo.count-1); // create an app-side sequence and map to file-side container app_cont = DMSLcontainer_create_sequence(dU.dmslModel, start_handle, einfo.count, 1); err = DMSLmodel_map_handles(app_cont, einfo.entity_container); CHK_DMSL_ERR(err, "Trouble mapping entity handles."); // map the coords storage if (xcoord_dtag != 0) { err = DMSLmodel_map_tag(coord_arrays[0], app_cont, (damsel_handle_ptr)&dU.xcoordsTag.mTagh); CHK_DMSL_ERR(err, "Trouble mapping x coordinate tag."); } if (ycoord_dtag != 0) { err = DMSLmodel_map_tag(coord_arrays[1], app_cont, (damsel_handle_ptr)&dU.ycoordsTag.mTagh); CHK_DMSL_ERR(err, "Trouble mapping y coordinate tag."); } if (zcoord_dtag != 0) { err = DMSLmodel_map_tag(coord_arrays[2], app_cont, (damsel_handle_ptr)&dU.zcoordsTag.mTagh); CHK_DMSL_ERR(err, "Trouble mapping z coordinate tag."); } } // save mapping from moab entity to einfo dmHandleRMap.insert(DMSLcontainer_handle_at_position(einfo.entity_container, 0), start_handle, einfo.count); rval = process_entity_tags(einfo.tag_count, einfo.tag_handle_container, app_cont, these_ents); return rval; }
std::streamsize write(const char* strm_ptr, std::streamsize n) { return get_contents()->write(strm_ptr, n); }
bool good() const { return get_contents()->good(); }
static struct pidset get_processes(void) { struct dirent *de; struct pidset ret; DIR *d; pidset_init(&ret); d = opendir("/proc"); if (!d) { perror("/proc: open\n"); exit(1); } while ((de = readdir(d)) != NULL) { int pid; char *cmdline, *status, *exe; int cmdlinelen; const char **argv; char filename[256]; struct procdata *proc; const char *name = de->d_name; if (name[strspn(name, "0123456789")]) continue; /* * The filename is numeric, i.e. we've found a pid. Try to * retrieve all the information we want about it. * * We expect this will fail every so often for random reasons, * e.g. if the pid has disappeared between us fetching a list * of them and trying to read their command lines. In that * situation, we won't bother reporting errors: we'll just * drop this pid and silently move on to the next one. */ pid = atoi(name); assert(pid >= 0 && pid < PIDMAX); sprintf(filename, "/proc/%d/cmdline", pid); if ((cmdline = get_contents(filename, &cmdlinelen)) == NULL) continue; sprintf(filename, "/proc/%d/status", pid); if ((status = get_contents(filename, NULL)) == NULL) { free(cmdline); continue; } sprintf(filename, "/proc/%d/exe", pid); exe = get_link_dest(filename); /* This may fail, if the process isn't ours, but we continue * anyway. */ /* * Now we've got all our raw data out of /proc. Process it * into the internal representation we're going to use in the * process-selection logic. */ proc = (struct procdata *)malloc(sizeof(struct procdata)); if (!proc) { fprintf(stderr, "pid: out of memory\n"); exit(1); } proc->pid = pid; proc->exe = exe; /* * cmdline contains a list of NUL-terminated strings. Scan * them to get the argv pointers. */ { const char *p; int nargs; /* Count the arguments. */ nargs = 0; for (p = cmdline; p < cmdline + cmdlinelen; p += strlen(p)+1) nargs++; /* Allocate space for the pointers. */ argv = (const char **)malloc((nargs+1) * sizeof(char *)); proc->argv = argv; if (!argv) { fprintf(stderr, "pid: out of memory\n"); exit(1); } /* Store the pointers. */ proc->argc = 0; for (p = cmdline; p < cmdline + cmdlinelen; p += strlen(p)+1) argv[proc->argc++] = p; /* Trailing NULL to match standard argv lists, just in case. */ assert(proc->argc == nargs); argv[proc->argc] = NULL; } /* * Scan status for the uid and the parent pid. This file * contains a list of \n-terminated lines of text. */ { const char *p; int got_ppid = 0, got_uid = 0; p = status; while (p && *p) { if (!got_ppid && sscanf(p, "PPid: %d", &proc->ppid) == 1) got_ppid = 1; if (!got_uid && sscanf(p, "Uid: %*d %d", &proc->uid) == 1) got_uid = 1; /* * Advance to next line. */ p = strchr(p, '\n'); if (p) p++; } if (!got_uid || !got_ppid) { /* arrgh, abort everything so far */ free(cmdline); free(exe); free(status); free(argv); free(proc); continue; } } /* * If we get here, we've got everything we need. Add the * process to the list of things we can usefully work * with. */ procs[pid] = proc; pidset_add(&ret, pid); } closedir(d); return ret; }
int main(int argc, char *argv[]) { int i, j, k; int num_threads = 3; int num_iterations = 5; if (argc >= 3) { num_threads = atoi(argv[1]); num_iterations = atoi(argv[2]); } char **md_source_filenames = NULL; char **md_sources = NULL; int *md_source_lengths = NULL; int num_md_sources = 0; for (i=0, j=0; i < argc; i++) { if (strcmp(argv[i], "--") == 0) { num_md_sources = argc-i-1; md_sources = (char **)malloc(sizeof(char*)*num_md_sources); md_source_filenames = (char **)malloc(sizeof(char*)*num_md_sources); md_source_lengths = (int *)malloc(sizeof(int*)*num_md_sources); } else if (md_sources != NULL) { printf("read %s\n", argv[i]); md_source_filenames[j] = argv[i]; FILE *f = fopen(argv[i], "r"); md_sources[j] = get_contents(f); fclose(f); md_source_lengths[j] = strlen(md_sources[j]); j++; } } if (md_sources == NULL) { printf("usage: %s [NUM_THREADS] [NUM_ITERATIONS] -- file1 [file2...]\n", basename(argv[0])); return 0; } printf("Parsing input in %i threads, %i iterations each.\n", num_threads, num_iterations); int colors[5] = {31,32,33,35,36}; int num_colors = 5; pthread_t threads[num_threads]; for (i=0, j=0, k=0; i < num_threads; i++, j = ((j+1) % num_md_sources), k = ((k+1) % num_colors)) { char *md_source_copy = malloc(md_source_lengths[j]+1); strcpy(md_source_copy, md_sources[j]); pthread_t thread; char *name = malloc(sizeof(char)*(strlen(md_source_filenames[j])+30)); sprintf(name, "\e[%imt%i\e[0m \e[34m<%s>\e[0m", colors[k], i+1, md_source_filenames[j]); threadinfo *ti = mk_threadinfo(md_source_copy, name, num_iterations); pthread_create(&thread, NULL, thread_run, (void *)ti); threads[i] = thread; } for (i = 0; i < num_threads; i++) pthread_join(threads[i], NULL); printf("done.\n"); return 0; }