简体   繁体   中英

Define member variables in functions blindly

I am having a bit of an issue with a current task of mine. Basically, I am given an XML file and am trying to parse it for key information. For example, some lines will be like this:

<IPAddress>123.45.67</IPAddress>

And I am to get the value of 123.45.67, nothing too bad at all. I was told not to use a XML parser and just parse it manually, which was pretty easy. However, I am having issues with the second part of the task. Basically, I am to make a class with certain member variables and declare them based on the values I parse. So let's say the class is called Something and there is a member variable called IPAddress. I am to then update the value of IPAddress to 123.45.67 so when someone calls Something.IPAddress in the main method, it returns 123.45.67. This was my initial attempt at it:

#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>

using namespace std;

class Something
{
   public:
    string location;
    string IPAddress;
    string theName;
    int aValue;

    //loop through the array from the method below
    void fillContent(string* array)
    {
        for(int i = 0; i < array->size(); i++)
        {
              string line = array[i];
              if((line.find("<") != std::string::npos) && (line.find(">")!= std::string::npos)) 
              {
                 unsigned first = line.find("<");
                 unsigned last = line.find(">");
                 string strNew = line.substr (first + 1, last - first - 1); //this line will get the key, in this case, "IPAddress"
             unsigned newfirst = line.find(">");
                 unsigned newlast = line.find_last_of("<");
             string strNew2 = line.substr(newfirst + 1, newlast - newfirst - 1); //this line will get the value, in this case, "123.45.67"
                if(strNew == "IPAddress")
                {
                    IPAddress = strNew2; //set the member variable to the IP Address
                }
              }
        }
    }

    //this method will create an array where each element is a line from the xml
        void fillVariables()
    {
        string line;
        ifstream myfile ("content.xml");
        long num = //function that gets size that I didn't add to make code shorter!;
        string *myArray;
        myArray = new string[num];
        string str1 = "";
        string strNew2 = "";
        int counter = 0;
        if (myfile.is_open())
        {
            while ( getline (myfile,line) )
            {
            myArray[counter] = line;
                counter++;
            }
            myfile.close();
        }
        fillContent(myArray);
    }

};


int main(int argc, char* argv[])
{
  Something local;
  local.fillVariables();
  cout << local.IPAddress<< endl; // should return "123.45.67"
  return 0;
}

Now this does do what I want it to do, however, you can see I need the if statement. Assuming I have at least 20 of these member variables, having 20 if-statements would be annoying and just frowned upon. Is there any other way I could somehow access the member variables from the class? Sorry if my question was long, I just wanted to make sure everything that is needed to understand the question is provided! Please let me know if anything crucial that may not be there should be added.

Thanks a lot!

This may be considered bad style, but I usually just do:

// at the top of the 'fillContent' function
std::map<string, string*> varmap{
    {"IPAddress", &IPAddress},
    {"AnotherField", &AnotherField}
 };
 // If you're not using C++11, you can also try:
 // std::map<string, string*> varmap;
 // varmap["IPAddress"] = &IPAddress;
 // varmap["AnotherField"] = &AnotherField;

 // parsing code goes here
 *varmap[strNew] = strNew2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM