File + Directory for Windows
Stefan Schuermans

Stefan Schuermans commited on 2017-10-28 20:23:07
Showing 6 changed files, with 200 additions and 25 deletions.

... ...
@@ -1,9 +1,9 @@
1 1
 CONFIG :=
2 2
 DEFINE :=
3 3
 INCLUDE := -Icommon
4
-CFLAGS := -Wall -Wextra -Werror -O2
5
-LDFLAGS :=
6
-LIBS := -lBlinkenLib
4
+CFLAGS := -Wall -Wextra -Werror -O2 $(EXTRA_CFLAGS)
5
+LDFLAGS := $(EXTRA_LDFLAGS)
6
+LIBS := -lBlinkenLib $(EXTRA_LIBS)
7 7
 TARGET := Blinker
8 8
 ifeq ($(CROSS_WIN),1)
9 9
 CXX := x86_64-w64-mingw32-g++
... ...
@@ -0,0 +1,90 @@
1
+/* Blinker
2
+   Copyright 2011-2014 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <dirent.h>
7
+#include <string>
8
+#include <string.h>
9
+#include <sys/stat.h>
10
+#include <sys/types.h>
11
+#include <unistd.h>
12
+
13
+#include "Directory.h"
14
+#include "File.h"
15
+
16
+namespace Blinker {
17
+
18
+/**
19
+ * @brief constructor from path
20
+ * @param[in] path path to directory
21
+ */
22
+Directory::Directory(const std::string &path):
23
+  File(path)
24
+{
25
+  if (m_path.empty() || m_path.at(m_path.length() - 1) != '\\'
26
+                     || m_path.at(m_path.length() - 1) != '/')
27
+    m_path += '\\';
28
+}
29
+
30
+/**
31
+ * @brief get list of entries in directory
32
+ * @param[in] type type of directory entries to return
33
+ * @param[out] entries list of directory entries
34
+ */
35
+void Directory::getEntries(Type type, std::list<std::string> &entries) const
36
+{
37
+  entries.clear();
38
+
39
+  if (m_path.empty())
40
+    return;
41
+  DIR *dir = opendir(m_path.c_str());
42
+  if (!dir)
43
+    return;
44
+
45
+  struct dirent *dirent;
46
+  while ((dirent = readdir(dir))) {
47
+    if (strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, "..") ) {
48
+
49
+      struct stat s;
50
+      if (stat((m_path + dirent->d_name).c_str(), &s))
51
+        continue; // cannot stat -> silently ignore
52
+
53
+      bool ok = false;
54
+      switch (type) {
55
+        case TypeSubdir: ok = S_ISDIR(s.st_mode); break;
56
+        case TypeFile:   ok = S_ISREG(s.st_mode); break;
57
+      }
58
+      if (ok)
59
+        entries.push_back(dirent->d_name);
60
+
61
+    } // if ! "." && ! ".."
62
+  } // while dirent
63
+
64
+  closedir(dir);
65
+
66
+  entries.sort();
67
+}
68
+
69
+/**
70
+ * @brief get subdirectory
71
+ * @param[in] name of subdirectory
72
+ * @return subdirectory object
73
+ */
74
+Directory Directory::getSubdir(const std::string &name) const
75
+{
76
+  return Directory(m_path + name);
77
+}
78
+
79
+/**
80
+ * @brief get file in directory
81
+ * @param[in] name of file
82
+ * @return file object
83
+ */
84
+File Directory::getFile(const std::string &name) const
85
+{
86
+  return File(m_path + name);
87
+}
88
+
89
+} // namespace Blinker
90
+
... ...
@@ -0,0 +1,59 @@
1
+/* Blinker
2
+   Copyright 2011-2014 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef BLINKER_DIRECTORY_H
7
+#define BLINKER_DIRECTORY_H
8
+
9
+#include <list>
10
+#include <string>
11
+
12
+#include "File.h"
13
+
14
+namespace Blinker {
15
+
16
+/// information about a directory
17
+class Directory: public File
18
+{
19
+public:
20
+  /// type of directory entries
21
+  enum Type {
22
+    TypeSubdir, ///< subdirectory
23
+    TypeFile    ///< file
24
+  };
25
+
26
+public:
27
+  /**
28
+   * @brief constructor from path
29
+   * @param[in] path path to directory
30
+   */
31
+  Directory(const std::string &path);
32
+
33
+public:
34
+  /**
35
+   * @brief get list of entries in directory
36
+   * @param[in] type type of directory entries to return
37
+   * @param[out] entries list of directory entries
38
+   */
39
+  void getEntries(Type type, std::list<std::string> &entries) const;
40
+
41
+  /**
42
+   * @brief get subdirectory
43
+   * @param[in] name of subdirectory
44
+   * @return subdirectory object
45
+   */
46
+  Directory getSubdir(const std::string &name) const;
47
+
48
+  /**
49
+   * @brief get file in directory
50
+   * @param[in] name of file
51
+   * @return file object
52
+   */
53
+  File getFile(const std::string &name) const;
54
+}; // class Directory
55
+
56
+} // namespace Blinker
57
+
58
+#endif // #ifndef BLINKER_DIRECTORY_H
59
+
... ...
@@ -4,7 +4,9 @@
4 4
    a blinkenarea.org project */
