c++ - TechRepublic
Question
October 7, 2008 at 11:43 PM
arabadaba

c++

by arabadaba . Updated 17 years, 9 months ago

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& 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& 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[i].Comments; // writes the comments if(content[i].Key == ""){ // Find out if this is a section outFile << ' ' << "[" << content[i].Section << "]" << endl; // Then format the section } else outFile << ' ' << content[i].Key << "=" << content[i].Value << endl; // Format the key or the valu } outFile.close(); // shotdown the file return true; } bool StaffPersonnelDataHandler::UserExists(string userID, string FileName) { vector content; // Holding the recent record // Holds the current record

if (getALLPersonnelData(FileName, content)) // Confirm if the file is loaded
{
vector::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::GetRecord(string KeyName, string userID, string FileName)
{
vector data; // Holds the return data
vector content; // Holds the current record

if (getALLPersonnelData(FileName, content)) // confirm the file is in getAllpersonnel data
{
vector::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 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::DeleteUser(string userID, string FileName)
{
vector 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[i].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 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 discussion is locked

All Comments