📄 propman.c
字号:
{ return propGetDoubleAtIndex(key, 0, dft);}/* set a double value for the specified key */utBool propSetDouble(Key_t key, double val){ return propSetDoubleAtIndex(key, 0, val);}/* add a double value to the specified key */utBool propAddDouble(Key_t key, double val){ return propAddDoubleAtIndex(key, 0, &val);}// ----------------------------------------------------------------------------/* get a null-terminated string value for the KeyValue entry */static utBool _propGetStringValue(KeyValue_t *kv, const char *(*val)){ if (!kv || !val) { // no place to get value return utFalse; } else if (KVT_TYPE(kv->type) == KVT_STRING) { KeyData_t *keyDataBuf = _propGetData(kv); //UInt16 keyDataSiz = _propGetDataCapacity(kv); *val = (char*)(keyDataBuf->b); // assume already terminated return utTrue; } else { // unsupported type return utFalse; }}/* set a null-terminated string value for the KeyValue entry */static utBool _propSetStringValue(KeyValue_t *kv, const char *(*val)){ if (!kv || !val) { // no specified value return utFalse; } else if (KVT_TYPE(kv->type) == KVT_STRING) { KeyData_t *keyDataBuf = _propGetData(kv); UInt16 keyDataSiz = _propGetDataCapacity(kv); if (keyDataSiz > 0) { int len = strlen(*val); if (len > (keyDataSiz - 1)) { len = (keyDataSiz - 1); } if ((char*)keyDataBuf->b != *val) { strncpy((char*)keyDataBuf->b, *val, len); } keyDataBuf->b[len] = 0; // terminate kv->lenNdx = 1; // a single string is defined kv->dataSize = len; // not including terminating null kv->attr |= KVA_NONDEFAULT; kv->attr |= KVA_CHANGED; return utTrue; } else { // insufficient size return utFalse; } } else { // unsupported type return utFalse; }}/* get a null-terminated string value for the specified key */const char *propGetString(Key_t key, const char *dft){ KeyValue_t *kv = propGetKeyValueEntry(key); if (!kv) { return dft; } else { const char *valChar = dft; PROP_LOCK { _propRefresh(PROP_REFRESH_GET, kv, (UInt8*)0, 0); // no args if (kv->lenNdx <= 0) { valChar = dft; } else { const char *v; utBool ok = _propGetStringValue(kv, &v); valChar = ok? v : dft; } } PROP_UNLOCK return valChar; }}utBool propSetString(Key_t key, const char *val){ KeyValue_t *kv = propGetKeyValueEntry(key); if (!kv) { return utFalse; } else { utBool ok = utFalse; PROP_LOCK { ok = _propSetStringValue(kv, &val); if (ok) { _propRefresh(PROP_REFRESH_SET, kv, (UInt8*)0, 0); } } PROP_UNLOCK return ok; }}// ----------------------------------------------------------------------------/* get a null-terminated string value for the KeyValue entry */static utBool _propGetBinaryValue(KeyValue_t *kv, const UInt8 *(*val), UInt16 *maxLen, UInt16 *dtaLen){ if (!kv || !val || !maxLen || !dtaLen) { // no place to get value return utFalse; } else if (KVT_TYPE(kv->type) == KVT_BINARY) { KeyData_t *keyDataBuf = _propGetData(kv); UInt16 keyDataSiz = _propGetDataCapacity(kv); *val = keyDataBuf->b; *maxLen = keyDataSiz; *dtaLen = kv->dataSize; return utTrue; } else { // unsupported type return utFalse; }}/* set a null-terminated string value for the KeyValue entry */static utBool _propSetBinaryValue(KeyValue_t *kv, const UInt8 *(*val), UInt16 dtaLen){ if (!kv || !val) { // no specified value return utFalse; } else if (KVT_TYPE(kv->type) == KVT_BINARY) { KeyData_t *keyDataBuf = _propGetData(kv); UInt16 keyDataSiz = _propGetDataCapacity(kv); if (keyDataSiz > 0) { int len = (dtaLen < keyDataSiz)? dtaLen : keyDataSiz; if (keyDataBuf->b != *val) { memcpy(keyDataBuf->b, *val, len); } kv->lenNdx = len; kv->dataSize = len; kv->attr |= KVA_NONDEFAULT; kv->attr |= KVA_CHANGED; return utTrue; } else { // insufficient size return utFalse; } } else { // unsupported type return utFalse; }}/* get a binary value for the specified key */const UInt8 *propGetBinary(Key_t key, const UInt8 *dft, UInt16 *maxLen, UInt16 *dtaLen){ KeyValue_t *kv = propGetKeyValueEntry(key); if (!kv) { // maxLen. dtaLen left as-is return dft; } else { const UInt8 *valChar = dft; PROP_LOCK { _propRefresh(PROP_REFRESH_GET, kv, (UInt8*)0, 0); // args? const UInt8 *v = (UInt8*)0; UInt16 max = 0, dta = 0; utBool ok = _propGetBinaryValue(kv, &v, &max, &dta); valChar = ok? v : dft; if (maxLen) { *maxLen = ok? max : 0; } if (dtaLen) { *dtaLen = ok? dta : 0; } } PROP_UNLOCK return valChar; }}/* set a binary value */utBool propSetBinary(Key_t key, const UInt8 *val, UInt16 dtaLen){ KeyValue_t *kv = propGetKeyValueEntry(key); if (!kv) { return utFalse; } else { utBool ok = utFalse; PROP_LOCK { ok = _propSetBinaryValue(kv, &val, dtaLen); if (ok) { _propRefresh(PROP_REFRESH_SET, kv, (UInt8*)0, 0); } } PROP_UNLOCK return ok; }}// ----------------------------------------------------------------------------/* get a GPSOdometer_t value for the KeyValue entry */static utBool _propGetGPSValue(KeyValue_t *kv, const GPSOdometer_t *(*gps)){ if (!kv || !gps) { // no place to get value return utFalse; } else if (KVT_TYPE(kv->type) == KVT_GPS) { KeyData_t *keyDataBuf = _propGetData(kv); UInt16 keyDataSiz = _propGetDataCapacity(kv); if (keyDataSiz >= sizeof(GPSOdometer_t)) { *gps = &(keyDataBuf->gps); return utTrue; } else { // insufficient size return utFalse; } } else { // unsupported type return utFalse; }}/* set a GPSOdometer_t value for the KeyValue entry */static utBool _propSetGPSValue(KeyValue_t *kv, const GPSOdometer_t *(*gps)){ if (!kv || !gps) { // 'gps' should not be null, however *gps may be // no specified value return utFalse; } else if (KVT_TYPE(kv->type) == KVT_GPS) { KeyData_t *keyDataBuf = _propGetData(kv); UInt16 keyDataSiz = _propGetDataCapacity(kv); if (keyDataSiz >= sizeof(GPSOdometer_t)) { if (!*gps) { // GPS is null, clear KeyData_t GPS point memset(&(keyDataBuf->gps), 0, sizeof(GPSOdometer_t)); gpsPointClear((GPSPoint_t*)&(keyDataBuf->gps)); } else if (&(keyDataBuf->gps) != *gps) { // copy iff the pointers are not the same memcpy(&(keyDataBuf->gps), *gps, sizeof(GPSOdometer_t)); } kv->lenNdx = 1; // a single GPS point is defined kv->dataSize = sizeof(GPSOdometer_t); kv->attr |= KVA_NONDEFAULT; kv->attr |= KVA_CHANGED; return utTrue; } else { // insufficient size return utFalse; } } else { // unsupported type return utFalse; }}/* get a GPSOdometer_t value for the KeyValue entry */const GPSOdometer_t *propGetGPS(Key_t key, const GPSOdometer_t *dft){ KeyValue_t *kv = propGetKeyValueEntry(key); if (!kv) { return dft; } else { const GPSOdometer_t *valGPS = dft; PROP_LOCK { _propRefresh(PROP_REFRESH_GET, kv, (UInt8*)0, 0); // no args if (kv->lenNdx <= 0) { valGPS = dft; } else { const GPSOdometer_t *gps; utBool ok = _propGetGPSValue(kv, &gps); valGPS = ok? gps : dft; } } PROP_UNLOCK return valGPS; }}/* set GPS property value */utBool propSetGPS(Key_t key, const GPSOdometer_t *gps){ KeyValue_t *kv = propGetKeyValueEntry(key); if (!kv) { // key not found return utFalse; } else { utBool ok = utFalse; PROP_LOCK { ok = _propSetGPSValue(kv, &gps); // gps may be null if (ok) { _propRefresh(PROP_REFRESH_SET, kv, (UInt8*)0, 0); } } PROP_UNLOCK return ok; }}// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// This section implements conversion of properties to/from binary values used// for getting/setting properties/* set the KeyValue entry value to the specified bytes */// The type of KeyValue determines how the data will be interpretedstatic PropertyError_t _propSetValue(KeyValue_t *kv, const UInt8 *data, int dataLen){ if (!kv) { // invalid key return PROP_ERROR_INVALID_KEY; } else if (KVT_TYPE(kv->type) == KVT_COMMAND) { // should not be here (pre-validated) return PROP_ERROR_INVALID_KEY; } else if (dataLen <= 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -