Subversion Repositories Games.Carmageddon

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 pmbaty 1
#include "assocarr.h"
2
#include "CORE/FW/fwsetup.h"
3
#include "CORE/FW/mem.h"
4
#include "CORE/FW/resource.h"
5
#include "CORE/FW/token.h"
6
#include "CORE/STD/brstdlib.h"
7
#include "harness/trace.h"
8
 
9
// IDA: br_associative_array* __cdecl BrAssociativeArrayAllocate()
10
br_associative_array* BrAssociativeArrayAllocate(void) {
11
    br_associative_array* pArray;
12
    LOG_TRACE("()");
13
 
14
    pArray = BrResAllocate(fw.res, sizeof(br_associative_array), BR_MEMORY_SCRATCH);
15
    if (pArray == NULL) {
16
        return NULL;
17
    }
18
    pArray->tv = BrResAllocate(pArray, 10 * sizeof(br_token_value), BR_MEMORY_SCRATCH);
19
    if (pArray->tv == NULL) {
20
        return NULL;
21
    }
22
    pArray->max_elements = 10;
23
    pArray->num_elements = 0;
24
    return pArray;
25
}
26
 
27
// IDA: br_error __usercall Set_Associative_Array_Value@<EAX>(br_associative_array *pArray@<EAX>, int index@<EDX>, br_value v@<EBX>)
28
br_error Set_Associative_Array_Value(br_associative_array* pArray, int index, br_value v) {
29
    LOG_TRACE("(%p, %d, %d)", pArray, index, v);
30
 
31
    if (BrTokenType(pArray->tv[index].t) == BRT_STRING) {
32
        if (pArray->tv[index].v.str != NULL) {
33
            BrResFree(pArray->tv[index].v.str);
34
        }
35
        if (v.str != NULL) {
36
            pArray->tv[index].v.str = BrResStrDup(pArray, v.str);
37
        } else {
38
            pArray->tv[index].v.str = NULL;
39
        }
40
    } else {
41
        pArray->tv[index].v = v;
42
    }
43
    return 0;
44
}
45
 
46
// IDA: br_error __usercall BrAssociativeArraySetEntry@<EAX>(br_associative_array *pArray@<EAX>, br_token t@<EDX>, br_value v@<EBX>)
47
br_error BrAssociativeArraySetEntry(br_associative_array* pArray, br_token t, br_value v) {
48
    br_uint_16 i;
49
    br_token_value* temp;
50
    LOG_TRACE("(%p, %d, %d)", pArray, t, v);
51
 
52
    for (i = 0; i < pArray->num_elements; i++) {
53
        if (pArray->tv[i].t == t) {
54
            return Set_Associative_Array_Value(pArray, i, v);
55
        }
56
    }
57
    if (pArray->num_elements < pArray->max_elements) {
58
        pArray->tv[pArray->num_elements].t = t;
59
        Set_Associative_Array_Value(pArray, pArray->num_elements, v);
60
        pArray->num_elements += 1;
61
        return 0;
62
    }
63
    temp = BrResAllocate(pArray, (pArray->max_elements + 10) * sizeof(br_token_value), BR_MEMORY_APPLICATION);
64
    BrMemCpy(temp, pArray->tv, pArray->max_elements * sizeof(br_token_value));
65
    BrResFree(pArray->tv);
66
    pArray->tv = temp;
67
    if (temp == NULL) {
68
        return 0x1002;
69
    }
70
    pArray->max_elements += 10;
71
    Set_Associative_Array_Value(pArray, pArray->num_elements, v);
72
    pArray->num_elements += 1;
73
    return 0;
74
}
75
 
76
// IDA: br_error __usercall BrAssociativeArrayRemoveEntry@<EAX>(br_associative_array *pArray@<EAX>, br_token t@<EDX>)
77
br_error BrAssociativeArrayRemoveEntry(br_associative_array* pArray, br_token t) {
78
    br_uint_16 i;
79
    br_boolean bFound;
80
    LOG_TRACE("(%p, %d)", pArray, t);
81
 
82
    bFound = 0;
83
    for (i = 0; i < pArray->num_elements; i++) {
84
        if (pArray->tv[i].t == t) {
85
            bFound = 1;
86
            break;
87
        }
88
    }
89
    if (bFound != 0) {
90
        if ((BrTokenType(t) == BRT_STRING) && (pArray->tv[i].v.str != NULL)) {
91
            BrResFree(pArray->tv[i].v.str);
92
        }
93
        for (; i < pArray->num_elements - 1; i++) {
94
            BrMemCpy(&pArray->tv[i], &pArray->tv[i+1], sizeof(br_token_value));
95
        }
96
        pArray->num_elements--;
97
        return 0; // BRE_OK
98
    }
99
    return 0x1002; // BRE_NOTFOUND
100
}
101
 
102
// IDA: br_error __usercall BrAssociativeArrayQuery@<EAX>(br_associative_array *pArray@<EAX>, br_token t@<EDX>, br_value *pValue@<EBX>)
103
br_error BrAssociativeArrayQuery(br_associative_array* pArray, br_token t, br_value* pValue) {
104
    br_uint_16 i;
105
    LOG_TRACE("(%p, %d, %p)", pArray, t, pValue);
106
 
107
    for (i = 0; i < pArray->num_elements; i++) {
108
        if (pArray->tv[i].t == t) {
109
            *pValue = pArray->tv[i].v;
110
            return 0;
111
        }
112
    }
113
    return 0x1002;
114
}