All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_dir.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #if !defined(_MSC_VER)
24 #include <dirent.h>
25 
26 typedef struct as_dir_s {
27  DIR* dir;
28  struct dirent* entry;
29 } as_dir;
30 
31 static inline bool
32 as_dir_exists(const char* directory)
33 {
34  DIR* dir = opendir(directory);
35 
36  if (dir) {
37  closedir(dir);
38  return true;
39  }
40  return false;
41 }
42 
43 static inline bool
44 as_dir_open(as_dir* dir, const char* directory)
45 {
46  dir->dir = opendir(directory);
47  return dir->dir != NULL;
48 }
49 
50 static inline const char*
52 {
53  dir->entry = readdir(dir->dir);
54  if (!dir->entry || !dir->entry->d_name[0]) {
55  return NULL;
56  }
57  return dir->entry->d_name;
58 }
59 
60 static inline void
62 {
63  closedir(dir->dir);
64 }
65 
66 #else // _MSC_VER
67 
68 #define WIN32_LEAN_AND_MEAN
69 #include <windows.h>
70 
71 typedef struct as_dir_s {
72  WIN32_FIND_DATAA dir;
73  HANDLE hFind;
74  const char* entry;
75 } as_dir;
76 
77 static inline bool
78 as_dir_exists(const char* directory)
79 {
80  DWORD attr = GetFileAttributesA(directory);
81  return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY);
82 }
83 
84 static inline bool
85 as_dir_open(as_dir* dir, const char* directory)
86 {
87  dir->hFind = FindFirstFileA(directory, &dir->dir);
88 
89  if (dir->hFind == INVALID_HANDLE_VALUE) {
90  return false;
91  }
92 
93  dir->entry = dir->dir.cFileName;
94  return true;
95 }
96 
97 static inline const char*
98 as_dir_read(as_dir* dir)
99 {
100  const char* entry = dir->entry;
101 
102  if (!entry) {
103  return NULL;
104  }
105 
106  if (FindNextFileA(dir->hFind, &dir->dir) && dir->dir.cFileName[0]) {
107  dir->entry = dir->dir.cFileName;
108  }
109  else {
110  dir->entry = NULL;
111  }
112  return entry;
113 }
114 
115 static inline void
116 as_dir_close(as_dir* dir)
117 {
118  FindClose(dir->hFind);
119 }
120 
121 #endif
122 
123 #ifdef __cplusplus
124 } // end extern "C"
125 #endif
struct dirent * entry
Definition: as_dir.h:28
static bool as_dir_open(as_dir *dir, const char *directory)
Definition: as_dir.h:44
static void as_dir_close(as_dir *dir)
Definition: as_dir.h:61
static bool as_dir_exists(const char *directory)
Definition: as_dir.h:32
static const char * as_dir_read(as_dir *dir)
Definition: as_dir.h:51
Definition: as_dir.h:26
DIR * dir
Definition: as_dir.h:27