$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