~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/cached_directory.h

Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
28
#ifndef MYSYS_CACHED_DIRECTORY_H
 
29
#define MYSYS_CACHED_DIRECTORY_H
 
30
 
 
31
#include <vector>
 
32
#include <string>
 
33
#include <dirent.h>
 
34
#include <stdlib.h>
 
35
#include <errno.h>
 
36
 
 
37
 
 
38
/**
 
39
 * A utility class to handle processing the entries/files within a directory.
 
40
 *
 
41
 * This class will allow the user to either get a list of the entry names 
 
42
 * within a given directory, or, alternatively, apply a function to each
 
43
 * entry as it is read from the directory.
 
44
 */
 
45
class CachedDirectory
 
46
{
 
47
public:
 
48
  class Entry
 
49
  {
 
50
  public:
 
51
    std::string filename;
 
52
    Entry(std::string in_name)
 
53
      : filename(in_name)
 
54
    {}
 
55
  };
 
56
  typedef std::vector<Entry *> Entries;
 
57
private:
 
58
  int error; ///< Error code stored from various syscalls
 
59
  Entries entries; ///< Entries in the directory
 
60
 
 
61
  /**
 
62
   * Encapsulate the logic to open the directory.
 
63
   * @param[in] dirPath The path to the directory to open and read.
 
64
   * @retval true Success
 
65
   * @retval false Failure
 
66
   */
 
67
  bool open(const std::string &dirPath);
 
68
 
 
69
public:
 
70
  explicit CachedDirectory()
 
71
    : error(0)
 
72
  {}
 
73
      
 
74
  /**
 
75
   * Constructor taking full directory path as sole parameter.
 
76
   *
 
77
   * @param[in] Path to the directory to open
 
78
   */
 
79
 CachedDirectory(const std::string &in_path); 
 
80
 /**
 
81
  * Destructor.  Cleans up any resources we've taken 
 
82
  */
 
83
  ~CachedDirectory();
 
84
 
 
85
  /**
 
86
   * Returns whether the CachedDirectory object is in a failed state
 
87
   */
 
88
  inline bool fail() const 
 
89
  {
 
90
    return error != 0;
 
91
  }
 
92
 
 
93
  /** 
 
94
   * Returns the stored error code of the last action the directory
 
95
   * object took (open, read, etc)
 
96
   */
 
97
  inline int getError() const
 
98
  {
 
99
    return error;
 
100
  }
 
101
 
 
102
  /**
 
103
   * Return a list of directory entries populated in the call to open().
 
104
   *
 
105
   * @note
 
106
   *   You must call the open() method before getList() if you want the
 
107
   *   list contain any elements.
 
108
   *
 
109
   * @returns
 
110
   *   A vector of strings containing the directory entry names.
 
111
   */
 
112
  inline const Entries &getEntries()
 
113
  {
 
114
    return entries;
 
115
  }
 
116
};
 
117
 
 
118
#endif  /* MYSYS_CACHED_DIRECTORY_H */