void ptree::for_each_loop(std::string& path, const function<void(fstring, obj&)>& op) { op(path, *this); for (size_t i = 0; i != children.end_i(); ++i) { if (children.is_deleted(i)) continue; fstring name = children.key(i); path.append("/"); path.append(name.p, name.n); if (ptree* x = dynamic_cast<ptree*>(children.val(i).get())) x->for_each_loop(path, op); else if (obj_array* x = dynamic_cast<obj_array*>(children.val(i).get())) array_for_each(path, x, op); else op(path, *children.val(i)); path.resize(path.size() - children.key(i).n); } }
/* figure out what dirs we want to process for cleaning and display results. */ static int qpkg_clean(char *dirp) { int i, count; size_t disp_units = 0; uint64_t num_all_bytes; struct dirent **dnames; set *vdb; if (chdir(dirp) != 0) return 1; if ((count = scandir(".", &dnames, filter_hidden, alphasort)) < 0) return 1; vdb = get_vdb_atoms(portroot, portvdb, 1); if (eclean) { size_t n; const char *overlay; array_for_each(overlays, n, overlay) cache_foreach_pkg(portroot, overlay, qpkg_cb, vdb, NULL); } num_all_bytes = qpkg_clean_dir(dirp, vdb); for (i = 0; i < count; i++) { char buf[_Q_PATH_MAX * 2]; snprintf(buf, sizeof(buf), "%s/%s", dirp, dnames[i]->d_name); num_all_bytes += qpkg_clean_dir(buf, vdb); } scandir_free(dnames, count); free_set(vdb); disp_units = KILOBYTE; if ((num_all_bytes / KILOBYTE) > 1000) disp_units = MEGABYTE; qprintf(" %s*%s Total space that would be freed in packages " "directory: %s%s %c%s\n", GREEN, NORM, RED, make_human_readable_str(num_all_bytes, 1, disp_units), disp_units == MEGABYTE ? 'M' : 'K', NORM); return 0; }
int qglsa_main(int argc, char **argv) { int i; char *fixed_list; qglsa_action action = GLSA_FUNKYTOWN; bool all_glsas = false; while ((i = GETOPT_LONG(QGLSA, qglsa, "")) != -1) { #define set_action(a) { if (action == 0) action = a; else err("cannot specify more than one action at a time"); } switch (i) { case 'l': set_action(GLSA_LIST); break; case 'd': set_action(GLSA_DUMP); break; case 't': set_action(GLSA_TEST); break; case 'p': pretend = 1; break; case 'f': set_action(GLSA_FIX); break; case 'i': set_action(GLSA_INJECT); break; COMMON_GETOPTS_CASES(qglsa) } } if (action == GLSA_FUNKYTOWN) qglsa_usage(EXIT_FAILURE); if (action != GLSA_LIST && optind == argc) err("specified action requires a list, either 'all', 'new', or GLSA numbers"); for (i = optind; i < argc; ++i) { if (!strcmp(argv[i], "all")) { all_glsas = true; if (optind+1 != argc) err("You may only use class names by themselves"); } else if (!strcmp(argv[i], "new")) { all_glsas = false; if (optind+1 != argc) err("You may only use class names by themselves"); } } fixed_list = qglsa_load_list(); int ret = 0; size_t n; const char *overlay; array_for_each(overlays, n, overlay) ret |= qglsa_run_action(overlay, action, fixed_list, all_glsas, optind, argc, argv); return ret; }
void test_array(void) { int i; double* d; void* A = array_create(0); fprintf(stdout, "call function : %s\n", __func__); array_object_show(A); fprintf(stdout, "\ntest function - array_push_back ===>\n"); { srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { NEW0(d); *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123; fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d); array_push_back(A, d); } array_object_show(A); array_for_each(A, array_element_display, NULL); array_for_each(A, array_element_destroy, NULL); array_object_show(A); array_clear(A); } array_object_show(A); fprintf(stdout, "\ntest function - array_pop_back ===>\n"); { srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { NEW0(d); *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123; fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d); array_push_back(A, d); } array_object_show(A); array_for_each(A, array_element_display, NULL); d = (double*)array_pop_back(A); fprintf(stdout, " the pop element {object=>0x%p,value=>%.3f}\n", d, *d); FREE(d); array_object_show(A); array_for_each(A, array_element_display, NULL); array_for_each(A, array_element_destroy, NULL); array_object_show(A); array_clear(A); } array_object_show(A); fprintf(stdout, "\ntest function - array_erase ===>\n"); { srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { NEW0(d); *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123; fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d); array_push_back(A, d); } array_object_show(A); array_for_each(A, array_element_display, NULL); d = (double*)array_erase(A, array_begin(A)); fprintf(stdout, " the erased begin element {object=>0x%p,value=>%.3f}\n", d, *d); FREE(d); array_object_show(A); array_for_each(A, array_element_display, NULL); d = (double*)array_erase(A, array_end(A)); fprintf(stdout, " the erased end element {object=>0x%p,value=>%.3f}\n", d, *d); FREE(d); array_object_show(A); array_for_each(A, array_element_display, NULL); array_for_each(A, array_element_destroy, NULL); array_object_show(A); array_clear(A); } array_object_show(A); fprintf(stdout, "\ntest function - array_remove ===>\n"); { srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { NEW0(d); *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123; fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d); array_push_back(A, d); } array_object_show(A); array_for_each(A, array_element_display, NULL); d = (double*)array_remove(A, 3); fprintf(stdout, " the removed [3] -> {object=>0x%p,value=>%.3f}\n", d, *d); FREE(d); array_object_show(A); array_for_each(A, array_element_display, NULL); array_for_each(A, array_element_destroy, NULL); array_object_show(A); array_clear(A); } array_object_show(A); fprintf(stdout, "\ntest function - array_copy ===>\n"); { void* copy_A; srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { NEW0(d); *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123; fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d); array_push_back(A, d); } array_object_show(A); array_for_each(A, array_element_display, NULL); copy_A = array_copy(A, 10); array_object_show(copy_A); array_for_each(copy_A, array_element_display, NULL); array_release(©_A); array_for_each(A, array_element_destroy, NULL); array_clear(A); } array_object_show(A); fprintf(stdout, "\ntest function - array_resize ===>\n"); { srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { NEW0(d); *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123; fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d); array_push_back(A, d); } array_object_show(A); array_for_each(A, array_element_display, NULL); array_resize(A, 20); array_object_show(A); array_for_each(A, array_element_display, NULL); array_for_each(A, array_element_destroy, NULL); array_clear(A); } array_object_show(A); fprintf(stdout, "\ntest function - array_release ===>\n"); array_release(&A); array_object_show(A); }