]> jfr.im git - irc/gameservirc.git/blob - gameserv/news.cpp
removed the old single calls to "using std::etc.." and replaced them with using
[irc/gameservirc.git] / gameserv / news.cpp
1 #include "extern.h"
2 #include <list>
3 #include <iterator>
4 #include <fstream>
5 #include <string>
6 #include <stdio.h>
7
8 using namespace std;
9
10 list<string> todaysnews;
11 void addNews(list<string> &news, const char *fmt, ...);
12 void clearNews(list<string> &news) {news.clear();};
13 void showNews(char *nick, list<string> &news);
14 void saveNews(char *filename, list<string> &news);
15 void loadNews(char *filename, list<string> &news);
16
17 void addNews(list<string> &news, const char *fmt, ...)
18 {
19 if (fmt[0] == '\0')
20 return;
21
22 va_list args;
23 char *input;
24 input = new char[1024];
25 memset(input, 0, 1024);
26 const char *t = fmt;
27
28 va_start(args, fmt);
29
30 for (; *t; t++)
31 {
32 if (*t == '%')
33 {
34 switch(*++t) {
35 case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break;
36 case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break;
37 case 'S': sprintf(input, "%s%s", input, s_GameServ); break;
38 case 'l':
39 if (*++t == 'd')
40 sprintf(input, "%s%ld", input, va_arg(args, long int)); break;
41 }
42 }
43 else
44 {
45 sprintf(input, "%s%c", input, *t);
46 }
47
48 }
49 #ifdef DEBUGMODE
50 log("New News Item: %s", input);
51 #endif
52
53 notice(s_GameServ, c_Forest, "News Flash: %s", input);
54 string *nstring;
55 nstring = new string(input);
56 news.push_back(*nstring);
57
58 delete [] input;
59 delete nstring;
60
61 va_end(args);
62 }
63
64 void showNews(char *nick, list<string> &news)
65 {
66 if (!news.empty())
67 {
68 list<string>::iterator iter;
69
70 notice(s_GameServ, nick, "The Daily Happenings: ");
71 for (iter=news.begin(); iter != news.end(); iter++)
72 {
73 notice(s_GameServ, nick, "%s", (*iter).c_str());
74 }
75 notice(s_GameServ, nick, "End of News");
76 }
77 }
78
79 void saveNews(char *filename, list<string> &news)
80 {
81 ofstream outfile;
82 outfile.open(filename);
83
84 if (outfile.fail())
85 {
86 log("Error opening %s", filename);
87 return;
88 }
89
90 list<string>::iterator iter;
91 for (iter=news.begin(); iter != news.end(); iter++)
92 {
93 outfile << (*iter).c_str() << endl;
94 }
95 outfile.close();
96 }
97
98 void loadNews(char *filename, list<string> &news)
99 {
100 // First clear the old news out
101 news.clear();
102
103 // Now load from the file
104 ifstream infile;
105 infile.open(filename);
106 if (infile.fail())
107 {
108 log("Error opening %s", filename);
109 return;
110 }
111
112 char *buf;
113 string *str;
114 buf = new char [1024];
115
116 while (infile.getline(buf, 1024, '\n'))
117 {
118 if (buf[0] == '\0' || buf[0] == '\n')
119 {
120 delete [] buf;
121 return;
122 }
123 str = new string(buf);
124 news.push_back(*str);
125 delete str;
126 }
127
128 delete [] buf;
129 }
130
131 void do_news(char *u)
132 {
133 showNews(u, todaysnews);
134 }