Quick question. I know how to do all the input and output with files to grab what ever chunks. But in some programs i'll see like this in a .conf file.
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# LogonServer Section
#
# Address:
# The address (no port) of the server.
#
# Port:
# The port on which the logon server listens. (*** NOT 3724 ***)
#
# Name:
# Not really relavant, but name the logon.
#
# RealmCount:
# The number of realms in the next section.
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
<LogonServer Address = "127.0.0.1"
Port = "8093"
Name = "Default Logon"
RealmCount = "1">
So I'm just wondering how they make those values at the bottom - equal something in the program.
Yes this particular program was made in c++ but I do not have the source code. I'm just curious how you pull the values out - and if C++ knows automatically about those shell comments.
No. You have to write a parser that will identify certain conditions in the input and decide how to handle it. In this case, all lines that begin with # are ignored.
Storing values from configuration files is done with associative arrays (usually called maps or dictionaries). A key of type T (usually a string of some kind) is associated to a value of type T2. Each key is unique to avoid ambiguity.
I'm googling at the moment all documentation on parsers - but I can't find any actual source code for them to have a look at how they work. Any ideas on a site that may have a good demo of one? Cheers for the above advice!
It's really not hard to write a parser for a configuration file:
Note: A word is everything between the current file position and the next whitespace or the next '='.
1. Read line and advance file pointer.
2. Skip whitespace.
3. If characters is '#' go to step 1.
4.a. If character is '<' and we're not inside a section, skip whitespace, read word, and use word as name for the section. We're now inside a section.
4.b. If character is '<' and we are inside a section go to step 1.
4.c. Else, go to step 6.
5. Skip whitespace.
6. Read word and use word for variable name. If we're inside a section, the variable belongs to it, otherwise, the variable is global.
7. Skip whitespace.
8. If character is not '=' discard variable name and goto step 1.
9. Skip whitespace.
10. Read string. If the line end before the string does, discard variable name and goto step 1.
11. Map string to variable name.
12. Skip whitespace.
13. If EOF and we're inside a section, discard whole section and end.
14. If character is '>' and we're inside a section, we're no longer inside a section.
15. If EOF, end.
16. Go to step 1.
That's right. I just wrote a parser in plain English off the top of my head.