Loading ...
Sorry, an error occurred while loading the content.

Loading parameters from a .txt file

Expand Messages
  • oemdesign
    Hi All! (Group newbie here..) I m a mature engineer who is new to Liberty Basic, but first learned Basic in the late 70s ;-) Very cool software. Brought
    Message 1 of 6 , Jul 5, 2007
    • 0 Attachment
      Hi All! (Group newbie here..)

      I'm a "mature" engineer who is new to Liberty Basic, but first
      learned Basic in the late 70s ;-) Very cool software. Brought me
      from programming test code under DOS to Windows XP (almost)
      painlessly.

      I need to create a routine that can load a bunch of numeric named
      parameters from an end-user editable file at run time into
      variables. I'm hoping there's a snipet out there somewhere I
      can "borrow" versus writing it myself.

      Details: Because I expect my users to edit this, I'm nervous about
      requireing a particular order. The trivial case would be to require
      the number first, then some delimiter, then a comment, all in a fixed
      order. I may wind up going with that.

      However, I'd like the test file to be more free-form, an look
      something like the following, and the lines to work in any order. I
      can do this from scratch, but I'm hoping to find a little tiny
      shortcut. There will be several hundred parameters in the finished
      file, more or less. I'll do range checking after the mass load.

      ...
      pmax = 12 ; Maximum test pressure in PSI
      pset = 10 ; Target setpoint for test pressure in PSI
      pmin = 6.4 ; Minimum test pressure in PSI
      ...

      Where the first text is a named variable, the number is loaded at run
      time, and everything after the ";" is ignored. Later in the program
      (for example) I want to be able to obtain a current pressure from my
      hardware (loaded in variable "cpres" type integer for example) and
      test it ( if cpres < pmin then [tellUserTooLow] ).

      Suggestons appreciated.

      -Bill
    • Bill
      Hi All. I m a Newbie to LB, but learned Basic about 30 years ago (on a mainframe, of all things). I m hoping someone can point me at a code snippet to set a
      Message 2 of 6 , Jul 11, 2007
      • 0 Attachment
        Hi All. I'm a Newbie to LB, but learned Basic about 30 years ago (on
        a mainframe, of all things).

        I'm hoping someone can point me at a code snippet to set a bunch of
        named parameters at run time. This would allow my end user to modify
        settings without tinkering with source code.

        I would like to have a "set.txt" file in the same sub-dir as a LB
        application. The ideal format of the .txt file would be something
        like the following, with the lines in the .txt file in no particular
        order:

        ...
        TPmin = 8 ' This is a comment - Minimum input test pressure in PSI
        TPmax = 12 ' More comments - this is the maximum test pressure
        ...

        There would be hundreds of parameters. I want to load them in an
        elegant way either at start-up to individual variables or a structure
        or to be able to scan for & look up as needed. I could do this more
        easily with a fixed format file, but I'd like to be able to allow
        users to set parameters in any order & have them shhow up in the
        right place. For example, my program would have a functionn to get
        the real time input pressure to my test system (I have this
        working). I would like to be able to have code something like the
        following:

        ...

        if RealTimeTestPres < TPmin [TellUserPressureTooLow]
        if RealTimeTestPres > TPmax [TellUserPressureTooHigh]
        ...

        Suggestions or pointers to places to look?

        Thanks in advance.

        -Bill
      • pendl2megabit
        ... Here is the way I use to do this: This one ignores any line that does not start with a known keyword, if you place the comments behind the values, you
        Message 3 of 6 , Jul 12, 2007
        • 0 Attachment
          --- In libertybasic@yahoogroups.com, "Bill" <oemdesign@...> wrote:
          >
          > I would like to have a "set.txt" file in the same sub-dir as a LB
          > application. The ideal format of the .txt file would be something
          > like the following, with the lines in the .txt file in no particular
          > order:
          >
          > ...
          > TPmin = 8 ' This is a comment - Minimum input test pressure in PSI
          > TPmax = 12 ' More comments - this is the maximum test pressure
          > ...
          >

          Here is the way I use to do this:
          This one ignores any line that does not start with a known keyword, if
          you place the comments behind the values, you would have to strip them
          off by yourself.

          I would use the following format for the settings file:

          # comment (# can be replaced by any character)
          Setting=Value

          This way the user does not have to take care about the comments and
          the file is easier to parse.

          '---code start (watch for line wraps)
          [GetMainDefaults]
          open DefaultsFile$ for input as #defaults
          while not(eof(#defaults))
          line input #defaults, readline$
          setting$ = word$(readline$, 1, "=")
          value$ = word$(readline$, 2, "=")

          select case setting$
          case "UGPostVersion"
          UGPostVersion$ = value$
          case "CAMConfigFile"
          CAMConfigFile$ = value$
          case "ProgCaption"
          ProgCaption$ = value$
          case "ProgFilter"
          ProgFilter$ = value$
          case "DefaultStartDir"
          DefaultStartDir$ = value$
          case "SNC64DestDir"
          SNC64DestDir$ = value$
          case "isEleProg"
          isEleProg$ = value$
          case "connection"
          TotalConnections = TotalConnections + 1
          connection$(TotalConnections,1) = word$(value$, 1, ";")
          connection$(TotalConnections,2) = word$(value$, 2, ";")
          connection$(TotalConnections,3) = word$(value$, 3, ";")
          case "TimeOut"
          TimeOut = val(value$)
          case "message"
          OperatorMessage$ = OperatorMessage$; value$; chr$(13);
          chr$(10)
          end select
          wend
          close #defaults
          return
          '---code end

          ---
          Stefan Pendl
        • Richard Russell
          ... Rather than reinventing the wheel you might like to consider using the standard Windows .ini file format. That would be particularly advantageous not
          Message 4 of 6 , Jul 12, 2007
          • 0 Attachment
            --- In libertybasic@yahoogroups.com, "Bill" <oemdesign@...> wrote:
            > I would like to have a "set.txt" file in the same sub-dir as a LB
            > application. The ideal format of the .txt file would be something
            > like the following, with the lines in the .txt file in no
            > particular order:

            Rather than 'reinventing the wheel' you might like to consider using
            the standard Windows '.ini' file format. That would be particularly
            advantageous not only because it's an established standard, but
            because you can get Windows to do most of the work for you by
            calling the GetPrivateProfileInt API (or a related one).

            The format of a .INI file for your application would be something
            like:

            [settings]
            TPmin = 8 ; Minimum input test pressure in PSI
            TPmax = 12 ; This is the maximum test pressure

            and the Liberty Basic code to retrieve those values from the file
            would be:

            inifile$ = "C:\path\appname.ini"
            calldll #kernel32, "GetPrivateProfileIntA", "settings" as ptr, _
            "TPmin" as ptr, 0 as long, inifile$ as ptr, TPmin as long
            calldll #kernel32, "GetPrivateProfileIntA", "settings" as ptr, _
            "TPmax" as ptr, 0 as long, inifile$ as ptr, TPmax as long

            (The values shown here as zero are the default values assigned if
            the file doesn't exist, and can be changed to suit your needs).

            Here inifile$ is the path/name of the file containing the settings
            in the above format (substitute a more appropriate name for your
            application). The section name 'settings' is just an example: you
            can use any name you like, and have multiple sections with different
            names. Given the large number of settings for your program that may
            be advantageous in dividing them into logical groups.

            The above code assumes your settings are all integers; if they're
            float values or strings the code required is slightly more
            complicated, but still a lot easier than writing it yourself. Your
            users will thank you for using a standard file format.

            Richard.
          • Stefan Pendl
            ... I do use the INI-file format myself in a large project, but for a simple settings file, I prefer the homemade version. It is still possible that Windows
            Message 5 of 6 , Jul 12, 2007
            • 0 Attachment
              >
              > Rather than 'reinventing the wheel' you might like to
              > consider using the standard Windows '.ini' file format. That
              > would be particularly advantageous not only because it's an
              > established standard, but because you can get Windows to do
              > most of the work for you by calling the GetPrivateProfileInt
              > API (or a related one).
              >
              ---snip
              >
              > The above code assumes your settings are all integers; if
              > they're float values or strings the code required is slightly
              > more complicated, but still a lot easier than writing it
              > yourself. Your users will thank you for using a standard file format.
              >

              I do use the INI-file format myself in a large project, but for a simple
              settings file, I prefer the homemade version.
              It is still possible that Windows will quit supporting the INI files in the
              future as the remarks in the description of the API function says.

              Carl said that Run BAISC may include a XML parser, which could be part of
              LB5 too.
              If that is the case, I would switch to XML for my setting files.

              ---
              Stefan Pendl
            • Richard Russell
              ... You don t explain *why* you prefer the homemade version; the only advantage I can see is that it is platform-independent. I use the INI method even if my
              Message 6 of 6 , Jul 12, 2007
              • 0 Attachment
                --- In libertybasic@yahoogroups.com, "Stefan Pendl" wrote:
                > I do use the INI-file format myself in a large project, but for a
                > simple settings file, I prefer the homemade version.

                You don't explain *why* you prefer the homemade version; the only
                advantage I can see is that it is platform-independent. I use the
                INI method even if my settings file has only one entry, since the
                code is so trivial and is guaranteed to work.

                Anyway the OP's requirements sounded quite substantial to me:

                "There would be hundreds of parameters... I want to load them in an
                elegant way either at start-up to individual variables or a
                structure or to be able to scan for & look up as needed".

                The INI method is ideally suited to this, and offers other
                advantages such as being able to subdivide the parameters into
                groups with related functions.

                > It is still possible that Windows will quit supporting the INI
                > files in the future

                Not a chance! Thousands of applications use them, and in any case
                with Vista just released I'm not expecting another major 'upgrade'
                of Windows any time soon! :-)

                Richard.
              Your message has been successfully submitted and would be delivered to recipients shortly.