~drizzle-trunk/drizzle/development

1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
/**
21
 * @file
22
 *   cached_directory.h
23
 *
24
 * @brief
25
 *   Defines the interface to the CachedDirectory class.
26
 */
27
1241.9.44 by Monty Taylor
Made magic with cached_directory.
28
#ifndef DRIZZLED_CACHED_DIRECTORY_H
29
#define DRIZZLED_CACHED_DIRECTORY_H
30
31
#include <dirent.h>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
32
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
33
#include <iostream>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
34
#include <vector>
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
35
#include <set>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
36
#include <string>
1241.9.44 by Monty Taylor
Made magic with cached_directory.
37
#include <cstdlib>
38
#include <cerrno>
39
40
namespace drizzled
41
{
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
42
43
/**
44
 * A utility class to handle processing the entries/files within a directory.
45
 *
46
 * This class will allow the user to either get a list of the entry names 
1108.4.1 by David Shrewsbury
Corrected comments
47
 * within a given directory.
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
48
 */
49
class CachedDirectory
50
{
51
public:
52
  class Entry
53
  {
1259.3.4 by Monty Taylor
Just a little bit of a cleanup.
54
    Entry();
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
55
  public:
56
    std::string filename;
1259.3.4 by Monty Taylor
Just a little bit of a cleanup.
57
    explicit Entry(std::string in_name)
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
58
      : filename(in_name)
59
    {}
60
  };
61
  typedef std::vector<Entry *> Entries;
62
  /**
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
63
   * Empty Constructor.
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
64
   */
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
65
  CachedDirectory();
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
66
      
67
  /**
68
   * Constructor taking full directory path as sole parameter.
69
   *
70
   * @param[in] Path to the directory to open
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
71
   * @param[in] File extensions to allow
72
   */
73
  CachedDirectory(const std::string& in_path); 
74
75
  /**
76
   * Constructor taking full directory path as sole parameter.
77
   *
78
   * @param[in] Path to the directory to open
79
   * @param[in] File extensions to allow
80
   */
81
  CachedDirectory(const std::string& in_path, std::set<std::string>& allowed_exts);
82
83
  /**
84
   * Destructor.  Cleans up any resources we've taken 
85
   */
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
86
  ~CachedDirectory();
87
88
  /**
89
   * Returns whether the CachedDirectory object is in a failed state
90
   */
91
  inline bool fail() const 
92
  {
93
    return error != 0;
94
  }
95
96
  /** 
97
   * Returns the stored error code of the last action the directory
98
   * object took (open, read, etc)
99
   */
100
  inline int getError() const
101
  {
102
    return error;
103
  }
104
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
105
  /** 
106
   * Returns the current path for the cached directory
107
   */
108
  inline const char *getPath() const
109
  {
110
    return path.c_str();
111
  }
112
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
113
  /**
1108.4.1 by David Shrewsbury
Corrected comments
114
   * Return the list of entries read from the directory
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
115
   *
116
   * @returns
117
   *   A vector of strings containing the directory entry names.
118
   */
119
  inline const Entries &getEntries()
120
  {
121
    return entries;
122
  }
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
123
private:
124
  std::string path; ///< Path to the directory
125
  int error; ///< Error code stored from various syscalls
126
  Entries entries; ///< Entries in the directory
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
127
128
  /**
129
   * Encapsulate the logic to open the directory.
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
130
   * @param[in] The path to the directory to open and read
131
   *
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
132
   * @retval true Success
133
   * @retval false Failure
134
   */
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
135
  bool open(const std::string &in_path);
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
136
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
137
  /**
138
   * Encapsulate the logic to open the directory with a set of allowed
139
   * file extensions to filter for.
140
   *
141
   * @param[in] The path to the directory to open and read
142
   * @param[in] File extensions to allow
143
   *
144
   * @retval true Success
145
   * @retval false Failure
146
   */
147
  bool open(const std::string &in_path, std::set<std::string> &allowable_exts);
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
148
};
149
1241.9.44 by Monty Taylor
Made magic with cached_directory.
150
} /* namespace drizzled */
151
152
#endif /* DRIZZLED_CACHED_DIRECTORY_H */