Question

Locked

c++

By arabadaba ·
hi there.
im new to c++ and i have this project im working on(staff personel system) using OOP.
ive started my project but im stuck.
need help.


#include "StaffPersonnelDataHandler.h"

StaffPersonnelDataHandler::StaffPersonnelDataHandler(void) // constructor
{
}
StaffPersonnelDataHandler::~StaffPersonnelDataHandler(void)
{
}


void Trim(std::string& str, const std::string & ChrsToTrim = " \t\n\r", int TrimDir = 0)
{
size_t startIndex = str.find_first_not_of(ChrsToTrim);
if (startIndex == std::string::npos){str.erase(); return;}
if (TrimDir < 2) str = str.substr(startIndex, str.size()-startIndex);
if (TrimDir!=1) str = str.substr(0, str.find_last_not_of(ChrsToTrim) + 1);
}


bool StaffPersonnelDataHandler::getALLPersonnelData(string FileName, vector<Record>& content)
{
string s; //Holding current line from the initial file
string CurrentSection; // Holding the section of the name

ifstream inFile (FileName.c_str()); // Creating inputing filestream
if (!inFile.is_open()) return false; // Returns false if the inputing file doesnt open
content.clear(); // Clearing vector content

string comments = ""; // String for storing comments

while(!std::getline(inFile, s).eof()) // Reading until it reaches end of the file
{
Trim(s); // Triming whitespace from the both ends
if(!s.empty()) // confirm that it is not a blank line
{
Record r; // Define a new record

if((s[0]=='#')||(s[0]==';')) // Find out if the line is commented?
{
if ((s.find('[')==string::npos)&& // If there is no [ or =
(s.find('=')==string::npos)) // Then it's a comment
{
comments += s + '\n'; // Add the comment to the current comments string
} else {
r.Commented = s[0]; // Saving the charcater of the comment
s.erase(s.begin()); // Removing the comment from another processing
Trim(s);
}// Remove any more whitespace
} else r.Commented = ' '; // else consider it as commented

if(s.find('[')!=string::npos) // Is this line a section?
{
s.erase(s.begin()); // clears leading brackets
s.erase(s.find(']')); // Erases bracket
r.Comments = comments; // Add the comments string (if any)
comments = ""; // Clear the comments for re-use
r.Section = s; // Set the Section value
r.Key = ""; // Set the Key value
r.Value = "";
CurrentSection = s;

}

if(s.find('=')!=string::npos) // Find out if this is a key or value
{
r.Comments = comments; // Add the comments string (if any)
comments = ""; // Clear the comments for re-use
r.Section = CurrentSection; // Set the section to the current Section
r.Key = s.substr(0,s.find('=')); // Set the Key value to everything before the = sign
r.Value = s.substr(s.find('=')+1); // Set the Value to everything after the = sign
}
if(comments == "") // Prevent adding record if it is a comment line
content.push_back(r); // Adds Record to a content
}
}

inFile.close(); // shotdown the file
return true;
}

bool StaffPersonnelDataHandler::Save(string FileName, vector<Record>& content)
{
ofstream outFile (FileName.c_str()); // Creating an ouput filestream
if (!outFile.is_open()) return false; // Return if the output file doesnt open
for (int i=0;i<(int)content.size();i++) // Looping through every vector
{
outFile << content.Comments; // writes the comments
if(content.Key == ""){ // Find out if this is a section
outFile << ' ' << "["
<< content.Section << "]" << endl;

// Then format the section
}
else
outFile << ' ' << content.Key
<< "=" << content.Value << endl; // Format the key or the valu
}

outFile.close(); // shotdown the file
return true;
}


bool StaffPersonnelDataHandler::UserExists(string userID, string FileName)
{
vector<Record> content; // Holding the recent record // Holds the current record

if (getALLPersonnelData(FileName, content)) // Confirm if the file is loaded
{
vector<Record>::iterator iter = std::find_if(content.begin(),
content.end(),
StaffPersonnelDataHandler::RecordSectionIs(userID)); // find out the section

if (iter == content.end()) return false; // The Section was not found
}
return true; // The Section was found
}

vector<StaffPersonnelDataHandler::Record> StaffPersonnelDataHandler::GetRecord(string KeyName, string userID, string FileName)
{
vector<Record> data; // Holds the return data
vector<Record> content; // Holds the current record

if (getALLPersonnelData(FileName, content)) // confirm the file is in getAllpersonnel data
{
vector<Record>::iterator iter = std::find_if(content.begin(),
content.end(),
StaffPersonnelDataHandler::RecordSectionKeyIs(userID,KeyName)); // find out the record

if (iter == content.end()) return data; // Record is not found

data.push_back (*iter); // Record is not found
}
return data; // Returning the record
}

string StaffPersonnelDataHandler::GetValue(string KeyName, string userID, string FileName)
{
vector<Record> content = GetRecord(KeyName,userID, FileName); // Getting record

if(!content.empty()) // Confirm that there is a value to return
return content[0].Value; // Then return the value

return ""; // Value not found
}

bool StaffPersonnelDataHandler:eleteUser(string userID, string FileName)
{
vector<Record> content; // Holding the recent record // Holds the current record

if (getALLPersonnelData(FileName, content)) // confirm the file is in getAllpersonnel data
{
for(int i=(int)content.size()-1;i>-1;i--) // Iterating through the content
{
if(content.Section == userID) // Find out if this is related to the section
content.erase (content.begin()+i); // Then Clear it
}

return Save(FileName,content); // Save the filename
}
return false; // Return false if the file doesnt getallperssoneldate
}


bool StaffPersonnelDataHandler::AddUser(string userID,string key, string value, string FileName)
{


vector<Record> content;

if(getALLPersonnelData(FileName,content))
{

Record r;

r.Comments = ""; // Add the comments string (if any)

r.Section = userID; // Seting the Section value
r.Key = key; // Seting the Key value
r.Value = value;

content.push_back(r);
// Format the key or the value
Save(FileName,content);
return true;
}


// Shotdown the file
return false;
// if the fle didnt open
}

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

So what's the question?

by Bizzo In reply to c++

In what way are you stuck?
Does it fail to compile?
Is the logic incorrect?
Is this a college assignment?

Back to Windows Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums