forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[SOLVED]Alphasort File listed

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Thu Mar 27, 2008 6:42 am    Post subject: [SOLVED]Alphasort File listed Reply with quote

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:

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


int scandir(const char *dir, struct dirent ***namelist,
            int (*select)(const struct dirent *),
            int (*compar)(const struct dirent **, const struct dirent **))
{
  DIR *d;
  struct dirent *entry;
  register int i=0;
  size_t entrysize;

  if ((d=opendir(dir)) == NULL)
     return(-1);

  *namelist=NULL;
  while ((entry=readdir(d)) != NULL)
  {
    if (select == NULL || (select != NULL && (*select)(entry)))
    {
      *namelist=(struct dirent **)realloc((void *)(*namelist),
                 (size_t)((i+1)*sizeof(struct dirent *)));
   if (*namelist == NULL) return(-1);
   entrysize=sizeof(struct dirent)-sizeof(entry->d_name)+strlen(entry->d_name)+1;
   (*namelist)[i]=(struct dirent *)malloc(entrysize);
   if ((*namelist)[i] == NULL) return(-1);
   memcpy((*namelist)[i], entry, entrysize);
   i++;
    }
  }
  if (closedir(d)) return(-1);
  if (i == 0) return(-1);
  if (compar != NULL)
    qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *), compar);
   
  return(i);
}

int alphasort(const struct dirent **a, const struct dirent **b)
{
  return(strcmp((*a)->d_name, (*b)->d_name));
}


Has anyone a solution?
Tanks in advance...


Last edited by ne0h on Fri Mar 28, 2008 6:21 am; edited 1 time in total
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Thu Mar 27, 2008 8:26 am    Post subject: Reply with quote

qsort + google = win.
Back to top
View user's profile Send private message
kuroneko



Joined: 08 Dec 2005
Posts: 24
Location: Chigasaki, Japan

PostPosted: Thu Mar 27, 2008 10:14 am    Post subject: Re: Alphasort File listed Reply with quote

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.
Back to top
View user's profile Send private message
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Thu Mar 27, 2008 3:48 pm    Post subject: Reply with quote

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_Searching/code16.html
(already had the strings in memory of course).
Doesn't help with the thread starters' problem sorry.
Art.
_________________
If not actually, then potentially.
Back to top
View user's profile Send private message
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Fri Mar 28, 2008 12:39 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Mar 28, 2008 4:15 am    Post subject: Reply with quote

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. :)
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group