File: messages.cpp

package info (click to toggle)
tworld 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,648 kB
  • sloc: ansic: 13,347; cpp: 3,175; perl: 2,465; makefile: 10
file content (113 lines) | stat: -rw-r--r-- 2,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* messages.cpp: Functions for end-of-game messages.
 *
 * Copyright (C) 2014 by Eric Schmidt, under the GNU General Public
 * License. No warranty. See COPYING for details.
 */

#include "messages.h"

#include "fileio.h"
#include "res.h"

#include <algorithm>
#include <bitset>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using std::bitset;
using std::ifstream;
using std::find;
using std::getline;
using std::istringstream;
using std::strcpy;
using std::string;
using std::vector;

namespace
{
    vector<string> messages;
    vector<size_t> typeindex[MessageTypeCount];
    size_t current[MessageTypeCount];
}

int const maxMessageSize = 511;
char const * messageTypeNames[MessageTypeCount] = { "win", "die", "time" };

int loadmessagesfromfile(char const *filename)
{
    char *fname = getpathforfileindir(resdir, filename);
    ifstream in(fname);
    free(fname);

    if (!in)
        return FALSE;

    vector<string> newmessages;
    vector<size_t> newtypeindex[MessageTypeCount];
    bitset<MessageTypeCount> isactive;
    isactive.set(MessageDie);
    string line;
    while (getline(in, line))
    {
        // Just in case DOS line endings on Linux. Not sure if needed.
        string::iterator rpos(find(line.begin(), line.end(), '\r'));
        if (rpos != line.end())
            line.erase(rpos);

        if (line.empty()) continue;
        if (line[0] == ':')
	{
            isactive.reset();

            istringstream in(line);
            in.get(); // Discard ':'
            string type;
            while (in >> type)
            {
                int typenum = find(messageTypeNames,
                    messageTypeNames + MessageTypeCount, type)
                    - messageTypeNames;
                if (typenum < MessageTypeCount)
                    isactive.set(typenum);
            }
        }
        else
	{
            for (size_t i = 0; i < isactive.size(); ++i)
            {
                if (isactive[i])
                    newtypeindex[i].push_back(newmessages.size());
            }
            line = line.substr(0, maxMessageSize+1);
            newmessages.push_back(line);
	}
    }

    messages.swap(newmessages);
    for (size_t i = 0; i < MessageTypeCount; ++i)
    {
        typeindex[i].swap(newtypeindex[i]);
        current[i] = 0;
    }

    return TRUE;
}

char const *getmessage(int type)
{
    static char buf[maxMessageSize+1];

    if ((type < 0) || (type >= MessageTypeCount) 
        || typeindex[type].size() == 0)
        return nullptr;

    size_t const mnum = typeindex[type][current[type]];
    char const *s = messages[mnum].c_str();
    current[type] = (current[type] + 1) % typeindex[type].size();

    strcpy(buf, s);
    return buf;
}