From a3a6f0ba0f61ab046d1b69e60e9893e436915900 Mon Sep 17 00:00:00 2001 From: Florian Becker Date: Fri, 2 Oct 2020 23:16:36 +0200 Subject: [PATCH] Add StringExtras and KeyState class --- README.md | 4 +- examples/double/main.cpp | 2 +- source/CMakeLists.txt | 8 ++- source/{Double.cpp => DoubleExtras.cpp} | 2 +- source/{Double.h => DoubleExtras.h} | 0 source/KeyState.cpp | 70 +++++++++++++++++++++++++ source/KeyState.h | 40 ++++++++++++++ source/StringExtras.cpp | 57 ++++++++++++++++++++ source/StringExtras.h | 47 +++++++++++++++++ 9 files changed, 225 insertions(+), 5 deletions(-) rename source/{Double.cpp => DoubleExtras.cpp} (99%) rename source/{Double.h => DoubleExtras.h} (100%) create mode 100644 source/KeyState.cpp create mode 100644 source/KeyState.h create mode 100644 source/StringExtras.cpp create mode 100644 source/StringExtras.h diff --git a/README.md b/README.md index 9a72dab..6b8e2e1 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,11 @@ make -j`nproc` ## Classes - **CPU** - Get CPU informations. -- **Double** - Less, Greater, Equal, Between, Round, Split. +- **DoubleExtras** - Less, Greater, Equal, Between, Round, Split. - **Exec** - Run command and return stdout or mixed (stdout and stderr) and result code. +- **KeyState** - Check for caps lock state. - **Serial** - Serial communication class. +- **StringExtras** - LeftTrim, RightTrim, Trim. - **Timing** - Measering time, cpu and real time. ## Template classes diff --git a/examples/double/main.cpp b/examples/double/main.cpp index ea1e488..fc407f7 100644 --- a/examples/double/main.cpp +++ b/examples/double/main.cpp @@ -32,7 +32,7 @@ #include /* modern.cpp.core header */ -#include +#include int main() { diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index f65feb0..a4453db 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -34,12 +34,16 @@ add_library(${PROJECT_NAME} ../README.md CPU.cpp CPU.h - Double.cpp - Double.h + DoubleExtras.cpp + DoubleExtras.h Exec.cpp Exec.h + KeyState.cpp + KeyState.h Serial.cpp Serial.h + StringExtras.cpp + StringExtras.h Timing.cpp Timing.h templates/CSVWriter.h diff --git a/source/Double.cpp b/source/DoubleExtras.cpp similarity index 99% rename from source/Double.cpp rename to source/DoubleExtras.cpp index 52ee4c1..2a9c869 100644 --- a/source/Double.cpp +++ b/source/DoubleExtras.cpp @@ -36,7 +36,7 @@ #include /* local header */ -#include "Double.h" +#include "DoubleExtras.h" namespace vx { diff --git a/source/Double.h b/source/DoubleExtras.h similarity index 100% rename from source/Double.h rename to source/DoubleExtras.h diff --git a/source/KeyState.cpp b/source/KeyState.cpp new file mode 100644 index 0000000..c1b6631 --- /dev/null +++ b/source/KeyState.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 Florian Becker (VX APPS). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef _WIN32 + #include +#elif defined(__APPLE__) + #include +#endif + +/* local header */ +#include "KeyState.h" + +namespace vx { + + bool KeyState::isCapsLockActive() { + + bool isActive = false; + +#ifdef _WIN32 + if ( GetKeyState( VK_CAPITAL ) & 0x0001 ) { + + isActive = true; + } +#elif __APPLE__ + +#if 1 + /* Variant 1 Carbon.framework */ + KeyMap keyMap; + GetKeys( keyMap ); + + int index = kVK_CapsLock >> 5; + int shift = kVK_CapsLock & 31; + + isActive = ( ( keyMap[ index ].bigEndianValue >> shift ) & 1 ) != 0; +#else + /* Variant 2 CoreGraphics.framework */ + isActive = CGEventSourceKeyState( kCGEventSourceStateHIDSystemState, kVK_CapsLock ); +#endif + +#endif + return isActive; + } +} diff --git a/source/KeyState.h b/source/KeyState.h new file mode 100644 index 0000000..dea8992 --- /dev/null +++ b/source/KeyState.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 Florian Becker (VX APPS). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +namespace vx { + + class KeyState { + + public: + static bool isCapsLockActive(); + }; +} diff --git a/source/StringExtras.cpp b/source/StringExtras.cpp new file mode 100644 index 0000000..872abd9 --- /dev/null +++ b/source/StringExtras.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020 Florian Becker (VX APPS). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* local header */ +#include "StringExtras.h" + +namespace vx { + + constexpr auto trimmed = " \t\n\r\f\v"; + + std::string &stringRightTrim( std::string &_string, + const std::string &_trim ) { + + _string.erase( _string.find_last_not_of( _trim.empty() ? trimmed : _trim ) + 1 ); + return _string; + } + + std::string &stringLeftTrim( std::string &_string, + const std::string &_trim ) { + + _string.erase( 0, _string.find_first_not_of( _trim.empty() ? trimmed : _trim ) ); + return _string; + } + + std::string &stringTrim( std::string &_string, + const std::string &_trim ) { + + return stringLeftTrim( stringRightTrim( _string, _trim.empty() ? trimmed : _trim ), _trim.empty() ? trimmed : _trim ); + } +} diff --git a/source/StringExtras.h b/source/StringExtras.h new file mode 100644 index 0000000..c95e70b --- /dev/null +++ b/source/StringExtras.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 Florian Becker (VX APPS). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* stl header */ +#include + +namespace vx { + + // trim from end of string (right) + std::string &stringRightTrim( std::string &_string, + const std::string &_trim = {} ); + + // trim from beginning of string (left) + std::string &stringLeftTrim( std::string &_string, + const std::string &_trim = {} ); + + // trim from both ends of string (right then left) + std::string &stringTrim( std::string &_string, + const std::string &_trim = {} ); +}