OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Write to a specific line in a text-file (PrivateProfileStrin

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
CorNouws
General User
General User


Joined: 16 Jul 2004
Posts: 16

PostPosted: Fri May 18, 2007 5:00 am    Post subject: Write to a specific line in a text-file (PrivateProfileStrin Reply with quote

Hi *,

I've written a function for Replacement of READING with MsWin System.PrivateProfileString. See below.
(Info: The MsFunctions not only allows to read/write registry, bot also for ini-files)

Before submitting to the code-snippet base and such, I would like to make the story complete. Thus I start to write a function to replace WRITING with MsWin System.PrivateProfileString.

Now my question:
is there any function avaialble to write only a certain line to a text-file?
I haven't seen one in the resouces on hand, but that doesn't mean there isn't any.

Thanks for your pointers,
Cor


'
Code:
=========================================================================
'  Nou&Off     |    24-04-07    |     www.nouenoff.nl
' Description :  Replacement for READING with MsWin System.PrivateProfileString
' Arguments   :  Strings for File, Section and Item
' Returns     :  String with value found
'-------------------------------------------------------------------------
Function ConoPrivateProfileString (sFile$, sSec$, sItem$) as String
   Dim n%
   Dim s$, s1$
   Dim bSecStart as Boolean
   Dim bSecEnd as Boolean
   
   On Error GoTo ErrorHandler
   If Dir(sFile) = "" Then   ' niet gevonden
      ConoPrivateProfileString = ""
      Exit Function
   End If
   
   sSec= "["& sSec & "]"
   n = FreeFile()
   Open sFile for Input As #n
   Do while NOT EOF(n)
      Input #n, s
      If NOT bSecStart Then
         If Left (s, len(sSec)) = sSec Then
            bSecStart = true
         End If
      Else
         If Left (s, len(sItem)) = sItem Then
            s1 = Right (s, (len(s) - len(sItem)-1))
            Exit Do
         ElseIf s = "" Then
            Exit Do
         End If
      End If
   Loop
   Close #n
   ConoPrivateProfileString = s1
   
   Exit Function
ErrorHandler:   
   MyErrMessenger ("ConoPrivateProfileString")
End Function

Sub MyErrMessenger (s$)

   msgBox "Error in " & ConoPrivateProfileString & " :" & CHR(13) & CHR(13)_
      & Error() & CHR(13) & "on line: " & Erl, 16, SMSGTITEL
End Sub
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Sat May 19, 2007 5:38 pm    Post subject: Reply with quote

I do not believe that any function exists for this, which makes sense if you think about it. The file routines do not allow an insertion. Probably the best you can do is to read data until you get to a specific line number. At that point, the line uses a specific number of bytes. If you write exactly the same number of bytes and over-write the lines, you are fine. If not, then you must literally copy the file, or, read the rest of the data and shift it to make room for more. Similar issue if you write less data than currently exists.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
levan82
General User
General User


Joined: 14 Jul 2006
Posts: 33

PostPosted: Wed Jun 13, 2007 1:11 pm    Post subject: Reply with quote

CorNouws

this code takes very long time to get data from ini-files, when there are many keys in it.
Is there any other way to access ini file info?

thanks a lot in advance
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Thu Jun 14, 2007 10:49 am    Post subject: Reply with quote

You can optimize the code if needed. For example, "If NOT bSecStart Then" is true only once, so move this out of the loop.

There are other obvious optimizations, such as calculating Len(sItem) every time it is used. This is wasteful in the extreme.

Finally, I am not convinced that this code is absolutely accurate, based on a cursory look. For example, what is the format of an INI file and what are valid values for sItem? What if you search for a key named "hello.one" and the key "hello.one.two" is also contained in the file. What is returned?
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
levan82
General User
General User


Joined: 14 Jul 2006
Posts: 33

PostPosted: Sat Jun 16, 2007 2:51 pm    Post subject: Reply with quote

pitonyak

please tell me more accurate code if you can? Sad
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Sat Jun 16, 2007 9:32 pm    Post subject: Reply with quote

For example, here are a few changes, staying with the spirit of the existing code:

Code:
REM First, change the arguments to values because they are changed in the method.
Function ConoPrivateProfileString2(ByVal sFile$, ByVal sSec$, ByVal sItem$) as String
   Dim n%
   Dim s$, s1$
   Dim bSecStart as Boolean
   Dim bSecEnd as Boolean
   
   On Error GoTo ErrorHandler
   If Dir(sFile) = "" Then   ' niet gevonden
      ConoPrivateProfileString2 = ""
      Exit Function
   End If
   
   sSec= "["& sSec & "]"
   n = FreeFile()
   Open sFile for Input As #n
   
   REM Find the proper section.
   REM Do this outside the loop so that it will be faster
   Dim nSecLen As Integer
   nSecLen = len(sSec)
   Do while NOT EOF(n)
     Input #n, s
     REM InStr is probably faster than using Left
     If InStr(s,sSec) = 1 Then
     'If Left (s, nSecLen) = sSec Then
        bSecStart = true
        Exit Do
     End If
   Loop
   
   REM Never found it, get out!
   If NOT bSecStart Then
     Close #n
     Exit Function
   End If

   REM The code assumes that it is followed by an equal sign and that there are
   REM no spaces. OK, lets assume that. Append an '=', which makes this safer.
   sItem = sItem & "=" 
   nSecLen = len(sItem)
   Do while NOT EOF(n)
     Input #n, s
     If InStr(s, sItem) = 1 Then
     'If Left (s, nSecLen) = sItem Then
        s1 = Right (s, (len(s) - nSecLen))
        Exit Do
     ElseIf s = "" Then
        Exit Do
     End If
   Loop
   Close #n
   ConoPrivateProfileString2 = s1
   
   Exit Function
ErrorHandler:   
   MyErrMessenger ("ConoPrivateProfileString")
End Function

On my sample INI file that I created, the time went from 4.8 system ticks to 4.1 system ticks.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
levan82
General User
General User


Joined: 14 Jul 2006
Posts: 33

PostPosted: Sun Jun 17, 2007 3:42 am    Post subject: Reply with quote

thanks pitonyak it is a littte faster, but still very slow

thanks anyway Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group