$TYPECHECK ON
$INCLUDE "rapidq.inc"
DIM slash AS STRING
DIM CR AS STRING
DECLARE FUNCTION IniRead(FileName AS STRING ,Section AS STRING,ParameterString AS STRING) AS STRING
DECLARE FUNCTION IniWrite(FileName AS STRING,Section AS STRING,ParameterString AS STRING,Value AS STRING) AS STRING
$IFDEF unix
Slash="/"
CR= CHR$(10)
$ELSE
Slash="\\"
CR=CHR$(13)+CHR$(10)
$ENDIF
CREATE form AS QFORM
center
width=300
height=200
END CREATE
PRINT "READ RESULT: ",INIRead("test.ini","options","copier")
PRINT "WRITE RESULT: ",INIWrite("test.ini","options","copier",CHR$(34)+"copy"+CHR$(34))
form.SHOWMODAL
FUNCTION StripQuotes(s AS STRING) AS STRING
DIM t AS STRING
t=s
IF MID$(s,1,1)=CHR$(34) THEN
IF RIGHT$(s,1)=CHR$(34) THEN
t= MID$(s,2,LEN(s)-2)
ELSE
StripQuotes="Missing quotes in parameter"
EXIT FUNCTION
END IF
END IF
StripQuotes=t
END SUB
FUNCTION IniRead(FileName AS STRING ,Section AS STRING,ParameterString AS STRING) AS STRING
DIM temp AS STRING
DIM uguale AS INTEGER
DIM leggi AS QFILESTREAM
DIM SectionFound AS INTEGER
DIM ParameterFound AS INTEGER
DIM riga$ AS STRING
DIM IniFile AS QFILESTREAM
DIM conta AS INTEGER
temp="-1"
IF FILEEXISTS(FileName)=False THEN
SHOWMESSAGE("READ ERROR:File "+CHR$(34)+FileName+CHR$(34)+" does not exist.")
INIRead="0"
EXIT FUNCTION
END IF
IniFile.OPEN(FileName,fmOpenRead)
SectionFound=False
WHILE IniFile.EOF()=0
riga$=IniFile.ReadLine()
IF INSTR(UCASE$(riga$),UCASE$(Section)) THEN
SectionFound=True
EXIT WHILE
END IF
WEND
IF SectionFound=False THEN
SHOWMESSAGE("READ ERROR:Section '"+Section+"' not found in file '"+FileName+"'")
INIRead="0"
EXIT FUNCTION
END IF
ParameterFound=False
WHILE IniFile.EOF = 0
riga$=IniFile.ReadLine()
IF INSTR(UCASE$(riga$),UCASE$(ParameterString))=1 AND LEFT$(LTRIM$(riga$),1)<>0 THEN
ParameterFound=True
EXIT WHILE
END IF
IF LEFT$(LTRIM$(riga$),1)="[" THEN
EXIT WHILE
END IF
WEND
IF ParameterFound=True THEN
temp=""
uguale=False
FOR conta = 1 TO LEN(riga$)
IF MID$(riga$,conta,1)="=" THEN
uguale=True
END IF
IF MID$(riga$,conta,1)<>"=" AND uguale=True THEN
temp=temp+MID$(riga$,conta,1)
END IF
NEXT
ELSE
SHOWMESSAGE("READ ERROR:File "+FileName+" contains '"+Section+"' section, but not '"+ParameterString+"'")
INIRead="0"
EXIT FUNCTION
END IF
INIFile.CLOSE
temp=StripQuotes(temp)
IF temp="Missing quotes in parameter" THEN
SHOWMESSAGE (temp+" '"+ParameterString+"' (section '"+Section+"')")
INIRead=""
EXIT FUNCTION
ELSE
INIRead=temp
END IF
END FUNCTION
FUNCTION IniWrite(FileName AS STRING,Section AS STRING,ParameterString AS STRING,Value AS STRING) AS STRING
DIM temp AS STRING
DIM INIFile AS QFILESTREAM
DIM tempstore AS QFILESTREAM
DIM SectionFound AS INTEGER
DIM ParameterFound AS INTEGER
DIM riga$ AS STRING
DIM conta AS INTEGER
DIM uguale AS INTEGER
IF FILEEXISTS(FileName)=False THEN
SHOWMESSAGE("File "+CHR$(34)+FileName+CHR$(34)+" does not exist.")
INIWrite="0"
EXIT FUNCTION
END IF
IniFile.OPEN(FileName,fmOpenRead)
tempstore.OPEN("temp.tmp",fmCreate)
SectionFound=False
WHILE IniFile.EOF()=False
riga$=IniFile.ReadLine()
tempstore.WriteLine(riga$)
IF INSTR(UCASE$(riga$),UCASE$(Section)) THEN
SectionFound=True
EXIT WHILE
END IF
WEND
IF SectionFound=False THEN
SHOWMESSAGE("WRITE ERROR: Section '"+Section+"' not found in file '"+Filename+"'")
INIWrite="0"
EXIT FUNCTION
END IF
ParameterFound=False
WHILE IniFile.EOF = 0
riga$=IniFile.ReadLine()
IF INSTR(UCASE$(riga$),UCASE$(ParameterString))=1 AND LEFT$(LTRIM$(riga$),1)<>0 THEN
ParameterFound=True
EXIT WHILE
ELSE
tempstore.WriteLine(riga$)
END IF
IF LEFT$(LTRIM$(riga$),1)="[" THEN
EXIT WHILE
END IF
WEND
IF ParameterFound=True THEN
temp=""
uguale=False
FOR conta = 1 TO LEN(riga$)
IF MID$(riga$,conta,1)="=" THEN
EXIT FOR
END IF
IF MID$(riga$,conta,1)<>"=" THEN
temp=temp+MID$(riga$,conta,1)
END IF
NEXT
temp=temp+"="+Value
tempstore.WriteLine(temp)
WHILE INIFile.EOF=False
tempstore.WriteLine(INIFile.ReadLine())
WEND
INIFile.CLOSE
tempstore.CLOSE
IniFile.OPEN(FileName,fmOpenWrite)
tempstore.OPEN("temp.tmp",fmOpenRead)
WHILE tempstore.EOF=False
INIFile.WriteLine(tempstore.ReadLine())
WEND
ELSE
SHOWMESSAGE("WRITE ERROR:File "+FileName+" contains '"+Section+"' section, but not '"+ParameterString+"'.")
INIWrite="0"
EXIT FUNCTION
END IF
INIFile.CLOSE
tempstore.CLOSE
INIWrite="-1"
END FUNCTION
|
|