File: unparse.cpp

package info (click to toggle)
tinymux 2.10.1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,212 kB
  • ctags: 8,535
  • sloc: cpp: 111,587; sh: 5,867; ansic: 141; makefile: 139
file content (247 lines) | stat: -rw-r--r-- 6,783 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*! \file unparse.cpp
 * \brief Produces human-readable key info from boolexps.
 *
 * $Id: unparse.cpp 4107 2009-01-18 17:24:43Z brazilofmux $
 *
 */

#include "copyright.h"
#include "autoconf.h"
#include "config.h"
#include "externs.h"

#include "mathutil.h"

// Boolexp decompile formats
//
#define F_EXAMINE   1  // Normal
#define F_QUIET     2  // Binary for db dumps
#define F_DECOMPILE 3  // @decompile output
#define F_FUNCTION  4  // [lock()] output

/*
 * Take a dbref (loc) and generate a string.  -1, -3, or (#loc) Note, this
 * will give players object numbers of stuff they don't control, but it's
 * only internal currently, so it's not a problem.
 */

static UTF8 *unparse_object_quiet(dbref player, dbref loc)
{
    UNUSED_PARAMETER(player);

    static UTF8 buf[SBUF_SIZE];

    mux_sprintf(buf, SBUF_SIZE, T("(#%d)"), loc);
    return buf;
}

static UTF8 boolexp_buf[LBUF_SIZE];
static UTF8 *buftop;

static void unparse_boolexp1(dbref player, BOOLEXP *b, UTF8 outer_type, int format)
{
    if ((b == TRUE_BOOLEXP))
    {
        if (format == F_EXAMINE)
        {
            safe_str(T("*UNLOCKED*"), boolexp_buf, &buftop);
        }
        return;
    }
    switch (b->type)
    {
    case BOOLEXP_AND:
        if (outer_type == BOOLEXP_NOT)
        {
            safe_chr('(', boolexp_buf, &buftop);
        }
        unparse_boolexp1(player, b->sub1, b->type, format);
        safe_chr(AND_TOKEN, boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub2, b->type, format);
        if (outer_type == BOOLEXP_NOT)
        {
            safe_chr(')', boolexp_buf, &buftop);
        }
        break;
    case BOOLEXP_OR:
        if (outer_type == BOOLEXP_NOT || outer_type == BOOLEXP_AND)
        {
            safe_chr('(', boolexp_buf, &buftop);
        }
        unparse_boolexp1(player, b->sub1, b->type, format);
        safe_chr(OR_TOKEN, boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub2, b->type, format);
        if (outer_type == BOOLEXP_NOT || outer_type == BOOLEXP_AND)
        {
            safe_chr(')', boolexp_buf, &buftop);
        }
        break;
    case BOOLEXP_NOT:
        safe_chr('!', boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub1, b->type, format);
        break;
    case BOOLEXP_INDIR:
        safe_chr(INDIR_TOKEN, boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub1, b->type, format);
        break;
    case BOOLEXP_IS:
        safe_chr(IS_TOKEN, boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub1, b->type, format);
        break;
    case BOOLEXP_CARRY:
        safe_chr(CARRY_TOKEN, boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub1, b->type, format);
        break;
    case BOOLEXP_OWNER:
        safe_chr(OWNER_TOKEN, boolexp_buf, &buftop);
        unparse_boolexp1(player, b->sub1, b->type, format);
        break;
    case BOOLEXP_CONST:

        if (mudstate.bStandAlone)
        {
            safe_str(unparse_object_quiet(player, b->thing),
                boolexp_buf, &buftop);
        }
        else
        {
            switch (format)
            {
            case F_QUIET:

                // Quiet output - for dumps and internal use. Always #Num.
                //
                safe_str(unparse_object_quiet(player, b->thing),
                     boolexp_buf, &buftop);
                break;

            case F_EXAMINE:

                // Examine output - informative. Name(#Num) or Name.
                //
                UTF8 *buff;

                buff = unparse_object(player, b->thing, false);
                safe_str(buff, boolexp_buf, &buftop);
                free_lbuf(buff);
                break;

            case F_DECOMPILE:

                // Decompile output - should be usable on other MUXes. Name if
                // player, Name if thing, else #Num.
                //
                switch (Typeof(b->thing))
                {
                case TYPE_PLAYER:

                    safe_chr('*', boolexp_buf, &buftop);

                case TYPE_THING:

                    safe_str(Name(b->thing), boolexp_buf, &buftop);
                    break;

                default:

                    safe_tprintf_str(boolexp_buf, &buftop, T("#%d"), b->thing);
                    break;
                }
                break;

            case F_FUNCTION:

                // Function output - must be usable by @lock cmd. Name if player,
                // else #Num.
                //
                switch (Typeof(b->thing))
                {
                case TYPE_PLAYER:

                    safe_chr('*', boolexp_buf, &buftop);
                    safe_str(Name(b->thing), boolexp_buf, &buftop);
                    break;

                default:

                    safe_tprintf_str(boolexp_buf, &buftop, T("#%d"), b->thing);
                    break;
                }
            }
        }
        break;

    case BOOLEXP_ATR:
    case BOOLEXP_EVAL:
        ATTR *ap;

        ap = atr_num(b->thing);
        if (ap && ap->number)
        {
            // Use the attribute name if the attribute exists.
            //
            safe_str(ap->name, boolexp_buf, &buftop);
        }
        else
        {
            // Otherwise use the attribute number.
            //
            // Only god or the db loader can create a new boolexp
            // with an invalid attribute, but anyone may keep a lock
            // whose attribute has subsequently disappeared.
            //
            safe_ltoa(b->thing, boolexp_buf, &buftop);
        }
        if (b->type == BOOLEXP_EVAL)
        {
            safe_chr('/', boolexp_buf, &buftop);
        }
        else
        {
            safe_chr(':', boolexp_buf, &buftop);
        }
        safe_str((UTF8 *)b->sub1, boolexp_buf, &buftop);
        break;

    default:

        // Bad type.
        //
        Log.WriteString(T("ABORT! unparse.cpp, fell off the end of switch in unparse_boolexp1()" ENDLINE));
        Log.Flush();
        abort();
        break;
    }
}

UTF8 *unparse_boolexp_quiet(dbref player, BOOLEXP *b)
{
    buftop = boolexp_buf;
    unparse_boolexp1(player, b, BOOLEXP_CONST, F_QUIET);
    *buftop = '\0';
    return boolexp_buf;
}

UTF8 *unparse_boolexp(dbref player, BOOLEXP *b)
{
    buftop = boolexp_buf;
    unparse_boolexp1(player, b, BOOLEXP_CONST, F_EXAMINE);
    *buftop = '\0';
    return boolexp_buf;
}

UTF8 *unparse_boolexp_decompile(dbref player, BOOLEXP *b)
{
    buftop = boolexp_buf;
    unparse_boolexp1(player, b, BOOLEXP_CONST, F_DECOMPILE);
    *buftop = '\0';
    return boolexp_buf;
}

UTF8 *unparse_boolexp_function(dbref player, BOOLEXP *b)
{
    buftop = boolexp_buf;
    unparse_boolexp1(player, b, BOOLEXP_CONST, F_FUNCTION);
    *buftop = '\0';
    return boolexp_buf;
}