国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

PB中對INI文件讀寫的補充函數:刪除指定的節或者指定節中某個項

2024-07-21 02:10:05
字體:
來源:轉載
供稿:網友

我們在使用pb的ini函數讀寫ini文件時,有時也可能需要動態地刪除某個節或者某個項,此函數即完成此功能。此函數是從pfc里分離出來的,希望對大家有用。

$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 //非回車或者換行符(即非空行)
 
    // 查找指定的節
    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
      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

fileclose(li_fileno)

return 1
end function

最大的網站源碼資源下載站,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀宁县| 荃湾区| 陆良县| 英山县| 东平县| 从江县| 云浮市| 乌海市| 巴彦淖尔市| 丰县| 成武县| 松桃| 沙坪坝区| 黄梅县| 吉林省| 搜索| 凭祥市| 庄河市| 通江县| 穆棱市| 曲靖市| 伊金霍洛旗| 婺源县| 永仁县| 甘孜县| 漳州市| 昌乐县| 赣榆县| 桐乡市| 商都县| 科技| 布尔津县| 大余县| 洞头县| 苗栗县| 垦利县| 阿坝县| 武定县| 漠河县| 池州市| 芜湖县|