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