-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjamnote.c
More file actions
336 lines (298 loc) · 8.01 KB
/
jamnote.c
File metadata and controls
336 lines (298 loc) · 8.01 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************/
/* */
/* Module: jamnote.c */
/* */
/* Copyright (C) Altera Corporation 1997 */
/* */
/* Description: Functions to extract NOTE fields from an JAM program */
/* */
/****************************************************************************/
/****************************************************************************/
/* */
/* Actel version 1.1 May 2003 */
/* */
/****************************************************************************/
#include "jamexprt.h"
#include "jamdefs.h"
#include "jamexec.h"
#include "jamutil.h"
/****************************************************************************/
/* */
BOOL jam_get_note_key
(
char *statement_buffer,
long *key_begin,
long *key_end
)
/* */
/* Description: This function finds the note key name in the statement */
/* buffer and returns the start and end offsets */
/* */
/* Returns: TRUE for success, FALSE if key not found */
/* */
/****************************************************************************/
{
int index = 0;
BOOL quoted_string = FALSE;
index = jam_skip_instruction_name(statement_buffer);
/*
* Check if key string has quotes
*/
if ((statement_buffer[index] == JAMC_QUOTE_CHAR) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
quoted_string = TRUE;
++index;
}
/*
* Mark the beginning of the key string
*/
*key_begin = index;
/*
* Now find the end of the key string
*/
if (quoted_string)
{
/* look for matching quote */
while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
(statement_buffer[index] != JAMC_QUOTE_CHAR) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
++index;
}
if (statement_buffer[index] == JAMC_QUOTE_CHAR)
{
*key_end = index;
}
}
else
{
/* look for white space */
while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
(!jam_isspace(statement_buffer[index])) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
++index; /* skip over white space */
}
if (jam_isspace(statement_buffer[index]))
{
*key_end = index;
}
}
return ((*key_end > *key_begin) ? TRUE : FALSE);
}
/****************************************************************************/
/* */
BOOL jam_get_note_value
(
char *statement_buffer,
long *value_begin,
long *value_end
)
/* */
/* Description: Finds the value field of a NOTE. Could be enclosed in */
/* quotation marks, or could not be. Must be followed by */
/* a semicolon. */
/* */
/* Returns: TRUE for success, FALSE for failure */
/* */
/****************************************************************************/
{
int index = 0;
BOOL quoted_string = FALSE;
BOOL status = FALSE;
/* skip over white space */
while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
(jam_isspace(statement_buffer[index])) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
++index;
}
/*
* Check if value string has quotes
*/
if ((statement_buffer[index] == JAMC_QUOTE_CHAR) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
quoted_string = TRUE;
++index;
}
/*
* Mark the beginning of the value string
*/
*value_begin = index;
/*
* Now find the end of the value string
*/
if (quoted_string)
{
/* look for matching quote */
while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
(statement_buffer[index] != JAMC_QUOTE_CHAR) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
++index;
}
if (statement_buffer[index] == JAMC_QUOTE_CHAR)
{
*value_end = index;
status = TRUE;
++index;
}
}
else
{
/* look for white space or semicolon */
while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
(statement_buffer[index] != JAMC_SEMICOLON_CHAR) &&
(!jam_isspace(statement_buffer[index])) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
++index; /* skip over non-white space */
}
if ((statement_buffer[index] == JAMC_SEMICOLON_CHAR) ||
(jam_isspace(statement_buffer[index])))
{
*value_end = index;
status = TRUE;
}
}
if (status)
{
while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
(jam_isspace(statement_buffer[index])) &&
(index < JAMC_MAX_STATEMENT_LENGTH))
{
++index; /* skip over white space */
}
/*
* Next character must be semicolon
*/
if (statement_buffer[index] != JAMC_SEMICOLON_CHAR)
{
status = FALSE;
}
}
return (status);
}
/****************************************************************************/
/* */
JAM_RETURN_TYPE jam_get_note
(
char *program,
long program_size,
long *offset,
char *key,
char *value,
int length
)
/* */
/* Description: Gets key and value of NOTE fields in the JAM file. */
/* Can be called in two modes: if offset pointer is NULL, */
/* then the function searches for note fields which match */
/* the key string provided. If offset is not NULL, then */
/* the function finds the next note field of any key, */
/* starting at the offset specified by the offset pointer. */
/* */
/* Returns: JAMC_SUCCESS for success, else appropriate error code */
/* */
/****************************************************************************/
{
JAM_RETURN_TYPE status = JAMC_SUCCESS;
char statement_buffer[JAMC_MAX_STATEMENT_LENGTH + 1];
char label_buffer[JAMC_MAX_NAME_LENGTH + 1];
JAME_INSTRUCTION instruction = JAM_ILLEGAL_INSTR;
long key_begin = 0L;
long key_end = 0L;
long value_begin = 0L;
long value_end = 0L;
BOOL done = FALSE;
char *tmp_program = jam_program;
long tmp_program_size = jam_program_size;
long tmp_current_file_position = jam_current_file_position;
long tmp_current_statement_position = jam_current_statement_position;
long tmp_next_statement_position = jam_next_statement_position;
jam_program = program;
jam_program_size = program_size;
jam_current_statement_position = 0L;
jam_next_statement_position = 0L;
if (offset == NULL)
{
/*
* We will search for the first note with a specific key, and
* return only the value
*/
status = jam_seek(0L);
jam_current_file_position = 0L;
}
else
{
/*
* We will search for the next note, regardless of the key, and
* return both the value and the key
*/
status = jam_seek(*offset);
jam_current_file_position = *offset;
}
/*
* Get program statements and look for NOTE statements
*/
while ((!done) && (status == JAMC_SUCCESS))
{
status = jam_get_statement(statement_buffer, label_buffer);
if (status == JAMC_SUCCESS)
{
instruction = jam_get_instruction(statement_buffer);
if (instruction == JAM_NOTE_INSTR)
{
if (jam_get_note_key(statement_buffer, &key_begin, &key_end))
{
statement_buffer[key_end] = JAMC_NULL_CHAR;
if ((offset != NULL) || (jam_stricmp(
key, &statement_buffer[key_begin]) == 0))
{
if (jam_get_note_value(&statement_buffer[key_end + 1],
&value_begin, &value_end))
{
done = TRUE;
value_begin += (key_end + 1);
value_end += (key_end + 1);
statement_buffer[value_end] = JAMC_NULL_CHAR;
if (offset != NULL)
{
*offset = jam_current_file_position;
}
}
else
{
status = JAMC_SYNTAX_ERROR;
}
}
}
else
{
status = JAMC_SYNTAX_ERROR;
}
}
}
}
/*
* Copy the key and value strings into buffers provided
*/
if (done && (status == JAMC_SUCCESS))
{
if (offset != NULL)
{
/* only copy the key string if we were looking for all NOTEs */
jam_strncpy(
key, &statement_buffer[key_begin], JAMC_MAX_NAME_LENGTH);
}
jam_strncpy(value, &statement_buffer[value_begin], length);
}
jam_program = tmp_program;
jam_program_size = tmp_program_size;
jam_current_file_position = tmp_current_file_position;
jam_current_statement_position = tmp_current_statement_position;
jam_next_statement_position = tmp_next_statement_position;
return (status);
}