?
Solved

I need help sorting a file_filelist by extension...  please help

Posted on 2003-03-14
10
Medium Priority
?
201 Views
Last Modified: 2010-04-17
i have a data structure file_list (defined below) that hold the location of a file, to save on memory i made the "directories" another dynamic array and simply mapped the locations with an interger array. so that if a lot files are contained in a single directory, i'll save a lot of memory =). i have designed all the appropriate dynamic allocation, so we can assume that the filenames and locations are already in the memory...

the dir_count and file_count, give the number of files (of course arrays start at [0] so the actual file_count position in the array is filecount-1 (same goes for the dir_count)...

i know that the directories array don't need to be sorted... so that just leaves the filenames array to be sorted by extension (but then again, somefiles don't have extension!)... but i don't know how =(.

pls teach me how do the my x_sort function.

struct file_list {
     char **directories;
     char **filenames;
     unsigned short *locations;
     unsigned short file_count;
     unsigned short dir_count;
};

void x_sort(file_list *flist) {
     // help needed
}
0
Comment
Question by:icedeocampo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 

Author Comment

by:icedeocampo
ID: 8141574
if i had more points i'd give it all... but i have only 345 left... i'm holding on to my last 100... but if i have more... i'd give 500 for this.. i know this kinda tough
0
 
LVL 84

Expert Comment

by:ozo
ID: 8141634
#include <stdlib.h>
#include <string.h>
int compar(const void *p1,const void *p2){
    char *e1,*e2;
    e1 = strrchr(*(char **)p1,'.');
    e2 = strrchr(*(char **)p2,'.');
    if( !e1 && !e2 ){ return 0; }
    if( !e1 ){ return -1; }
    if( !e2 ){ return 1; }
    return strcmp(e1,e2);
   
}
void x_sort(struct file_list *flist) {
     qsort(flist->filenames,flist->file_count,sizeof(char *),compar);
}
0
 

Author Comment

by:icedeocampo
ID: 8141653
does this sort by extension or by filename, i need to sort by extension...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

Expert Comment

by:ozo
ID: 8141745
It sorts by the part of the filename following the '.'
0
 

Author Comment

by:icedeocampo
ID: 8141912
but there files with two or more "."

such as "a.out.exe"

i have an idea but i can't seem to implement it... my idea is to scan the string from the last character and find the first occurence from the last character (which is the last ".") ... but i can't do it ... darn =(
0
 
LVL 1

Expert Comment

by:MartinGreen
ID: 8142295
summerising the above posts I have added a routine that gets the file extension (running from the back of the filename). The entire thing looks like this:-
#include <stdlib.h>
#include <string.h>

using namespace System;

struct file_list {
    char **directories;
    char **filenames;
    unsigned short *locations;
    unsigned short file_count;
    unsigned short dir_count;
};

char* get_extension(char* file)
{
     int length = strlen(file);
     int ix = length - 1;
     bool found_extension = false;

     while (ix >= 0 && !found_extension)
     {
          if (file[ix] == '.')
          {
               found_extension = true;
          }
          else
          {
               ix--;
          }
     }

     if (found_extension)
     {
          // Add 1 to skip over the . which we don't wish to sort on
          return &file[ix + 1];
     }
     
     return NULL;
}

int compar(const void *p1,const void *p2){
   char *e1,*e2;
   e1 = get_extension(*(char **)p1);
   e2 = get_extension(*(char **)p2);
   if( !e1 && !e2 ){ return 0; }
   if( !e1 ){ return -1; }
   if( !e2 ){ return 1; }
   return strcmp(e1,e2);
   
}
void x_sort(struct file_list *flist) {
    qsort(flist->filenames,flist->file_count,sizeof(char *),compar);
}


// This is the entry point for this application
int _tmain(void)
{
     char file1[30] = "c:\\test\\temp.tst.tmp";
     char file2[30] = "c:\\test\\temp.tst.exe";
     char file3[30] = "c:\\test\\temp.tst.abc";
     char file4[30] = "c:\\test\\temp.abc";
     char file5[30] = "c:\\test\\temp.tst.csv";
     char* files[5] = {file1, file2, file3, file4, file5};

     file_list list;
     list.filenames = files;
     list.file_count = 5;
     
     x_sort(&list);

    Console::WriteLine(list.filenames[0]);
    Console::WriteLine(list.filenames[1]);
    Console::WriteLine(list.filenames[2]);
    Console::WriteLine(list.filenames[3]);
    Console::WriteLine(list.filenames[4]);
    return 0;
}

I have included my _tmain function that sets up the test data so you can follow my take on your problem (Just incase I misunserstood!!)

Hope this helps.

Regards

Martin Green
0
 
LVL 1

Expert Comment

by:MartinGreen
ID: 8142298
Sorry, forgot to mention I am using .NET hence the Console.Writelines in my _tmain. Just replace these with printf() commands, I forgot to do that before posting it!!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 8142349
e1 = strrchr(*(char **)p1,'.');
finds the last . ie the ".exe" in "a.out.exe"
Did you want to compare from the first . ?
0
 
LVL 1

Accepted Solution

by:
MartinGreen earned 980 total points
ID: 8142781
icedeocampo mentioned before my post that he was after the last extension, hence my backwards search!

I think we could do with a comment from icedeocampo now to see which one of us has picked up thw wrong end of the stick ;)

Regards

Martin Green
0
 

Author Comment

by:icedeocampo
ID: 9077470
martingreen's post is my pick.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Introduction to Processes
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question