Skip to content
Snippets Groups Projects
Commit c96bf163 authored by yorhel's avatar yorhel
Browse files

Changed struct dir from a doubly to a singly linked list (less memory usage, faster, and easier)

git-svn-id: svn://blicky.net/ncdu/trunk@13 ce56bc8d-f834-0410-b703-f827bd498a76
parent eb10ca8a
No related branches found
No related tags found
No related merge requests found
...@@ -26,13 +26,8 @@ ...@@ -26,13 +26,8 @@
#include "ncdu.h" #include "ncdu.h"
struct dir *bcur; struct dir *bcur;
int helpwin;
struct dir * removedir(struct dir *dr) {
return(dr);
}
int cmp(struct dir *x, struct dir *y) { int cmp(struct dir *x, struct dir *y) {
struct dir *a, *b; struct dir *a, *b;
int r = 0; int r = 0;
...@@ -57,7 +52,7 @@ int cmp(struct dir *x, struct dir *y) { ...@@ -57,7 +52,7 @@ int cmp(struct dir *x, struct dir *y) {
if(r == 0) if(r == 0)
r = a->size > b->size ? 1 : (a->size == b->size ? 0 : -1); r = a->size > b->size ? 1 : (a->size == b->size ? 0 : -1);
if(r == 0) if(r == 0)
r = strcmp(a->name, b->name); r = strcmp(x->name, y->name);
return(r); return(r);
} }
...@@ -68,8 +63,6 @@ struct dir *sortFiles(struct dir *list) { ...@@ -68,8 +63,6 @@ struct dir *sortFiles(struct dir *list) {
struct dir *p, *q, *e, *tail; struct dir *p, *q, *e, *tail;
int insize, nmerges, psize, qsize, i; int insize, nmerges, psize, qsize, i;
while(list->prev != NULL)
list = list->prev;
insize = 1; insize = 1;
while(1) { while(1) {
p = list; p = list;
...@@ -98,7 +91,6 @@ struct dir *sortFiles(struct dir *list) { ...@@ -98,7 +91,6 @@ struct dir *sortFiles(struct dir *list) {
} }
if(tail) tail->next = e; if(tail) tail->next = e;
else list = e; else list = e;
e->prev = tail;
tail = e; tail = e;
} }
p = q; p = q;
...@@ -135,8 +127,8 @@ void drawBrowser(int change) { ...@@ -135,8 +127,8 @@ void drawBrowser(int change) {
erase(); erase();
/* exit if there are no items to display */ /* exit if there are no items to display */
if(bcur->parent == NULL) { if(bcur == NULL || bcur->parent == NULL) {
if(bcur->sub == NULL) { if(bcur == NULL || bcur->sub == NULL) {
erase(); erase();
refresh(); refresh();
endwin(); endwin();
...@@ -159,10 +151,9 @@ void drawBrowser(int change) { ...@@ -159,10 +151,9 @@ void drawBrowser(int change) {
mvhline(1, 0, '-', wincols); mvhline(1, 0, '-', wincols);
mvaddstr(1, 3, cropdir(getpath(bcur, tmp), wincols-5)); mvaddstr(1, 3, cropdir(getpath(bcur, tmp), wincols-5));
/* make sure we have the first item, and the items are in correct order */ /* make sure the items are in correct order */
bcur = sortFiles(bcur); bcur = sortFiles(bcur);
while(bcur->prev != NULL) bcur->parent->sub = bcur;
bcur = bcur->prev;
/* get maximum size and selected item */ /* get maximum size and selected item */
for(n = bcur, selected = i = 0; n != NULL; n = n->next, i++) { for(n = bcur, selected = i = 0; n != NULL; n = n->next, i++) {
...@@ -346,13 +337,13 @@ void showBrowser(void) { ...@@ -346,13 +337,13 @@ void showBrowser(void) {
case KEY_RIGHT: case KEY_RIGHT:
n = selected(); n = selected();
if(n->flags & FF_PAR) if(n->flags & FF_PAR)
bcur = bcur->parent; bcur = bcur->parent->parent->sub;
else if(n->sub != NULL) else if(n->sub != NULL)
bcur = n->sub; bcur = n->sub;
break; break;
case KEY_LEFT: case KEY_LEFT:
if(bcur->parent->parent != NULL) { if(bcur->parent->parent != NULL) {
bcur = bcur->parent; bcur = bcur->parent->parent->sub;
} }
break; break;
...@@ -360,17 +351,14 @@ void showBrowser(void) { ...@@ -360,17 +351,14 @@ void showBrowser(void) {
case 'r': case 'r':
if((n = showCalc(getpath(bcur, tmp))) != NULL) { if((n = showCalc(getpath(bcur, tmp))) != NULL) {
/* free current items */ /* free current items */
t = bcur; d = bcur;
bcur = bcur->parent; bcur = bcur->parent;
while(t->prev != NULL)
t = t->prev;
d = t;
while(d != NULL) { while(d != NULL) {
t = d; t = d;
d = t->next; d = t->next;
freedir(t); freedir(t);
} }
/* update parent dir */ /* update parent dir */
bcur->sub = n->sub; bcur->sub = n->sub;
bcur->files = n->files; bcur->files = n->files;
...@@ -394,7 +382,6 @@ void showBrowser(void) { ...@@ -394,7 +382,6 @@ void showBrowser(void) {
strcpy(t->name, ".."); strcpy(t->name, "..");
t->parent = bcur; t->parent = bcur;
t->next = bcur->sub; t->next = bcur->sub;
t->next->prev = t;
bcur->sub = t; bcur->sub = t;
} }
...@@ -418,6 +405,8 @@ void showBrowser(void) { ...@@ -418,6 +405,8 @@ void showBrowser(void) {
n = selected(); n = selected();
if(!(n->flags & FF_PAR)) if(!(n->flags & FF_PAR))
bcur = showDelete(n); bcur = showDelete(n);
if(bcur && bcur->parent)
bcur = bcur->parent->sub;
break; break;
case 'q': case 'q':
goto endloop; goto endloop;
......
...@@ -229,7 +229,7 @@ int updateProgress(char *path) { ...@@ -229,7 +229,7 @@ int updateProgress(char *path) {
/* recursive */ /* recursive */
int calcDir(struct dir *dest, char *path) { int calcDir(struct dir *dest, char *path) {
struct dir *d, *t; struct dir *d, *t, *last;
struct stat fs; struct stat fs;
DIR *dir; DIR *dir;
struct dirent *dr; struct dirent *dr;
...@@ -257,6 +257,7 @@ int calcDir(struct dir *dest, char *path) { ...@@ -257,6 +257,7 @@ int calcDir(struct dir *dest, char *path) {
} }
/* read directory */ /* read directory */
last = NULL;
while((dr = readdir(dir)) != NULL) { while((dr = readdir(dir)) != NULL) {
f = dr->d_name; f = dr->d_name;
if(f[0] == '.' && (f[1] == '\0' || (f[1] == '.' && f[2] == '\0'))) if(f[0] == '.' && (f[1] == '\0' || (f[1] == '.' && f[2] == '\0')))
...@@ -272,11 +273,11 @@ int calcDir(struct dir *dest, char *path) { ...@@ -272,11 +273,11 @@ int calcDir(struct dir *dest, char *path) {
/* allocate dir and fix references */ /* allocate dir and fix references */
d = calloc(sizeof(struct dir), 1); d = calloc(sizeof(struct dir), 1);
d->parent = dest; d->parent = dest;
if(dest->sub != NULL) { if(dest->sub == NULL)
d->prev = dest->sub; dest->sub = d;
d->prev->next = d; if(last != NULL)
} last->next = d;
d->parent->sub = d; last = d;
/* set d->name */ /* set d->name */
d->name = malloc(strlen(f)+1); d->name = malloc(strlen(f)+1);
...@@ -339,10 +340,6 @@ int calcDir(struct dir *dest, char *path) { ...@@ -339,10 +340,6 @@ int calcDir(struct dir *dest, char *path) {
} }
if(dest->sub) { if(dest->sub) {
/* update dest->sub to point to the first item */
while(dest->sub->prev)
dest->sub = dest->sub->prev;
/* add reference to parent dir */ /* add reference to parent dir */
d = calloc(sizeof(struct dir), 1); d = calloc(sizeof(struct dir), 1);
d->flags |= FF_PAR; d->flags |= FF_PAR;
...@@ -350,7 +347,6 @@ int calcDir(struct dir *dest, char *path) { ...@@ -350,7 +347,6 @@ int calcDir(struct dir *dest, char *path) {
strcpy(d->name, ".."); strcpy(d->name, "..");
d->next = dest->sub; d->next = dest->sub;
d->parent = dest; d->parent = dest;
d->next->prev = d;
dest->sub = d; dest->sub = d;
/* calculate subdirectories */ /* calculate subdirectories */
...@@ -403,7 +399,6 @@ struct dir *showCalc(char *path) { ...@@ -403,7 +399,6 @@ struct dir *showCalc(char *path) {
/* remove reference to parent dir if we are in the parent dir */ /* remove reference to parent dir if we are in the parent dir */
t = parent->sub; t = parent->sub;
parent->sub = t->next; parent->sub = t->next;
parent->sub->prev = NULL;
free(t->name); free(t->name);
free(t); free(t);
......
...@@ -117,7 +117,8 @@ struct dir *deleteDir(struct dir *dr) { ...@@ -117,7 +117,8 @@ struct dir *deleteDir(struct dir *dr) {
struct timeval tv; struct timeval tv;
getpath(dr, file); getpath(dr, file);
strcat(file, "/"); if(file[strlen(file)-1] != '/')
strcat(file, "/");
strcat(file, dr->name); strcat(file, dr->name);
/* check for input or screen resizes */ /* check for input or screen resizes */
...@@ -145,15 +146,11 @@ struct dir *deleteDir(struct dir *dr) { ...@@ -145,15 +146,11 @@ struct dir *deleteDir(struct dir *dr) {
if(dr->flags & FF_DIR) { if(dr->flags & FF_DIR) {
if(dr->sub != NULL) { if(dr->sub != NULL) {
nxt = dr->sub; nxt = dr->sub;
while(nxt->prev != NULL)
nxt = nxt->prev;
while(nxt != NULL) { while(nxt != NULL) {
cur = nxt; cur = nxt;
nxt = cur->next; nxt = cur->next;
if(cur->flags & FF_PAR) { if(cur->flags & FF_PAR)
freedir(cur);
continue; continue;
}
if(deleteDir(cur) == NULL) if(deleteDir(cur) == NULL)
return(NULL); return(NULL);
} }
......
...@@ -110,7 +110,7 @@ ...@@ -110,7 +110,7 @@
* S T R U C T U R E S * S T R U C T U R E S
*/ */
struct dir { struct dir {
struct dir *parent, *next, *prev, *sub; struct dir *parent, *next, *sub;
char *name; char *name;
off_t size; off_t size;
unsigned int files, dirs; unsigned int files, dirs;
......
...@@ -98,8 +98,6 @@ void ncresize(void) { ...@@ -98,8 +98,6 @@ void ncresize(void) {
void freedir_rec(struct dir *dr) { void freedir_rec(struct dir *dr) {
struct dir *tmp, *tmp2; struct dir *tmp, *tmp2;
tmp2 = dr; tmp2 = dr;
while(tmp2->prev != NULL)
tmp2 = tmp2->prev;
while((tmp = tmp2) != NULL) { while((tmp = tmp2) != NULL) {
if(tmp->sub) freedir_rec(tmp->sub); if(tmp->sub) freedir_rec(tmp->sub);
free(tmp->name); free(tmp->name);
...@@ -127,21 +125,22 @@ struct dir *freedir(struct dir *dr) { ...@@ -127,21 +125,22 @@ struct dir *freedir(struct dir *dr) {
/* update references */ /* update references */
cur = NULL; cur = NULL;
if(dr->next != NULL) { dr->next->prev = dr->prev; cur = dr->next; } if(dr->parent) {
if(dr->prev != NULL) { dr->prev->next = dr->next; cur = dr->prev; } /* item is at the top of the dir, refer to next item */
if(cur != NULL) if(dr->parent->sub == dr) {
cur->flags |= FF_BSEL;
if(dr->parent && dr->parent->sub == dr) {
if(dr->prev != NULL)
dr->parent->sub = dr->prev;
else if(dr->next != NULL)
dr->parent->sub = dr->next; dr->parent->sub = dr->next;
else { cur = dr->next;
dr->parent->sub = NULL;
cur = dr->parent;
} }
/* else, get the previous item and update it's "next"-reference */
else
for(tmp = dr->parent->sub; tmp != NULL; tmp = tmp->next)
if(tmp->next == dr) {
tmp->next = dr->next;
cur = tmp;
}
} }
if(cur != NULL)
cur->flags |= FF_BSEL;
free(dr->name); free(dr->name);
free(dr); free(dr);
...@@ -150,18 +149,18 @@ struct dir *freedir(struct dir *dr) { ...@@ -150,18 +149,18 @@ struct dir *freedir(struct dir *dr) {
} }
char *getpath(struct dir *cur, char *to) { char *getpath(struct dir *cur, char *to) {
struct dir *d; struct dir *d, *list[100];
d = cur; int c = 0;
while(d->parent != NULL) {
d->parent->sub = d; for(d = cur; (d = d->parent) != NULL; )
d = d->parent; list[c++] = d;
}
to[0] = '\0'; to[0] = '\0';
while(d->parent != cur->parent) { while(c--) {
if(d->parent != NULL && d->parent->name[strlen(d->parent->name)-1] != '/') if(list[c]->parent && list[c]->name[strlen(list[c]->name)-1] != '/')
strcat(to, "/"); strcat(to, "/");
strcat(to, d->name); strcat(to, list[c]->name);
d = d->sub;
} }
return to; return to;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment