提交 a9ca4df5 编写于 作者: G Gymee 提交者: Gitee

tweaks code for optimization

上级 de54ae53
......@@ -143,12 +143,9 @@ KalErrCode KalTimerDelete(KalTimerId timerId)
}
KalTimer* tmpPtr = (KalTimer *)timerId;
int ret = timer_delete(tmpPtr->timerPtr);
if (ret != 0) {
free(timerId);
return KAL_ERR_INNER;
}
free(timerId);
return KAL_OK;
timerId = NULL;
return (ret != 0) ? KAL_ERR_INNER : KAL_OK;
}
unsigned int KalTimerIsRunning(KalTimerId timerId)
......
......@@ -40,11 +40,12 @@ static int GetValueByFile(const char* key, char* value, unsigned int len)
if (fd < 0) {
return EC_FAILURE;
}
if (UtilsFileRead(fd, value, valueLen) < 0) {
UtilsFileClose(fd);
int ret = UtilsFileRead(fd, value, valueLen);
UtilsFileClose(fd);
fd = -1;
if (ret < 0) {
return EC_FAILURE;
}
UtilsFileClose(fd);
value[valueLen] = '\0';
return valueLen;
}
......@@ -55,12 +56,10 @@ static int SetValueToFile(const char* key, const char* value)
if (fd < 0) {
return EC_FAILURE;
}
if (UtilsFileWrite(fd, value, strlen(value)) < 0) {
UtilsFileClose(fd);
return EC_FAILURE;
}
int ret = UtilsFileWrite(fd, value, strlen(value));
UtilsFileClose(fd);
return EC_SUCCESS;
fd = -1;
return (ret < 0) ? EC_FAILURE : EC_SUCCESS;
}
static int GetCurrentItem(void)
......@@ -70,13 +69,10 @@ static int GetCurrentItem(void)
return 0;
}
char value[KV_SUM_INDEX] = {0};
if (UtilsFileRead(fd, value, KV_SUM_INDEX) < 0) {
UtilsFileClose(fd);
return 0;
}
int ret = UtilsFileRead(fd, value, KV_SUM_INDEX);
UtilsFileClose(fd);
int sum = atoi(value);
return sum;
fd = -1;
return (ret < 0) ? 0 : atoi(value);
}
static int SetCurrentItem(const int num)
......@@ -89,12 +85,10 @@ static int SetCurrentItem(const int num)
if (fd < 0) {
return EC_FAILURE;
}
if (UtilsFileWrite(fd, value, KV_SUM_INDEX) < 0) {
UtilsFileClose(fd);
return EC_FAILURE;
}
int ret = UtilsFileWrite(fd, value, KV_SUM_INDEX);
UtilsFileClose(fd);
return EC_SUCCESS;
fd = -1;
return (ret < 0) ? EC_FAILURE : EC_SUCCESS;
}
static boolean NewItem(const char* key)
......@@ -159,10 +153,8 @@ int UtilsDeleteValue(const char* key)
DeleteKVCache(key);
#endif
int ret = UtilsFileDelete(key);
int currentNum = GetCurrentItem();
if (ret == EC_SUCCESS) {
currentNum--;
ret = SetCurrentItem(currentNum);
ret = SetCurrentItem(GetCurrentItem() - 1);
}
return ret;
}
......
......@@ -86,12 +86,12 @@ static int GetValueByFile(const char* dataPath, const char* key, char* value, un
if (fd < 0) {
return EC_FAILURE;
}
if (read(fd, value, info.st_size) < 0) {
close(fd);
return EC_FAILURE;
}
int ret = read(fd, value, info.st_size);
close(fd);
fd = -1;
if (ret < 0) {
return EC_FAILURE;
}
value[info.st_size] = '\0';
return info.st_size;
}
......@@ -112,12 +112,10 @@ static int SetValueToFile(const char* dataPath, const char* key, const char* val
if (fd < 0) {
return EC_FAILURE;
}
if (write(fd, value, strlen(value)) < 0) {
close(fd);
return EC_FAILURE;
}
int ret = write(fd, value, strlen(value));
close(fd);
return EC_SUCCESS;
fd = -1;
return (ret < 0) ? EC_FAILURE : EC_SUCCESS;
}
static int DeleteValueFromFile(const char* dataPath, const char* key)
......@@ -317,10 +315,7 @@ int UtilsSetEnv(const char* path)
return EC_FAILURE;
}
pthread_mutex_lock(&g_kvGlobalMutex);
if (strcpy_s(g_dataPath, MAX_KEY_PATH + 1, path) != EOK) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
int ret = strcpy_s(g_dataPath, MAX_KEY_PATH + 1, path);
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_SUCCESS;
return (ret != EOK) ? EC_FAILURE : EC_SUCCESS;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册