[SOLVED]Alphasort File listed

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

[SOLVED]Alphasort File listed

Post by ne0h »

I've this code to list all file in a directory and alphasort the file printed, but the file are listed in the same position as the directory, but i want list the file with this sequence:

.
..
DIR
dir
FILE.EXT
file.ext

The scandir function library:

Code: Select all

#include<dirent.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>


int scandir&#40;const char *dir, struct dirent ***namelist,
            int &#40;*select&#41;&#40;const struct dirent *&#41;,
            int &#40;*compar&#41;&#40;const struct dirent **, const struct dirent **&#41;&#41;
&#123;
  DIR *d;
  struct dirent *entry;
  register int i=0;
  size_t entrysize;

  if &#40;&#40;d=opendir&#40;dir&#41;&#41; == NULL&#41;
     return&#40;-1&#41;;

  *namelist=NULL;
  while &#40;&#40;entry=readdir&#40;d&#41;&#41; != NULL&#41;
  &#123;
    if &#40;select == NULL || &#40;select != NULL && &#40;*select&#41;&#40;entry&#41;&#41;&#41;
    &#123;
      *namelist=&#40;struct dirent **&#41;realloc&#40;&#40;void *&#41;&#40;*namelist&#41;,
                 &#40;size_t&#41;&#40;&#40;i+1&#41;*sizeof&#40;struct dirent *&#41;&#41;&#41;;
	if &#40;*namelist == NULL&#41; return&#40;-1&#41;;
	entrysize=sizeof&#40;struct dirent&#41;-sizeof&#40;entry->d_name&#41;+strlen&#40;entry->d_name&#41;+1;
	&#40;*namelist&#41;&#91;i&#93;=&#40;struct dirent *&#41;malloc&#40;entrysize&#41;;
	if &#40;&#40;*namelist&#41;&#91;i&#93; == NULL&#41; return&#40;-1&#41;;
	memcpy&#40;&#40;*namelist&#41;&#91;i&#93;, entry, entrysize&#41;;
	i++;
    &#125;
  &#125;
  if &#40;closedir&#40;d&#41;&#41; return&#40;-1&#41;;
  if &#40;i == 0&#41; return&#40;-1&#41;;
  if &#40;compar != NULL&#41;
    qsort&#40;&#40;void *&#41;&#40;*namelist&#41;, &#40;size_t&#41;i, sizeof&#40;struct dirent *&#41;, compar&#41;;
    
  return&#40;i&#41;;
&#125;

int alphasort&#40;const struct dirent **a, const struct dirent **b&#41;
&#123;
  return&#40;strcmp&#40;&#40;*a&#41;->d_name, &#40;*b&#41;->d_name&#41;&#41;;
&#125;
Has anyone a solution?
Tanks in advance...
Last edited by ne0h on Fri Mar 28, 2008 6:21 am, edited 1 time in total.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

qsort + google = win.
kuroneko
Posts: 24
Joined: Thu Dec 08, 2005 11:32 am
Location: Chigasaki, Japan

Re: Alphasort File listed

Post by kuroneko »

ne0h wrote:Has anyone a solution?
Just sorting by name isn't going to get you anywhere as readdir() does not guarantee any order for the items returned. When you sort you'll have to consider type (dir/file) first, then name.

IIRC, stat() requires an absolute file name so once you read the entry with readdir() insert some function which obtains the type then pass this and the actual name to the comparator.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

Well thanks for putting me onto qsort. I never knew about it.
I used this to sort an array of strings (file paths) for my mp3 browser:
http://www.c.happycodings.com/Sorting_S ... ode16.html
(already had the strings in memory of course).
Doesn't help with the thread starters' problem sorry.
Art.
If not actually, then potentially.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

Funny one of my first botched attempts treats the path strings as integers or something,
and is a good way to seemingly randomise an mp3 playlist without copying into a duplicate
buffer or anything, then you can just alpha sort them properly again.
What a bonus :) But it's the same every time,
so I think I'll go for randomising the selection numbers and go from there.
If not actually, then potentially.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

I knew I wasn't dealing with many files, so I just did my own simple bubble sort in Doom's file requester. It sorts directories first, and everything alphabetically. Sorting is one of those things you get like the first week of any programming class. It's not a big deal. :)
Post Reply