~shim/libdesktop-agnostic/vala-0.18

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
/*
 * Desktop Agnostic Library: glob() wrapper.
 *
 * Copyright (C) 2009 Mark Lee <libdesktop-agnostic@lazymalevolence.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Author : Mark Lee <libdesktop-agnostic@lazymalevolence.com>
 */

using POSIX;

namespace DesktopAgnostic.VFS
{
  public errordomain GlobError
  {
    NOSPACE,     // ran out of memory
    ABORTED,     // read error
    NOMATCH,     // no found matches
    BAD_PATTERN, // invalid pattern
    BAD_FLAGS,   // invalid flags
    ERRNO        // via errno
  }
  public class Glob : Object
  {
    private glob_t glob;
    public size_t offset
    {
      get
      {
        return this.glob.offset;
      }
    }
    private string _pattern;
    public string pattern
    {
      get
      {
        return this._pattern;
      }
      set
      {
        if (value != "")
        {
          this._pattern = value;
        }
        else
        {
          throw new GlobError.BAD_PATTERN ("Invalid pattern.");
        }
      }
    }
    private int _flags = glob_t.MARK | glob_t.BRACE | glob_t.TILDE_CHECK;
    public int flags
    {
      get
      {
        return this._flags;
      }
      set
      {
        if (value >= 0)
        {
          this._flags = value;
        }
        else
        {
          throw new GlobError.BAD_FLAGS ("Invalid flags.");
        }
      }
    }
    public static Glob
    execute (string pattern) throws GlobError
    {
      Glob g = (Glob)Object.new (typeof (Glob), "pattern", pattern);
      g.run_glob (g._pattern, g._flags);
      return g;
    }
    public static Glob
    execute_with_flags (string pattern, int flags) throws GlobError
    {
      Glob g = (Glob)Object.new (typeof (Glob),
                                 "pattern", pattern,
                                 "flags", flags);
      g.run_glob (g._pattern, g._flags);
      return g;
    }
    public void
    append (string pattern) throws GlobError
    {
      this.run_glob (pattern, this._flags | glob_t.APPEND);
    }
    public unowned string[]?
    get_paths ()
    {
      return this.glob.paths;
    }
    private void
    run_glob (string pattern, int flags) throws GlobError
    {
      int res = POSIX.glob (pattern, flags, (void*)on_glob_error,
                            out this.glob);
      if (res != 0)
      {
        switch (res)
        {
          case glob_t.ERR_NOSPACE:
            throw new GlobError.NOSPACE ("Ran out of memory.");
          case glob_t.ERR_ABORTED:
            throw new GlobError.ABORTED ("Read error.");
          case glob_t.ERR_NOMATCH:
            throw new GlobError.NOMATCH ("No matches found.");
          default:
            critical ("Unknown error code: %d", res);
            break;
        }
      }
    }
    private static int
    on_glob_error (string path, int eerrno)
    {
      switch (eerrno)
      {
        case Posix.EACCES:
        case Posix.EBADF:
        case Posix.EMFILE:
        case Posix.ENFILE:
        case Posix.ENOENT:
        case Posix.ENOMEM:
        case Posix.ENOTDIR:
        case Posix.EFAULT:
        case Posix.EINVAL:
        case Posix.ELOOP:
        case Posix.ENAMETOOLONG:
          throw new GlobError.ERRNO ("Miscellaneous error for '%s' (%d): %s",
                                     path, eerrno,
                                     Posix.strerror (eerrno));
        default:
          critical ("Unknown error code: %d", eerrno);
          break;
      }
      return 1;
    }
  }
}

// vim: set et ts=2 sts=2 sw=2 ai :