Subversion Repositories HomeAutomation

Rev

Rev 1939 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1939 Rev 1987
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
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
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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
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.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include "common.h"
21
#include "common.h"
22
 
22
 
23
#include <iostream>
23
#include <iostream>
24
 
24
 
25
namespace atom {
25
namespace atom {
26
namespace common {
26
namespace common {
27
 
27
 
28
std::string PadNumber(unsigned int number, unsigned int length)
28
std::string PadNumber(unsigned int number, unsigned int length)
29
{
29
{
30
    std::string padded_string = boost::lexical_cast<std::string>(number);
30
    std::string padded_string = boost::lexical_cast<std::string>(number);
31
   
31
   
32
    while (padded_string.length() < length)
32
    while (padded_string.length() < length)
33
    {
33
    {
34
        padded_string = "0" + padded_string;
34
        padded_string = "0" + padded_string;
35
    }
35
    }
36
   
36
   
37
    return padded_string;
37
    return padded_string;
38
}
38
}
39
   
39
   
40
std::string ToHex(unsigned int value)
40
std::string ToHex(unsigned int value)
41
{
41
{
42
    char hex_string[11];
42
    char hex_string[11];
43
   
43
   
44
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
44
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
45
   
45
   
46
    return std::string(hex_string);
46
    return std::string(hex_string);
47
}
47
}
48
 
48
 
49
std::string ToHex8bit(unsigned int value)
49
std::string ToHex8bit(unsigned int value)
50
{
50
{
51
    char hex_string[3];
51
    char hex_string[3];
52
   
52
   
53
    if (value > 255)
53
    if (value > 255)
54
    {
54
    {
55
      snprintf(hex_string, sizeof(hex_string), "--");
55
      snprintf(hex_string, sizeof(hex_string), "--");
56
    }
56
    }
57
    else
57
    else
58
    {
58
    {
59
      snprintf(hex_string, sizeof(hex_string), "%02x", value);
59
      snprintf(hex_string, sizeof(hex_string), "%02x", value);
60
    }
60
    }
61
   
61
   
62
    return std::string(hex_string);
62
    return std::string(hex_string);
63
}
63
}
64
 
64
 
65
 
65
 
66
std::string ToHex4bit(unsigned int value)
66
std::string ToHex4bit(unsigned int value)
67
{
67
{
68
    char hex_string[2];
68
    char hex_string[2];
69
 
69
 
70
    if (value > 15)
70
    if (value > 15)
71
    {
71
    {
72
      snprintf(hex_string, sizeof(hex_string), "-");
72
      snprintf(hex_string, sizeof(hex_string), "-");
73
    }
73
    }
74
    else
74
    else
75
    {
75
    {
76
      snprintf(hex_string, sizeof(hex_string), "%01x", value);
76
      snprintf(hex_string, sizeof(hex_string), "%01x", value);
77
    }
77
    }
78
   
78
   
79
    return std::string(hex_string);
79
    return std::string(hex_string);
80
}
80
}
81
 
81
 
82
unsigned int FromHex(std::string hex_chars)
82
unsigned int FromHex(std::string hex_chars)
83
{
83
{
84
    return static_cast<unsigned int>(strtoll(hex_chars.data(), NULL, 16));
84
    return static_cast<unsigned int>(strtoll(hex_chars.data(), NULL, 16));
85
}
85
}
86
 
86
 
87
std::string ToHexIfNumber(std::string data)
87
std::string ToHexIfNumber(std::string data)
88
{
88
{
89
  bool is_number = true;
89
  bool is_number = true;
90
 
90
 
91
  if (data.length() == 0)
91
  if (data.length() == 0)
92
  {
92
  {
93
    return data;
93
    return data;
94
  }
94
  }
95
 
95
 
96
  for (unsigned int n = 0; n < data.length(); n++)
96
  for (unsigned int n = 0; n < data.length(); n++)
97
  {
97
  {
98
    if (!isdigit(data[n]))
98
    if (!isdigit(data[n]))
99
    {
99
    {
100
      is_number = false;
100
      is_number = false;
101
      break;
101
      break;
102
    }
102
    }
103
  }
103
  }
104
 
104
 
105
  if (is_number)
105
  if (is_number)
106
  {
106
  {
107
    char *end = NULL;
107
    char *end = NULL;
108
    long value;
108
    long value;
109
   
109
   
110
    value = strtol(data.c_str(), &end, 10);
110
    value = strtol(data.c_str(), &end, 10);
111
   
111
   
112
    return ToHex(value);
112
    return ToHex(value);
113
  }
113
  }
114
 
114
 
115
  return data;
115
  return data;
-
 
116
}
-
 
117
 
-
 
118
std::string ToHex(std::string data)
-
 
119
{
-
 
120
    std::string debug_string;
-
 
121
   
-
 
122
    for (unsigned int n = 0; n < data.size(); n++)
-
 
123
    {
-
 
124
        debug_string += ToHex((unsigned int)data[n]) + ",";
-
 
125
    }
-
 
126
   
-
 
127
    return debug_string;
116
}  
128
}
117
   
129
   
118
}; // namespace type
130
}; // namespace type
119
}; // namespace atom
131
}; // namespace atom
120
 
132