943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2179
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 26th, 2006
0

Variable pointers or References for .conf parser?

Expand Post »
hello I am new to C++. I understand pointers and references a little and I must be missing something in conf.cpp. Would you help by pointing out my errors for my pointers coding errors related to my problem? Main reason I am asking is because I get a segment fault where 'servername' is.

system header and conf.h's relative include:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;

the example varible and an shortcut I picked up the source conf.h:
C++ Syntax (Toggle Plain Text)
  1. #define E extern
  2. E string &servername;

termed source of conf.cpp for veiwing purposes:
C++ Syntax (Toggle Plain Text)
  1. string &servername;
  2.  
  3. //parse line
  4. int cur;
  5. int dirint;
  6. int smspace = 1;
  7.  
  8. void parse(string line) {
  9. string sub;
  10. string pram;
  11. string subval;
  12. int len = line.length();
  13.  
  14. for (int i = 0; i < len; i++)
  15. {
  16. if (line[i] == '\t') {
  17. } else if (line[i] == '"') {
  18. if (line[i + 1] == ';') {
  19. cout << "|\";|"<< endl;
  20. smspace = 1;
  21. cur = 0;
  22. } else {
  23.  
  24. cur = 1;
  25. cout << "|\"|";
  26. smspace = 0;
  27. }
  28. }else if(smspace == 1 && line[i] == ' ') {
  29. } else {
  30.  
  31. if (sub == "servername"){
  32. if (cur == 0)
  33. servername = "test";
  34. cout << &servername;
  35. }
  36. if (cur == 1) {
  37. pram += line[i];
  38. } else {
  39. sub += line[i];
  40. }
  41. }
  42.  
  43. }
  44. }
  45. void read_conf() {
  46. ifstream in ( CONF_FILE );
  47.  
  48. if ( in.is_open() )
  49. {
  50. string line;
  51. int ret = 0;
  52. int linenum = 0;
  53. while ( getline ( in, line ) ) {
  54. string::size_type i = line.find_first_not_of ( " \t\n\v" );
  55. linenum++;
  56. if ( i != string::npos && line[i] == '#' )
  57. {
  58. continue;
  59. }
  60. if (ret == 1)
  61. {
  62. if ( i != string::npos && line[i] == '*' )
  63. {
  64. int ptr = i + 1;
  65. if ( i != string::npos && line[ptr] == '/' )
  66. {
  67. ret = 0;
  68. }
  69. continue;
  70. }
  71. }
  72. if ( i != string::npos && line[i] == '/' )
  73. {
  74. int ptr = i + 1;
  75. if ( i != string::npos && line[ptr] == '*' )
  76. {
  77. ret = 1;
  78. continue;
  79. }
  80. };
  81. parse(line);
  82. }
  83.  
  84. };
  85. };
Last edited by mknight; Nov 26th, 2006 at 7:05 am. Reason: bad format
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mknight is offline Offline
6 posts
since Nov 2006
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Firstly what does your input file look like? Post an example.

Second give an exact description of what you expect your program to do.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

sorry! I just realized I forgot the most important detail the .conf file:
C++ Syntax (Toggle Plain Text)
  1. /* general server configurations */
  2.  
  3. #################
  4. #Server Settings#
  5. #################
  6. servername "NightServ.NightIRC.org";
  7. description "NightIRC service an rare constellation of stars";
  8. myaddress "127.0.0.1";
  9. port "8087";
Last edited by mknight; Nov 26th, 2006 at 7:16 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mknight is offline Offline
6 posts
since Nov 2006
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Click to Expand / Collapse  Quote originally posted by mknight ...
sorry! I just realized I forgot the most important detail the .conf file:
C++ Syntax (Toggle Plain Text)
  1. /* general server configurations */
  2.  
  3. #################
  4. #Server Settings#
  5. #################
  6. servername "NightServ.NightIRC.org";
  7. description "NightIRC service an rare constellation of stars";
  8. myaddress "127.0.0.1";
  9. port "8087";

Ok I assume that's your input file. Can you give a brief description of what your program will do.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Most certainly! My program is going to be an IRC Services server. At this stage of engineering I am as you see working on the configurations file routings. getting the pram variable data into the an variable that will be accessible through out the program is my problem. As for the main.cpp it is only calling the read_conf(); in conf.cpp.
Last edited by mknight; Nov 26th, 2006 at 7:29 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mknight is offline Offline
6 posts
since Nov 2006
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Click to Expand / Collapse  Quote originally posted by mknight ...
Most certainly! My program is going to be an IRC Services server. At this stage of engineering I am as you see working on the configurations file routings. getting the pram variable data into the an variable that will be accessible through out the program. As for the main.cpp it is only calling the read_conf(); in conf.cpp.
Ok that was a rather generic explantion. I was looking for something more detailed. Such as:

My input file looks like this:

server.txt
C++ Syntax (Toggle Plain Text)
  1. /* general server configurations */
  2.  
  3. #################
  4. #Server Settings#
  5. #################
  6. servername "NightServ.NightIRC.org";
  7. description "NightIRC service an rare constellation of stars";
  8. myaddress "127.0.0.1";
  9. port "8087";

I want my program to go through the file and store information from the file into string variables.

i.e

string servername = "NightServ.NightIRC.org";

That would help me help you better now wouldn't it?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Yes, I am sorry. I'll do better next time, now I got an idea of how to form my posts. As for the description you gave was right on the nose.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mknight is offline Offline
6 posts
since Nov 2006
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Ok I think I understand this better now

Here is the most important information:

C++ Syntax (Toggle Plain Text)
  1. servername "NightServ.NightIRC.org";
  2. description "NightIRC service an rare constellation of stars";
  3. myaddress "127.0.0.1";
  4. port "8087";

Yep, so first we need to split the .txt file into lines.

Storing each line as a string.


Next we need to establish what the first word is of each line is. Then we can match this word with our string variables.

i.e.

C++ Syntax (Toggle Plain Text)
  1. string sub; = servername
  2. string pram; = description
  3. string subval; = myaddress
  4. port???

Finally once we have identified which line contains which variable we just need to extract the information between the quotes. And then store that into the corresponding variable.

Is this right so far?
Last edited by iamthwee; Nov 26th, 2006 at 7:51 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Ok I think I understand this better now

Here is the most important information:

C++ Syntax (Toggle Plain Text)
  1. servername "NightServ.NightIRC.org";
  2. description "NightIRC service an rare constellation of stars";
  3. myaddress "127.0.0.1";
  4. port "8087";
Yep, so first we need to split the .txt file into lines.

Storing each line as a string.


Next we need to establish what the first word is of each line is. Then we can match this word with our string variables.

i.e.

C++ Syntax (Toggle Plain Text)
  1. string sub; = servername
  2. string pram; = description
  3. string subval; = myaddress
  4. port???
Finally once we have identified which line contains which variable we just need to extract the information between the quotes. And then store that into the corresponding variable.

Is this right so far?
I am so sorry again! I didn't remove the unused variables like the subval, dirint.

you got the idea of what I am trying todo just that the sub is the directive word and pram is the value. As shown
C++ Syntax (Toggle Plain Text)
  1. string sub = servername
  2. string pram = NightServ.NightIRC.org
The code would cycle through the lines and test the word then assign the matching value to the matching variable:
C++ Syntax (Toggle Plain Text)
  1. void parse(string line) {
  2. string sub;
  3. string pram;
  4. int len = line.length();
  5.  
  6. for (int i = 0; i < len; i++)
  7. {
  8. if (line[i] == '\t') {
  9. } else if (line[i] == '"') {
  10. if (line[i + 1] == ';') {
  11. cout << "|\";|"<< endl;
  12. smspace = 1;
  13. cur = 0;
  14. } else {
  15.  
  16. cur = 1;
  17. cout << "|\"|";
  18. smspace = 0;
  19. }
  20. }else if(smspace == 1 && line[i] == ' ') {
  21. } else {
  22.  
  23. if (sub == "servername"){
  24. if (cur == 0)
  25. servername = "test";
  26. cout << &servername;
  27. }
  28. if (cur == 1) {
  29. pram += line[i];
  30. } else {
  31. sub += line[i];
  32. }
  33. }
  34.  
  35. }
  36. }
To explain the code better the 'read_conf()' loads a line into the line test to see if it should ingore the comment lines then send it to parse to evaluate the line further. if the program will also ingore tabs. As for spaces it uses an logic controls to allow or disallow spaces smspace is set to 1 so it will ingore spaces by default. As for 'cur' variable which stands for cursor to which variable it would be either 'sub' or 'pram' variables.
C++ Syntax (Toggle Plain Text)
  1. if (cur == 1) {
  2. pram += line[i];
  3. } else {
  4. sub += line[i];
  5. }
After that it would compare the first word to the corsponding if control to find out what variable is going to host the 'pram' values. this as I said is the part I am having problems with.
Last edited by mknight; Nov 26th, 2006 at 8:46 am. Reason: learnning the way this site works I goofed again :)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mknight is offline Offline
6 posts
since Nov 2006
Nov 26th, 2006
0

Re: Variable pointers or References for .conf parser?

Normally I don't do this but I don't have time to explain.

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string quoteExtract( string );
  8. string extractFirstWord( string );
  9.  
  10. int main()
  11. {
  12. string line;
  13.  
  14.  
  15. ifstream open ( "c:\\yourfile.txt" );
  16.  
  17. while (getline(open,line,'\n')) //read in each line from the file
  18. {
  19.  
  20. if (extractFirstWord(line)== "servername")
  21. {
  22. cout << quoteExtract (line);
  23. }
  24.  
  25.  
  26. }
  27. open.close(); //close file
  28. cin.get(); //pause program
  29. }
  30.  
  31. /*
  32.  * Input: std::string
  33.  * Returns: std::string
  34.  * Purpose: To extract the characters between only ONE pair of quotes
  35.  */
  36. string quoteExtract (string line)
  37. {
  38.  
  39. int start,len; //Declare variables
  40.  
  41. //string line = "description \"NightIRC service an rare constellation of stars\";";
  42.  
  43. start = line.find_first_of( "\"" ); //stores position of first quote
  44. len = line.rfind( "\"" ); //stores position of last quote
  45.  
  46. //cout << "Found first quote at:";
  47. //cout << start;
  48. //cout << "\nFound second quote at:";
  49. //cout << len << "\n";
  50.  
  51. string extract;
  52. extract = line.substr( start + 1, ( len -( start + 1 ) ) );
  53. //extracts the characters between quotes
  54. return extract;
  55.  
  56. //cin.get();
  57. }
  58.  
  59. /* Extracts the first word in a file
  60.  * Assumes no preceding whitespace before the
  61.  * first word
  62.  */
  63. string extractFirstWord( string line )
  64. {
  65. int start;
  66. start = line.find_first_of( " " ); //stores position of first space
  67. string extract;
  68. extract = line.substr( 0, start ); //extracts the first word
  69.  
  70. return extract;
  71. }

It might not do exactly as you want but it should give a starting point.
Last edited by iamthwee; Nov 26th, 2006 at 10:25 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Using a Database with C
Next Thread in C++ Forum Timeline: Using functions and arrays to create a program for grades





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC