00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <assert.h>
00029 #include <string.h>
00030 #include "kapi_internal.h"
00031 #include "kapi_parse.h"
00032 #include "kapi_util.h"
00033 #include "kapi_error.h"
00034
00035 int
00036 KAPI_VariableCardinality( void *pConfig, char *pchAttribute )
00037 {
00038 stn_t *pstn;
00039 knobs_t *pknobs = pConfig;
00040
00041 KAPI_error_attribute = 0;
00042 pstn = kapi_pstnLookup_noadd( pknobs, pchAttribute );
00043 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00044 KAPI_error_attribute = -1;
00045 return( -1 );
00046 }
00047
00048 if ( pstn->u.vfi.ptfi->tty == ttyARRAY ) {
00049 if ( pstn->u.vfi.ptfi->ptfiArrayIndexType->tty == ttyENUM ) {
00050 return( pstn->u.vfi.ptfi->ptfiArrayIndexType->nEnumConst );
00051 } else {
00052 return( 1 );
00053 }
00054 } else {
00055 return( 1 );
00056 }
00057 }
00058
00059 char *
00060 KAPI_GetEnumVariableName( void *pConfig, char *pchVariable, int idx )
00061 {
00062 stn_t *pstn;
00063 valhdr_t valhdr;
00064
00065 knobs_t *pknobs = pConfig;
00066
00067 KAPI_error_attribute = 0;
00068 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00069 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00070 KAPI_error_attribute = -1;
00071 return( NULL );
00072 }
00073
00074
00075 if ( ! ( pstn->u.vfi.ptfi->tty == ttyENUM
00076 ||
00077 ( pstn->u.vfi.ptfi->tty == ttyARRAY
00078 &&
00079 pstn->u.vfi.ptfi->ptfiArrayEltType->tty == ttyENUM
00080 )
00081 ) ) {
00082 KAPI_error_attribute = -1;
00083 return( NULL );
00084 }
00085
00086 kapi_LookUpVariable_valhdr( pstn, &valhdr, idx );
00087 if ( valhdr.vals == valsUNSET ) {
00088 KAPI_error_attribute = -1;
00089 return( NULL );
00090 }
00091
00092 return( kapi_pchCopy( valhdr.pvalnList->val.pch ) );
00093 }
00094
00095 int
00096 KAPI_GetEnumVariable( void *pConfig, char *pchVariable, int idx )
00097 {
00098 stn_t *pstn;
00099 valhdr_t valhdr;
00100 knobs_t *pknobs = pConfig;
00101
00102 KAPI_error_attribute = 0;
00103 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00104 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00105 KAPI_error_attribute = -1;
00106 return( -1 );
00107 }
00108
00109
00110 if ( ! ( pstn->u.vfi.ptfi->tty == ttyENUM
00111 ||
00112 ( pstn->u.vfi.ptfi->tty == ttyARRAY
00113 &&
00114 pstn->u.vfi.ptfi->ptfiArrayEltType->tty == ttyENUM
00115 )
00116 ) ) {
00117 KAPI_error_attribute = -1;
00118 return( -1 );
00119 }
00120
00121 kapi_LookUpVariable_valhdr( pstn, &valhdr, idx );
00122 if ( valhdr.vals == valsUNSET ) {
00123 KAPI_error_attribute = -1;
00124 return( -1 );
00125 }
00126
00127 return( pstnEnum2idx( kapi_pstnLookup( pknobs, valhdr.pvalnList->val.pch ) ) );
00128 }
00129
00130 int
00131 KAPI_ArrayIndex( void *pConfig, char *pchVariable, char *pchIndex )
00132 {
00133 stn_t *pstn;
00134 int iPos;
00135 knobs_t *pknobs = pConfig;
00136
00137 KAPI_error_attribute = 0;
00138 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00139 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00140 KAPI_error_attribute = -1;
00141 return( -1 );
00142 }
00143
00144 if ( pstn->u.vfi.ptfi->tty != ttyARRAY ) {
00145 KAPI_error_attribute = -1;
00146 return( -1 );
00147 }
00148
00149 iPos = idxped4pstn( pstn, pchIndex );
00150
00151 if ( iPos == -1 ) {
00152 KAPI_error_attribute = -1;
00153 return( -1 );
00154 }
00155
00156 return( iPos );
00157 }
00158
00159
00160 int
00161 KAPI_GetIntegerVariable( void *pConfig, char *pchVariable, int idx )
00162 {
00163 stn_t *pstn;
00164 valhdr_t valhdr;
00165 knobs_t *pknobs = pConfig;
00166
00167 KAPI_error_attribute = 0;
00168 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00169 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00170 KAPI_error_attribute = -1;
00171 return( -1 );
00172 }
00173
00174 if ( !( pstn->u.vfi.ptfi->tty == ttyINT
00175 ||
00176 ( pstn->u.vfi.ptfi->tty == ttyARRAY
00177 &&
00178 pstn->u.vfi.ptfi->ptfiArrayEltType->tty == ttyINT
00179 )
00180 ) ) {
00181 KAPI_error_attribute = -1;
00182 return( -1 );
00183 }
00184
00185 kapi_LookUpVariable_valhdr( pstn, &valhdr, idx );
00186 if ( valhdr.vals == valsUNSET ) {
00187 KAPI_error_attribute = -1;
00188 return( -1 );
00189 }
00190
00191 return( valhdr.pvalnList->val.i );
00192 }
00193
00194 bv_t *
00195 KAPI_GetBvVariable( void *pConfig, char *pchVariable, int idx )
00196 {
00197 stn_t *pstn;
00198 valhdr_t valhdr;
00199 knobs_t *pknobs = pConfig;
00200
00201
00202 KAPI_error_attribute = 0;
00203 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00204 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00205 KAPI_error_attribute = -1;
00206 return( NULL );
00207 }
00208
00209 if ( !( pstn->u.vfi.ptfi->tty == ttyBITMASK
00210 ||
00211 ( pstn->u.vfi.ptfi->tty == ttyARRAY
00212 &&
00213 pstn->u.vfi.ptfi->ptfiArrayEltType->tty == ttyBITMASK
00214 )
00215 ) ) {
00216 KAPI_error_attribute = -1;
00217 return( NULL );
00218 }
00219
00220 kapi_LookUpVariable_valhdr( pstn, &valhdr, idx );
00221 if ( valhdr.vals == valsUNSET ) {
00222 KAPI_error_attribute = -1;
00223 return( NULL );
00224 }
00225
00226 return( pbvBuild4valhdr( pstn, &(valhdr) ) );
00227 }
00228
00229 double
00230 KAPI_GetDoubleVariable( void *pConfig, char *pchVariable, int idx )
00231 {
00232 stn_t *pstn;
00233 valhdr_t valhdr;
00234 knobs_t *pknobs = pConfig;
00235
00236 KAPI_error_attribute = 0;
00237 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00238 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00239 KAPI_error_attribute = -1;
00240 return( -1.0 );
00241 }
00242
00243 if ( !( pstn->u.vfi.ptfi->tty == ttyREAL
00244 ||
00245 ( pstn->u.vfi.ptfi->tty == ttyARRAY
00246 &&
00247 pstn->u.vfi.ptfi->ptfiArrayEltType->tty == ttyREAL
00248 )
00249 ) ) {
00250 KAPI_error_attribute = -1;
00251 return( -1.0 );
00252 }
00253
00254 kapi_LookUpVariable_valhdr( pstn, &valhdr, idx );
00255 if ( valhdr.vals == valsUNSET ) {
00256 KAPI_error_attribute = -1;
00257 return( -1.0 );
00258 }
00259
00260 return( valhdr.pvalnList->val.r );
00261 }
00262
00263 char *
00264 KAPI_GetStringVariable( void *pConfig, char *pchVariable, int idx )
00265 {
00266 stn_t *pstn;
00267 valhdr_t valhdr;
00268
00269 knobs_t *pknobs = pConfig;
00270
00271 KAPI_error_attribute = 0;
00272 pstn = kapi_pstnLookup_noadd( pknobs, pchVariable );
00273 if ( pstn == NULL || pstn->ity != ityVARNAME ) {
00274 KAPI_error_attribute = -1;
00275 return( NULL );
00276 }
00277
00278 if ( !( pstn->u.vfi.ptfi->tty == ttySTRING
00279 ||
00280 ( pstn->u.vfi.ptfi->tty == ttyARRAY
00281 &&
00282 pstn->u.vfi.ptfi->ptfiArrayEltType->tty == ttySTRING
00283 )
00284 ) ) {
00285 KAPI_error_attribute = -1;
00286 return( NULL );
00287 }
00288
00289 kapi_LookUpVariable_valhdr( pstn, &valhdr, idx );
00290 if ( valhdr.vals == valsUNSET ) {
00291 KAPI_error_attribute = -1;
00292 return( NULL );
00293 }
00294
00295 return( kapi_pchCopy( valhdr.pvalnList->val.pch ) );
00296 }
00297
00298
00299
00300
00301 int
00302 KAPI_count4attribute( void *pConfig, char *pchAttribute )
00303 {
00304 stn_t *pstn;
00305 knobs_t *pknobs = pConfig;
00306
00307 KAPI_error_attribute = 0;
00308 pstn = kapi_pstnLookup_noadd( pknobs, pchAttribute );
00309 if ( pstn == NULL || pstn->ity != ityATTRIBUTENAME ) {
00310 KAPI_error_attribute = -1;
00311 return( 0 );
00312 }
00313
00314 return( pstn->u.afih.nAttr );
00315 }
00316
00317 char *
00318 KAPI_attribute4index( void *pConfig, char *pchAttribute, int idx )
00319 {
00320 stn_t *pstn;
00321 knobs_t *pknobs = pConfig;
00322
00323
00324
00325 assert( pknobs->fRestructuredAttributes != 0 );
00326
00327 KAPI_error_attribute = 0;
00328 pstn = kapi_pstnLookup_noadd( pknobs, pchAttribute );
00329 if ( pstn == NULL || pstn->ity != ityATTRIBUTENAME ) {
00330 KAPI_error_attribute = -1;
00331 return( NULL );
00332 }
00333
00334 if ( idx >= pstn->u.afih.nAttr ) {
00335 KAPI_error_attribute = -1;
00336 return( NULL );
00337 }
00338
00339 if (idx == 511)
00340 {
00341
00342 }
00343
00344 if ((pstn->u.afih.u.dmppch[ idx ])[0] == 0)
00345 {
00346
00347 }
00348 return( kapi_pchCopy( pstn->u.afih.u.dmppch[ idx ] ) );
00349 }
00350
00351
00352
00353 int
00354 KAPI_EnumIndex( void *pConfig, char *pchType, char *pchEnumName )
00355 {
00356 stn_t *pstnType, *pstnEnum;
00357 valn_t *pvalnEnum;
00358 int i;
00359 knobs_t *pknobs = pConfig;
00360
00361 KAPI_error_attribute = 0;
00362 pstnType = kapi_pstnLookup_noadd( pknobs, pchType );
00363 if ( pstnType == NULL || pchEnumName == NULL ) {
00364 KAPI_error_attribute = -1;
00365 return( -1 );
00366 }
00367
00368
00369 if ( pstnType == NULL || pstnType->ity != ityTYPENAME ) {
00370 KAPI_error_attribute = -1;
00371 return( -1 );
00372 }
00373
00374
00375 if ( 0 == strcmp( "port_t", pstnType->pchName ) ) {
00376 kapi_Warning(0,0,"kapi 2.x port_t compatability not supported!");
00377 return( -1 );
00378 }
00379
00380 pvalnEnum = pstnType->u.tfi.pvalnEnums;
00381 for ( i=0; i < pstnType->u.tfi.nEnumConst; i++ ) {
00382 pstnEnum = kapi_pstnLookup_noadd( pknobs, pvalnEnum->val.pch );
00383 if ( pstnEnum != NULL
00384 && 0 == strcmp( pstnEnum->pchName, pchEnumName ) ) {
00385 return( i );
00386 }
00387 pvalnEnum = pvalnEnum->pvalnNext;
00388 }
00389
00390 KAPI_error_attribute = -1;
00391 return( -1 );
00392 }
00393
00394 int
00395 KAPI_EnumCardinality( void *pConfig, char *pchType )
00396 {
00397 stn_t *pstn;
00398 knobs_t *pknobs = pConfig;
00399
00400 KAPI_error_attribute = 0;
00401 pstn = kapi_pstnLookup_noadd( pknobs, pchType );
00402 if ( pstn == NULL || pstn->ity != ityTYPENAME ) {
00403 KAPI_error_attribute = -1;
00404 return( -1 );
00405 }
00406
00407
00408 if ( 0 == strcmp( pstn->pchName, "port_t" ) ) {
00409 kapi_Warning(0,0,"kapi 2.x port_t compatability not supported!");
00410 return( -1 );
00411 }
00412
00413 return( pstn->u.tfi.nEnumConst );
00414 }
00415
00416 char *
00417 KAPI_EnumName( void *pConfig, int enumconst, char *pchType )
00418 {
00419 stn_t *pstnType;
00420
00421 knobs_t *pknobs = pConfig;
00422
00423 if ( enumconst < 0 ) {
00424 return( NULL );
00425 }
00426
00427 pstnType = kapi_pstnLookup_noadd( pknobs, pchType );
00428 if ( pstnType == NULL || pstnType->ity != ityTYPENAME ) {
00429 return( NULL );
00430 }
00431
00432
00433
00434 if ( 0 == strcmp( "port_t", pchType ) ) {
00435 kapi_Warning(0,0,"kapi 2.x port_t compatability not supported!");
00436 }
00437
00438 return( kapi_pchCopy( kapi_enumname( pConfig, &(pstnType->u.tfi), enumconst ) ) );
00439 }
00440
00441
00442
00443
00444
00445 char *
00446 kapi_enumname( knobs_t *pknobs, tfi_t *ptfi, int enumconst )
00447 {
00448 int i;
00449 stn_t *pstnEnum;
00450 valn_t *pvalnEnum;
00451
00452 pvalnEnum = ptfi->pvalnEnums;
00453 for ( i=0; i < enumconst; i++ ) {
00454 pvalnEnum = pvalnEnum->pvalnNext;
00455 }
00456
00457 if ( pvalnEnum ) {
00458 pstnEnum = kapi_pstnLookup_noadd( pknobs, pvalnEnum->val.pch );
00459 } else {
00460 pstnEnum = NULL;
00461 }
00462
00463 if ( pstnEnum ) {
00464 return( pstnEnum->pchName );
00465 } else {
00466 return( NULL );
00467 }
00468 }
00469