1 /*************************************************************************
2 ** Directory.cpp **
3 ** **
4 ** This file is part of dvisvgm -- the DVI to SVG converter **
5 ** Copyright (C) 2005-2015 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 <config.h>
22 #include "Directory.h"
23
24 using namespace std;
25
26 #ifdef __WIN32__
27 #include <windows.h>
28 #else
29 #include <errno.h>
30 #include <sys/stat.h>
31 #endif
32
33
Directory()34 Directory::Directory () {
35 #if __WIN32__
36 handle = INVALID_HANDLE_VALUE;
37 firstread = true;
38 memset(&fileData, 0, sizeof(WIN32_FIND_DATA));
39 #else
40 dir = 0;
41 dirent = 0;
42 #endif
43 }
44
45
Directory(string dirname)46 Directory::Directory (string dirname) {
47 #if __WIN32__
48 handle = INVALID_HANDLE_VALUE;
49 firstread = true;
50 memset(&fileData, 0, sizeof(WIN32_FIND_DATA));
51 #else
52 dir = 0;
53 dirent = 0;
54 #endif
55 open(dirname);
56 }
57
58
~Directory()59 Directory::~Directory () {
60 close();
61 }
62
63
open(string dname)64 bool Directory::open (string dname) {
65 _dirname = dname;
66 #ifdef __WIN32__
67 firstread = true;
68 if (dname[dname.length()-1] == '/' || dname[dname.length()-1] == '\\')
69 dname = dname.substr(0, dname.length()-1);
70 dname += "\\*";
71 handle = FindFirstFile(dname.c_str(), &fileData);
72 return handle != INVALID_HANDLE_VALUE;
73 #else
74 dir = opendir(_dirname.c_str());
75 return dir;
76 #endif
77 }
78
79
close()80 void Directory::close () {
81 #ifdef __WIN32__
82 FindClose(handle);
83 #else
84 closedir(dir);
85 #endif
86 }
87
88
89 /** Reads first/next directory entry.
90 * @param[in] type type of entry to return (a: file or dir, f: file, d: dir)
91 * @return name of entry */
read(EntryType type)92 const char* Directory::read (EntryType type) {
93 #ifdef __WIN32__
94 if (handle == INVALID_HANDLE_VALUE)
95 return 0;
96 while (firstread || FindNextFile(handle, &fileData)) {
97 firstread = false;
98 if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
99 if (type == ET_FILE_OR_DIR || type == ET_DIR)
100 return fileData.cFileName;
101 }
102 else if (type == ET_FILE_OR_DIR || type == ET_FILE)
103 return fileData.cFileName;
104 }
105 FindClose(handle);
106 handle = INVALID_HANDLE_VALUE;
107 return 0;
108 #else
109 if (!dir)
110 return 0;
111 while ((dirent = readdir(dir))) {
112 string path = string(_dirname) + "/" + dirent->d_name;
113 struct stat stats;
114 if (stat(path.c_str(), &stats) == 0) {
115 if (S_ISDIR(stats.st_mode)) {
116 if (type == ET_FILE_OR_DIR || type == ET_DIR)
117 return dirent->d_name;
118 }
119 else if (type == ET_FILE_OR_DIR || type == ET_FILE)
120 return dirent->d_name;
121 }
122 }
123 closedir(dir);
124 dir = 0;
125 return 0;
126 #endif
127 }
128
129
130