|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <cassert> |
|
17 #include <string> |
|
18 #include <iostream> |
|
19 #include <sstream> |
|
20 #include <iomanip> |
|
21 |
|
22 #include "messagedefparser\integeridentifier.h" |
|
23 |
|
24 namespace Parser |
|
25 { |
|
26 |
|
27 |
|
28 bool CIntegerTypeIdentifier::ValueInRange(unsigned int aSize, int aValue) |
|
29 { |
|
30 return false; |
|
31 // TODO: This wont work - update for signed ints |
|
32 // return ((((~0L) >> (32 - aSize)) & aValue) == aValue); |
|
33 } |
|
34 |
|
35 |
|
36 bool CIntegerTypeIdentifier::ValueInRange(unsigned int aSize, unsigned int aValue) |
|
37 { |
|
38 return ((((~0UL) >> (32 - aSize)) & aValue) == aValue); |
|
39 } |
|
40 |
|
41 |
|
42 bool CIntegerTypeIdentifier::ValueInRange(int aValue) const |
|
43 { |
|
44 return CIntegerTypeIdentifier::ValueInRange(iSize << 3, aValue); |
|
45 } |
|
46 |
|
47 |
|
48 bool CIntegerTypeIdentifier::ValueInRange(unsigned int aValue) const |
|
49 { |
|
50 return CIntegerTypeIdentifier::ValueInRange(iSize << 3, aValue); |
|
51 } |
|
52 |
|
53 |
|
54 void CIntegerTypeIdentifier::Describe(const unsigned char* aData, unsigned int aLength, const void* aOptions, std::ostream& aOutput) const |
|
55 { |
|
56 #ifdef _DEBUG |
|
57 assert (aLength == iSize); |
|
58 #endif |
|
59 |
|
60 const TIntegerIdentifierOptions* options = reinterpret_cast<const TIntegerIdentifierOptions*>(aOptions); |
|
61 |
|
62 if (!options || options->iFormatAsHex) |
|
63 { |
|
64 if (iSize == 1) |
|
65 { |
|
66 aOutput << "0x" << std::noshowbase << std::setw(2) |
|
67 << std::setfill('0') << std::nouppercase << std::hex |
|
68 << (0xff & *reinterpret_cast<const unsigned int*>(aData)); |
|
69 } |
|
70 else if (iSize == 2) |
|
71 { |
|
72 aOutput << "0x" << std::noshowbase << std::setw(4) |
|
73 << std::setfill('0') << std::nouppercase << std::hex |
|
74 << (0xffff & *reinterpret_cast<const unsigned int*>(aData)); |
|
75 } |
|
76 else if (iSize == 4) |
|
77 { |
|
78 aOutput << "0x" << std::noshowbase << std::setw(8) |
|
79 << std::setfill('0') << std::nouppercase << std::hex |
|
80 << *reinterpret_cast<const unsigned int*>(aData); |
|
81 } |
|
82 } |
|
83 else if (iSigned) |
|
84 { |
|
85 // Signed and format as decimal |
|
86 int v; |
|
87 if (iSize == 1) |
|
88 { |
|
89 char n = (char)(*aData); |
|
90 v = n; |
|
91 } |
|
92 else if (iSize == 2) |
|
93 { |
|
94 short int n = *reinterpret_cast<const short int*>(aData); |
|
95 v = n; |
|
96 } |
|
97 else if (iSize == 4) |
|
98 { |
|
99 v = *reinterpret_cast<const int*>(aData); |
|
100 } |
|
101 aOutput << std::noshowbase << std::dec << v; |
|
102 } |
|
103 else |
|
104 { |
|
105 // Unsigned and format as decimal |
|
106 unsigned int v; |
|
107 if (iSize == 1) |
|
108 { |
|
109 unsigned char n = (unsigned char)(*aData); |
|
110 v = n; |
|
111 } |
|
112 else if (iSize == 2) |
|
113 { |
|
114 unsigned short int n = *reinterpret_cast<const unsigned short int*>(aData); |
|
115 v = n; |
|
116 } |
|
117 else if (iSize == 4) |
|
118 { |
|
119 v = *reinterpret_cast<const unsigned int*>(aData); |
|
120 } |
|
121 aOutput << std::noshowbase << std::dec << v; |
|
122 } |
|
123 } |
|
124 |
|
125 |
|
126 } // namespace Parser |
|
127 |