Subversion Repositories HomeAutomation

Rev

Rev 1602 | Rev 1675 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #ifndef TYPE_COMMON_H
  22. #define TYPE_COMMON_H
  23.  
  24. #include <iostream>
  25. #include <string>
  26. #include <vector>
  27. #include <map>
  28. #include <stdexcept>
  29.  
  30. #include <stdio.h>
  31.  
  32. #include <boost/shared_ptr.hpp>
  33. #include <boost/lexical_cast.hpp>
  34.  
  35. namespace atom {
  36. namespace common {
  37.  
  38. typedef boost::shared_ptr<unsigned char> BytePointer;
  39. typedef std::vector<std::string> StringList;
  40. typedef std::map<std::string, std::string> StringMap;
  41.  
  42. #define SWAP_BYTE_ORDER_16(x) (((x << 8) | (x >> 8)) & 0xFFFF)
  43. #define GET_HIGH_BYTE_16(x)   ((x >> 8) & 0xFF);
  44. #define GET_LOW_BYTE_16(x)    (x & 0xFF)
  45.  
  46. template <typename ElemT>
  47. struct HexTo {
  48.     ElemT value;
  49.     operator ElemT() const {return value;}
  50.     friend std::istream& operator>>(std::istream& in, HexTo& out) {
  51.         in >> std::hex >> out.value;
  52.         return in;
  53.     }
  54. };
  55.  
  56. inline unsigned int FromHex(std::string hex_chars)
  57. {
  58.     int value = 0;
  59.     int ascii_value;
  60.    
  61.     for (int n = hex_chars.length() - 1; n >= 0; n--)
  62.     {
  63.         ascii_value = (int)hex_chars.data()[n];
  64.    
  65.         if (48 <= ascii_value && ascii_value <= 57)
  66.         {
  67.             value |= (ascii_value - 48) << n;
  68.         }
  69.         else if (65 <= ascii_value && ascii_value <= 70)
  70.         {
  71.             value |= (ascii_value - 65) << n;
  72.         }
  73.         else
  74.         {
  75.             throw std::runtime_error("This is not an hex string, hex_chars = " + hex_chars + ", ascii_value = " + boost::lexical_cast<std::string>(ascii_value) + ", n = " + boost::lexical_cast<std::string>(n) + ", length = " + boost::lexical_cast<std::string>(hex_chars.length()));
  76.         }
  77.     }
  78.    
  79.     return value;
  80. }
  81.  
  82. inline std::string ToHex(unsigned int value)
  83. {
  84.     char hex_string[11];
  85.    
  86.     snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
  87.    
  88.     return std::string(hex_string);
  89.    
  90. }
  91.    
  92. }; // namespace type
  93. }; // namespace atom
  94.  
  95. #endif // TYPE_COMMON_H