summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/CLPrefs.c
blob: 7aaa28ae61eb4111eb0d41fbf2f04ec53bfae2e7 (plain)
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
#include "cmdline.h"

struct PrefPanel {
    char *name;
    Handle data;
    Handle workHandle;
    PrefPanel *next;
};

static PrefPanel *panellist;

PrefPanel *PrefPanel_New(const char *name, void *initdata, SInt32 initdatasize) {
    PrefPanel *pnl;
    pnl = xmalloc(NULL, sizeof(PrefPanel));
    if (!pnl)
        return pnl;

    pnl->name = xstrdup(name);
    pnl->data = NewHandle(initdatasize);
    if (!pnl->data) {
        fprintf(stderr, "\n*** Out of memory\n");
        exit(-23);
    }

    pnl->workHandle = NULL;

    HLock(pnl->data);
    if (initdata)
        memcpy(*pnl->data, initdata, initdatasize);
    else
        memset(*pnl->data, 0, initdatasize);
    HUnlock(pnl->data);

    pnl->next = NULL;
    return pnl;
}

Handle PrefPanel_GetHandle(PrefPanel *panel) {
    UInt32 len = GetHandleSize(panel->data);
    if (!panel->workHandle || !*panel->workHandle)
        panel->workHandle = NewHandle(len);
    else
        SetHandleSize(panel->workHandle, len);

#line 60
    OPTION_ASSERT(MemError()==noErr);

    HLock(panel->data);
    HLock(panel->workHandle);
    memcpy(*panel->workHandle, *panel->data, len);
    HUnlock(panel->workHandle);
    HUnlock(panel->data);
    return panel->workHandle;
}

int PrefPanel_PutHandle(PrefPanel *panel, Handle data) {
    UInt32 len = GetHandleSize(data);
    if (data != panel->workHandle) {
        if (!panel->workHandle)
            panel->workHandle = NewHandle(len);
        else
            SetHandleSize(panel->workHandle, len);
        if (MemError() != noErr)
            return 0;

        HLock(panel->workHandle);
        HLock(data);
        memcpy(*panel->workHandle, *data, len);
        HUnlock(data);
        HUnlock(panel->workHandle);
    }

    HLock(panel->data);
    HLock(panel->workHandle);
    memcpy(*panel->data, *panel->workHandle, len);
    HUnlock(panel->workHandle);
    HUnlock(panel->data);
    return 1;
}

void Prefs_Initialize() {
    panellist = NULL;
}

void Prefs_Terminate() {
    PrefPanel *scan;
    PrefPanel *next;

    for (scan = panellist; scan; scan = next) {
        xfree(scan->name);
        DisposeHandle(scan->data);
        next = scan->next;
        xfree(scan);
    }
}

Boolean Prefs_AddPanel(PrefPanel *panel) {
    PrefPanel **scan;

    for (scan = &panellist; *scan; scan = &(*scan)->next) {
        if (!strcmp((*scan)->name, panel->name)) {
            CLReportError(90, panel->name);
            return 0;
        }
    }

    if (clState.pluginDebug)
        CLPrint("Defining/adding pref panel '%s'\n", panel->name);

    *scan = panel;
    return 1;
}

PrefPanel *Prefs_FindPanel(const char *name) {
    PrefPanel *scan;

    for (scan = panellist; scan; scan = scan->next) {
        if (!ustrcmp(scan->name, name))
            break;
    }

    return scan;
}