Variable pointers or References for .conf parser?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 6
Reputation: mknight is an unknown quantity at this point 
Solved Threads: 0
mknight mknight is offline Offline
Newbie Poster

Variable pointers or References for .conf parser?

 
0
  #1
Nov 26th, 2006
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:
  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:
  1. #define E extern
  2. E string &servername;

termed source of conf.cpp for veiwing purposes:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Variable pointers or References for .conf parser?

 
0
  #2
Nov 26th, 2006
Firstly what does your input file look like? Post an example.

Second give an exact description of what you expect your program to do.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: mknight is an unknown quantity at this point 
Solved Threads: 0
mknight mknight is offline Offline
Newbie Poster

Re: Variable pointers or References for .conf parser?

 
0
  #3
Nov 26th, 2006
sorry! I just realized I forgot the most important detail the .conf file:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Variable pointers or References for .conf parser?

 
0
  #4
Nov 26th, 2006
Originally Posted by mknight View Post
sorry! I just realized I forgot the most important detail the .conf file:
  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: mknight is an unknown quantity at this point 
Solved Threads: 0
mknight mknight is offline Offline
Newbie Poster

Re: Variable pointers or References for .conf parser?

 
0
  #5
Nov 26th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Variable pointers or References for .conf parser?

 
0
  #6
Nov 26th, 2006
Originally Posted by mknight View Post
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
  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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: mknight is an unknown quantity at this point 
Solved Threads: 0
mknight mknight is offline Offline
Newbie Poster

Re: Variable pointers or References for .conf parser?

 
0
  #7
Nov 26th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Variable pointers or References for .conf parser?

 
0
  #8
Nov 26th, 2006
Ok I think I understand this better now

Here is the most important information:

  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.

  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: mknight is an unknown quantity at this point 
Solved Threads: 0
mknight mknight is offline Offline
Newbie Poster

Re: Variable pointers or References for .conf parser?

 
0
  #9
Nov 26th, 2006
Originally Posted by iamthwee View Post
Ok I think I understand this better now

Here is the most important information:

  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.

  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
  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:
  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.
  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 :)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Variable pointers or References for .conf parser?

 
0
  #10
Nov 26th, 2006
Normally I don't do this but I don't have time to explain.

  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC