summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/resources/buddylist.cpp124
-rw-r--r--src/resources/buddylist.h21
2 files changed, 39 insertions, 106 deletions
diff --git a/src/resources/buddylist.cpp b/src/resources/buddylist.cpp
index 3e40ec86..9772ff19 100644
--- a/src/resources/buddylist.cpp
+++ b/src/resources/buddylist.cpp
@@ -29,113 +29,59 @@
BuddyList::BuddyList()
{
// Find saved buddy list file
- filename = new std::string(std::string(homeDir) + "buddy.xml");
-
- // Create buddy file
- buddyXmlwriterMemory();
+ filename = new std::string(std::string(homeDir) + "buddy.txt");
// Load buddy from file
- streamFile();
+ loadFile();
}
BuddyList::~BuddyList()
{
- int rc;
-
- std::ofstream outputStream(filename->c_str(), std::ios::trunc);
- if( !outputStream ) {
- std::cerr << "Error opening output stream" << std::endl;
- return;
- }
-
- /* Close last element. */
- rc = xmlTextWriterEndElement(writer);
- if (rc < 0) {
- std::cerr << "buddyXmlwriterFilename: Error at xmlTextWriterEndElement" << std::endl;
- return;
- }
-
- xmlFreeTextWriter(writer);
- xmlCleanupParser();
-
- /* Write and close file */
- outputStream << (const char*)buf->content;
- outputStream.close();
- xmlBufferFree(buf);
-
delete filename;
}
-void BuddyList::streamFile(void) {
- int ret;
+void BuddyList::loadFile(void)
+{
+ char buddy[LEN_USERNAME];
- reader = xmlReaderForFile(filename->c_str(), NULL, 0);
- if (reader != NULL) {
- ret = xmlTextReaderRead(reader);
- while (ret == 1) {
- processNode();
- ret = xmlTextReaderRead(reader);
- }
- xmlFreeTextReader(reader);
- if (ret != 0) {
- std::cerr << filename << " : failed to parse" << std::endl;
- }
- } else {
- std::cerr << "Unable to open " << filename << std::endl;
+ // Open file
+ std::ifstream inputStream(filename->c_str(), std::ios::in);
+ if( !inputStream ) {
+ std::cerr << "Error opening input stream" << std::endl;
+ return;
}
-}
-
-void BuddyList::processNode(void) {
- const xmlChar *name, *value;
- name = xmlTextReaderConstName(reader);
- if (name == NULL)
- name = BAD_CAST "--";
-
- value = xmlTextReaderConstValue(reader);
-
- if(!xmlTextReaderIsEmptyElement(reader) && xmlTextReaderHasValue(reader))
- addBuddy((char *)value);
+ do {
+ inputStream.getline(buddy, LEN_USERNAME);
+ buddylist.push_back(buddy);
+ } while(!inputStream.eof());
+ // Read buddy and close file
+ inputStream.close();
}
-void BuddyList::buddyXmlwriterMemory(void)
-{
- int rc;
-
- /* Create a new XML buffer, to which the XML document will be
- * written */
- buf = xmlBufferCreate();
- if (buf == NULL) {
- std::cerr << "buddyXmlwriterMemory: Error creating the xml buffer" << std::endl;
- return;
- }
- // Create a new XmlWriter for uri, with no compression
- writer = xmlNewTextWriterMemory(buf, 0);
- if (writer == NULL) {
- std::cerr << "buddyXmlwriterMemory: Error creating the xml writer" << std::endl;
- return;
- }
+void BuddyList::saveFile(void)
+{
+ std::string str;
- // Start with the xml default version and UTF-8 encoding
- rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
- if (rc < 0) {
- std::cerr << "buddyXmlwriterFilename: Error at xmlTextWriterStartDocument" << std::endl;
+ // Open file
+ std::ofstream outputStream(filename->c_str(), std::ios::trunc);
+ if( !outputStream ) {
+ std::cerr << "Error opening output stream" << std::endl;
return;
}
- // Start with the root named "LIST"
- rc = xmlTextWriterStartElement(writer, BAD_CAST "LIST");
- if (rc < 0) {
- std::cerr << "buddyXmlwriterFilename: Error at xmlTextWriterStartElement" << std::endl;
- return;
+ // Write buddy and close file
+ for (buddyit = buddylist.begin(); buddyit != buddylist.end(); buddyit++)
+ {
+ str = *buddyit;
+ outputStream << (const char*) str.c_str() << std::endl;
}
+ outputStream.close();
}
bool BuddyList::addBuddy(const std::string buddy)
{
- int rc;
-
for (buddyit = buddylist.begin(); buddyit != buddylist.end(); buddyit++)
{
// Buddy already exist
@@ -145,12 +91,8 @@ bool BuddyList::addBuddy(const std::string buddy)
// Buddy doesnt exist, add it
buddylist.push_back(buddy);
- // Store buddy in "PEOPLE"
- rc = xmlTextWriterWriteElement(writer, BAD_CAST "PEOPLE", (xmlChar *) buddy.c_str());
- if (rc < 0) {
- std::cerr << "buddyXmlwriterFilename: Error at xmlTextWriterWriteElement" << std::endl;
- return false;
- }
+ // Save file
+ saveFile();
return true;
}
@@ -163,11 +105,13 @@ bool BuddyList::removeBuddy(const std::string buddy)
// Buddy exist, remove it
if (*buddyit == buddy) {
buddylist.remove(buddy);
+
+ // Save file
+ saveFile();
return true;
}
}
}
- // TODO: Remove from buddy.xml
// Buddy doesnt exist
return false;
diff --git a/src/resources/buddylist.h b/src/resources/buddylist.h
index 32cce06a..5f082ba7 100644
--- a/src/resources/buddylist.h
+++ b/src/resources/buddylist.h
@@ -27,9 +27,6 @@
#include <guichan.hpp>
#include <list>
#include <string>
-#include <libxml/encoding.h>
-#include <libxml/xmlwriter.h>
-#include <libxml/xmlreader.h>
class BuddyList : public gcn::ListModel {
public:
@@ -64,27 +61,19 @@ class BuddyList : public gcn::ListModel {
std::string getElementAt(int number);
private:
- /**
- * Create a new XmlWriter for memory
- */
- void buddyXmlwriterMemory(void);
-
/**
- * Process node and load buddy
+ * Save buddy to file
*/
- void processNode(void);
+ void saveFile(void);
/**
- * Parse a buddy XML file
+ * Load buddy from file
*/
- void streamFile(void);
+ void loadFile(void);
std::list<std::string> buddylist; /**< Buddy list */
std::list<std::string>::iterator buddyit; /**< Iterator */
- xmlTextWriterPtr writer;
- xmlTextReaderPtr reader;
- xmlBufferPtr buf;
- std::string *filename; /* File to work with */
+ std::string *filename; /* File to work with */
};
#endif /* _TMW_BUDDYLIST_H */