Guidance
指路人
g.yi.org
software / rapidq / Examples / Registry & INI / inirw-2-0.bas

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
'Read/Write ini files
'By Luca Cassioli 2003
' Originally created for wxBasic language - ported to RapidQ on October, 2004

     $TYPECHECK ON
     $INCLUDE "rapidq.inc"

     DIM slash AS STRING
     DIM CR AS STRING

'CONST false = 0
'CONST true = 1

     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



'*****************************************************
'*** These lines are only for test purpose ***********
'*****************************************************
     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
'print "cerco parametro " + ParameterString + " in sezione " + Section + " del file "+FileName
      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" ' Returned value if param not found
      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  'Scan all file for SECTION.
       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  'Scan all file for PARAMETERSTRING.
       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$) ' Store parameter on right of "=".
        IF MID$(riga$,conta,1)="=" THEN  ' Look for "=".
         uguale=True
        END IF
        IF MID$(riga$,conta,1)<>"=" AND uguale=True THEN
         temp=temp+MID$(riga$,conta,1)
        END IF
       NEXT
            'wxMessageBox("Found '"+ParameterString+"' parameter into '"+Section+"' section of '"+FileName+"' file. Value: "+temp,"DEBUG",wxICON)
      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) 'Temporary file.
      SectionFound=False
      WHILE IniFile.EOF()=False  'Scan all file for SECTION.
       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  'Scan all file for PARAMETERSTRING.
       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$) ' Copy row if does not contain parameter.
       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$) ' Store parameter name.
        IF MID$(riga$,conta,1)="=" THEN  ' Look for "=".
         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
            'MessageBox("[WRITE]Found '"+ParameterString+"' parameter into '"+Section+"' section of '"+FileName+"' file. Value: "+temp,"DEBUG",2)
       INIFile.CLOSE
       tempstore.CLOSE
       IniFile.OPEN(FileName,fmOpenWrite)
       tempstore.OPEN("temp.tmp",fmOpenRead) 'Temporary file.
'            fOpen(FileName,"w") ' update INI file:
'            tempstore=fOpen("temp.tmp","r")
       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
掌柜推荐
 
 
¥289.00 ·
 
 
¥1,080.00 ·
 
 
¥1,380.00 ·
 
 
¥638.00 ·
 
 
¥1,290.00 ·
 
 
¥1,420.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2005-04-16 12:41:33