Thanks to you, I've got a working version. Yes, I've got some rearranging of code to do for error checking and whatnot, but I ran a file through this and it works.
Code:
include <iostream>
#include <fstream>
#include <string>
using namespace std;
string trim(string const& source, char const* delims = " \t\r\n") {
string result(source);
string::size_type index = result.find_last_not_of(delims);
if(index != string::npos)
result.erase(++index);
index = result.find_first_not_of(delims);
if(index != string::npos)
result.erase(0, index);
else
result.erase();
return result;
}
struct read_exception { };
class settings
{
private:
settings() { }
// Declare away -- Declare but do not define; attempted use will generate
// a linker error
settings(const settings&);
void read_data(string& thisuser) const;
struct data;
static data* data_ptr;
static settings instance_obj;
public:
static settings& instance();
//deconstruct
~settings();
//references publc functions stored elsewhere
//hmmm could they be incorporated into other classes?
string get_username(string thisuser) const;
string get_email(string thisuser) const;
string get_lastdatecontacted(string thisuser) const;
};
//PUBLIC CLASS STRUCT
struct settings::data
{
//string value;
string username;
string email;
string lastdatecontacted;
};
//END PUBLIC CLASS STRUCT
settings settings::instance_obj;
settings::data* settings::data_ptr = 0;
//???
settings& settings::instance()
{
return instance_obj;
}
//PUBLIC CLASS FUNCTIONS
//deconstruct
settings::~settings()
{
//cout << "cleaning up pointer ..." << endl;
delete data_ptr;
data_ptr = 0;
}
//end deconstruct
//get values
string settings::get_username(string thisuser) const
{
cout << "getting username ..." << endl;
if(!data_ptr)
{
cout << "reading stream ..." << endl;
read_data(thisuser);
}
return data_ptr->username;
}
string settings::get_email(string thisuser) const
{
cout << "getting email ..." << endl;
if(!data_ptr)
{
cout << "reading stream ..." << endl;
read_data(thisuser);
}
return data_ptr->email;
}
string settings::get_lastdatecontacted(string thisuser) const
{
cout << "getting last date contacted ..." << endl;
if(!data_ptr)
{
cout << "reading stream ..." << endl;
read_data(thisuser);
}
return data_ptr->lastdatecontacted;
}
//end get values
//read_data
void settings::read_data(string& thisuser) const
{
// Fail if the stream is bad... perhaps do data checking too
if(!data_ptr)
{
cout << "initializing pointer ..." << endl;
data_ptr = new data;
ifstream fin;
fin.open("test.txt", ios::in);
if(fin.fail())
{
throw read_exception();
}
char detailline[140];
string sdetailline;
while(!fin.fail() && !fin.eof())
{
fin.getline(detailline, 90, '\n');
sdetailline = detailline;
if (trim(sdetailline.substr(0,10)) == thisuser)
{
data_ptr->username = sdetailline.substr(10,10);
data_ptr->lastdatecontacted = sdetailline.substr(20,10);
data_ptr->email = sdetailline.substr(30,50);
}
}
fin.close();
}
}
//end read_data
//END PUBLIC CLASS FUNCTIONS
int main()
{
settings& usr = settings::instance();
cout << usr.get_username("bob") << endl
<< usr.get_lastdatecontacted("bob") << endl
<< usr.get_email("bob") << endl;
cin;
return 1;
}