How should I construct my objects when reading ctor args from a text file?

E

Eric Lilja

Hello, I have a text file that contains a number of entries describing a
recipe. Each entry consists of a number of strings. Here's an example file
with only one entry (recipe):
Name=Maple Quill
Process=Interim
Level=10
Technique=Fletching
Knowledge=Woodworking
Device=Sawhorse
Primary components=Refined Maple
Build components=1 Stroma Oil
Fuel=1 abrasive sandpaper

I've made a function that reads this text file and for each valid and
complete entry is supposed to create an object of type Recipe (a simple
class I wrote) and add it to a std::vector<Recipe>. The constructor of the
class Recipe is:

Recipe(const std::string& name,
const std::string& process,
unsigned short level,
const std::string& technique,
const std::string& knowledge,
const std::string& device,
/* There exists an "or" relationship between each primary
component. */
const std::vector<std::string>& primary_components,
/* There exists an "and" relationship between build components. */
const std::vector<std::string>& build_components,
const std::string& fuel)
:
m_name(name),
m_process(process),
m_level(level),
m_technique(technique),
m_knowledge(knowledge),
m_device(device),
m_fuel(fuel)
{
for(std::vector<std::string>::size_type i = 0; i <
primary_components.size(); ++i)
m_primary_components.push_back(primary_components);

for(std::vector<std::string>::size_type i = 0; i <
build_components.size(); ++i)
m_build_components.push_back(build_components);
}

The function reading the text file (along with a simple helper function) is:

static void
read_entries(ifstream& file, vector<Recipe>& recipes)
{
const unsigned short NAME = 0;
const unsigned short PROCESS = 1;
const unsigned short LEVEL = 2;
const unsigned short TECHNIQUE = 3;
const unsigned short KNOWLEDGE = 4;
const unsigned short DEVICE = 5;
const unsigned short PRIMARY_COMPONENTS = 6;
const unsigned short BUILD_COMPONENTS = 7;
const unsigned short FUEL = 8;

vector<string> mm(9);

mm[NAME] = "Name";
mm[PROCESS] = "Process";
mm[LEVEL] = "Level";
mm[TECHNIQUE] = "Technique";
mm[KNOWLEDGE] = "Knowledge";
mm[DEVICE] = "Device";
mm[PRIMARY_COMPONENTS] = "Primary components";
mm[BUILD_COMPONENTS] = "Build components";
mm[FUEL] = "Fuel";

string current_line;
unsigned short pos = 0;
unsigned short num_recipes = 0;

while(getline(file, current_line))
{
if(!current_line.length() || current_line[0] == '#') /* # denotes
comment */
continue;

if(!starts_with(current_line, mm[pos]))
{
cerr << "Attempted to read option " << mm[pos]
<< ", but found only " << current_line << endl;

return;
}

/* +1 to account for = */
string value = current_line.substr(mm[pos].length() + 1,
current_line.length());
cout << "read value " << value << " for option " << mm[pos] << endl;

if(++pos == mm.size())
{
cout << "Recipe data read for recipe " << ++num_recipes << endl;
pos = 0;
}
}
}

static bool
starts_with(const string& s, const string& starts_with)
{
if(s.length() < starts_with.length())
return false;

string str = s.substr(0, starts_with.length());

if(str == starts_with)
return true;

return false;
}

As you can see the value ends up in the variable value (of type
std::string).
The ouput of the program when I run with a text file containing the one
entry above is:
read value Maple Quill for option Name
read value Interim for option Process
read value 10 for option Level
read value Fletching for option Technique
read value Woodworking for option Knowledge
read value Sawhorse for option Device
read value Refined Maple for option Primary components
read value 1 Stroma Oil for option Build components
read value 1 abrasive sandpaper for option Fuel
Recipe data read for recipe 1

Oops, this turned out to be a long message...anyway, my question: How should
I go about creating my Recipe object in this function? I am reading each
argument to the Recipe constructor one at a time from the file and it ends
up in a std::string, not sure how I should go from that to actually
constructing an object of type Recipe? Please help me with this homework.

Thanks for reading and replying

/ Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top