PmAbcMagic.icl 19.6 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
implementation module PmAbcMagic

import StdArray, StdEnum, StdFile, StdOverloaded, StdInt, StdMisc
import StdBool, StdList
import StdMaybe

import UtilStrictLists
from PmPath import MakeImpPathname, MakeDefPathname

import PmFileInfo
import UtilNewlinesFile
import PmDirCache

14
from PmTypes	import :: LinkObjFileName, :: LinkLibraryName
15
from StdLibMisc	import :: Date{..}, :: Time{..}
Diederik van Arkel's avatar
Diederik van Arkel committed
16
17
18
19
20
21
22
23
24
25
26
27
28

//import RWSDebug
(->>) l r :== l

//--	ABCOptions: Compiler options that are stored in the abc file

:: ABCOptions =
	{ abcMemoryProfile 			:: !Bool
	, abcTimeProfile			:: !Bool
	, abcStrictnessAnalysis		:: !Bool
	, abcGiveWarnings			:: !Bool
	, abcBeVerbose				:: !Bool
	, abcGenerateComments		:: !Bool
29
30
	, abcReuseUniqueNodes 		:: !Bool
	, abcFusion					:: !Bool
31
	, abc64Bits					:: !Bool
Diederik van Arkel's avatar
Diederik van Arkel committed
32
33
34
35
36
37
38
39
40
41
42
	}

DefaultABCOptions :: ABCOptions;
DefaultABCOptions =
	{ abcMemoryProfile			= False
	, abcTimeProfile			= False
	, abcStrictnessAnalysis		= True
	, abcGiveWarnings			= False
	, abcBeVerbose				= False
	, abcGenerateComments		= False
	, abcReuseUniqueNodes 		= False
43
	, abcFusion					= False
44
	, abc64Bits					= False
Diederik van Arkel's avatar
Diederik van Arkel committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	}

//-- abc file handling


//
//	Extract the following info from the ABC file:
//		- Does it contain sequential code and stack layout info
//		- Is it a system file
//		- Compiler version it was generated by
//		- Compiler options it was generated with
//

DebugMask					:== 0;
DontReuseUniqueNodesMask	:== 1;
ParallelMask				:== 2;
NoMemoryProfileMask			:== 3;
StrictnessMask				:== 4;
NoTimeProfileMask			:== 5;
VerboseMask					:== 6;
WarningsMask				:== 7;
SystemMask					:== 8;
67
FusionOffset				:== 9;
68
Abc64BitsOffset				:==10;
69
70

MinimumNrOfOptions			:== 9;
71
NrOfOptions					:== 11;
Diederik van Arkel's avatar
Diederik van Arkel committed
72
73
74

Combined :: !Pathname !DATE !ABCCache !Files -> (!((!Bool, !Bool, !Int, !ABCOptions),(!List Modulename, !Maybe ModuleDate, !List ModuleDate, !List LinkObjFileName, !List LinkLibraryName),!ABCCache),!Files)
Combined path date abccache files
75
	# (opened, file, files)	= fopen path FReadData files
Diederik van Arkel's avatar
Diederik van Arkel committed
76
77
78
79
80
81
	| not opened
		= ((dummyCinf,dummyDinf,abccache),files)
	# (file,cInfo)
							= Read_Version_and_Options file
	# (file,dInfo)			= ParseABCDependencies file
	# (_,files)				= fclose file files
82
83
84
85
86
87
88
89

	#! maxi=abccache.maxi
	# (cache_index,abccache) = search_path_in_cache path maxi abccache								
	| cache_index<0
		#! abccache = AC_Add path date dInfo abccache
		= ((cInfo,dInfo,abccache),files)
		#! abccache = {abccache & cache.[cache_index]={c_path=path,c_date=date,c_info=dInfo}}
		= ((cInfo,dInfo,abccache),files)
Diederik van Arkel's avatar
Diederik van Arkel committed
90
91
92
93
94
95
96
97
98
99
100
101
where
	dummyCinf = (False,False,-1,DefaultABCOptions)
	dummyDinf = dummyDepInfo

/*
everything in place to add abc_opts caching...
need to check if cache size doesn't increase dramatically and destroys OK behaviour...
*/
GetABCCompiledInfo :: !Bool !Pathname !ABCCache !Files -> ((!Bool, !Bool, !Int, !ABCOptions, !ABCCache),!Files)
GetABCCompiledInfo update path cache files
	| (not update) && in_cache
		= ((c_sys,c_seq,c_ver,c_opt,cache),files)
102
	#	(opened, file, files)	= fopen path FReadData files
Diederik van Arkel's avatar
Diederik van Arkel committed
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
	| not opened
		= ((False,False,-1,DefaultABCOptions,cache),files)
	// otherwise
	#	(file,(sys, stack_seq,version,options))	= Read_Version_and_Options file
		(_,files)								= fclose file files
	= ((sys, stack_seq,version,options,cache),files)
where
	in_cache = False
	c_sys = False
	c_seq = False
	c_ver = -1
	c_opt = DefaultABCOptions
	
Read_Version_and_Options :: !*File -> (!*File,!(!Bool,!Bool,!Int,!ABCOptions));
Read_Version_and_Options file
	#	(eof,file)										= fend file
	| eof
		= (file,(False,False,-1,DefaultABCOptions))
	#	(str,file)										= readLine file
	#	(end_of_info,sys,stack_seq,version,abcOptions)	= Find_Version_and_Options str;
	| end_of_info
		= (file,(sys,stack_seq,version,abcOptions))
	= Read_Version_and_Options file
where
	Find_Version_and_Options :: !String -> (!Bool,!Bool,!Bool,!Int,!ABCOptions);
	Find_Version_and_Options str
129
130
131
132
133
134
135
136
137
138
		| match_endinfo < len_str
			= (True,False,False,-1,DefaultABCOptions);
		| match_options - start`` < MinimumNrOfOptions
			= (True,False,False,-1,DefaultABCOptions);
			# version		= SubStringToInt 0 start` (dec match_version) str;
			  abcOptions	= (StringToCompilerOptions start`` match_options str);
			  stack_seq		= ProjectABCFile start`` str;
			  sys			= SystemABCFile start`` str;
			= (True,sys,stack_seq,version,abcOptions);
	where
Diederik van Arkel's avatar
Diederik van Arkel committed
139
140
141
142
143
144
145
146
147
148
		len_str			= size str;
		start			= SkipSpaces 0 len_str str;
		stop_endinfo	= start + 7;
		stop_comp		= start + 4;
		match_endinfo	= MatchS start stop_endinfo 8 len_str ".endinfo" str;
		match_comp		= MatchS start stop_comp 5 len_str  ".comp" str;
		start`			= SkipSpaces match_comp len_str str;
		match_version	= SkipDigits start` len_str str;
		start``			= SkipSpaces match_version len_str str;
		match_options	= SkipBits start`` len_str str;
149
150
151
152
153

StringToCompilerOptions :: !Int !Int !String -> ABCOptions;
StringToCompilerOptions start end opt
	= {	abcMemoryProfile 		= not (opt.[start+NoMemoryProfileMask]		== '1'),
		abcTimeProfile			= not (opt.[start+NoTimeProfileMask]		== '1'),
Diederik van Arkel's avatar
Diederik van Arkel committed
154
155
156
157
		abcStrictnessAnalysis	= opt .[start+StrictnessMask]				== '1',
		abcGiveWarnings			= opt .[start+WarningsMask]					== '1',
		abcBeVerbose			= opt .[start+VerboseMask]					== '1',
		abcGenerateComments		= opt .[start+DebugMask]					== '1',
158
		abcReuseUniqueNodes		= not (opt.[start+DontReuseUniqueNodesMask]	== '1'),
159
160
		abcFusion = start+FusionOffset<end && opt.[start+FusionOffset]=='1',
		abc64Bits = start+Abc64BitsOffset<end && opt.[start+Abc64BitsOffset]=='1'
161
	  };
Diederik van Arkel's avatar
Diederik van Arkel committed
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

ProjectABCFile :: !Int !String -> Bool;
ProjectABCFile start opt =	opt .[start+ParallelMask] == '0';

SystemABCFile :: !Int !String -> Bool;
SystemABCFile start opt = opt .[start+SystemMask] == '1';

SubStringToInt :: !Int !Int !Int String -> Int;
SubStringToInt acc start stop str
	| start > stop	= acc;
	# curc	= str.[start];
	| isDigit curc
		# acc`	= 10 * acc + toInt curc - toInt '0';
		= SubStringToInt acc` (inc start) stop str;
	= acc;
	

//--

:: ModuleDate :== DateTime

//-- Cached parse

:: *ABCCache =
	{ cache	:: !*{!ABCStuff}
	, curi	:: !Int
	, maxi	:: !Int
	}

191
ABCCacheSize :==  512			// fixed cache size => yuch!
Diederik van Arkel's avatar
Diederik van Arkel committed
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

:: ABCStuff =
	{ c_path :: !Pathname
	, c_date :: !DATE
	, c_info :: !ABCInfo
	}

:: ABCInfo :==
	( !List Modulename			// dep mods
	, !Maybe ModuleDate			// mod date
	, !List ModuleDate			// dep dates
	, !List LinkObjFileName		// dep objs
	, !List LinkLibraryName		// dep libs
	)

EmptyABCS =
	{ c_path = ""
	, c_date = NoDate
	, c_info =
	( Nil
	, Nothing
	, Nil
	, Nil
	, Nil
	)
	}
Diederik van Arkel's avatar
Diederik van Arkel committed
218
219

AC_Init :: ABCCache
Diederik van Arkel's avatar
Diederik van Arkel committed
220
AC_Init =
221
	{ cache = createArray ABCCacheSize EmptyABCS
Diederik van Arkel's avatar
Diederik van Arkel committed
222
223
224
225
	, maxi	= -1
	, curi	= 0
	}

Diederik van Arkel's avatar
Diederik van Arkel committed
226
AC_Add :: !Pathname !DATE !ABCInfo !*ABCCache -> *ABCCache
227
AC_Add path date info abccache
Diederik van Arkel's avatar
Diederik van Arkel committed
228
229
230
231
	#! c = abccache.cache
	#! i = abccache.curi
	#! m = abccache.maxi
	#! c = {c & [i] = newstuff}
232
	=	{ abccache
Diederik van Arkel's avatar
Diederik van Arkel committed
233
		& cache = c
Diederik van Arkel's avatar
Diederik van Arkel committed
234
		, curi = (inc i) rem ABCCacheSize
Diederik van Arkel's avatar
Diederik van Arkel committed
235
236
237
		, maxi = if (m < (dec ABCCacheSize)) i m
		}
where
Diederik van Arkel's avatar
Diederik van Arkel committed
238
	newstuff :: ABCStuff
Diederik van Arkel's avatar
Diederik van Arkel committed
239
240
241
242
243
244
245
246
	newstuff = {c_path=path,c_date=date,c_info=info}

ParseABCDependencies` :: !Pathname !DATE !*ABCCache !Files -> ((!Bool, !List Modulename, !Maybe ModuleDate, !List ModuleDate, !List LinkObjFileName, !List LinkLibraryName, !*ABCCache/*, !FileInfoCache*/), !Files);
ParseABCDependencies` path abcdate abccache files
	= AC_Look path abccache files
where
	AC_Look path abccache=:{maxi} files
		# date = abcdate
247
248
		# (cache_index,abccache) = search_path_in_cache path maxi abccache								
		| cache_index<0
249
			# (opened,file,files)		= fopen path FReadData files
Diederik van Arkel's avatar
Diederik van Arkel committed
250
251
252
253
254
255
256
			| not opened
				# (b,c,d,e,f)			= dummyDepInfo
				= ((False,b,c,d,e,f,abccache),files)
			# (file,(b,c,d,e,f))		= ParseABCDependencies file
			# (_,files)					= fclose file files
			#! abccache					= AC_Add path date (b,c,d,e,f) abccache
			= ((True,b,c,d,e,f,abccache),files)
257
258
259
260
261
262
263
264
265
266
267
268
		# ({c_date,c_info},abccache) = abccache!cache.[cache_index]
		| eqDate c_date date
			# (b,c,d,e,f)=c_info
			= ((True,b,c,d,e,f,abccache),files)
			# (opened,file,files)		= fopen path FReadData files
			| not opened
				# (b,c,d,e,f)			= dummyDepInfo
				= ((False,b,c,d,e,f,abccache),files)
			# (file,(b,c,d,e,f))		= ParseABCDependencies file
			# (_,files)					= fclose file files
			#! abccache = {abccache & cache.[cache_index]={c_path=path,c_date=date,c_info=(b,c,d,e,f)}}
			= ((True,b,c,d,e,f,abccache),files)
Diederik van Arkel's avatar
Diederik van Arkel committed
269
270
271
272
273
274
275
276
277
278

	eqDate l r
		=  l.exists == r.exists
		&& l.yy == r.yy
		&& l.mm == r.mm
		&& l.dd == r.dd
		&& l.DATE.h == r.DATE.h
		&& l.m == r.m
		&& l.s == r.s

279
280
281
282
283
284
285
286
search_path_in_cache :: !{#Char} !Int !*ABCCache -> *(!Int,!*ABCCache)
search_path_in_cache path n c
	| n < 0
		= (-1,c)
	| c.cache.[n].c_path == path
		= (n,c)
		= search_path_in_cache path (dec n) c

Diederik van Arkel's avatar
Diederik van Arkel committed
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
//	Find the module depencies in the .abc files

dummyDepInfo = (Nil, Nothing, Nil, Nil, Nil)

ParseABCDependencies
	:: !*File
	-> (!*File, !(!List Modulename, !Maybe ModuleDate, !List ModuleDate, !List LinkObjFileName, !List LinkLibraryName))
ParseABCDependencies file
	= Parse_lines file dummyDepInfo
where 
	Parse_lines
		::  !*File  !(!(List Modulename), !(Maybe ModuleDate), !(List ModuleDate), !(List LinkObjFileName), !(List LinkLibraryName))
		-> (!*File, !(!List Modulename, !Maybe ModuleDate, !List ModuleDate, !List LinkObjFileName, !List LinkLibraryName))
	Parse_lines file depInfo
		# (eof, file)				= fend file
		| eof
			= (file, depInfo)
		# (line, file) 				= readLine file
		  (depInfo, end_of_info)	= Parse_line line depInfo
		| end_of_info
			= (file, depInfo)
		= Parse_lines file depInfo
	
	Parse_line
		:: !String !(!(List Modulename), !(Maybe ModuleDate), !(List ModuleDate), !(List LinkObjFileName), !(List LinkLibraryName))
		-> (!(!List Modulename, !Maybe ModuleDate, !List ModuleDate, !List LinkObjFileName, !List LinkLibraryName), !Bool);
	Parse_line str (modnames, mmoddate, depdates, objnames, libnames)
		| match_endinfo < len_str
			=  ((modnames, mmoddate, depdates, objnames, libnames), True)
		| match_depend < len_str && last_q_depdate < len_str
			= ((modname :! modnames, mmoddate, S2DT depdate :! depdates, objnames, libnames), False)
		| match_depend < len_str && last_q_depend < len_str
			= ((modname :! modnames, mmoddate, emptydate :! depdates, objnames, libnames), False)
		| match_impobj < len_str && last_q_impobj < len_str
			= ((modnames, mmoddate, depdates, objname :! objnames, libnames), False)
		| match_implib < len_str && last_q_implib < len_str
			= ((modnames, mmoddate, depdates, objnames, libname :! libnames), False)
//		| match_date < len_str && last_q_moddate < len_str
//			= ((modnames, Just (S2DT moddate), depdates, objnames, libnames), False)
		| match_module < len_str && last_q_moddate < len_str
			#! moddate = S2DT moddate
			= ((modnames, Just moddate, depdates, objnames, libnames), False)
		=  ((modnames, mmoddate, depdates, objnames, libnames), False)
	where 
331
		emptydate = ({year=0,month=0,day=0,dayNr=0},{hours=0,minutes=0,seconds=0})
Diederik van Arkel's avatar
Diederik van Arkel committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
		len_str			= size str
		start			= SkipSpaces 0 len_str str
		stop_endinfo	= start +  7; // dec (size ".endinfo")
		stop_depend		= startplussix; // dec (size ".depend")
		stop_importobj	= startplussix; // dec (size ".impobj")
		stop_importlib	= startplussix; // dec (size ".implib")
//		stop_date		= start +  4; // dec (size ".date")
		stop_module		= startplussix; // dec (size ".module")
		startplussix	= start +  6;
		match_endinfo	= MatchS start stop_endinfo 8 len_str ".endinfo" str
		match_depend	= MatchS start stop_depend 7 len_str ".depend" str
		match_impobj	= MatchS start stop_importobj 7 len_str ".impobj" str
		match_implib	= MatchS start stop_importlib 7 len_str ".implib" str
//		match_date		= MatchS start stop_date 5 len_str ".date" str
		match_module	= MatchS start stop_module 7 len_str ".module" str
		(last_q_depend, modname)
						= characters_between_apostrophes match_depend len_str str
		(last_q_impobj, objname)
						= characters_between_apostrophes match_impobj len_str str	
		(last_q_implib, libname)
						= characters_between_apostrophes match_implib len_str str
		(last_q_depdate, depdate)
						= characters_between_apostrophes (inc last_q_depend) len_str str
//		(last_q_moddate, moddate)
//						= characters_between_apostrophes match_date len_str str
		(last_q_modname, mainmodname)
						= characters_between_apostrophes match_module len_str str
		(last_q_moddate, moddate)
						= characters_between_apostrophes (inc last_q_modname) len_str str

characters_between_apostrophes :: !.Int !.Int !.String -> .(!Int, !String)
characters_between_apostrophes after_keyword_pos len_str str
	= (last_q, str % (inc first_q, dec last_q))
where
	start`			= SkipSpaces after_keyword_pos len_str str
	first_q			= FindQuoteChar str len_str start`
	last_q			= FindQuoteChar str len_str (inc first_q)
	
FindChar	:: !Char !.String !.Int !Int -> Int;
FindChar c line linelen pos
	| pos >= linelen		=  pos;
	| c ==  line.[pos]		=  pos;
							=  FindChar c line linelen (inc pos);
	
//	FindQuoteChar	:: !String !Int !Int -> Int;
FindQuoteChar str len pos	:== FindChar '\"' str len pos;
	
//--

/*
ABC dates format:
.date "module.icl modified date,time"
.depend "modulename" "dcl modified date,time"
*/
DT2S :: !DateTime -> String
DT2S (date,time) = "\"" +++ yyyymmddnhhmmss +++"\""
where
	yyyymmddnhhmmss = yyyy +++ mm +++ dd /*+++ nn*/ +++ hs +++ ms +++ ss
390
391
392
393
394
395
396
	yyyy = pad 4 date.year
	mm = pad 2 date.month
	dd = pad 2 date.day
//	nn = pad 1 date.dayNr
	hs = pad 2 time.hours
	ms = pad 2 time.minutes
	ss = pad 2 time.seconds
Diederik van Arkel's avatar
Diederik van Arkel committed
397
398
399
400
401
402
403
404
405
	pad n x
		# s = toString x
		# l = size s
		# p = {'0' \\ i <- [1..n-l]}
		= p+++s

S2DT :: !String -> DateTime
S2DT s = (date,time)
where
406
407
	date = {year=yy,month=mm,day=dd,dayNr=nn}
	time = {hours=hs,minutes=ms,seconds=ss}
Diederik van Arkel's avatar
Diederik van Arkel committed
408
409
410
411
412
413
414
415
416
417
418
419
	yy = toInt (s%(0,3))
	mm = toInt (s%(4,5))
	dd = toInt (s%(6,7))
	nn = 0//toInt (s%(8,8))
	hs = toInt (s%(8,9))	//(s%(9,10))
	ms = toInt (s%(10,11))	//(s%(11,12))
	ss = toInt (s%(12,13))	//(s%(13,14))

//--

PatchABCDates :: !Pathname !*DirCache !*Files -> ((Bool,*DirCache),*Files)
PatchABCDates abcpath dircache files
420
	#! (opened,file,files)		= fopen abcpath FReadData files
Diederik van Arkel's avatar
Diederik van Arkel committed
421
422
423
424
425
426
	| not opened
		=  ((False, dircache),files)
	# (file, lines,dircache)	= Patch_lines False file Nil dircache
	# (closed,files)			= fclose file files
	| not closed
		=  ((False, dircache),files)
427
	#! (opened,file,files)		= fopen abcpath FWriteData files
Diederik van Arkel's avatar
Diederik van Arkel committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
	| not opened
		= ((False, dircache),files)
	#! (written,file)			= Write_lines file lines
	#! (closed,files)			= fclose file files
	=  ((written && closed, dircache),files)
where
	modname = RemovePath (MakeImpPathname abcpath)

	dateline modname dircache
		# (_,_,moddate,dircache) = DC_Search modname dircache
		= ("|| .module "+++ modname +++ " \"" +++ modname+++ "\" "+++DT2S moddate+++"\n",dircache)
//		= ("|| .date "+++DT2S moddate+++"\n",dircache)

	dependline depname dircache
		# (_,_,depdate,dircache) = DC_Search (MakeDefPathname depname) dircache
		= (".depend \""+++depname+++"\" || "+++DT2S depdate+++"\n",dircache)

	Patch_lines datedone file lines dircache
		#	(eof, file)									= fend file
		| eof
			| datedone
				# lines = Reverse lines
				= (file, lines,dircache)
			# (dl,dircache) = dateline modname dircache
			# lines = dl :! lines
			# lines = Reverse lines
			= (file, lines,dircache)
		#	(line, file) 								= readLine file
			(end_of_info,datedone,line,dircache)		= Patch_line datedone line dircache
		| end_of_info
			| datedone
				# lines = line :! lines
				= Copy_lines file lines dircache
			# (dl,dircache) = dateline modname dircache
			# lines = dl :! lines
			# lines = line :! lines
			= Copy_lines file lines dircache
		= Patch_lines datedone file (line :! lines) dircache
	Copy_lines file lines dircache
		#	(eof, file)									= fend file
		| eof
			# lines = Reverse lines
			= (file, lines,dircache)
		#	(line, file) 								= readLine file
		= Copy_lines file (line :! lines) dircache
	Write_lines file Nil
		= (True,file)
	Write_lines file (line :! lines)
		# file = file <<< line
		= Write_lines file lines
	Patch_line datedone str dircache
		| match_endinfo < len_str
			=  (True,datedone,str,dircache)
		| match_depend < len_str && last_q_depend < len_str
			# (line,dircache) = dependline modname dircache
			= (False,datedone,line,dircache)	// patch on mod date
		| match_date < len_str && last_q_date < len_str
			# (line,dircache) = dateline modname dircache
			= (False,True,line,dircache)
		=  (False,datedone,str,dircache)
	where 
		len_str			= size str
		start			= SkipSpaces 0 len_str str
		stop_endinfo	= start +  7; // dec (size ".endinfo")
		stop_depend		= start +  6; // dec (size ".depend")
		stop_date		= start +  4; // dec (size ".date")
		match_endinfo	= MatchS start stop_endinfo 8 len_str ".endinfo" str
		match_depend	= MatchS start stop_depend 7 len_str ".depend" str
		match_date		= MatchS start stop_date 5 len_str ".date" str
		(last_q_depend, modname)
						= characters_between_apostrophes match_depend len_str str
		(last_q_date, _/*date*/)
						= characters_between_apostrophes match_date len_str str
Diederik van Arkel's avatar
Diederik van Arkel committed
501

Diederik van Arkel's avatar
Diederik van Arkel committed
502
503
504
505
506
507
508
509

// patch abc file of system module for change in profiling settings

PatchSystemABC :: !Int !Bool !Pathname /*!Bool*/ !Bool !*Files -> (!Bool, !*Files)
PatchSystemABC version doPatch abcPath /*memoryProfile*/ timeProfile files
	| not doPatch
		=	(True, files)
	# (opened, file, files)
510
		=	fopen abcPath FReadData files
Diederik van Arkel's avatar
Diederik van Arkel committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
	| not opened
		=	(False, files)  ->> (abcPath +++ " not opened\n")
	# (patched, file)
		=	patchFile file
	# (closed, files)
		=	fclose file files
	= (patched && closed, files)
where
	patchFile :: *File -> (Bool, *File)
	patchFile file
		# (firstLine, file)
			=	freadline file	//readLine file
		# (patched, firstLine)
			=	patchLine firstLine//(RemoveNewlineChar firstLine)	// ??? RemoveNewlineChar ???
		| not patched
			=	(False, file)  ->> (abcPath +++ " not patched\n")
		# (reopened, file)
			=	freopen file FAppendData
		| not reopened
			=	(False, file)  ->> (abcPath +++ " not reopened\n")

		# (sought, file)
			=	fseek file 0 FSeekSet
		| not sought
			=	(False, file)  ->> (abcPath +++ " not sought\n")
		# file
			=	fwrites firstLine file
		=	(True, file)
539

Diederik van Arkel's avatar
Diederik van Arkel committed
540
541
	patchLine :: {#Char} -> (Bool, {#Char})
	patchLine line
542
		# (found, offset) = findOptionStringOffset line
Diederik van Arkel's avatar
Diederik van Arkel committed
543
		| found
544
			=	(found, {copy line & /*[offset+NoMemoryProfileMask] = if memoryProfile '0' '1',*/
Diederik van Arkel's avatar
Diederik van Arkel committed
545
546
547
548
549
550
551
552
				                         [offset+NoTimeProfileMask] = if timeProfile '0' '1'})
			=	(False, line)

	copy array
		=	{el \\ el <-: array}

	findOptionStringOffset :: {#Char} -> (Bool, Int)
	findOptionStringOffset string
553
		=	(offset+MinimumNrOfOptions < stringLength, offset)
Diederik van Arkel's avatar
Diederik van Arkel committed
554
		where
Diederik van Arkel's avatar
Diederik van Arkel committed
555
556
			versionString	= toString version
			versionSize		= size versionString
Diederik van Arkel's avatar
Diederik van Arkel committed
557
			offset
Diederik van Arkel's avatar
Diederik van Arkel committed
558
559
560
561
562
563
				# init_offset	= SkipSpaces 0 stringLength string
				# comp_offset	= MatchS init_offset (init_offset + 4) 5 stringLength ".comp" string
				# next_offset	= SkipSpaces comp_offset stringLength string
				# vers_offset	= MatchS next_offset (next_offset + versionSize - 1) versionSize stringLength versionString string
				=	SkipSpaces vers_offset stringLength string

Diederik van Arkel's avatar
Diederik van Arkel committed
564
			stringLength
565
				=	size string
Diederik van Arkel's avatar
Diederik van Arkel committed
566

Diederik van Arkel's avatar
Diederik van Arkel committed
567
568
569
570
571
572
573
574
575
//==

SkipSpaces	:: !Int !Int !String -> Int;
SkipSpaces i len str | i >= len ||  str.[i]  <> ' '	=  i;
	                     							=  SkipSpaces (inc i) len str;
	                     										                     
SkipBits :: !Int !Int !String -> Int;
SkipBits i len str | i >= len || (c <> '0' && c <> '1')	= i;
	                    								= SkipBits (inc i) len str;
Diederik van Arkel's avatar
Diederik van Arkel committed
576
					where
Diederik van Arkel's avatar
Diederik van Arkel committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
	c	= str.[i];
	
	
SkipDigits :: !Int !Int !String -> Int;
SkipDigits i len str | i >= len || c < '0' || '9' < c	= i;
														= SkipDigits (inc i) len str;
	where 
	c	= str.[i];
	
MatchS	:: !Int !Int !Int !Int !String !String -> Int;
MatchS start stop patlen strlen pat str
	| stop >= strlen					= stop;
	| MatchS2 0 start patlen pat str	= inc stop;
										= MatchS (inc start) (inc stop) patlen strlen pat str;
where
	MatchS2 :: !Int !Int !Int !String !String -> Bool;
	MatchS2 patpos strpos patlen pat str
		| patpos >= patlen					= True;
		| pat.[patpos] == str.[strpos]		= MatchS2 (inc patpos) (inc strpos) patlen pat str;
											= False;
										
Diederik van Arkel's avatar
Diederik van Arkel committed
598