/* Recalculates virtual lines of a view with line wrapping. */ static void calc_vlines_wrapped(void) { const char *p; char *q; int i; const int nlines = count_lines(text, INT_MAX); data = reallocarray(NULL, nlines, sizeof(*data)); nvlines = 0; p = text; q = text - 1; for(i = 0; i < nlines; ++i) { char saved_char; q = until_first(q + 1, '\n'); saved_char = *q; *q = '\0'; data[i][0] = nvlines++; data[i][1] = utf8_strsw_with_tabs(p, cfg.tab_stop); data[i][2] = p - text; nvlines += data[i][1]/viewport_width; *q = saved_char; p = q + 1; } }
static node_t * find_node(node_t *root, const char *name, int create, node_t **last) { const char *end; size_t name_len; node_t *prev = NULL, *curr; node_t *new_node; name = skip_char(name, '/'); if(*name == '\0') return root; end = until_first(name, '/'); name_len = end - name; curr = root->child; while(curr != NULL) { int comp = strnoscmp(name, curr->name, name_len); if(comp == 0 && curr->name_len == name_len) { if(curr->valid && last != NULL) *last = curr; return find_node(curr, end, create, last); } else if(comp < 0) { break; } prev = curr; curr = curr->next; } if(!create) return NULL; new_node = malloc(sizeof(*new_node)); if(new_node == NULL) return NULL; if((new_node->name = malloc(name_len + 1)) == NULL) { free(new_node); return NULL; } strncpy(new_node->name, name, name_len); new_node->name[name_len] = '\0'; new_node->name_len = name_len; new_node->valid = 0; new_node->child = NULL; new_node->next = curr; if(root->child == curr) root->child = new_node; else prev->next = new_node; return find_node(new_node, end, create, last); }
int check_directory_for_color_scheme(int left, const char dir[]) { char *p; char t; int altered; union { char *name; tree_val_t buf; } u; if(dirs == NULL_TREE) { return 0; } curr_stats.cs = left ? &lwin.cs : &rwin.cs; assign_color_scheme(curr_stats.cs, &cfg.cs); /* TODO: maybe use split_and_get() here as in io/iop:iop_mkdir(). */ p = (char *)dir; altered = 0; do { t = *p; *p = '\0'; if(tree_get_data(dirs, dir, &u.buf) == 0 && color_scheme_exists(u.name)) { (void)source_cs(u.name); altered = 1; } *p = t; p = until_first(p + 1, '/'); } while(t != '\0'); curr_stats.cs = &cfg.cs; if(!altered) { return 0; } check_color_scheme(curr_stats.cs); load_color_pairs(curr_stats.cs); return 1; }
int set_trash_dir(const char new_specs[]) { char **dirs = NULL; int ndirs = 0; int error; char *free_this; char *spec; error = 0; free_this = strdup(new_specs); spec = free_this; for(;;) { char *const p = until_first(spec, ','); const int last_element = *p == '\0'; *p = '\0'; if(!validate_spec(spec)) { error = 1; break; } ndirs = add_to_string_array(&dirs, ndirs, 1, spec); if(last_element) { break; } spec = p + 1; } free(free_this); if(!error) { free_string_array(specs, nspecs); specs = dirs; nspecs = ndirs; copy_str(cfg.trash_dir, sizeof(cfg.trash_dir), new_specs); } else { free_string_array(dirs, ndirs); } return error; }
/* Extracts last part of the path into buf. Assumes that size of the buf is * large enough. */ static void extract_last_path_component(const char path[], char buf[]) { const char *const last = get_last_path_component(path); snprintf(buf, until_first(last, '/') - last + 1, "%s", last); }