-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.c
219 lines (196 loc) · 4.83 KB
/
files.c
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
// Copyright (C) 1999-2012 Core Technologies.
//
// This file is part of tpasm.
//
// tpasm is free software; you can redistribute it and/or modify
// it under the terms of the tpasm LICENSE AGREEMENT.
//
// tpasm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// tpasm LICENSE AGREEMENT for more details.
//
// You should have received a copy of the tpasm LICENSE AGREEMENT
// along with tpasm; see the file "LICENSE.TXT".
// File handling
#include "include.h"
#define PATH_SEP '/' // path separation character (change this for your OS if needed)
struct PATH_HEADER
{
PATH_HEADER
*nextPath;
char
pathName[1]; // variable length path name
};
static PATH_HEADER
*topPath,
*bottomPath;
void CloseTextOutputFile(FILE *file)
// close the text output file
{
fclose(file);
}
FILE *OpenTextOutputFile(const char *name)
// Open a file for writing text output into
// If there is a problem, complain and return NULL
{
FILE
*file;
if(!(file=fopen(name,"w")))
{
ReportComplaint(true,"Could not open file '%s'\nOS Reports: %s\n",name,strerror(errno));
}
return(file);
}
void CloseBinaryOutputFile(FILE *file)
// Close the binary output file
{
fclose(file);
}
FILE *OpenBinaryOutputFile(const char *name)
// Open a file for writing binary output into
// If there is a problem, complain and return NULL
{
FILE
*file;
if(!(file=fopen(name,"wb")))
{
ReportComplaint(true,"Could not open file '%s'\nOS Reports: %s\n",name,strerror(errno));
}
return(file);
}
static SYM_TABLE_NODE *CreateFileNameSymbol(const char *name)
// Create (or locate) an entry in the global file name symbol table for name
// NOTE: this table is used to keep track of all files accessed during assembly (across
// all passes).
// If there is a problem, return NULL
{
SYM_TABLE_NODE
*node;
node=STFindNode(fileNameSymbols,name);
if(!node) // if node could not be located, try to create one
{
node=STAddEntryAtEnd(fileNameSymbols,name,NULL);
}
return(node);
}
void CloseSourceFile(FILE *file)
// Close the source file (leave symbol table entry around because
// things created when parsing the file are still referencing it)
{
fclose(file);
}
FILE *OpenSourceFile(const char *name,bool huntForIt,SYM_TABLE_NODE **fileNameSymbol)
// Open a source/include file
// If there is a problem, complain and return NULL
// if huntForIt is true, then look through the include paths trying to locate
// it.
// If a file is successfully opened, a pointer to it is returned, along with
// a pointer to a symbol table entry for its name
{
FILE
*file;
char
newPath[MAX_FILE_PATH];
unsigned int
nameLength;
PATH_HEADER
*path;
strcpy(newPath,name);
if(!(file=fopen(newPath,"rb"))) // try to open the passed file
{
if(huntForIt) // if we failed, see if we need to hunt for it
{
nameLength=strlen(name);
path=topPath;
while(path&&!file)
{
if(nameLength+strlen(path->pathName)<sizeof(newPath)) // make sure what we are about to do will not overflow
{
sprintf(newPath,"%s%s",path->pathName,name);
file=fopen(newPath,"rb");
}
path=path->nextPath;
}
}
}
if(file)
{
if(((*fileNameSymbol)=CreateFileNameSymbol(newPath)))
{
return(file);
}
else
{
AssemblyComplaint(NULL,true,"Failed to create file name symbol table entry\n");
}
fclose(file);
}
else
{
AssemblyComplaint(NULL,true,"Could not open source file '%s': %s\n",name,strerror(errno));
}
return(NULL);
}
bool AddIncludePath(const char *pathName)
// Add path to the list of paths to be searched for include files
// If there is a problem, return false
{
unsigned int
length;
PATH_HEADER
*path;
bool
addPathSep;
length=strlen(pathName);
addPathSep=false;
if(length&&pathName[length-1]!=PATH_SEP) // see if we need to add a path separator to the end of the path
{
addPathSep=true;
length++; // make room for it
}
if((path=(PATH_HEADER *)NewPtr(sizeof(PATH_HEADER)+length)))
{
strcpy(&path->pathName[0],pathName); // copy over the path name
if(addPathSep) // see if need to add seperator
{
path->pathName[length-1]=PATH_SEP;
path->pathName[length]='\0';
}
// link this to the list at the end (so they are searched in the order given)
path->nextPath=NULL;
if(bottomPath)
{
bottomPath->nextPath=path;
bottomPath=path;
}
else
{
topPath=bottomPath=path;
}
}
return(path!=NULL);
}
void UnInitFiles()
// undo what InitFiles did
{
PATH_HEADER
*nextPath;
while(topPath)
{
nextPath=topPath->nextPath;
DisposePtr(topPath);
topPath=nextPath;
}
STDisposeSymbolTable(fileNameSymbols); // get rid of the file names symbol table
}
bool InitFiles()
// Initialize file handling
{
if((fileNameSymbols=STNewSymbolTable(100)))
{
topPath=bottomPath=NULL;
return(true);
}
return(false);
}