~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/cached_directory.h

  • Committer: Brian Aker
  • Date: 2009-10-16 10:27:33 UTC
  • mfrom: (1183.1.4 merge)
  • Revision ID: brian@gaz-20091016102733-b10po5oup0hjlilh
MergeĀ EngineĀ changes.

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.
 
43
 */
 
44
class CachedDirectory
 
45
{
 
46
public:
 
47
  class Entry
 
48
  {
 
49
  public:
 
50
    std::string filename;
 
51
    Entry(std::string in_name)
 
52
      : filename(in_name)
 
53
    {}
 
54
  };
 
55
  typedef std::vector<Entry *> Entries;
 
56
private:
 
57
  int error; ///< Error code stored from various syscalls
 
58
  Entries entries; ///< Entries in the directory
 
59
 
 
60
  /**
 
61
   * Encapsulate the logic to open the directory.
 
62
   * @param[in] dirPath The path to the directory to open and read.
 
63
   * @retval true Success
 
64
   * @retval false Failure
 
65
   */
 
66
  bool open(const std::string &dirPath);
 
67
 
 
68
public:
 
69
  explicit CachedDirectory()
 
70
    : error(0)
 
71
  {}
 
72
      
 
73
  /**
 
74
   * Constructor taking full directory path as sole parameter.
 
75
   *
 
76
   * @param[in] Path to the directory to open
 
77
   */
 
78
 CachedDirectory(const std::string &in_path); 
 
79
 /**
 
80
  * Destructor.  Cleans up any resources we've taken 
 
81
  */
 
82
  ~CachedDirectory();
 
83
 
 
84
  /**
 
85
   * Returns whether the CachedDirectory object is in a failed state
 
86
   */
 
87
  inline bool fail() const 
 
88
  {
 
89
    return error != 0;
 
90
  }
 
91
 
 
92
  /** 
 
93
   * Returns the stored error code of the last action the directory
 
94
   * object took (open, read, etc)
 
95
   */
 
96
  inline int getError() const
 
97
  {
 
98
    return error;
 
99
  }
 
100
 
 
101
  /**
 
102
   * Return the list of entries read from the directory
 
103
   *
 
104
   * @returns
 
105
   *   A vector of strings containing the directory entry names.
 
106
   */
 
107
  inline const Entries &getEntries()
 
108
  {
 
109
    return entries;
 
110
  }
 
111
};
 
112
 
 
113
#endif /* MYSYS_CACHED_DIRECTORY_H */