0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* Filename: UCCS_CDataField.cpp
|
|
16 |
* System Includes
|
|
17 |
*
|
|
18 |
*/
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
#include <string.h>
|
|
23 |
#include <stdlib.h>
|
|
24 |
#include <stdio.h>
|
|
25 |
#include <assert.h>
|
|
26 |
|
|
27 |
/***********************************************************************************
|
|
28 |
*
|
|
29 |
* Local Includes
|
|
30 |
*
|
|
31 |
**********************************************************************************/
|
|
32 |
#include "UCCS_CDataField.h"
|
|
33 |
|
|
34 |
/***********************************************************************************
|
|
35 |
*
|
|
36 |
* Definitions
|
|
37 |
*
|
|
38 |
**********************************************************************************/
|
|
39 |
#define MAXINTEGERSTRINGLEN 16
|
|
40 |
|
|
41 |
|
|
42 |
/***********************************************************************************
|
|
43 |
*
|
|
44 |
* Constructor
|
|
45 |
*
|
|
46 |
**********************************************************************************/
|
|
47 |
CDataField::CDataField( char* aName )
|
|
48 |
{
|
|
49 |
// check the params
|
|
50 |
assert( aName != NULL );
|
|
51 |
assert( strlen(aName) < MAXNAMELENGTH );
|
|
52 |
|
|
53 |
// initialise all state vars
|
|
54 |
strcpy( iSelfObjectName, aName );
|
|
55 |
iFieldValue = NULL;
|
|
56 |
iFieldValueLength = 0;
|
|
57 |
iType = UDF_UNDEFINED;
|
|
58 |
}
|
|
59 |
|
|
60 |
|
|
61 |
/***********************************************************************************
|
|
62 |
*
|
|
63 |
* Destructor
|
|
64 |
*
|
|
65 |
**********************************************************************************/
|
|
66 |
CDataField::~CDataField()
|
|
67 |
{
|
|
68 |
// free any remaining value memory
|
|
69 |
if( iFieldValue != NULL )
|
|
70 |
{
|
|
71 |
free( iFieldValue );
|
|
72 |
iFieldValue = NULL;
|
|
73 |
}
|
|
74 |
|
|
75 |
iSelfObjectName[0] = 0;
|
|
76 |
iFieldValueLength = 0;
|
|
77 |
iType = UDF_UNDEFINED;
|
|
78 |
}
|
|
79 |
|
|
80 |
//----------------------------------------------------------------------------------
|
|
81 |
// Retrieves the item name
|
|
82 |
char* CDataField::GetName()
|
|
83 |
{
|
|
84 |
return iSelfObjectName;
|
|
85 |
}
|
|
86 |
|
|
87 |
//----------------------------------------------------------------------------------
|
|
88 |
// Returns boolean for whether the passed string matches the name of the field
|
|
89 |
int CDataField::IsItem(char* anItemName)
|
|
90 |
{
|
|
91 |
int difference = 0;
|
|
92 |
|
|
93 |
// check params
|
|
94 |
assert( anItemName != NULL );
|
|
95 |
|
|
96 |
// compare string
|
|
97 |
difference = strncmp( anItemName, iSelfObjectName, MAXNAMELENGTH );
|
|
98 |
return ((difference == 0) ? 1 : 0);
|
|
99 |
}
|
|
100 |
|
|
101 |
//----------------------------------------------------------------------------------
|
|
102 |
// Set string value
|
|
103 |
void CDataField::SetValue(char* aValue)
|
|
104 |
{
|
|
105 |
// first free previous value
|
|
106 |
if(iFieldValue != NULL)
|
|
107 |
{
|
|
108 |
free(iFieldValue);
|
|
109 |
iFieldValueLength = 0;
|
|
110 |
}
|
|
111 |
|
|
112 |
// set the type
|
|
113 |
iType = UDF_STRING;
|
|
114 |
|
|
115 |
// if the value is null then we still want an empty string -- otherwise just copy the string
|
|
116 |
if( aValue == NULL ) {
|
|
117 |
iFieldValue = malloc( 1 );
|
|
118 |
((char*)iFieldValue)[0] = 0;
|
|
119 |
iFieldValueLength = 1;
|
|
120 |
} else {
|
|
121 |
iFieldValue = malloc(strlen(aValue) + 1 );
|
|
122 |
strcpy((char*)iFieldValue, aValue);
|
|
123 |
iFieldValueLength = strlen((char*)iFieldValue) + 1;
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
//----------------------------------------------------------------------------------
|
|
128 |
// Sets integer value
|
|
129 |
void CDataField::SetValue(int aValue)
|
|
130 |
{
|
|
131 |
// first free previous value
|
|
132 |
if(iFieldValue != NULL)
|
|
133 |
{
|
|
134 |
free(iFieldValue);
|
|
135 |
iFieldValueLength = 0;
|
|
136 |
}
|
|
137 |
|
|
138 |
// now copy new value
|
|
139 |
iType = UDF_STRING;
|
|
140 |
iFieldValue = malloc( MAXINTEGERSTRINGLEN + 1 );
|
|
141 |
sprintf((char*)iFieldValue, "%d", aValue);
|
|
142 |
iFieldValueLength = (MAXINTEGERSTRINGLEN + 1);
|
|
143 |
}
|
|
144 |
|
|
145 |
//----------------------------------------------------------------------------------
|
|
146 |
// Sets buffer value
|
|
147 |
void CDataField::SetValue(void *aValue, int aLen)
|
|
148 |
{
|
|
149 |
// first free previous value
|
|
150 |
if(iFieldValue != NULL)
|
|
151 |
{
|
|
152 |
free(iFieldValue);
|
|
153 |
iFieldValueLength = 0;
|
|
154 |
}
|
|
155 |
|
|
156 |
// now copy new value
|
|
157 |
iType = UDF_BUFFER;
|
|
158 |
iFieldValue = malloc(aLen);
|
|
159 |
memcpy(iFieldValue, aValue, aLen);
|
|
160 |
iFieldValueLength = aLen;
|
|
161 |
}
|
|
162 |
|
|
163 |
//----------------------------------------------------------------------------------
|
|
164 |
// Returns the string value from the object (could contain anything)
|
|
165 |
char* CDataField::GetStrValue()
|
|
166 |
{
|
|
167 |
return (char*)iFieldValue;
|
|
168 |
}
|
|
169 |
|
|
170 |
//----------------------------------------------------------------------------------
|
|
171 |
// Returns an integer value from the object
|
|
172 |
int CDataField::GetIntValue()
|
|
173 |
{
|
|
174 |
int intvalue = 0;
|
|
175 |
|
|
176 |
if( iFieldValue != NULL )
|
|
177 |
{
|
|
178 |
intvalue = atoi((char*)iFieldValue);
|
|
179 |
}
|
|
180 |
return intvalue;
|
|
181 |
}
|
|
182 |
|
|
183 |
//----------------------------------------------------------------------------------
|
|
184 |
// Returns a value from the buffer
|
|
185 |
void* CDataField::GetBufValue( int *aLen )
|
|
186 |
{
|
|
187 |
assert( aLen != NULL );
|
|
188 |
*aLen = iFieldValueLength;
|
|
189 |
return iFieldValue;
|
|
190 |
}
|
|
191 |
|
|
192 |
//----------------------------------------------------------------------------------
|
|
193 |
// Sets the type of the item (i.e. command, error code etc)
|
|
194 |
void CDataField::SetType( TFieldType aType )
|
|
195 |
{
|
|
196 |
iType = aType;
|
|
197 |
}
|
|
198 |
|
|
199 |
//----------------------------------------------------------------------------------
|
|
200 |
//returns the type
|
|
201 |
TFieldType CDataField::GetType()
|
|
202 |
{
|
|
203 |
return iType;
|
|
204 |
}
|
|
205 |
|
|
206 |
//----------------------------------------------------------------------------------
|
|
207 |
void CDataField::PrintField( FILE *aFile )
|
|
208 |
{
|
|
209 |
if( iType == UDF_STRING ) {
|
|
210 |
fprintf( aFile, "%s: %s\n", iSelfObjectName, iFieldValue );
|
|
211 |
} else if( iType == UDF_BUFFER ) {
|
|
212 |
fprintf( aFile, "%s: %d byte buffer\n", iSelfObjectName, iFieldValueLength );
|
|
213 |
} else {
|
|
214 |
fprintf( aFile, "%s: unknown contents\n", iSelfObjectName );
|
|
215 |
}
|
|
216 |
}
|