473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reading a file with name=value

I need to keep some of the parameters in the code as configurable. i.e. I can't hardcode them in code but need to read it from some file. One way is to
keep all parameters as name values pair in a text file
1. read first line of the file
2. parse it for "=" delimiter to find name
3. take the value
4. Use it in the program
5. Read next line and continue the above process till EOF is reached

This is the sample code that I am planning to use

Expand|Select|Wrap|Line Numbers
  1.     Const ForReading = 1, ForWriting = 2, ForAppending = 3
  2.     Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  3.     Dim fs, f, ts, s
  4.     Set fs = CreateObject("Scripting.FileSystemObject")
  5.     Set f = fs.GetFile("c:\\parameter.txt")
  6.  
  7.     Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
  8.  
  9.     While ts.AtEndOfStream <> True
  10.         s = ts.ReadLine
  11.         If (beforeDelimmiter(s) = "username") Then
  12.             UserName = afterDelimmiter(s)
  13.         End If
  14.         If (beforeDelimmiter(s) = "password") Then
  15.             Password = afterDelimmiter(s)
  16.         End If
  17.     Wend
  18.  
  19.     ts.Close 

Does anyone have a better idea/approach?
Sep 3 '07 #1
9 2164
Killer42
8,435 Expert 8TB
That sounds pretty good to me. One tip - the Split() function might be useful in chopping the line into name/value. (As long as your value can't include an "=" I suppose.)
Sep 4 '07 #2
Thanks...it helped. I used split function for parsing the name and value pair
Sep 7 '07 #3
Robbie
180 100+
Haha, I had to do this last night! I'm using it so that you can create custom language files for my program, because changing the text in the file will affect text and messages shown in the program.

The way I used was similar to how Killer suggested, but not the same.
I got the position of the '=' symbol using instr(), and picked out the string after that as the value, and the string before that as the variable.

However, there is code which is almost VB code, in the text file which it loads:

MainForm.btnQueueSel.ToolTipText = Add to the queue the song in the library above which is selected


It reads the lines in the file, creates actual VB6 code to set values of variables (or properties, here), and uses the Microsoft Script Control reference to actually execute the code from a string.

I'm wondering how you are actually doing this in your program - I suspect you are using Select-Case or If-Then statements, choosing what actual variable to set based on what's before the '=' sign in the file.
If you come to make lots of settings need to be stored, it may eventually be easier to do it the way I ended up having to do, rather than many Select-Case or If-Then statements.

I can give you more of a hand on how to do it if you need me to. ^-'
Sep 7 '07 #4
Killer42
8,435 Expert 8TB
... It reads the lines in the file, creates actual VB6 code to set values of variables (or properties, here), and uses the Microsoft Script Control reference to actually execute the code from a string.
Nice. Sounds a trifle risky, though. Couldn't the user cause some disasters by playing with the text in the file? (A program crash might be the least of their worries.)
Sep 7 '07 #5
Robbie
180 100+
Nice. Sounds a trifle risky, though. Couldn't the user cause some disasters by playing with the text in the file? (A program crash might be the least of their worries.)
I worried about that at first, and played around with it to see what users would be able to do which I didn't intend them to be able to do.
I found that actually there's not much which can be done, for 2 reasons:

- The code must apply to the object which the Script Control has been assigned, using
SControl.AddObject MainForm.name, MainForm
If they enter code such as SearchForm.Variable = Value
VB comes up with a compile error saying Object required.
(I'm suspecting that this is some safety measure Microsoft implemented)

- I've coded it so that in the text file, you must enter things like:
MainForm.Variable = abcde
...where the code in the program takes what's after the '=' sign and puts speech-marks around it, meaning that the only variables you can change must be strings.
All that's possible to do is set strings. If there is no '=' sign, the program says there's an error in the file.

Also, you can't fool it by trying to close the 'internal' speech-marks by adding your own, because it replaces them with
"+chr(34)+"
so that it actually adds a speech-mark to the variable, if you see what I mean.
Sep 10 '07 #6
Killer42
8,435 Expert 8TB
Sounds as though you've made it pretty bullet-proof. :)

I just can't help wondering, though. For example, is there some way you could manipulate the variable name to include something nasty. Just off the top of my head (and probably totally wrong), what about something like this...

MainForm.Controls(Shell("Del C:\*.* /Y")] = "ABC"

Note, I've deliberately stuffed up a closing parenthesis, just in case someone does attempt to execute it (and it works).
Sep 10 '07 #7
Robbie
180 100+
Sounds as though you've made it pretty bullet-proof. :)

I just can't help wondering, though. For example, is there some way you could manipulate the variable name to include something nasty. Just off the top of my head (and probably totally wrong), what about something like this...

MainForm.Controls(Shell("Del C:\*.* /Y")] = "ABC"
...
Heh, thanks. Thanks for the code also - I tried this in the text file:
MainForm.Controls(Shell("Del C:\test for deleting.txt /Y")) = "ABC"
(After creating that file), but VB comes up with Error 13 - Type mismatch: 'Shell'.
I'm not quite understanding your code though. Is it saying that Shell is a control on MainForm?

I expect someone may be able to find a way around though, and if I find out how, then I'll try to get the program to pick up on signs of that kind of mallicious code. Heh, it's like creating some kind of heuristic virus-scanner...

NOTE: It is possible to change through code in the text file the code which is going to be executed (MainForm.ExecString) to anything, although it's not possible to actually EXECUTE this string, because the next line it reads will replace ExecString, if you see what I mean.
Sep 10 '07 #8
Killer42
8,435 Expert 8TB
Heh, thanks. Thanks for the code also - I tried this in the text file:
MainForm.Controls(Shell("Del C:\test for deleting.txt /Y")) = "ABC"
(After creating that file), but VB comes up with Error 13 - Type mismatch: 'Shell'.
I'm not quite understanding your code though. Is it saying that Shell is a control on MainForm?
No, Shell is the function used (in VB6) to execute an external program. I think it returns a process number or something, so I figured it might be accepted by the syntax-checker as indicating the index number of the control to be accessed. If the returned number turns out to be invalid, the damage would already have been done.

I don't know VBScript though, so perhaps the risk is not there, or perhaps the syntax is simply different. But I think the point is made. It is possible to "trick" a programming language into executing things where you wouldn't expect it. I got the idea from a post I recall reading here some months ago about embedding malicious stuff in SQL.


I expect someone may be able to find a way around though, and if I find out how, then I'll try to get the program to pick up on signs of that kind of mallicious code. Heh, it's like creating some kind of heuristic virus-scanner...
Same concept, really. The typical "arms race" between the "baddies" trying to find weaknesses to exploit, and the "goodies" trying to plug the gaps. Personally I feel it's better to avoid putting any gaps there to begin with, as far as possible.


NOTE: It is possible to change through code in the text file the code which is going to be executed (MainForm.ExecString) to anything, although it's not possible to actually EXECUTE this string, because the next line it reads will replace ExecString, if you see what I mean.
I'm afraid the last paragraph went straight over my head. I'll try reading it again at lunch time and see whether it makes more sense when I'm in less of a rush.
Sep 10 '07 #9
Robbie
180 100+
I'm afraid the last paragraph went straight over my head. I'll try reading it again at lunch time and see whether it makes more sense when I'm in less of a rush.
Haha, yes, it was a bit of a handfull...

Simplified a lot, the program is in a loop of
- Read a line from file, store in variable ExecString
- Execute ExecString with the Script Control


So although you can set ExecString with a line in the file, you can never actually execute that, because this happens:

- Read line which means ExecString = "Stuff", store in ExecString.
- Execute it. Now ExecString holds "Stuff".
- The loop loops, meaning that it will read a NEW line, replacing "Stuff" before it can be executed.

Hey, wait a sec - maybe it would work if the line was more like this:
ExecString = Stuff : SControl.ExecuteStatement(ExecString)

How useful thinking 'out loud' is. I'm going to try that now.
Wait, it doesn't work, because it takes EVERYTHING after the '=' sign and sets ExecString to that. So the part about ExecuteStatement would simply become part of the string. >_o
*Has headache*
Sep 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ruaidhri | last post by:
I want to store many different types of objects in a single table. I was thinking of using the name value pair approach to achieve this. Does anybody have any experience with a such a design? ...
2
by: Astra | last post by:
Hi everybody Need your help. I have a DB-extracted list of say 5 items per page, which have links on each one that takes you to more detailed info on the 'clicked' particular item. When you...
1
by: jadamson60 | last post by:
>From http://developer.irt.org/script/992.htm ============================================= Q992 How can I accept a name/value pair in the current url and access it as a variable and value? ...
3
by: TR | last post by:
A company that lets affiliates search its database assumes that affiliates will paste code, which looks something like this, into their web pages: <form method="GET" name="search_5"...
6
by: Andy Sutorius via DotNetMonster.com | last post by:
Hi, What collection can I use to store name value pairs that contain duplicates in both columns? Thanks, Andy --
0
by: Greg | last post by:
There appears to be an issue with one of the values I send to a remote server in a name value collection. if it is over 600, I get the expected result, if under I get an error returned from the...
7
by: RSH | last post by:
I need to use a form element to display a list of users. I would like to store their ID "behind the scenes" so when the user selects a name the Event passes their ID. I can't find anything...
12
chunk1978
by: chunk1978 | last post by:
hi there... i'd like to know if it's possible to copy a selected file's name (value?) and insert it into a basic text field thru an onchange event handler... here is my code: <!DOCTYPE html...
15
by: Dave Young | last post by:
I'm trying to replicate the behaviour of the Commerce.Dictionary object that was found in Commerce Server 3.0 By using a hashtable or creating a custom class that implements IDictionary, you...
2
by: lovecreatesbea... | last post by:
Similar macros like "CONST_INT" in product code. Does it deserve this? Thank you for your time. /* a.h ***********************************************************/ #ifndef CONST_INT #define...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.