|
@@ -1,10 +1,10 @@
|
|
|
+#include "utils.h"
|
|
|
#include <fstream>
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <string>
|
|
|
-
|
|
|
-#include "utils.h"
|
|
|
+#include <time.h>
|
|
|
|
|
|
|
|
|
int randint(int N) { return rand() / (RAND_MAX / N + 1); }
|
|
@@ -281,4 +281,149 @@ std::string &find_new_text(std::ifstream &infile,
|
|
|
return line;
|
|
|
}
|
|
|
return line;
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+IConv::IConv(const char *to, const char *from) : ic(iconv_open(to, from)) {}
|
|
|
+IConv::~IConv() { iconv_close(ic); }
|
|
|
+
|
|
|
+int IConv::convert(char *input, char *output, size_t outbufsize) {
|
|
|
+ size_t inbufsize = strlen(input);
|
|
|
+ size_t orig_size = outbufsize;
|
|
|
+
|
|
|
+
|
|
|
+ int r = iconv(ic, &input, &inbufsize, &output, &outbufsize);
|
|
|
+ *output = 0;
|
|
|
+ return r;
|
|
|
+}
|
|
|
+
|
|
|
+static IConv converter("UTF-8", "CP437");
|
|
|
+
|
|
|
+static time_t last_time = 0;
|
|
|
+
|
|
|
+
|
|
|
+ * harry_level:
|
|
|
+ *
|
|
|
+ * 0 - inactive
|
|
|
+ * 1 - Week 1 (1-7)
|
|
|
+ * 2 - Week 2 (8-14)
|
|
|
+ * 3 - Week 3 (15-21)
|
|
|
+ * 4 - Week 4 (22-31)
|
|
|
+ */
|
|
|
+int harry_level(void) {
|
|
|
+ struct tm *tmp;
|
|
|
+ time_t now = time(NULL);
|
|
|
+ if (last_time < now + 10) {
|
|
|
+ last_time = now;
|
|
|
+
|
|
|
+
|
|
|
+ tmp = gmtime(&now);
|
|
|
+ if (tmp->tm_mon != 9)
|
|
|
+ return 0;
|
|
|
+ if (tmp->tm_mday < 8)
|
|
|
+ return 1;
|
|
|
+ if (tmp->tm_mday < 15)
|
|
|
+ return 2;
|
|
|
+ if (tmp->tm_mday < 22)
|
|
|
+ return 3;
|
|
|
+ return 4;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * This is similar to repr, but --
|
|
|
+ *
|
|
|
+ * It converts high ASCII to UTF8, so it will display correctly
|
|
|
+ * in the logfiles!
|
|
|
+ */
|
|
|
+const char *logrepr(const char *input) {
|
|
|
+ static char buffer[10240];
|
|
|
+ char *cp;
|
|
|
+
|
|
|
+ strcpy(buffer, input);
|
|
|
+ cp = buffer;
|
|
|
+ while (*cp != 0) {
|
|
|
+ unsigned char c = *cp;
|
|
|
+
|
|
|
+ if (c == ' ') {
|
|
|
+ cp++;
|
|
|
+ continue;
|
|
|
+ };
|
|
|
+
|
|
|
+ * horizontal tab ('\t'), and vertical tab ('\v') */
|
|
|
+ if (strchr("\f\n\r\t\v", c) != NULL) {
|
|
|
+ memmove(cp + 1, cp, strlen(cp) + 1);
|
|
|
+ *cp = '\\';
|
|
|
+ cp++;
|
|
|
+ switch (c) {
|
|
|
+ case '\f':
|
|
|
+ *cp = 'f';
|
|
|
+ cp++;
|
|
|
+ break;
|
|
|
+ case '\n':
|
|
|
+ *cp = 'n';
|
|
|
+ cp++;
|
|
|
+ break;
|
|
|
+ case '\r':
|
|
|
+ *cp = 'r';
|
|
|
+ cp++;
|
|
|
+ break;
|
|
|
+ case '\t':
|
|
|
+ *cp = 't';
|
|
|
+ cp++;
|
|
|
+ break;
|
|
|
+ case '\v':
|
|
|
+ *cp = 'v';
|
|
|
+ cp++;
|
|
|
+ break;
|
|
|
+
|
|
|
+ *cp = '?';
|
|
|
+ cp++;
|
|
|
+ break; */
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (c == '\\') {
|
|
|
+ memmove(cp + 1, cp, strlen(cp) + 1);
|
|
|
+ *cp = '\\';
|
|
|
+ cp += 2;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (c == '"') {
|
|
|
+ memmove(cp + 1, cp, strlen(cp) + 1);
|
|
|
+ *cp = '\\';
|
|
|
+ cp += 2;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (strchr("[()]{}:;,.<>?!@#$%^&*", c) != NULL) {
|
|
|
+ cp++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (strchr("0123456789", c) != NULL) {
|
|
|
+ cp++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", c) !=
|
|
|
+ NULL) {
|
|
|
+ cp++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((int)c < 0x20) {
|
|
|
+
|
|
|
+ memmove(cp + 3, cp, strlen(cp) + 1);
|
|
|
+ char buffer[10];
|
|
|
+ int slen;
|
|
|
+ slen = snprintf(buffer, sizeof(buffer), "\\x%02x", (int)c & 0xff);
|
|
|
+ strncpy(cp, buffer, 4);
|
|
|
+ cp += 4;
|
|
|
+ continue;
|
|
|
+ };
|
|
|
+ cp++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ static char fancybuffer[16384];
|
|
|
+ converter.convert(buffer, fancybuffer, sizeof(fancybuffer));
|
|
|
+ return fancybuffer;
|
|
|
+}
|