Qbasicnews.com
May 26, 2020, 05:42:09 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: A failing INI sub....  (Read 1973 times)
winlineos
New Member

Posts: 3



WWW
« on: March 11, 2003, 02:30:25 AM »

I am trying to make a function were you can save a key to an INI file.

But it only works if the [section] doesn't exist.

Here is the code and I hope you can help me on this one!

Code:

FUNCTION saveINIkey$ (file$, section$, key$, value$)
what$ = "[" + section$ + "]"
OPEN file$ FOR APPEND AS #1
CLOSE #1

what = LEN(section$) + 2

OPEN file$ FOR INPUT AS #1
C = 1
DIM M$(1 TO 1000)
DO WHILE NOT EOF(1) AND C <= 1000
      INPUT #1, M$(C)
      IF LEFT$(M$(C), what) = what$ THEN lne = C
      C = C + 1
LOOP
CLOSE #1

IF lne = 0 THEN
OPEN file$ FOR APPEND AS #1
PRINT #1, what$
PRINT #1, key$ + "=" + value$
CLOSE #1
EXIT FUNCTION
END IF

END FUNCTION

it will only work if the section you are writing it to doesn't exist.

I need this changed so if the section DOES exist yet the key not it will ad it after the section starts

And then if the section exists and the key exists that it would overite the key with the new key provided when you call the sub.

I need help on this one!
Tim Groeneveld,
Logged

isit the WINLINE OS site:: http://winline.sytes.net
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #1 on: March 11, 2003, 03:52:17 AM »

but how is lne ever going to be 0 (in your test on it)?  lne is set equal to C and C starts out as 1 and only increases from there.
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
winlineos
New Member

Posts: 3



WWW
« Reply #2 on: March 11, 2003, 04:32:32 AM »

Quote from: "Glenn"
but how is lne ever going to be 0 (in your test on it)?  lne is set equal to C and C starts out as 1 and only increases from there.


IF LEFT$(M$(C), what) = what$ THEN lne = C

Because if lne is not given a value, lne will equal 0.

Simpaly, it will equal 0 if not found - yet if found it will equal the line that it IS found on.
Logged

isit the WINLINE OS site:: http://winline.sytes.net
winlineos
New Member

Posts: 3



WWW
« Reply #3 on: March 14, 2003, 11:46:36 PM »

Code:
DECLARE FUNCTION saveINIkey$ (file$, section$, key$, value$)
DECLARE FUNCTION getINIkey$ (file$, section$, key$)

CLS
CLEAR 3000
tim$ = saveINIkey("tim.ini", "Boot", "Copy", "(C) 2002 WNLINE Networks")

FUNCTION getINIkey$ (file$, section$, key$)
what$ = "[" + section$ + "]"
OPEN file$ FOR APPEND AS #1
CLOSE #1

      what = LEN(section$) + 2


OPEN file$ FOR INPUT AS #1
C = 1
DIM M$(1 TO 1000)
DO WHILE NOT EOF(1) AND C <= 1000
      INPUT #1, M$(C)
      IF LEFT$(M$(C), what) = what$ THEN lne = C
      C = C + 1
LOOP
CLOSE #1
IF lne = 0 THEN
EXIT FUNCTION
END IF

UT = lne
kl = LEN(key$)
DO WHILE UT < C
IF LEFT$(M$(UT), kl) = key$ THEN
        temp$ = RIGHT$(M$(UT), LEN(M$(UT)) - kl - 1)
        getINIkey$ = temp$
EXIT FUNCTION
END IF
        UT = UT + 1
LOOP


END FUNCTION

FUNCTION saveINIkey$ (file$, section$, key$, value$)
what$ = "[" + section$ + "]"
OPEN file$ FOR APPEND AS #1
CLOSE #1

what = LEN(section$) + 2
wat2 = LEN(key$)


OPEN file$ FOR INPUT AS #1
C = 1
DIM M$(1 TO 1000)
DO WHILE NOT EOF(1) AND C <= 1000
      INPUT #1, M$(C)
      IF LEFT$(M$(C), wat2) = key$ THEN kln = C
      IF LEFT$(M$(C), what) = what$ THEN lne = C
      C = C + 1
LOOP
CLOSE #1

' ***************************************************************
' * START HERE THE CHEAKING IF BOTH THE SECTION AND KEY DOESN'T *
' * EXIST IN THE INI FILE.                                      *
' ***************************************************************
IF lne = 0 AND kln = 0 THEN
OPEN file$ FOR APPEND AS #1
PRINT #1, what$
PRINT #1, key$ + "=" + value$
CLOSE #1
GOTO xlear
END IF

' ***************************************************************
' * START HERE THE CHEAKING IF THE SECTION DOES EXIST BUT THE   *
' * KEY DOES EXIST IN THE INI FILE.                             *
' ***************************************************************

IF lne <> 0 AND kln = 0 THEN
OPEN file$ FOR INPUT AS #1
DIM OLD$(1 TO 1000)
DIM NEW$(1 TO 1000)
C = 1
N = 1
DO WHILE NOT EOF(1) AND C <= 1000
      INPUT #1, OLD$(C)
      NEW$(N) = OLD$(C)
      IF LEFT$(OLD$(C), what) = what$ THEN
                N = N + 1
                NEW$(N) = key$ + "=" + value$
      END IF
      C = C + 1
      N = N + 1
LOOP
CLOSE #1
OPEN file$ FOR OUTPUT AS #1
Y = 1
DO WHILE Y <> N
PRINT #1, NEW$(Y)
Y = Y + 1
LOOP
CLOSE #1
GOTO xlear
END IF

' ***************************************************************
' * START HERE THE CHEAKING IF THE SECTION DOES EXIST AND THE   *
' * KEY DOES EXIST IN THE INI FILE TO.                          *
' ***************************************************************

IF lne <> 0 AND kln <> 0 THEN
OPEN file$ FOR INPUT AS #1
DIM OLD$(1 TO 1000)
C = 1
DO WHILE NOT EOF(1) AND C <= 1000
      INPUT #1, OLD$(C)
      IF LEFT$(OLD$(C), wat2) = key$ THEN
                OLD$(C) = key$ + "=" + value$
      END IF
      C = C + 1
LOOP
CLOSE #1
OPEN file$ FOR OUTPUT AS #1
Y = 1
DO WHILE Y <> C
PRINT #1, OLD$(Y)
Y = Y + 1
LOOP
GOTO xlear
END IF
CLOSE #1

xlear:
ERASE NEW$, OLD$, M$
END FUNCTION


That nearly works but what doesn't work is once you run the program it 'chachs' the content in the ini file and you will have to run it twich to get it workink again Huh  :evil:

Could you please help me on this one?
Tim
Logged

isit the WINLINE OS site:: http://winline.sytes.net
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!