summaryrefslogtreecommitdiff
path: root/src/resources/buddylist.cpp
diff options
context:
space:
mode:
authorBjörn Steinbrink <B.Steinbrink@gmx.de>2006-03-09 05:16:27 +0000
committerBjörn Steinbrink <B.Steinbrink@gmx.de>2006-03-09 05:16:27 +0000
commite5f795ad9952b8bba6993ee3e324b22a0f104816 (patch)
tree1602ac0f74865135eae5acf935b0a285a8e918c2 /src/resources/buddylist.cpp
parent7593d6c71e2331c3e43c732db60ad03ee4d5385d (diff)
downloadmana-e5f795ad9952b8bba6993ee3e324b22a0f104816.tar.gz
mana-e5f795ad9952b8bba6993ee3e324b22a0f104816.tar.bz2
mana-e5f795ad9952b8bba6993ee3e324b22a0f104816.tar.xz
mana-e5f795ad9952b8bba6993ee3e324b22a0f104816.zip
Made all class members named like mClassMember.
Diffstat (limited to 'src/resources/buddylist.cpp')
-rw-r--r--src/resources/buddylist.cpp67
1 files changed, 25 insertions, 42 deletions
diff --git a/src/resources/buddylist.cpp b/src/resources/buddylist.cpp
index e41327a5..9327ef60 100644
--- a/src/resources/buddylist.cpp
+++ b/src/resources/buddylist.cpp
@@ -30,34 +30,27 @@
BuddyList::BuddyList()
{
// Find saved buddy list file
- filename = new std::string(std::string(config.getValue("homeDir", "") + "/buddy.txt"));
+ mFilename = config.getValue("homeDir", "") + "/buddy.txt";
// Load buddy from file
loadFile();
}
-BuddyList::~BuddyList()
-{
- delete filename;
-}
-
void BuddyList::loadFile()
{
- char *buddy;
-
// Open file
- std::ifstream inputStream(filename->c_str(), std::ios::in);
- if( !inputStream ) {
+ std::ifstream inputStream(mFilename.c_str(), std::ios::in);
+ if (!inputStream) {
std::cerr << "Error opening input stream" << std::endl;
return;
}
do {
- buddy = (char *) calloc(LEN_MAX_USERNAME, sizeof(char));
+ char *buddy = new char[LEN_MAX_USERNAME];
inputStream.getline(buddy, LEN_MAX_USERNAME);
// Ugly ?
- if(strcmp(buddy,"") != 0) buddylist.push_back(buddy);
- free(buddy);
+ if(strcmp(buddy,"")) mBuddylist.push_back(buddy);
+ delete [] buddy;
} while(!inputStream.eof());
// Read buddy and close file
@@ -69,31 +62,29 @@ void BuddyList::saveFile()
std::string str;
// Open file
- std::ofstream outputStream(filename->c_str(), std::ios::trunc);
- if( !outputStream ) {
+ std::ofstream outputStream(mFilename.c_str(), std::ios::trunc);
+ if (!outputStream) {
std::cerr << "Error opening output stream" << std::endl;
return;
}
// Write buddy and close file
- for (buddyit = buddylist.begin(); buddyit != buddylist.end(); buddyit++)
+ for (BuddyIterator i = mBuddylist.begin(); i != mBuddylist.end(); ++i)
{
- str = *buddyit;
- outputStream << (const char*) str.c_str() << std::endl;
+ outputStream << (const char*) i->c_str() << std::endl;
}
outputStream.close();
}
bool BuddyList::addBuddy(const std::string buddy)
{
- for (buddyit = buddylist.begin(); buddyit != buddylist.end(); buddyit++)
+ if (find(mBuddylist.begin(), mBuddylist.end(), buddy) != mBuddylist.end())
{
- // Buddy already exist
- if (*buddyit == buddy) return false;
+ return false;
}
// Buddy doesnt exist, add it
- buddylist.push_back(buddy);
+ mBuddylist.push_back(buddy);
// Save file
saveFile();
@@ -103,37 +94,29 @@ bool BuddyList::addBuddy(const std::string buddy)
bool BuddyList::removeBuddy(const std::string buddy)
{
- if (buddylist.size() > 0) {
- for (buddyit = buddylist.begin(); buddyit != buddylist.end(); buddyit++)
- {
- // Buddy exist, remove it
- if (*buddyit == buddy) {
- buddylist.remove(buddy);
-
- // Save file
- saveFile();
- return true;
- }
- }
+ BuddyIterator i = find(mBuddylist.begin(), mBuddylist.end(), buddy);
+
+ if (i != mBuddylist.end()) {
+ mBuddylist.erase(i);
+ saveFile();
+ return true;
}
- // Buddy doesnt exist
return false;
}
int BuddyList::getNumberOfElements()
{
- return buddylist.size();
+ return mBuddylist.size();
}
std::string BuddyList::getElementAt(int number)
{
- if (number <= (int) buddylist.size() - 1)
- {
- buddyit = buddylist.begin();
- std::advance(buddyit, number);
- return *buddyit;
+ if (number >= (int) mBuddylist.size()) {
+ return "";
}
- return "";
+ BuddyIterator i = mBuddylist.begin();
+ std::advance(i, number);
+ return *i;
}