~drizzle-trunk/drizzle/development

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
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems
 *
 *  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 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file
 *   cached_directory.h
 *
 * @brief
 *   Defines the interface to the CachedDirectory class.
 */

#ifndef MYSYS_CACHED_DIRECTORY_H
#define MYSYS_CACHED_DIRECTORY_H

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>


/**
 * A utility class to handle processing the entries/files within a directory.
 *
 * This class will allow the user to either get a list of the entry names 
 * within a given directory.
 */
class CachedDirectory
{
  std::string path;

public:
  class Entry
  {
  public:
    std::string filename;
    Entry(std::string in_name)
      : filename(in_name)
    {}
  };
  typedef std::vector<Entry *> Entries;
private:
  int error; ///< Error code stored from various syscalls
  Entries entries; ///< Entries in the directory

public:
  /**
   * Emptry Constructor.
   *
   */
  CachedDirectory();
      
  /**
   * Constructor taking full directory path as sole parameter.
   *
   * @param[in] Path to the directory to open
   * @param[in] File extensions to mask against.
   */
 CachedDirectory(const std::string& in_path); 
 CachedDirectory(const std::string& in_path, std::set<std::string>& exts);

 /**
  * Destructor.  Cleans up any resources we've taken 
  */
  ~CachedDirectory();

  /**
   * Returns whether the CachedDirectory object is in a failed state
   */
  inline bool fail() const 
  {
    return error != 0;
  }

  /** 
   * Returns the stored error code of the last action the directory
   * object took (open, read, etc)
   */
  inline int getError() const
  {
    return error;
  }

  /** 
   * Returns the current path for the cached directory
   */
  inline const char *getPath() const
  {
    return path.c_str();
  }

  /**
   * Return the list of entries read from the directory
   *
   * @returns
   *   A vector of strings containing the directory entry names.
   */
  inline const Entries &getEntries()
  {
    return entries;
  }

  /**
   * Encapsulate the logic to open the directory.
   * @param[in] dirPath The path to the directory to open and read.
   * @param[in] File extensions to mask against.
   * @retval true Success
   * @retval false Failure
   */
  bool open(const std::string &dirPath);
  bool open(const std::string &in_path, std::set<std::string> exts, bool honor_exts= true);

};

#endif /* MYSYS_CACHED_DIRECTORY_H */