$PBExportHeader$pfc_delprofilestring.srf 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);// 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