File: DirIterator.cc

package info (click to toggle)
exactimage 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,040 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (171 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download | duplicates (5)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: utility/src/DirIterator.cc
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

#include "DirIterator.hh"

#include <sys/types.h>
#include <sys/stat.h>

#include <iostream>
#include <sstream> // for stringstream

int Utility::FindUniqueName (std::string& fname, const std::string& base,
			     const std::string& ext, int first_tried_index)
{
  std::stringstream t_fname;
  
  int t_index = first_tried_index;
  bool t_found = false;
  while (!t_found)
    {
      t_fname.str ("");
      t_fname << base << t_index << ext;
      std::cout << "testing: (statting): " << t_fname.str () << std::endl;
      struct stat t_stat;
      if (stat (t_fname.str().c_str (), &t_stat) < 0)
	t_found = true;
      else
	++ t_index;
    }
  fname = t_fname.str ();
  return t_index;
}

// ---

Utility::DirList::Iterator::Iterator ()
{
  m_open = false;
  m_end = false;
  m_dirlist = 0;
  m_internal_dir_entry = 0;
}

Utility::DirList::Iterator::Iterator (DirList* i_dirlist)
{
  m_open = false;
  m_end = false;
  m_dirlist = i_dirlist;
  m_entry_name = "";
}

Utility::DirList::Iterator::Iterator (const Iterator& i_other)
{
  m_open = false;
  operator=(i_other);
}

Utility::DirList::Iterator::~Iterator ()
{
  if (m_open)
    Close ();
}

const Utility::FileType Utility::DirList::Iterator::Type ()
{
  if (!m_open)
    Open();
  
  FileType type;
  
  // use d_type _if_ usable ...
  if (m_internal_dir_entry && m_internal_dir_entry->d_type)
    type = DTTOIF(m_internal_dir_entry->d_type);
  else {
    m_file.SetFile (m_dirlist->m_dirname + '/' + m_entry_name);
    type = m_file.Type();
  }

  return type;
}

void Utility::DirList::Iterator::Open ()
{
  if (m_open) {
    std::cout << "  already open" << std::endl;
    return;
  }
  
  // special case: we have a dirname but are not yet open (e.g.
  // after a operator= and so want to search to the correct entry
  std::string t_entry_name = m_entry_name;
  
  m_internal_dir = opendir (m_dirlist->m_dirname.c_str () );
  if (m_internal_dir == 0) {
    m_end = true;
  }
  else {
    m_open = true;
    m_end = false;
    
    Next ();
    
    // special case (also see above) we want to search an entry
    if (t_entry_name != "") {
      while (m_entry_name != t_entry_name && !m_end)
	Next ();
    }
  }
}

void Utility::DirList::Iterator::Close ()
{
  if (!m_open || !m_internal_dir)
    return;
  
  closedir (m_internal_dir);

  m_open = false;
  m_internal_dir = 0;
}

// ---

Utility::DirList::DirList (const std::string& i_dirname)
{
  m_dirname = i_dirname;
}

Utility::DirList::~DirList ()
{
  
}

const Utility::DirList::Iterator Utility::DirList::Begin ()
{
  // create a virgin Iterator
  Iterator it (this);
  it.Open();
  return it;
}

const Utility::DirList::Iterator Utility::DirList::End ()
{
  // create an Iterator with end flag set
  Iterator it (this);
  it.m_end = true;
  it.m_entry_name = "";
  return it;
}