When we read and write the INI file using the PB, sometimes you may need to dynamically delete a section or an item, this function completes this feature. This function is separated from the PFC, I hope to be useful to everyone.
$ PBEXPORTHEADER $ PFC_DELPROFISTRING.SRF $ PBEXPORTCOMMENTS $ delete section or key in inI fileglobal type pfc_delprofilestring from function_objectnd type
Forward PrototypeSGLobal 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 thing shop be deleted from /// (This parameter deletes the entire section) .// Returns: integer // 1 Success // 0 Section Does Not Exist, or Key Name Does NOTEST / / HASIN specified section// -1 file error // -2 if .ini file does not exist or Has Not Been Specified.
// variable declaration boolean lb_skiplineboolean lb_sectionfoundboolean lb_entryremovedinteger li_fileinteger li_rc = 1integer li_keylengthlong ll_lengthlong ll_firstlong ll_lastlong ll_posstring ls_linestring ls_sectionstring ls_tempstring ls_newfile
/ / Judgment if the specified INI file exists if not fileexists (as_file) Then Return -2END IF
// Open the specified INI file ll_length = filelength (as_file) li_file = fileopen (as_file) if li_file = -1 Then Return LI_FILE
/// The INI file is read by line and delete the specified section or the item // principle specified in the specified section: The judgment section and the entry need to be deleted. If you need to delete it, skip this line, // otherwise, will The row is saved to the new file string variable. Then write a new // string to replace the data in the original INI file.
// li_keylength = len (as_key) do while li_rc> = 0 // Read a row of data li_rc = fileread (li_file, ls_line) if Li_rc = -1 Then Return -1 end if if as_key = "" THEN // Non-specified deletion Enterprise, delete the entire section if li_rc> = 1 THEN / / Decision whether the current line is a section ll_first = POS (Ls_Line, "[") LL_LAST = POS (LS_LINE, "]") IF LL_First> 0 and LL_LAST> 0 THEN / Current behavior festival, tether 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) // Judgment Whether the current festival is required to delete the section if = limited = LOWER (AS_SECTION) THEN / / Remove from the current row, LB_SECTIONFOUND = true lb_skipline = true else // The current behavior is not the specified section name, specified The section has been deleted LB_SKIPLINE = false end if end if end if end if // Add line end character LS_LINE = LS_LINE "~ 013 ~ 010" // Create a new file if ls_rc> = 0 and not lb_skipline the ls_newfile = ls_newfile ls_line END IF ELSE IF NOT LB_ENTRYREMOVED THEN / The specified entry has not been removed if li_rc> 0 dam // Non-return or wrap (ie non-spacer) // Find the specified section LL_First = POS (Ls_Line, "[") LL_LAST = POS (Ls_Line, "]") // Decision line is a section IF LL_First> 0 and LL_LAST> 0 THEN / / This behavior festival, tether 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) // Deconed whether this section is the section IF Lower (Ls_Section) = LOWER (as_section) that // required to be deleted. Section lb_sectionfound = true else // does not need to delete the section LB_SECTIONFOUND =
False end if End if else // The current line does not for the section IF LB_SECTIONFOUND1 // Find the specified section, find the specified entry LS_TEMP = TRIM (LS_LINE) // Judgment whether the entry IF Lower that needs to be deleted (LS_TEMP, LI_KEYLENGTH)) = LOWER (as_key) THEN / / This entry is an entry that needs to be deleted LS_TEMP = TRIM (MID (Ls_Temp, Li_Keylength 1)) if char (ls_temp) = "=" the first character after the entry To "=" // Remove the entry LB_ENTRYREMOVED = true // entry already deleted LB_SKIPLINE = True End if End if End if End if End if END IF END IF END IF ELSE // The specified entry has been deleted, skip lb_skipline = false end IF =/ Add line end LS_LINE = LS_LINE "~ 013 ~ 010" if li_rc> = 0 and not lb_skipline life // Create a new file (preparation data) ls_newfile = ls_newfile ls_line end if end ifloop // close the input filefileclose (li_file)
// Didn't find the specified section or the specified entry returns 0iF (not lb_sectionfound) Then Return 0nd IF
IF (not lb_entryremoved) and (as_key <> ") THEN RETURN 0nd IF
// Replace INI file INTEger Li_Fileno, Li_Writes, LI_CNTLONG LL_STRLEN, LL_CURRENTPOSSTRING LS_TEXT
Li_fileno = fileopen (as_file, streammode!, write!, lockreadwrite !, replace!) if li_fileno <0 Then Return -1
LL_STRLEN = LEN (Ls_NewFile)
// One can only write up to 32765 characters 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 ifelse li_writes = 1END 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 ifnext
FILECLOSE (Li_Fileno)
Return 1END FUNCTION