Stefan Schuermans commited on 2017-09-24 11:03:40
Showing 4 changed files, with 459 additions and 0 deletions.
| ... | ... |
@@ -0,0 +1,72 @@ |
| 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 <string> |
|
| 7 |
+#include <windows.h> |
|
| 8 |
+ |
|
| 9 |
+#include "File.h" |
|
| 10 |
+#include "Time.h" |
|
| 11 |
+ |
|
| 12 |
+namespace Blinker {
|
|
| 13 |
+ |
|
| 14 |
+/** |
|
| 15 |
+ * @brief constructor from path |
|
| 16 |
+ * @param[in] path path to file |
|
| 17 |
+ */ |
|
| 18 |
+File::File(const std::string &path): |
|
| 19 |
+ m_path(path) |
|
| 20 |
+{
|
|
| 21 |
+ // get modification time |
|
| 22 |
+ checkModified(); |
|
| 23 |
+} |
|
| 24 |
+ |
|
| 25 |
+/** |
|
| 26 |
+ * @brief get path to file |
|
| 27 |
+ * @return path to file |
|
| 28 |
+ */ |
|
| 29 |
+const std::string & File::getPath() |
|
| 30 |
+{
|
|
| 31 |
+ return m_path; |
|
| 32 |
+} |
|
| 33 |
+ |
|
| 34 |
+/** |
|
| 35 |
+ * @brief check if file has been modified |
|
| 36 |
+ * @return if file has been modified since last check |
|
| 37 |
+ */ |
|
| 38 |
+bool File::checkModified() |
|
| 39 |
+{
|
|
| 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); |
|
| 61 |
+ |
|
| 62 |
+ // consider file modified if modify time changed since last check |
|
| 63 |
+ bool mod = modifyTime > m_lastModifyTime; |
|
| 64 |
+ |
|
| 65 |
+ // remember new modify time |
|
| 66 |
+ m_lastModifyTime = modifyTime; |
|
| 67 |
+ |
|
| 68 |
+ return mod; |
|
| 69 |
+} |
|
| 70 |
+ |
|
| 71 |
+} // namespace Blinker |
|
| 72 |
+ |
| ... | ... |
@@ -0,0 +1,46 @@ |
| 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_FILE_H |
|
| 7 |
+#define BLINKER_FILE_H |
|
| 8 |
+ |
|
| 9 |
+#include <string> |
|
| 10 |
+ |
|
| 11 |
+#include "Time.h" |
|
| 12 |
+ |
|
| 13 |
+namespace Blinker {
|
|
| 14 |
+ |
|
| 15 |
+/// information about a file |
|
| 16 |
+class File |
|
| 17 |
+{
|
|
| 18 |
+public: |
|
| 19 |
+ /** |
|
| 20 |
+ * @brief constructor from path |
|
| 21 |
+ * @param[in] path path to file |
|
| 22 |
+ */ |
|
| 23 |
+ File(const std::string &path); |
|
| 24 |
+ |
|
| 25 |
+public: |
|
| 26 |
+ /** |
|
| 27 |
+ * @brief get path to file |
|
| 28 |
+ * @return path to file |
|
| 29 |
+ */ |
|
| 30 |
+ const std::string & getPath(); |
|
| 31 |
+ |
|
| 32 |
+ /** |
|
| 33 |
+ * @brief check if file has been modified |
|
| 34 |
+ * @return if file has been modified since last check |
|
| 35 |
+ */ |
|
| 36 |
+ bool checkModified(); |
|
| 37 |
+ |
|
| 38 |
+protected: |
|
| 39 |
+ std::string m_path; ///< path to file |
|
| 40 |
+ Time m_lastModifyTime; ///< last known modification time was checked |
|
| 41 |
+}; // class File |
|
| 42 |
+ |
|
| 43 |
+} // namespace Blinker |
|
| 44 |
+ |
|
| 45 |
+#endif // #ifndef BLINKER_FILE_H |
|
| 46 |
+ |
| ... | ... |
@@ -0,0 +1,234 @@ |
| 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 <math.h> |
|
| 7 |
+#include <stdint.h> |
|
| 8 |
+#include <windows.h> |
|
| 9 |
+ |
|
| 10 |
+#include "Time.h" |
|
| 11 |
+ |
|
| 12 |
+namespace Blinker {
|
|
| 13 |
+ |
|
| 14 |
+const Time Time::zero(0); ///< zero time |
|
| 15 |
+ |
|
| 16 |
+/** |
|
| 17 |
+ * @brief get current time |
|
| 18 |
+ * @return current time |
|
| 19 |
+ */ |
|
| 20 |
+Time Time::now() |
|
| 21 |
+{
|
|
| 22 |
+ Time now; |
|
| 23 |
+ |
|
| 24 |
+ SYSTEMTIME st; |
|
| 25 |
+ GetSystemTime(&st); |
|
| 26 |
+ FILETIME ft; |
|
| 27 |
+ SystemTimeToFileTime(&st, &ft); |
|
| 28 |
+ now.fromFileTime(ft); |
|
| 29 |
+ |
|
| 30 |
+ return now; |
|
| 31 |
+} |
|
| 32 |
+ |
|
| 33 |
+/// constructor |
|
| 34 |
+Time::Time(): |
|
| 35 |
+ m_sec(0), |
|
| 36 |
+ m_ns(0) |
|
| 37 |
+{
|
|
| 38 |
+} |
|
| 39 |
+ |
|
| 40 |
+/** |
|
| 41 |
+ * @brief constructor from seconds |
|
| 42 |
+ * @param[in] t time in seconds |
|
| 43 |
+ */ |
|
| 44 |
+Time::Time(time_t t): |
|
| 45 |
+ m_sec(t), |
|
| 46 |
+ m_ns(0) |
|
| 47 |
+{
|
|
| 48 |
+} |
|
| 49 |
+ |
|
| 50 |
+/// comparison |
|
| 51 |
+//@{
|
|
| 52 |
+ |
|
| 53 |
+int Time::compare(const Time &that) const |
|
| 54 |
+{
|
|
| 55 |
+ if (m_sec < that.m_sec) |
|
| 56 |
+ return -1; |
|
| 57 |
+ if (m_sec > that.m_sec) |
|
| 58 |
+ return 1; |
|
| 59 |
+ if (m_ns < that.m_ns) |
|
| 60 |
+ return -1; |
|
| 61 |
+ if (m_ns > that.m_ns) |
|
| 62 |
+ return 1; |
|
| 63 |
+ return 0; |
|
| 64 |
+} |
|
| 65 |
+ |
|
| 66 |
+bool Time::operator==(const Time &that) const |
|
| 67 |
+{
|
|
| 68 |
+ return compare(that) == 0; |
|
| 69 |
+} |
|
| 70 |
+ |
|
| 71 |
+bool Time::operator!=(const Time &that) const |
|
| 72 |
+{
|
|
| 73 |
+ return compare(that) != 0; |
|
| 74 |
+} |
|
| 75 |
+ |
|
| 76 |
+bool Time::operator<(const Time &that) const |
|
| 77 |
+{
|
|
| 78 |
+ return compare(that) < 0; |
|
| 79 |
+} |
|
| 80 |
+ |
|
| 81 |
+bool Time::operator>(const Time &that) const |
|
| 82 |
+{
|
|
| 83 |
+ return compare(that) > 0; |
|
| 84 |
+} |
|
| 85 |
+ |
|
| 86 |
+bool Time::operator<=(const Time &that) const |
|
| 87 |
+{
|
|
| 88 |
+ return compare(that) <= 0; |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 91 |
+bool Time::operator>=(const Time &that) const |
|
| 92 |
+{
|
|
| 93 |
+ return compare(that) >= 0; |
|
| 94 |
+} |
|
| 95 |
+ |
|
| 96 |
+//@} |
|
| 97 |
+ |
|
| 98 |
+/// arithmetic |
|
| 99 |
+//@{
|
|
| 100 |
+ |
|
| 101 |
+const Time & Time::operator+=(const Time &that) |
|
| 102 |
+{
|
|
| 103 |
+ m_sec += that.m_sec; |
|
| 104 |
+ m_ns += that.m_ns; |
|
| 105 |
+ fix(); |
|
| 106 |
+ return *this; |
|
| 107 |
+} |
|
| 108 |
+ |
|
| 109 |
+const Time & Time::operator-=(const Time &that) |
|
| 110 |
+{
|
|
| 111 |
+ m_sec -= that.m_sec; |
|
| 112 |
+ m_ns -= that.m_ns; |
|
| 113 |
+ fix(); |
|
| 114 |
+ return *this; |
|
| 115 |
+} |
|
| 116 |
+ |
|
| 117 |
+Time Time::operator+(const Time &that) const |
|
| 118 |
+{
|
|
| 119 |
+ Time result(*this); |
|
| 120 |
+ result += that; |
|
| 121 |
+ return result; |
|
| 122 |
+} |
|
| 123 |
+ |
|
| 124 |
+Time Time::operator-(const Time &that) const |
|
| 125 |
+{
|
|
| 126 |
+ Time result(*this); |
|
| 127 |
+ result -= that; |
|
| 128 |
+ return result; |
|
| 129 |
+} |
|
| 130 |
+ |
|
| 131 |
+//@} |
|
| 132 |
+ |
|
| 133 |
+/** |
|
| 134 |
+ * @brief convert from floating point seconds |
|
| 135 |
+ * @param[in] s time in seconds |
|
| 136 |
+ */ |
|
| 137 |
+void Time::fromFloatSec(float s) |
|
| 138 |
+{
|
|
| 139 |
+ m_sec = (int64_t)truncf(s); |
|
| 140 |
+ m_ns = (int64_t)((s - m_sec) * 1.0e9); |
|
| 141 |
+} |
|
| 142 |
+ |
|
| 143 |
+/** |
|
| 144 |
+ * @brief convert from milliseconds |
|
| 145 |
+ * @param[in] ms milliseconds |
|
| 146 |
+ */ |
|
| 147 |
+void Time::fromMs(int ms) |
|
| 148 |
+{
|
|
| 149 |
+ if (ms >= 0) {
|
|
| 150 |
+ m_sec = ms / 1000; |
|
| 151 |
+ m_ns = (ms % 1000) * 1000000; |
|
| 152 |
+ } else {
|
|
| 153 |
+ m_sec = -(-ms / 1000); |
|
| 154 |
+ m_ns = -(-ms % 1000) * 1000000; |
|
| 155 |
+ } |
|
| 156 |
+} |
|
| 157 |
+ |
|
| 158 |
+/** |
|
| 159 |
+ * @brief convert to seconds |
|
| 160 |
+ * @return seconds |
|
| 161 |
+ */ |
|
| 162 |
+time_t Time::toSec() const |
|
| 163 |
+{
|
|
| 164 |
+ if (m_ns >= 500000000) |
|
| 165 |
+ return m_sec + 1; |
|
| 166 |
+ else if (m_ns <= -500000000) |
|
| 167 |
+ return m_sec - 1; |
|
| 168 |
+ else |
|
| 169 |
+ return m_sec; |
|
| 170 |
+} |
|
| 171 |
+ |
|
| 172 |
+/** |
|
| 173 |
+ * @brief convert to floating point seconds |
|
| 174 |
+ * @return time in seconds |
|
| 175 |
+ */ |
|
| 176 |
+float Time::toFloatSec() const |
|
| 177 |
+{
|
|
| 178 |
+ return m_sec + m_ns * 1.0e-9f; |
|
| 179 |
+} |
|
| 180 |
+ |
|
| 181 |
+/// fix internal time representation after calculation |
|
| 182 |
+void Time::fix() |
|
| 183 |
+{
|
|
| 184 |
+ if (m_ns >= 1000000000) {
|
|
| 185 |
+ m_sec += m_ns / 1000000000; |
|
| 186 |
+ m_ns = m_ns % 1000000000; |
|
| 187 |
+ } else if (m_ns <= -1000000000) {
|
|
| 188 |
+ m_sec -= -m_ns / 1000000000; |
|
| 189 |
+ m_ns = -(-m_ns % 1000000000); |
|
| 190 |
+ } |
|
| 191 |
+ if (m_sec > 0 && m_ns < 0) {
|
|
| 192 |
+ m_sec -= 1; |
|
| 193 |
+ m_ns += 1000000000; |
|
| 194 |
+ } else if (m_sec < 0 && m_ns > 0) {
|
|
| 195 |
+ m_sec += 1; |
|
| 196 |
+ m_ns -= 1000000000; |
|
| 197 |
+ } |
|
| 198 |
+} |
|
| 199 |
+ |
|
| 200 |
+/** |
|
| 201 |
+ * @brief convert from file time strcuture |
|
| 202 |
+ * @param[in] ft file time structure |
|
| 203 |
+ */ |
|
| 204 |
+void Time::fromFileTime(FILETIME const &ft) |
|
| 205 |
+{
|
|
| 206 |
+ union {
|
|
| 207 |
+ FILETIME ft; |
|
| 208 |
+ ULARGE_INTEGER u; |
|
| 209 |
+ } ft_u; |
|
| 210 |
+ ft_u.ft = ft; |
|
| 211 |
+ m_sec = ft_u.u.QuadPart / 10000000ULL; |
|
| 212 |
+ m_ns = (ft_u.u.QuadPart % 10000000ULL) * 100ULL; |
|
| 213 |
+} |
|
| 214 |
+ |
|
| 215 |
+/// sleep for duration |
|
| 216 |
+void Time::sleepFor() const |
|
| 217 |
+{
|
|
| 218 |
+ // do not sleep for negative time |
|
| 219 |
+ if (*this < zero) |
|
| 220 |
+ return; |
|
| 221 |
+ |
|
| 222 |
+ // sleep |
|
| 223 |
+ DWORD ms = m_ns / 1000000ULL + m_sec * 1000; |
|
| 224 |
+ Sleep(ms); |
|
| 225 |
+} |
|
| 226 |
+ |
|
| 227 |
+/// sleep until time |
|
| 228 |
+void Time::sleepUntil() const |
|
| 229 |
+{
|
|
| 230 |
+ (*this - now()).sleepFor(); |
|
| 231 |
+} |
|
| 232 |
+ |
|
| 233 |
+} // namespace Blinker |
|
| 234 |
+ |
| ... | ... |
@@ -0,0 +1,107 @@ |
| 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_TIME_H |
|
| 7 |
+#define BLINKER_TIME_H |
|
| 8 |
+ |
|
| 9 |
+#include <windows.h> |
|
| 10 |
+#include <stdint.h> |
|
| 11 |
+ |
|
| 12 |
+namespace Blinker {
|
|
| 13 |
+ |
|
| 14 |
+/// time, either point in time or duration |
|
| 15 |
+class Time |
|
| 16 |
+{
|
|
| 17 |
+public: |
|
| 18 |
+ static const Time zero; ///< zero time |
|
| 19 |
+ |
|
| 20 |
+public: |
|
| 21 |
+ /** |
|
| 22 |
+ * @brief get current time |
|
| 23 |
+ * @return current time |
|
| 24 |
+ */ |
|
| 25 |
+ static Time now(); |
|
| 26 |
+ |
|
| 27 |
+public: |
|
| 28 |
+ /// constructor |
|
| 29 |
+ Time(); |
|
| 30 |
+ |
|
| 31 |
+ /** |
|
| 32 |
+ * @brief constructor from seconds |
|
| 33 |
+ * @param[in] t time in seconds |
|
| 34 |
+ */ |
|
| 35 |
+ Time(time_t t); |
|
| 36 |
+ |
|
| 37 |
+public: |
|
| 38 |
+ /// comparison |
|
| 39 |
+ //@{
|
|
| 40 |
+ int compare(const Time &that) const; |
|
| 41 |
+ bool operator==(const Time &that) const; |
|
| 42 |
+ bool operator!=(const Time &that) const; |
|
| 43 |
+ bool operator<(const Time &that) const; |
|
| 44 |
+ bool operator>(const Time &that) const; |
|
| 45 |
+ bool operator<=(const Time &that) const; |
|
| 46 |
+ bool operator>=(const Time &that) const; |
|
| 47 |
+ //@} |
|
| 48 |
+ |
|
| 49 |
+ /// arithmetic |
|
| 50 |
+ //@{
|
|
| 51 |
+ const Time & operator+=(const Time &that); |
|
| 52 |
+ const Time & operator-=(const Time &that); |
|
| 53 |
+ Time operator+(const Time &that) const; |
|
| 54 |
+ Time operator-(const Time &that) const; |
|
| 55 |
+ //@} |
|
| 56 |
+ |
|
| 57 |
+ /** |
|
| 58 |
+ * @brief convert from floating point seconds |
|
| 59 |
+ * @param[in] s time in seconds |
|
| 60 |
+ */ |
|
| 61 |
+ void fromFloatSec(float s); |
|
| 62 |
+ |
|
| 63 |
+ /** |
|
| 64 |
+ * @brief convert from milliseconds |
|
| 65 |
+ * @param[in] ms milliseconds |
|
| 66 |
+ */ |
|
| 67 |
+ void fromMs(int ms); |
|
| 68 |
+ |
|
| 69 |
+ /** |
|
| 70 |
+ * @brief convert to seconds |
|
| 71 |
+ * @return seconds |
|
| 72 |
+ */ |
|
| 73 |
+ time_t toSec() const; |
|
| 74 |
+ |
|
| 75 |
+ /** |
|
| 76 |
+ * @brief convert to floating point seconds |
|
| 77 |
+ * @return time in seconds |
|
| 78 |
+ */ |
|
| 79 |
+ float toFloatSec() const; |
|
| 80 |
+ |
|
| 81 |
+ /** |
|
| 82 |
+ * @brief convert from file time strcuture |
|
| 83 |
+ * @param[in] ft file time structure |
|
| 84 |
+ */ |
|
| 85 |
+ void fromFileTime(FILETIME const &ft); |
|
| 86 |
+ |
|
| 87 |
+public: |
|
| 88 |
+ /// sleep for duration |
|
| 89 |
+ void sleepFor() const; |
|
| 90 |
+ |
|
| 91 |
+ /// sleep until time |
|
| 92 |
+ void sleepUntil() const; |
|
| 93 |
+ |
|
| 94 |
+protected: |
|
| 95 |
+ /// fix internal time representation after calculation |
|
| 96 |
+ void fix(); |
|
| 97 |
+ |
|
| 98 |
+protected: |
|
| 99 |
+public: |
|
| 100 |
+ int64_t m_sec; ///< seconds |
|
| 101 |
+ int64_t m_ns; ///< nanoseconds |
|
| 102 |
+}; // class Time |
|
| 103 |
+ |
|
| 104 |
+} // namespace Blinker |
|
| 105 |
+ |
|
| 106 |
+#endif // #ifndef BLINKER_TIME_H |
|
| 107 |
+ |
|
| 0 | 108 |