$pbexportheader$pfc_delprofilestring.srf $pbexportcomments$delete section or key in ini file global type pfc_delprofilestring from function_object end type
forward prototypes global function integer pfc_delprofilestring (string as_file, string as_section, string as_key) end prototypes
global function integer pfc_delprofilestring (string as_file, string as_section, string as_key);// function: of_delete // arguments: // as_file the .ini file. // as_section the section name that the entry to be deleted is in. // as_key the key name of the entry that should be deleted from // the specified section. // (此参数为空值时删除整个节). // returns: integer // 1 success // 0 section does not exist, or key name does not exist // within specified section. // -1 file error // -2 if .ini file does not exist or has not been specified.
//变量声明 boolean lb_skipline boolean lb_sectionfound boolean lb_entryremoved integer li_file integer li_rc = 1 integer li_keylength long ll_length long ll_first long ll_last long ll_pos string ls_line string ls_section string ls_temp string ls_newfile
// 判断指定的ini文件是否存在 if not fileexists (as_file) then return -2 end if
// 打开指定的ini文件 ll_length = filelength (as_file) li_file = fileopen (as_file) if li_file = -1 then return li_file
////////////////////////////////////////////////////////////////////////////// // 逐行读取ini文件并删除指定的节或者指定节中指定的项目 // 原理:判断节和条目是否需要删除,如果需要删除,则跳过此行, // 否则,将此行保存到新文件字串变量中。然后再写入新的 // 字串替换原来ini文件中的数据。 ////////////////////////////////////////////////////////////////////////////// li_keylength = len (as_key) do while li_rc >= 0 // 读取一行数据 li_rc = fileread (li_file, ls_line) if li_rc = -1 then return -1 end if
if as_key="" then // 未指定删除条目,删除整个节 if li_rc >= 1 then //判断当前行是否为节 ll_first = pos (ls_line, "[") ll_last = pos (ls_line, "]")
if ll_first >0 and ll_last >0 then // 当前行为节,取节名 ls_temp = trim (ls_line) if left (ls_temp, 1) = "[" then ll_pos = pos (ls_temp, "]") ls_section = mid (ls_temp, 2, ll_pos - 2) //判断当前节是否为需要删除的节 if lower (ls_section) = lower (as_section) then // 此节全部删除,从当前行开始 lb_sectionfound = true lb_skipline = true else // 当前行为节但不是指定的节名,指定的节已经删除 lb_skipline = false end if end if end if end if
// 添加行结束符 ls_line = ls_line + "~013~010"
// 创建新文件 if li_rc >= 0 and not lb_skipline then ls_newfile=ls_newfile + ls_line end if else if not lb_entryremoved then //指定的条目尚未删除 if li_rc > 0 then //非回车或者换行符(即非空行)
// 判断行是否为节 if ll_first > 0 and ll_last > 0 then // 此行为节,取节名 ls_temp = trim(ls_line) if left (ls_temp, 1) = "[" then ll_pos = pos (ls_temp, "]") ls_section = mid (ls_temp, 2, ll_pos - 2) // 判断此节是否为要删除的节 if lower (ls_section) = lower (as_section) then // 为需要删除的节 lb_sectionfound = true else // 不是需要删除的节 lb_sectionfound = false end if end if else // 当前行不为节 if lb_sectionfound then // 已经查找到指定节,查找指定的条目 ls_temp = trim (ls_line) // 判断是否为需要删除的条目 if lower (left (ls_temp, li_keylength)) = lower (as_key) then // 此条目为需要删除的条目 ls_temp = trim (mid (ls_temp, li_keylength + 1)) if char (ls_temp) = "=" then // 条目后第一个字符必定为“=”号 // 删除条目 lb_entryremoved = true //条目已经删除 lb_skipline = true end if end if end if end if end if else // 指定的条目已经删除,skip lb_skipline = false end if
// 添加行结束符 ls_line = ls_line + "~013~010"
if li_rc >= 0 and not lb_skipline then //创建新文件(准备数据) ls_newfile=ls_newfile+ ls_line end if end if loop
// close the input file fileclose (li_file)
// 没有找到指定的节或者指定的条目返回0 if (not lb_sectionfound) then return 0 end if
if (not lb_entryremoved) and (as_key<>"") then return 0 end if
//使用新的数据替换ini文件 integer li_fileno, li_writes, li_cnt long ll_strlen, ll_currentpos string ls_text
li_fileno = fileopen(as_file, streammode!, write!, lockreadwrite!, replace!) if li_fileno < 0 then return -1
ll_strlen = len(ls_newfile)
// 一次最多只能写入32765个字符 if ll_strlen > 32765 then if mod(ll_strlen, 32765) = 0 then li_writes = ll_strlen / 32765 else li_writes = (ll_strlen / 32765) + 1 end if else li_writes = 1 end if
ll_currentpos = 1
for li_cnt = 1 to li_writes ls_text = mid(ls_newfile, ll_currentpos, 32765) ll_currentpos += 32765 if filewrite(li_fileno, ls_text) = -1 then return -1 end if next