]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/news.cpp
Removed myString class and replaced with use of the STL string class...
[irc/gameservirc.git] / gameserv / news.cpp
index 44e36ba5ffe9e0e6240945ca5604d8b305b15eb8..ab51e6d6f95323ca4eb6880c32e567929b6c8ee9 100644 (file)
@@ -1,10 +1,21 @@
 #include "extern.h"
+#include <fstream>
+#include <string>
+#include <stdio.h>
 
-List<myString> todaysnews;
-void addNews(List<myString> &news, const char *fmt, ...);
-void showNews(char *nick, List<myString> &news);
+using std::ofstream;
+using std::ifstream;
 
-void addNews(List<myString> &news, const char *fmt, ...)
+using namespace std;
+
+List<string> todaysnews;
+void addNews(List<string> &news, const char *fmt, ...);
+void clearNews(List<string> &news) {news.deleteNodes();};
+void showNews(char *nick, List<string> &news);
+void saveNews(char *filename, List<string> &news);
+void loadNews(char *filename, List<string> &news);
+
+void addNews(List<string> &news, const char *fmt, ...)
 {
     if (fmt[0] == '\0')
         return;
@@ -12,6 +23,7 @@ void addNews(List<myString> &news, const char *fmt, ...)
     va_list args;
     char *input;
     input = new char[1024];
+    memset(input, 0, 1024);
     const char *t = fmt;
 
     va_start(args, fmt);
@@ -38,8 +50,10 @@ void addNews(List<myString> &news, const char *fmt, ...)
     #ifdef DEBUGMODE
         log("New News Item: %s", input);
     #endif
-    myString *nstring;
-    nstring = new myString(input);
+
+    notice(s_GameServ, c_Forest, "News Flash: %s", input);
+    string *nstring;
+    nstring = new string(input);
     news.insertAtBack(nstring);
 
     delete [] input;
@@ -48,19 +62,86 @@ void addNews(List<myString> &news, const char *fmt, ...)
 va_end(args);
 }
 
-void showNews(char *nick, List<myString> &news)
+void showNews(char *nick, List<string> &news)
 {
     if (!news.isEmpty())
     {
-       ListNode<myString> *it;
+       ListNode<string> *it;
        it = news.First();
 
        notice(s_GameServ, nick, "The Daily Happenings: ");
        while (it)
        {
-           notice(s_GameServ, nick, "%s", it->getData()->getString());
+           notice(s_GameServ, nick, "%s", it->getData()->c_str());
            it = it->Next();
        }
        notice(s_GameServ, nick, "End of News");
     }
 }
+
+void saveNews(char *filename, List<string> &news)
+{
+    ofstream outfile;
+    outfile.open(filename);
+
+    if (outfile.fail())
+    {
+       log("Error opening %s", filename);
+       return;
+    }
+
+    ListNode<string> *it;
+    it = news.First();
+    while (it)
+    {
+       outfile << it->getData()->c_str() << endl;
+       it = it->Next();
+    }
+    outfile.close();
+}
+
+void loadNews(char *filename, List<string> &news)
+{
+    // First clear the old news out
+    ListNode<string> *it, *temp;
+    it = news.First();
+
+    while (it)
+    {
+        temp = it;
+       it = it->Next();
+       delete temp;
+    }
+
+    // Now load from the file
+    ifstream infile;
+    infile.open(filename);
+    if (infile.fail())
+    {
+       log("Error opening %s", filename);
+       return;
+    }
+
+    char *buf;
+    string *str;
+    buf = new char [1024];
+
+    while (infile.getline(buf, 1024, '\n'))
+    {
+       if (buf[0] == '\0' || buf[0] == '\n')
+       {
+           delete [] buf;
+           return;
+       }
+       str = new string(buf);
+       news.insertAtBack(str);
+       delete str;
+    }
+
+    delete [] buf;
+}
+
+void do_news(char *u)
+{
+    showNews(u, todaysnews);
+}