Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
87 user(s) are online (79 user(s) are browsing Forums)

Members: 0
Guests: 87

more...

Support us!

Headlines

 
  Register To Post  

What the best and fast way to get list of files/dirs in the directory
Home away from home
Home away from home


See User information
I need to implement GetFileNamesInDirectory("some_directory"); function, which i later can just:
std::list<std::stringgetnames(const chardir)
{
    
std::list<std::stringfilenames;
    
char **rc GetFileNameInDirectory("directory");
    if ( *
rc == NULL )
    {
        
printf("Error: Failed to enumerate filenames in directory '%s'\n"dir);
        return 
filenames;
    }
    
char **i;
    
std::string file;
    for ( 
rc; *!= NULLi++ )
    {
        
file = *i;
        
printf(" * We've got [%s].\n"file.c_str());
        
filenames.push_back(file);
    }
        return 
filenames;
}


And it should work on public versions of os4 (at least on FE), so no beta DOS's stuff can be used. How do it the best ?

Thanks

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: What the best and fast way to get list of files/dirs in the directory
Just popping in
Just popping in


See User information
Something like this?

std::list<std::string> *getnames (const chardir
{
    
std::list<std::string> *filenames = new std::list<std::string>;
    
struct ExamineData *data_p IDOS->ExamineObjectTags (EX_StringNameInputdirTAG_END);
    
    if (
data_p)
        {
            if (
EXD_IS_DIRECTORY (data_p))
                {
                    
APTR context_p IDOS->ObtainDirContextTags (EX_StringNameInputdir,
                        
EX_DataFields, (EXF_NAME EXF_LINK EXF_TYPE),
                        
TAG_END);
                
                    if (
context_p)
                        {
                            
struct ExamineData *dat_p;

                            
/*
                                context_p takes care of deleting the returned dat_p objects
                                when we call ReleaseDirContext, so we mustn't call 
                                FreeDosObject on them
                            */
                            
while ((dat_p IDOS->ExamineDir (context_p)))
                                {
                                    if (
EXD_IS_FILE (dat_p))
                                        {
                                            
filenames -> push_back (dat_p -> Name);        
                                        }
                                }
                
                            if (
ERROR_NO_MORE_ENTRIES == IDOS->IoErr ())
                                {
                                    
success TRUE;           /* normal exit */
                                
}
                            else
                                {
                                    
IDOS->Printf ("Failed to obtain directory context for "%s"\n"dir);
                                    
IDOS->PrintFault (IDOS->IoErr (), NULL); /* failure - why ? */
                                
}                        
                                            
                            
IDOS->ReleaseDirContext (context_p);          /* NULL safe */
                        
}        /* if (context_p) */
                    
else
                        {
                            
IDOS->Printf ("oops\n");
                            
IDOS->PrintFault (IDOS->IoErr (), NULL);  /* no context - why ? */
                        
}                    
                    
                }        
/* if (EXD_IS_DIRECTORY (data_p)) */    
        
            
IDOS->FreeDosObject (DOS_EXAMINEDATAdata_p);
        }        
/* if (data_p) */

    
return filenames;
}

Go to top
Re: What the best and fast way to get list of files/dirs in the directory
Home away from home
Home away from home


See User information
Thanks, but getnames should be stay as it, its GetFileNameInDirectory i need to implement, which will return it all to char **rc

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: What the best and fast way to get list of files/dirs in the directory
Supreme Council
Supreme Council


See User information
There appears to be some source you can use here:

https://tpgit.github.io/Leptonica/sarray_8c_source.html

Line 1778 starts the function you need...

Simon

Comments made in any post are personal opinion, and are in no-way representative of any commercial entity unless specifically stated as such.
----
http://codebench.co.uk
Go to top
Re: What the best and fast way to get list of files/dirs in the directory
Just can't stay away
Just can't stay away


See User information
@Rigo

is this library available also for amigaos4?

Go to top
Re: What the best and fast way to get list of files/dirs in the directory
Just popping in
Just popping in


See User information
@kas1e

char **GetFileNameInDirectory (const chardir
{
    
char **filenames_pp NULL;
    
struct ExamineData *data_p IDOS->ExamineObjectTags (EX_StringNameInputdirTAG_END);
    
    if (
data_p)
        {
            if (
EXD_IS_DIRECTORY (data_p))
                {
                    
APTR context_p IDOS->ObtainDirContextTags (EX_StringNameInputdir,
                        
EX_DataFields, (EXF_NAME EXF_LINK EXF_TYPE),
                        
TAG_END);
                
                    if (
context_p)
                        {
              
struct List *names_p IExec->AllocSysObjectTags (ASOT_LISTTAG_DONE);

              if (
names_p)
                {
                  
struct ExamineData *dat_p;
                  
size_t num_entries 0;

                                
/*
                                    context_p takes care of deleting the returned dat_p objects
                                    when we call ReleaseDirContext, so we mustn't call 
                                    FreeDosObject on them
                                */
                                
while ((dat_p IDOS->ExamineDir (context_p)))
                                    {
                                        if (
EXD_IS_FILE (dat_p))
                                            {
                          
char *copied_name_p CopyToNewString (dat_p -> Name);

                          if (
copied_name_p)
                            {
                              
struct Node *node_p IExec->AllocSysObjectTags (ASOT_NODE,
                                                      
ASONODE_Namecopied_name_p,
                                                      
ASONODE_Sizesizeof (struct Node),
                                                    
TAG_DONE);       
                                                
                                                  if (
node_p)
                                                      {
                                                        
IExec->AddTail (names_pnode_p);
                                  ++ 
num_entries;
                                                      }
                                                  }
                                              }
                                    }
                    
                                if (
ERROR_NO_MORE_ENTRIES != IDOS->IoErr ())
                                    {
                                        
IDOS->Printf ("Failed to obtain directory context for "%s"n"dir);
                                        
IDOS->PrintFault (IDOS->IoErr (), NULL); /* failure - why ? */
                                    
}                        
                  else
                    {
                      
filenames_pp = (char **) IExec->AllocVecTags (num_entries sizeof (char *), TAG_DONE);

                      if (
filenames_pp)
                        {
                          
char *current_filename_pp filenames_pp;
                          
struct Node *node_p IExec->GetHead (names_p);
                          
struct Node *next_node_p NULL;

                            while (
node_p)
                                {
                                    if (
node_p -> ln_Name)
                                        {
                                  *
current_filename_pp node_p -> ln_Name;

                                  ++ 
current_filename_pp;
                                            (
node_p -> ln_Name NULL;
                                        }

                                    
next_node_p IExec->GetSucc (node_p);
                                    
IExec->FreeSysObject (ASOT_NODEnode_p);
                                    
node_p next_node_p;
                                }

                        }

                    }

                  
IExec->FreeSysObject (ASOT_LIST, (APTRlist_p);
                }
                                        
                            
IDOS->ReleaseDirContext (context_p);          /* NULL safe */
                        
}        /* if (context_p) */
                    
else
                        {
                            
IDOS->Printf ("oopsn");
                            
IDOS->PrintFault (IDOS->IoErr (), NULL);  /* no context - why ? */
                        
}                    
                    
                }        
/* if (EXD_IS_DIRECTORY (data_p)) */    
        
            
IDOS->FreeDosObject (DOS_EXAMINEDATAdata_p);
        }        
/* if (data_p) */

    
return filenames_pp;
}


char *CopyToNewString (const char *src_p)
{
  
char *dest_p NULL;

  if (
src_p)
    {
      
size_t len strlen (src_p);
        
dest_p = (char *) IExec->AllocVecTags (len 1TAG_DONE);

        if (
dest_p)
            {
                
strcpy (dest_pstart_p);
            }
     }
  
  return 
dest_p;
}

Go to top
Re: What the best and fast way to get list of files/dirs in the directory
Home away from home
Home away from home


See User information
@all
Thanks, it helps !

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project