summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/Project/CLFiles.c
blob: e651c733e296ebace5020c52f8726eba0b10f5b8 (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
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
#include "cmdline.h"

File *File_New(void) {
    File *file;
    file = xmalloc(NULL, sizeof(File));
    if (!file) {
        return NULL;
    } else {
        memset(file, 0, sizeof(File));
        return file;
    }
}

void File_Free(File *file) {
    if (file) {
        if (file->textdata) {
            DisposeHandle(file->textdata);
            file->textdata = NULL;
        }
        if (file->objectdata) {
            DisposeHandle(file->objectdata);
            file->objectdata = NULL;
        }
        if (file->browsedata) {
            DisposeHandle(file->browsedata);
            file->browsedata = NULL;
        }
        xfree(file);
    }
}

Boolean Files_Initialize(Files *this) {
    OS_ASSERT(47, this != NULL);
    return 1;
}

Boolean Files_Terminate(Files *this) {
    File *file;
    File *next;

    OS_ASSERT(56, this != NULL);

    for (file = this->fileList; file; file = next) {
        next = file->next;
        File_Free(file);
    }

    return 1;
}

Boolean Files_AddFile(Files *this, File *file) {
    return Files_InsertFile(this, file, this->fileCount);
}

Boolean Files_InsertFile(Files *this, File *file, SInt32 position) {
    File **scan;

    OS_ASSERT(80, this != NULL);
    OS_ASSERT(81, file != NULL);

    if (position < 0)
        position = 0;
    else if (position > this->fileCount)
        position = this->fileCount;

    scan = &this->fileList;
    while (position > 0) {
        scan = &(*scan)->next;
        position--;
    }

    file->filenum = this->fileCount++;
    file->next = *scan;
    *scan = file;
    return 1;
}

File *Files_GetFile(Files *this, SInt32 filenum) {
    File *file;

    OS_ASSERT(104, this != NULL);
    OS_ASSERT(105, filenum >= 0);

    file = this->fileList;
    while (file && file->filenum != filenum)
        file = file->next;
    return file;
}

File *Files_FindFile(Files *this, OSSpec *spec) {
    File *file;

    file = this->fileList;
    while (file && !OS_EqualSpec(&file->srcfss, spec))
        file = file->next;
    return file;
}

int Files_Count(Files *this) {
    OS_ASSERT(127, this != NULL);

    return this->fileCount;
}

Boolean VFiles_Initialize(VFile **list) {
    *list = NULL;
    return 1;
}

void VFiles_Terminate(VFile **list) {
    VFile *next;

    while (*list) {
        next = (*list)->next;
        DisposeHandle((*list)->data);
        xfree(*list);
        list = &next;
    }

    *list = NULL;
}

VFile *VFile_New(const char *name, Handle data) {
    VFile *vf;

    vf = xmalloc(NULL, sizeof(VFile));
    if (!vf)
        return NULL;

    strncpy(vf->displayName, name, sizeof(vf->displayName));
    vf->displayName[sizeof(vf->displayName) - 1] = 0;

    vf->data = NewHandle(0);
    if (!vf->data || HandAndHand(data, vf->data)) {
        xfree(vf);
        return NULL;
    }

    FixTextHandle(vf->data);
    vf->next = NULL;
    return vf;
}

Boolean VFiles_Add(VFile **list, VFile *entry) {
    VFile **scan = list;
    while (*scan)
        scan = &(*scan)->next;
    *scan = entry;
    return 1;
}

VFile *VFiles_Find(VFile *list, const char *name) {
    VFile *scan = list;
    while (scan) {
        if (!strcmp(scan->displayName, name))
            break;
        scan = scan->next;
    }
    return scan;
}