w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Directory.cpp
Go to the documentation of this file.
1 /*************************************************************************
2 ** Directory.cpp **
3 ** **
4 ** This file is part of dvisvgm -- a fast DVI to SVG converter **
5 ** Copyright (C) 2005-2021 Martin Gieseking <martin.gieseking@uos.de> **
6 ** **
7 ** This program is free software; you can redistribute it and/or **
8 ** modify it under the terms of the GNU General Public License as **
9 ** published by the Free Software Foundation; either version 3 of **
10 ** the License, or (at your option) any later version. **
11 ** **
12 ** This program is distributed in the hope that it will be useful, but **
13 ** WITHOUT ANY WARRANTY; without even the implied warranty of **
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
15 ** GNU General Public License for more details. **
16 ** **
17 ** You should have received a copy of the GNU General Public License **
18 ** along with this program; if not, see <http://www.gnu.org/licenses/>. **
19 *************************************************************************/
20 
21 #include "Directory.hpp"
22 
23 using namespace std;
24 
25 #ifndef _WIN32
26  #include <cerrno>
27  #include <sys/stat.h>
28 #endif
29 
30 
32 #ifdef _WIN32
33  memset(&_fileData, 0, sizeof(WIN32_FIND_DATA));
34 #endif
35 }
36 
37 
38 Directory::Directory (const string &dirname) : Directory() {
39  open(dirname);
40 }
41 
42 
44  close();
45 }
46 
47 
48 bool Directory::open (string dirname) {
49  _dirname = dirname;
50 #ifdef _WIN32
51  _firstread = true;
52  if (dirname[dirname.length()-1] == '/' || dirname[dirname.length()-1] == '\\')
53  dirname = dirname.substr(0, dirname.length()-1);
54  dirname += "\\*";
55  _handle = FindFirstFile(dirname.c_str(), &_fileData);
56  return _handle != INVALID_HANDLE_VALUE;
57 #else
58  _dir = opendir(_dirname.c_str());
59  return bool(_dir);
60 #endif
61 }
62 
63 
65 #ifdef _WIN32
66  FindClose(_handle);
67 #else
68  if (_dir) {
69  closedir(_dir);
70  _dir = nullptr;
71  }
72 #endif
73 }
74 
75 
76 /** Reads first/next directory entry.
77  * @param[in] type type of entry to return (a: file or dir, f: file, d: dir)
78  * @return name of entry */
80 #ifdef _WIN32
81  if (_handle == INVALID_HANDLE_VALUE)
82  return nullptr;
83  while (_firstread || FindNextFile(_handle, &_fileData)) {
84  _firstread = false;
85  if (_fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
86  if (type == ET_FILE_OR_DIR || type == ET_DIR)
87  return _fileData.cFileName;
88  }
89  else if (type == ET_FILE_OR_DIR || type == ET_FILE)
90  return _fileData.cFileName;
91  }
92  FindClose(_handle);
93  _handle = INVALID_HANDLE_VALUE;
94  return nullptr;
95 #else
96  if (_dir) {
97  while ((_dirent = readdir(_dir))) {
98  string path = _dirname + "/" + _dirent->d_name;
99  struct stat stats;
100  if (stat(path.c_str(), &stats) == 0) {
101  if (S_ISDIR(stats.st_mode)) {
102  if (type == ET_FILE_OR_DIR || type == ET_DIR)
103  return _dirent->d_name;
104  }
105  else if (type == ET_FILE_OR_DIR || type == ET_FILE)
106  return _dirent->d_name;
107  }
108  }
109  closedir(_dir);
110  _dir = nullptr;
111  }
112  return nullptr;
113 #endif
114 }
115 
116 
static char * dirname(mrb_state *mrb, const char *path)
Definition: apilist.c:64
#define type(a)
Definition: aptex-macros.h:171
#define bool
Definition: autosp.c:101
bool open(std::string path)
Definition: Directory.cpp:48
std::string _dirname
Definition: Directory.hpp:44
struct dirent * _dirent
Definition: Directory.hpp:51
const char * read(EntryType type=ET_FILE_OR_DIR)
Definition: Directory.cpp:79
DIR * _dir
Definition: Directory.hpp:50
@ ET_FILE_OR_DIR
Definition: Directory.hpp:33
void close()
Definition: Directory.cpp:64
struct dirent * readdir(DIR *dirp)
Definition: dirent.c:138
int closedir(DIR *dirp)
Definition: dirent.c:123
DIR * opendir(char *fname)
Definition: dirent.c:60
#define S_ISDIR(m)
Definition: win32lib.h:203
int stat(const char *path, struct stat *sbuf)
static struct statistic stats[]
Definition: lstatslib.c:289
STL namespace.
bstring c int memset(void *s, int c, int length)
char d_name[260]
Definition: dirent.h:37
pointer path
Definition: t1imager.h:36