#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;
va_list args;
char *input;
input = new char[1024];
+ memset(input, 0, 1024);
const char *t = fmt;
va_start(args, 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;
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);
+}