summaryrefslogtreecommitdiff
path: root/ACE/ace/Dirent_Selector.cpp
blob: 5d7279fde9e867a78fbeea77603175ae80c97a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "ace/Dirent_Selector.h"

#if !defined (__ACE_INLINE__)
#include "ace/Dirent_Selector.inl"
#endif /* __ACE_INLINE__ */

#include "ace/OS_NS_dirent.h"
#include "ace/OS_NS_stdlib.h"

#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

// Construction/Destruction

ACE_Dirent_Selector::ACE_Dirent_Selector ()
  : namelist_ (0),
    n_ (0)
{
}

ACE_Dirent_Selector::~ACE_Dirent_Selector ()
{
  // Free up any allocated resources.
  this->close();
}

int
ACE_Dirent_Selector::open (const ACE_TCHAR *dir,
                           ACE_SCANDIR_SELECTOR sel,
                           ACE_SCANDIR_COMPARATOR cmp)
{
  n_ = ACE_OS::scandir (dir, &this->namelist_, sel, cmp);
  return n_;
}

int
ACE_Dirent_Selector::close ()
{
  for (--n_; n_ >= 0; --n_)
    {
#if defined (ACE_LACKS_STRUCT_DIR)
      // Only the lacking-struct-dir emulation allocates this. Native
      // scandir includes d_name in the dirent struct itself.
#if defined (ACE_HAS_ALLOC_HOOKS)
      ACE_Allocator::instance()->free (this->namelist_[n_]->d_name);
#else
      ACE_OS::free (this->namelist_[n_]->d_name);
#endif /* ACE_HAS_ALLOC_HOOKS */
#endif
#if defined (ACE_HAS_ALLOC_HOOKS)
      ACE_Allocator::instance()->free (this->namelist_[n_]);
#else
      ACE_OS::free (this->namelist_[n_]);
#endif /* ACE_HAS_ALLOC_HOOKS */
    }

#if defined (ACE_HAS_ALLOC_HOOKS)
    ACE_Allocator::instance()->free (this->namelist_);
#else
  ACE_OS::free (this->namelist_);
#endif /* ACE_HAS_ALLOC_HOOKS */
  this->namelist_ = 0;
  return 0;
}

ACE_END_VERSIONED_NAMESPACE_DECL