5 5
 
6 6
 #include <string>
7
-#include <windows.h>
7
+#include <sys/stat.h>
8
+#include <sys/types.h>
9
+#include <unistd.h>
8 10
 
9 11
 #include "File.h"
10 12
 #include "Time.h"
... ...
@@ -37,27 +39,12 @@ const std::string & File::getPath()
37 39
  */
38 40
 bool File::checkModified()
39 41
 {
40
-  // get handle to file
41
-  HANDLE handle = CreateFile(m_path.c_str(), GENERIC_READ,
42
-                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
43
-                             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
44
-  if (handle == INVALID_HANDLE_VALUE) {
45
-    return false; // cannot open -> silently ignore
46
-  }
47
-
48
-  // get last write time
49
-  FILETIME wrTime;
50
-  if (! GetFileTime(handle, NULL, NULL, &wrTime)) {
51
-    CloseHandle(handle);
52
-    return false; // cannot get last write time -> silently ignore
53
-  }
54
-
55
-  // close handle
56
-  CloseHandle(handle);
57
-
58
-  // modification time is last write time
59
-  Time modifyTime;
60
-  modifyTime.fromFileTime(wrTime);
42
+  // get modification/change times
43
+  struct stat s;
44
+  if (stat(m_path.c_str(), &s))
45
+    return false; // cannot stat -> silently ignore
46
+  Time modifyTime(s.st_mtime);
47
+  // s.st_ctime not valid for FAT filesystems, so do not use it
61 48
 
62 49
   // consider file modified if modify time changed since last check
63 50
   bool mod = modifyTime > m_lastModifyTime;
... ...
@@ -0,0 +1,20 @@
1
+/* Blinker
2
+   Copyright 2011-2014 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <winsock2.h>
7
+#include <stdint.h>
8
+
9
+#include "NetHost.h"
10
+
11
+namespace Blinker {
12
+
13
+/// convert 32 bit unsigned integer from network to host byte order
14
+uint32_t Net2Host32(uint32_t val)
15
+{
16
+  return ntohl(val);
17
+}
18
+
19
+} // namespace Blinker
20
+
... ...
@@ -0,0 +1,19 @@
1
+/* Blinker
2
+   Copyright 2011-2014 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef BLINKER_NETHOST_H
7
+#define BLINKER_NETHOST_H
8
+
9
+#include <stdint.h>
10
+
11
+namespace Blinker {
12
+
13
+/// convert 32 bit unsigned integer from network to host byte order
14
+uint32_t Net2Host32(uint32_t val);
15
+
16
+} // namespace Blinker
17
+
18
+#endif // #ifndef BLINKER_NETHOST_H
19
+
0 20