From 988a11325c2c5ebd4708febb6168829bf6373c38 Mon Sep 17 00:00:00 2001 From: outely <834843362@qq.com> Date: Wed, 1 Aug 2018 16:55:38 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E5=8E=9Fef=5Fset=5Fenv()=E4=B8=ADvalue=20=3D=3D=20NUL?= =?UTF-8?q?L=E7=9A=84=E5=88=A0=E9=99=A4=E6=9D=A1=E4=BB=B6=20Signed-off-by:?= =?UTF-8?q?=20outely=20<834843362@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/api.md | 4 ++-- easyflash/src/ef_env.c | 34 ++++++++++++++++++++-------------- easyflash/src/ef_env_wl.c | 33 +++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 30 deletions(-) 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();