diff --git a/docs/zh/api.md b/docs/zh/api.md index ebc2857..5e46300 100644 --- a/docs/zh/api.md +++ b/docs/zh/api.md @@ -52,11 +52,11 @@ char *ef_get_env(const char *key) #### 1.2.4 设置环境变量 -使用此方法可以实现对环境变量的增加、修改功能。(注意:此处的环境变量指代的已加载到内存中的环境变量) +使用此方法可以实现对环境变量的增加、修改及删除功能。(注意:此处的环境变量指代的已加载到内存中的环境变量) - **增加** :当环境变量表中不存在该名称的环境变量时,则会执行新增操作; - - **修改** :入参中的环境变量名称在当前环境变量表中存在,则把该环境变量值修改为入参中的值; +- **删除**:当入参中的value为NULL时,则会删除入参名对应的环境变量。 ```C diff --git a/easyflash/src/ef_env.c b/easyflash/src/ef_env.c index d2e8f60..a0e0b68 100644 --- a/easyflash/src/ef_env.c +++ b/easyflash/src/ef_env.c @@ -478,7 +478,7 @@ static EfErrCode del_env(const char *key) { } /** - * Set an ENV. + * Set an ENV.If it value is NULL, delete it. * If not find it in ENV table, then create it. * * @param key ENV name @@ -498,22 +498,28 @@ EfErrCode ef_set_env(const char *key, const char *value) { /* lock the ENV cache */ ef_port_env_lock(); - old_env = find_env(key); - /* If find this ENV, then compare the new value and old value. */ - if (old_env) { - /* find the old value address */ - old_env = strchr(old_env, '='); - old_value = old_env + 1; - /* If it is changed then delete it and recreate it */ - if (strcmp(old_value, value)) { - result = del_env(key); - if (result == EF_NO_ERR) { - result = create_env(key, value); + /* if ENV value is NULL, delete it */ + if (value == NULL) { + result = del_env(key); + } else { + old_env = find_env(key); + /* If find this ENV, then compare the new value and old value. */ + if (old_env) { + /* find the old value address */ + old_env = strchr(old_env, '='); + old_value = old_env + 1; + /* If it is changed then delete it and recreate it */ + if (strcmp(old_value, value)) { + result = del_env(key); + if (result == EF_NO_ERR) { + result = create_env(key, value); + } } + } else { + result = create_env(key, value); } - } else { - result = create_env(key, value); } + /* unlock the ENV cache */ ef_port_env_unlock(); diff --git a/easyflash/src/ef_env_wl.c b/easyflash/src/ef_env_wl.c index ef7292f..1cfea0f 100644 --- a/easyflash/src/ef_env_wl.c +++ b/easyflash/src/ef_env_wl.c @@ -494,7 +494,7 @@ static EfErrCode del_env(const char *key) { } /** - * Set an ENV. + * Set an ENV.If it value is NULL, delete it. * If not find it in ENV table, then create it. * * @param key ENV name @@ -514,21 +514,26 @@ EfErrCode ef_set_env(const char *key, const char *value) { /* lock the ENV cache */ ef_port_env_lock(); - old_env = find_env(key); - /* If find this ENV, then compare the new value and old value. */ - if (old_env) { - /* find the old value address */ - old_env = strchr(old_env, '='); - old_value = old_env + 1; - /* If it is changed then delete it and recreate it */ - if (strcmp(old_value, value)) { - result = del_env(key); - if (result == EF_NO_ERR) { - result = create_env(key, value); + /* if ENV value is NULL, delete it */ + if (value == NULL) { + result = del_env(key); + } else { + old_env = find_env(key); + /* If find this ENV, then compare the new value and old value. */ + if (old_env) { + /* find the old value address */ + old_env = strchr(old_env, '='); + old_value = old_env + 1; + /* If it is changed then delete it and recreate it */ + if (strcmp(old_value, value)) { + result = del_env(key); + if (result == EF_NO_ERR) { + result = create_env(key, value); + } } + } else { + result = create_env(key, value); } - } else { - result = create_env(key, value); } /* unlock the ENV cache */ ef_port_env_unlock();