1 //----------------------------------------------------------------------------- 2 // 3 // Utils.h 4 // 5 // Miscellaneous helper functions 6 // 7 // Copyright (c) 2010 Mal Lansell <openzwave@lansell.org> 8 // 9 // SOFTWARE NOTICE AND LICENSE 10 // 11 // This file is part of OpenZWave. 12 // 13 // OpenZWave is free software: you can redistribute it and/or modify 14 // it under the terms of the GNU Lesser General Public License as published 15 // by the Free Software Foundation, either version 3 of the License, 16 // or (at your option) any later version. 17 // 18 // OpenZWave is distributed in the hope that it will be useful, 19 // but WITHOUT ANY WARRANTY; without even the implied warranty of 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 // GNU Lesser General Public License for more details. 22 // 23 // You should have received a copy of the GNU Lesser General Public License 24 // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>. 25 // 26 //----------------------------------------------------------------------------- 27 28 #ifndef _Utils_H 29 #define _Utils_H 30 31 #include "platform/Mutex.h" 32 #include "platform/Log.h" 33 34 #include <string> 35 #include <locale> 36 #include <algorithm> 37 #include <sstream> 38 #include <vector> 39 40 namespace OpenZWave 41 { 42 /** 43 * Convert a string to all upper-case. 44 * \param _str the string to be converted. 45 * \return the upper-case string. 46 * \see ToLower, Trim 47 */ 48 string ToUpper( string const& _str ); 49 50 /** 51 * Convert a string to all lower-case. 52 * \param _str the string to be converted. 53 * \return the lower-case string. 54 * \see ToUpper, Trim 55 */ 56 string ToLower( string const& _str ); 57 58 /** 59 * Split a String into a Vector, separated by separators 60 * \param lst the vector to store the results in 61 * \param input the input string to split 62 * \param separators a string containing a list of valid separators 63 * \param remove_empty if after splitting a string, the any of the results are a empty string, should we preserve them or not 64 */ 65 void split (std::vector<std::string>& lst, const std::string& input, const std::string& separators, bool remove_empty = true); 66 67 /** 68 * Trim Whitespace from the start and end of a string. 69 * \param s the string to trim 70 * \return the trimmed string 71 */ 72 std::string &trim ( std::string &s ); 73 74 75 void PrintHex(std::string prefix, uint8_t const *data, uint32 const length); 76 string PktToString(uint8 const *data, uint32 const length); 77 78 struct LockGuard 79 { LockGuardLockGuard80 LockGuard(Mutex* mutex) : _ref(mutex) 81 { 82 //std::cout << "Locking" << std::endl; 83 _ref->Lock(); 84 }; 85 ~LockGuardLockGuard86 ~LockGuard() 87 { 88 #if 0 89 if (_ref->IsSignalled()) 90 std::cout << "Already Unlocked" << std::endl; 91 else 92 std::cout << "Unlocking" << std::endl; 93 #endif 94 if (!_ref->IsSignalled()) 95 _ref->Unlock(); 96 } UnlockLockGuard97 void Unlock() 98 { 99 // std::cout << "Unlocking" << std::endl; 100 _ref->Unlock(); 101 } 102 private: 103 LockGuard(const LockGuard&); 104 LockGuard& operator = ( LockGuard const& ); 105 106 107 Mutex* _ref; 108 }; 109 110 111 112 } // namespace OpenZWave 113 114 #endif 115 116 117 118