-
Notifications
You must be signed in to change notification settings - Fork 12
/
TADSInterface.m
2280 lines (1976 loc) · 111 KB
/
TADSInterface.m
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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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
501
502
503
504
505
506
507
508
509
510
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef TADSInterface < handle
% =====================================================================
% =========================== PROPERTIES ==============================
% =====================================================================
properties (Constant, Access = private)
% SETTINGS_FILE = fullfile(prefdir,'ADSInterfaceSettings.mat');
FVersion = '1.4'
FADSVersion = '2016.01, 2017, 2019, 2022'
% FToolboxAuthor = 'Oleg Iupikov, [email protected], [email protected]';
FToolboxAuthor = 'Oleg Iupikov, <a href="mailto:[email protected]">[email protected]</a>';
FOrganization = 'Chalmers University of Technology, Sweden';
FFirstEdit = '21-02-2018';
FLastEdit = '12-03-2022';
end
% properties that can be used from outside through dependent properties (see next section)
properties (Access = private)
% properties (Access = public) % TEMP!!!
FDataset
FDatasetFile
FNetlistFile
FMessageLevelOfDetails
FdMessageLevelOfDetails % same meaning as FMessageLevelOfDetails, but it is integer value in range 0...3. It is to speed-up printing function
FTimeFormat = 'hh:MM:ss';
FDisplayPrefix = '[ADS][#t] ';
end
% purely private properties
properties (Access = private)
FDatasetTextFile
end
% dependent properties
properties (Dependent)
Dataset
DatasetFile
NetlistFile
TimeFormat
DisplayPrefix
% Only levels 0 and 3 are implemented so far
MessageLevelOfDetails % 0 - "quiet" mode; 1 - only "main" messages; 2 - "detailed" messages; 3 - display "everything"
end
properties (Hidden)
tstClassTesting = false
end
% =====================================================================
% ======================== GET SET Methods ============================
% =====================================================================
methods
% -----------------------------------------------------------------
% Dataset
% -----------------------------------------------------------------
function data = get.Dataset(this)
data = this.FDataset;
end
% function this = set.(this, data)
% this.F = data;
% end
% -----------------------------------------------------------------
% DatasetFile
% -----------------------------------------------------------------
function data = get.DatasetFile(this)
data = this.FDatasetFile;
end
function set.DatasetFile(this, data)
assert(ischar(data), 'ADSInterface:ReadDataset:DatasetFileMustBeString', '"DatasetFile" must be a string.')
this.FDatasetFile = data;
end
% -----------------------------------------------------------------
% NetlistFile
% -----------------------------------------------------------------
function data = get.NetlistFile(this)
data = this.FNetlistFile;
end
function set.NetlistFile(this, data)
assert(ischar(data), 'ADSInterface:ReadDataset:NetlistFileFileMustBeString', '"NetlistFile" must be a string.')
this.FNetlistFile = data;
end
% -----------------------------------------------------------------
% MessageLevelOfDetails
% -----------------------------------------------------------------
function data = get.MessageLevelOfDetails(this)
data = this.FMessageLevelOfDetails;
end
function set.MessageLevelOfDetails(this, data)
ValidLODS = {'none', 'main', 'detailed', 'everything'};
if isnumeric(data)
intLOD = round(data);
assert(isscalar(intLOD) && (intLOD>=0) && (intLOD<=3), '"MessageLevelOfDetails" must be an integer scalar value in range [0...3].');
strLOD = ValidLODS{intLOD+1};
elseif ischar(data)
strLOD = validatestring(data, ValidLODS, 'set.MessageLevelOfDetails', '"MessageLevelOfDetails"');
intLOD = find(strcmp(ValidLODS,strLOD),1)-1;
end
% % Temporary: Current limitation check:
% assert(intLOD==0 || intLOD==3, 'Currently only MessageLevelOfDetails = 0 ("none") or = 3 ("everything") is implemented.');
this.FMessageLevelOfDetails = strLOD;
this.FdMessageLevelOfDetails = intLOD;
end
% -----------------------------------------------------------------
% TimeFormat
% -----------------------------------------------------------------
function data = get.TimeFormat(this)
data = this.FTimeFormat;
end
function set.TimeFormat(this, data)
% test that it is correct format
try
datestr(now,data); % test new format
this.FTimeFormat = data;
this.fprintf(3,'The time format has been changed.\n');
catch ME
error('Cannot change the time format. Is it correct?\nThe message is:\n%s',ME.message);
end
end
% -----------------------------------------------------------------
% DisplayPrefix
% -----------------------------------------------------------------
function data = get.DisplayPrefix(this)
data = this.FDisplayPrefix;
end
function set.DisplayPrefix(this, data)
assert(ischar(data), 'ADSInterface:Settings:DisplayPrefixNotChar', '"DisplayPrefix" must be a string.')
this.FDisplayPrefix = data;
end
% % -----------------------------------------------------------------
% %
% % -----------------------------------------------------------------
% function data = get.(this)
% data = this.F;
% end
% function set.(this, data)
% this.F = data;
% end
% % -----------------------------------------------------------------
% %
% % -----------------------------------------------------------------
% function data = get.(this)
% data = this.F;
% end
% function set.(this, data)
% this.F = data;
% end
end
% =====================================================================
% ======================== STATIC Methods ============================
% =====================================================================
methods (Static)
function ExitCode = RunProcessAsync(CmdLine, cArguments, WorkingDir, OutputLineCallbackFn)
import java.lang.*
import java.io.*
% create process builder
pb = ProcessBuilder([{CmdLine}, cArguments]);
pb.directory(File(WorkingDir)); % java.io.File
% start the process
process = pb.start();
% and make sure that process will be killed even if user terminates the simulation by Ctrl+C
finishup = onCleanup(@()process.destroy());
is = process.getInputStream();
reader = BufferedReader(InputStreamReader(is));
Running = true;
while Running
% check that process is still running
try
ExitCode = process.exitValue;
Running = false;
catch
Running = true;
end
% process line
tline = char(reader.readLine());
OutputLineCallbackFn(tline);
tline = char(reader.readLine());
while ~isempty(tline)
OutputLineCallbackFn(tline);
tline = char(reader.readLine());
end
end
% close the input stream
is.close();
end
function str = SecToString(sec, ShowMSec)
if nargin<2, ShowMSec = false; end
[Y, M, D, H, MN, S] = datevec(sec/3600/24);
str = '';
if Y>0, str = sprintf('%s%i years ',str,Y); end
if M>0, str = sprintf('%s%i months ',str,M); end
if D>0, str = sprintf('%s%i days ',str,D); end
if H>0, str = sprintf('%s%i hours ',str,H); end
if MN>0, str = sprintf('%s%02i min ',str,MN); end
if ShowMSec
Sr = floor(S);
if Sr>0, str = sprintf('%s%02i sec ',str,Sr); end
str = sprintf('%s%03i msec ',str,round((S-Sr)*1000));
else
str = sprintf('%s%02i sec ',str,round(S));
end
str = str(1:end-1);
end
function File = GetFullPath(File, Style)
% GetFullPath - Get absolute canonical path of a file or folder
% Absolute path names are safer than relative paths, when e.g. a GUI or TIMER
% callback changes the current directory. Only canonical paths without "." and
% ".." can be recognized uniquely.
% Long path names (>259 characters) require a magic initial key "\\?\" to be
% handled by Windows API functions, e.g. for Matlab's FOPEN, DIR and EXIST.
%
% FullName = GetFullPath(Name, Style)
% INPUT:
% Name: String or cell string, absolute or relative name of a file or
% folder. The path need not exist. Unicode strings, UNC paths and long
% names are supported.
% Style: Style of the output as string, optional, default: 'auto'.
% 'auto': Add '\\?\' or '\\?\UNC\' for long names on demand.
% 'lean': Magic string is not added.
% 'fat': Magic string is added for short names also.
% The Style is ignored when not running under Windows.
%
% OUTPUT:
% FullName: Absolute canonical path name as string or cell string.
% For empty strings the current directory is replied.
% '\\?\' or '\\?\UNC' is added on demand.
%
% NOTE: The M- and the MEX-version create the same results, the faster MEX
% function works under Windows only.
% Some functions of the Windows-API still do not support long file names.
% E.g. the Recycler and the Windows Explorer fail even with the magic '\\?\'
% prefix. Some functions of Matlab accept 260 characters (value of MAX_PATH),
% some at 259 already. Don't blame me.
% The 'fat' style is useful e.g. when Matlab's DIR command is called for a
% folder with les than 260 characters, but together with the file name this
% limit is exceeded. Then "dir(GetFullPath([folder, '\*.*], 'fat'))" helps.
%
% EXAMPLES:
% cd(tempdir); % Assumed as 'C:\Temp' here
% GetFullPath('File.Ext') % 'C:\Temp\File.Ext'
% GetFullPath('..\File.Ext') % 'C:\File.Ext'
% GetFullPath('..\..\File.Ext') % 'C:\File.Ext'
% GetFullPath('.\File.Ext') % 'C:\Temp\File.Ext'
% GetFullPath('*.txt') % 'C:\Temp\*.txt'
% GetFullPath('..') % 'C:\'
% GetFullPath('..\..\..') % 'C:\'
% GetFullPath('Folder\') % 'C:\Temp\Folder\'
% GetFullPath('D:\A\..\B') % 'D:\B'
% GetFullPath('\\Server\Folder\Sub\..\File.ext')
% % '\\Server\Folder\File.ext'
% GetFullPath({'..', 'new'}) % {'C:\', 'C:\Temp\new'}
% GetFullPath('.', 'fat') % '\\?\C:\Temp\File.Ext'
%
% COMPILE:
% Automatic: InstallMex GetFullPath.c uTest_GetFullPath
% Manual: mex -O GetFullPath.c
% Download: http://www.n-simon.de/mex
% Run the unit-test uTest_GetFullPath after compiling.
%
% Tested: Matlab 6.5, 7.7, 7.8, 7.13, WinXP/32, Win7/64
% Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008/2010
% Assumed Compatibility: higher Matlab versions
% Author: Jan Simon, Heidelberg, (C) 2009-2013 matlab.THISYEAR(a)nMINUSsimon.de
%
% See also: CD, FULLFILE, FILEPARTS.
% $JRev: R-G V:032 Sum:7Xd/JS0+yfax Date:15-Jan-2013 01:06:12 $
% $License: BSD (use/copy/change/redistribute on own risk, mention the author) $
% $UnitTest: uTest_GetFullPath $
% $File: Tools\GLFile\GetFullPath.m $
% History:
% 001: 20-Apr-2010 22:28, Successor of Rel2AbsPath.
% 010: 27-Jul-2008 21:59, Consider leading separator in M-version also.
% 011: 24-Jan-2011 12:11, Cell strings, '~File' under linux.
% Check of input types in the M-version.
% 015: 31-Mar-2011 10:48, BUGFIX: Accept [] as input as in the Mex version.
% Thanks to Jiro Doke, who found this bug by running the test function for
% the M-version.
% 020: 18-Oct-2011 00:57, BUGFIX: Linux version created bad results.
% Thanks to Daniel.
% 024: 10-Dec-2011 14:00, Care for long names under Windows in M-version.
% Improved the unittest function for Linux. Thanks to Paul Sexton.
% 025: 09-Aug-2012 14:00, In MEX: Paths starting with "\\" can be non-UNC.
% The former version treated "\\?\C:\<longpath>\file" as UNC path and
% replied "\\?\UNC\?\C:\<longpath>\file".
% 032: 12-Jan-2013 21:16, 'auto', 'lean' and 'fat' style.
% Initialize: ==================================================================
% Do the work: =================================================================
% #############################################
% ### USE THE MUCH FASTER MEX ON WINDOWS!!! ###
% #############################################
% Difference between M- and Mex-version:
% - Mex does not work under MacOS/Unix.
% - Mex calls Windows API function GetFullPath.
% - Mex is much faster.
% Magix prefix for long Windows names:
if nargin < 2
Style = 'auto';
end
% Handle cell strings:
% NOTE: It is faster to create a function @cell\GetFullPath.m under Linux, but
% under Windows this would shadow the fast C-Mex.
if isa(File, 'cell')
for iC = 1:numel(File)
File{iC} = TADSInterface.GetFullPath(File{iC}, Style);
end
return;
end
% Check this once only:
isWIN = strncmpi(computer, 'PC', 2);
MAX_PATH = 260;
% Warn once per session (disable this under Linux/MacOS):
persistent hasDataRead
if isempty(hasDataRead)
% Test this once only - there is no relation to the existence of DATAREAD!
%if isWIN
% Show a warning, if the slower Matlab version is used - commented, because
% this is not a problem and it might be even useful when the MEX-folder is
% not inlcuded in the path yet.
% warning('JSimon:GetFullPath:NoMex', ...
% ['GetFullPath: Using slow Matlab-version instead of fast Mex.', ...
% char(10), 'Compile: InstallMex GetFullPath.c']);
%end
% DATAREAD is deprecated in 2011b, but still available. In Matlab 6.5, REGEXP
% does not know the 'split' command, therefore DATAREAD is preferred:
hasDataRead = ~isempty(which('dataread'));
end
if isempty(File) % Accept empty matrix as input:
if ischar(File) || isnumeric(File)
File = cd;
return;
else
error(['JSimon:', mfilename, ':BadTypeInput1'], ...
['*** ', mfilename, ': Input must be a string or cell string']);
end
end
if ischar(File) == 0 % Non-empty inputs must be strings
error(['JSimon:', mfilename, ':BadTypeInput1'], ...
['*** ', mfilename, ': Input must be a string or cell string']);
end
if isWIN % Windows: --------------------------------------------------------
FSep = '\';
File = strrep(File, '/', FSep);
% Remove the magic key on demand, it is appended finally again:
if strncmp(File, '\\?\', 4)
if strncmpi(File, '\\?\UNC\', 8)
File = ['\', File(7:length(File))]; % Two leading backslashes!
else
File = File(5:length(File));
end
end
isUNC = strncmp(File, '\\', 2);
FileLen = length(File);
if isUNC == 0 % File is not a UNC path
% Leading file separator means relative to current drive or base folder:
ThePath = cd;
if File(1) == FSep
if strncmp(ThePath, '\\', 2) % Current directory is a UNC path
sepInd = strfind(ThePath, '\');
ThePath = ThePath(1:sepInd(4));
else
ThePath = ThePath(1:3); % Drive letter only
end
end
if FileLen < 2 || File(2) ~= ':' % Does not start with drive letter
if ThePath(length(ThePath)) ~= FSep
if File(1) ~= FSep
File = [ThePath, FSep, File];
else % File starts with separator:
File = [ThePath, File];
end
else % Current path ends with separator:
if File(1) ~= FSep
File = [ThePath, File];
else % File starts with separator:
ThePath(length(ThePath)) = [];
File = [ThePath, File];
end
end
elseif FileLen == 2 && File(2) == ':' % "C:" current directory on C!
% "C:" is the current directory on the C-disk, even if the current
% directory is on another disk! This was ignored in Matlab 6.5, but
% modern versions considers this strange behaviour.
if strncmpi(ThePath, File, 2)
File = ThePath;
else
try
File = cd(cd(File));
catch % No MException to support Matlab6.5...
if exist(File, 'dir') % No idea what could cause an error then!
rethrow(lasterror); %#ok<LERR>
else % Reply "K:\" for not existing disk:
File = [File, FSep];
end
end
end
end
end
else % Linux, MacOS: ---------------------------------------------------
FSep = '/';
File = strrep(File, '\', FSep);
if strcmp(File, '~') || strncmp(File, '~/', 2) % Home directory:
HomeDir = getenv('HOME');
if ~isempty(HomeDir)
File(1) = [];
File = [HomeDir, File];
end
elseif strncmpi(File, FSep, 1) == 0
% Append relative path to current folder:
ThePath = cd;
if ThePath(length(ThePath)) == FSep
File = [ThePath, File];
else
File = [ThePath, FSep, File];
end
end
end
% Care for "\." and "\.." - no efficient algorithm, but the fast Mex is
% recommended at all!
% if ~isempty(strfind(File, [FSep, '.']))
if contains(File, [FSep, '.'])
if isWIN
if strncmp(File, '\\', 2) % UNC path
index = strfind(File, '\');
if length(index) < 4 % UNC path without separator after the folder:
return;
end
Drive = File(1:index(4));
File(1:index(4)) = [];
else
Drive = File(1:3);
File(1:3) = [];
end
else % Unix, MacOS:
isUNC = false;
Drive = FSep;
File(1) = [];
end
hasTrailFSep = (File(length(File)) == FSep);
if hasTrailFSep
File(length(File)) = [];
end
if hasDataRead
if isWIN % Need "\\" as separator:
C = dataread('string', File, '%s', 'delimiter', '\\'); %#ok<REMFF1>
else
C = dataread('string', File, '%s', 'delimiter', FSep); %#ok<REMFF1>
end
else % Use the slower REGEXP, when DATAREAD is not available anymore:
C = regexp(File, FSep, 'split');
end
% Remove '\.\' directly without side effects:
C(strcmp(C, '.')) = [];
% Remove '\..' with the parent recursively:
R = 1:length(C);
for dd = reshape(find(strcmp(C, '..')), 1, [])
index = find(R == dd);
R(index) = [];
if index > 1
R(index - 1) = [];
end
end
if isempty(R)
File = Drive;
if isUNC && ~hasTrailFSep
File(length(File)) = [];
end
elseif isWIN
% If you have CStr2String, use the faster:
% File = CStr2String(C(R), FSep, hasTrailFSep);
File = sprintf('%s\\', C{R});
if hasTrailFSep
File = [Drive, File];
else
File = [Drive, File(1:length(File) - 1)];
end
else % Unix:
File = [Drive, sprintf('%s/', C{R})];
if ~hasTrailFSep
File(length(File)) = [];
end
end
end
% "Very" long names under Windows:
if isWIN
if ~ischar(Style)
error(['JSimon:', mfilename, ':BadTypeInput2'], ...
['*** ', mfilename, ': Input must be a string or cell string']);
end
if (strncmpi(Style, 'a', 1) && length(File) >= MAX_PATH) || ...
strncmpi(Style, 'f', 1)
% Do not use [isUNC] here, because this concerns the input, which can
% '.\File', while the current directory is an UNC path.
if strncmp(File, '\\', 2) % UNC path
File = ['\\?\UNC', File(2:end)];
else
File = ['\\?\', File];
end
end
end
end
end
% =====================================================================
% ======================== PRIVATE Methods ============================
% =====================================================================
methods (Access = private)
% methods (Access = public) % TEMP!!!
function nbytes = fprintf(this, LOD, varargin)
if isempty(LOD), LOD = 1; end % if LOD (MessageLevelOfDetails) is empty, consider this is a main message
if this.FdMessageLevelOfDetails < LOD
nbytes = 0;
return;
end
if contains(this.FDisplayPrefix,'#t')
Time = datestr(now,this.FTimeFormat);
Prefix = strrep(this.FDisplayPrefix, '#t', Time);
else
Prefix = this.FDisplayPrefix;
end
if ischar(varargin{1}) % no file id
varargin{1} = [Prefix varargin{1}];
else % with a file id
varargin{2} = [Prefix varargin{2}];
end
nbytes = fprintf(varargin{:});
end
function PrintHeader(this)
this.fprintf(2,'\n');
this.fprintf(2,'*****************************************************************************\n');
this.fprintf(2,'*** <strong>ADS interface v%s</strong> ***\n', this.FVersion);
this.fprintf(3,'*** Tested with ADS v%s ***\n', this.FADSVersion);
this.fprintf(3,'*** %s ***\n', this.FOrganization);
this.fprintf(3,'*** %s ***\n', this.FToolboxAuthor);
this.fprintf(3,'*** Last edit: %s ***\n', this.FLastEdit);
% this.fprintf(3,'*** Today: %s ***\n', datestr(now,'dd-mm-yyyy'));
this.fprintf(2,'*****************************************************************************\n');
end
function PrintChangeInfo(this, LineOrig, LineNew, Test, Str1, iNetlistLine, OldAndNewInOneLine)
if nargin<7, OldAndNewInOneLine = false; end
% For LOD = "everything", we can afford to use more lines for displaying
if this.FdMessageLevelOfDetails>=3
if Test, strAct='would have changed to'; else, strAct='has changed to'; end
% extra info Str1 and the netlist line in which the change has occured
this.fprintf(2,' <a href="matlab: opentoline(''%s'',%i)">%sat the netlist line %i:</a>\n',...
this.FNetlistFile, iNetlistLine, Str1, iNetlistLine);
% original netlist line with highlighted substring that has been changed
this.fprintf(2,' %s\n', strtrim(LineOrig));
% some arrows
c=8615; this.fprintf(2,' %s%s%s %s %s%s%s\n', c,c,c,strAct,c,c,c); % 8595 ? 8659 ? 8681 ? 8615 ?
% new netlist line with highlighted substring that has been changed
this.fprintf(2,' %s\n', strtrim(LineNew));
% for LOD = "detailed", use more compact format
else
if OldAndNewInOneLine
this.fprintf(2,' "%s" => "%s"\n', strtrim(LineOrig), strtrim(LineNew));
else
this.fprintf(2,' (old) %s\n', strtrim(LineOrig));
this.fprintf(2,' (new) %s\n', strtrim(LineNew));
end
end
end
% **************************************************************
% ************** READ DATA FROM THE VECTORSET ******************
% **************************************************************
function DSs = dsReadADSDataset(this, DatasetFile, varargin)
% make the dataset dump in a text file
[FilePath, FileName] = fileparts(DatasetFile);
DatasetTextFile = fullfile(FilePath, [FileName '_dump.txt']);
% CmdLine = ['dsdump "' DatasetFile '" > "' DatasetTextFile '"'];
if nargin<=2 || (nargin>2 && varargin{1})
CmdLine = ['dsdump "' TADSInterface.GetFullPath(DatasetFile) '" > "' TADSInterface.GetFullPath(DatasetTextFile) '"'];
[status, result] = system(CmdLine);
assert(status==0, 'ADSInterface:ReadDataset:CannotDump', 'Cannot dump the dataset file "%s" using "dsdump".\nThe system message is:\n"%s"', DatasetFile,deblank(result));
end
this.FDatasetFile = DatasetFile;
this.FDatasetTextFile = DatasetTextFile;
% Read lines from the text dataset file to the cell array Lines
% this.fprintf(1,'<strong>Reading dataset file "%s"</strong>\n',DatasetFile);
this.fprintf(1,'Reading dataset file "%s"\n',DatasetFile);
fid = fopen(DatasetTextFile,'r');
if fid<0, error('ADSInterface:ReadDataset:CantOpenDatasetTxt', 'Can''t open file "%s", which should have been just created by "dsdump" ADS utility. Does the file exist?', DatasetTextFile); end
try
S = fread(fid, '*char');
catch ME
fclose(fid);
rethrow(ME);
end
fclose(fid);
assert(~isempty(S), 'ADSInterface:ReadDataset:DatasetTxtEmpty', 'The dataset converted to the text file "%s" is empty.', DatasetTextFile);
Lines = strsplit(S.',newline); % split on lines
% Initialize
DSs = cell(0,1);
NVectorsets = 0;
% find next vectorset and read it
iln = 1;
while iln<=length(Lines)
if isempty(Lines{iln}), iln = iln+1; continue; end
% if the line starts from #, a new vectorset is found. Process it.
if Lines{iln}(1) == '#'
NVectorsets = NVectorsets+1;
[DSs{NVectorsets}, iln] = this.dsReadVectorset(Lines, iln, NVectorsets);
continue;
end
iln = iln + 1;
end
end
function [DS, iln] = dsReadVectorset(this, Lines, iln, iVectorset)
% Reads one vectorset in the dataset text Lines
% Parse Vectorset header like:
% ------------------------------------------------------
% * Vectorset name: "Sweep2.Sweep1.SP1.SP"
% * Vectorset number of attributes: 0
% * Independent number of variables: 3
% 0: "s2" 0 r
% number of attributes: 1
% "flags" = "type=real indep=yes"
% 1: "s1" 0 r
% number of attributes: 1
% "flags" = "type=real indep=yes"
% 2: "freq" 0 r
% number of attributes: 1
% "flags" = "frequency type=real indep=yes mixop=neg"
% * Dependent number of variables: 6
% 0: "S[1,1]" 0 c
% number of attributes: 1
% "flags" = "s-param type=complex indep=no"
% 1: "S[1,2]" 0 c
% number of attributes: 1
% "flags" = "s-param type=complex indep=no"
% ...
% ------------------------------------------------------
[DS, iln, Info] = this.dsParseVectorsetHeader(Lines, iln);
NIndepVars = length(DS.IndependentVars);
NDepVars = length(DS.DependentVars);
% this.fprintf(1,'<strong>%i) Reading vectorset "%s"</strong>\n', iVectorset, DS.VectorsetName);
this.fprintf(3,'%i) Reading vectorset "%s"\n', iVectorset, DS.VectorsetName);
this.fprintf(3,' <a href="matlab: opentoline(''%s'',%i)">Independent variables:</a>\n', this.FDatasetTextFile, Info.iLineIndepVarNum);
for n=1:NIndepVars
this.fprintf(3,' %s\n', DS.IndependentVars{n});
end
this.fprintf(3,' <a href="matlab: opentoline(''%s'',%i)">Dependent variables:</a>\n', this.FDatasetTextFile, Info.iLineDepVarNum);
for n=1:NDepVars
this.fprintf(3,' %s\n', DS.DependentVars{n});
end
this.fprintf(3,'\n');
% Scan all lines to get values for all independent variables except "point" variable.
% We do it only if there are 2 or more independent vars.
% ------------------------------------------------------
% * Leaf node name: "DN=3=0,0,0"
% * Leaf node number of attributes: 0
% * Independent indexing: 0 0 | We scan all file for these data and save
% Ivalue 2: "s2" = 0.000254 | variables names/values. In this example we have 3 indep. vars, 2 of which
% Ivalue 1: "s1" = 3.81e-005 | are specified in this block, and 3rd one ("freq") is running below ("0: freq1", "1: freq2", ...)
% * Number of dependents: 6
% * Number of points: 91
% 0: 1000000000
% (0.0242370098884019, -0.999706170710759)
% ...
% 1: 1100000000
% (-0.156443913680543, -0.987686690626375)
% ...
% ------------------------------------------------------
[cVarNames, cVarValues] = this.dsScanForAllIndependentVarsExceptPointVar(Lines, iln);
% DS.VarNames = cVarNames;
% DS.VarValues = cVarValues;
% Get "point" variable name (the variable which is swept within this LeafNode)
sPointVarName = this.dsGetPointVarName(DS, cVarNames);
% DS.PointVarName = sPointVarName;
% it is more convenient to store "point" var in 2nd dimension
ind = find(strcmp(DS.IndependentVars,sPointVarName),1);
DS.IndependentVars(ind) = [];
DS.IndependentVars = [{sPointVarName} DS.IndependentVars];
% Read LeafNode header
% ------------------------------------------------------
% * Leaf node name: "DN=3=0,0,0"
% * Leaf node number of attributes: 0
% * Independent indexing: 0 0
% Ivalue 2: "s2" = 0.000254
% Ivalue 1: "s1" = 3.81e-005
% * Number of dependents: 6
% * Number of points: 91
% ------------------------------------------------------
[LNH, iln] = this.dsReadLeafNodeHeader(Lines, iln, DS);
if ~isempty(LNH)
iln = iln+1;
tline = Lines{iln};
end
% Allocate memory
% ... dependent variables: DS.Data = NDepVars x NIndepVar1 x NIndepVar2 x ...
dimszs = [{LNH.NPoints}, num2cell( cellfun(@length,cVarValues) )];
DS.Data = nan(NDepVars, dimszs{:});
% Independent variables (points only);
Points = nan(1,LNH.NPoints);
% read whole Vectorset (all LeafNodes)
while ~isempty(LNH)
% read the data for all points
for ipt=1:LNH.NPoints
% get point index and value
assert(contains(tline,':'), 'ADSInterface:ReadDataset:NoColonInLine', 'The line is expected to have a colon');
d = strsplit(tline,':');
assert(length(d)==2, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Unexpected number of tokens when getting the point index/value.');
iPoint = str2double(d{1})+1;
Points(iPoint) = str2double(d{2});
iln = iln+1; tline = Lines{iln};
% read dependent variables
Val = nan(NDepVars,1);
for iDepVar=1:NDepVars
% if line contains "(invalid)", the vectorset is invalid, return
if contains(tline, '(invalid)')
iln = iln+1; tline = Lines{iln};
continue;
end
% if line starts from "(" - it is complex number
if tline(1)=='('
str = strrep(tline,'(',''); str = strrep(str,')','');
% d = strsplit(str,',');
% assert(length(d)==2, 'Unexpected number of tokens when parsing a complex number.');
% Val(iDepVar) = str2double(d{1}) + 1i*str2double(d{2});
n=find(str==',',1);
assert(length(n)==1, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Unexpected number of tokens when parsing a complex number.');
Val(iDepVar) = str2double(str(1:n-1)) + 1i*str2double(str(n+1:end));
else
Val(iDepVar) = str2double(tline);
end
iln = iln+1; tline = Lines{iln};
end
% ... and store them
if NIndepVars>1
inds = num2cell(LNH.VarInds);
DS.Data(:,iPoint,inds{:}) = Val;
else
DS.Data(:,iPoint) = Val;
end
end
% next leaf node
% iln = iln-1;
[LNH, iln] = this.dsReadLeafNodeHeader(Lines, iln, DS);
if ~isempty(LNH)
iln = iln+1;
tline = Lines{iln};
end
end
% DS.VarNames = cVarNames;
DS.IndependentValues = [{Points}, cVarValues];
end
function [DS, iln, Info] = dsParseVectorsetHeader(this, Lines, iln)
Action = 'Find vectorset name';
while iln<=length(Lines)
tline = Lines{iln};
switch Action
case 'Find vectorset name'
if contains(tline,'Vectorset name')
DS.VectorsetName = this.dsGetRegExp1TokenString(tline, '.*Vectorset name: "(.+)".*', Action);
% next Action
Action = 'Get number of indep vars';
end
case 'Get number of indep vars'
if contains(tline,'Independent number of variables')
NIndepVars = this.dsGetRegExp1TokenDouble(tline, '.*Independent number of variables: (\d+).*', Action);
% next Action
Action = 'Get indep vars';
DS.IndependentVars = cell(1,NIndepVars);
DS.IndependentValues = cell(1,NIndepVars);
% Info
Info.iLineIndepVarNum = iln;
end
case 'Get indep vars'
if contains(tline,': "')
[tok1, tok2] = this.dsGetRegExp2Tokens(tline, '\s*(\d+): "(.*)" \d+.*', Action);
ind = str2double(tok1)+1;
DS.IndependentVars{ind} = tok2;
% next Action
if ind==NIndepVars
Action = 'Get number of dependent vars';
end
end
case 'Get number of dependent vars'
if contains(tline,'Dependent number of variables')
NDepVars = this.dsGetRegExp1TokenDouble(tline, '.*Dependent number of variables: (\d+).*', Action);
% next Action
DS.DependentVars = cell(1,NDepVars);
Action = 'Get dependent vars';
% Info
Info.iLineDepVarNum = iln;
end
case 'Get dependent vars'
d = regexp(tline, '\s*(\d+): "(.*)" \d+.*', 'tokens');
if ~isempty(d)
assert(length(d)==1, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was matched several times.', Action);
assert(length(d{1})==2, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was matched not expected number of times.', Action);
ind = str2double(d{1}{1})+1;
DS.DependentVars{ind} = d{1}{2};
% next Action
if ind==NDepVars
return;
% % convert names of independent and dependent vars to correct Malab variables name
% IndepVars = this.dsValidateVariablesName(DS.IndependentVars);
% DepVars = this.dsValidateVariablesName(DS.DependentVars);
% % if number of indep. vars are more than 1, we should look for the data then
% if NIndepVars>1,
% % cVarNames = [];
% Action = 'Get independent names, indices and values';
% else
% % [cVarNames, cVarValues] = this.dsScanForAllIndependentVarsExceptPointVar(Lines, iln, Action);
% Action = 'Get number of points';
% end
end
end
otherwise, error('Bug: wrong "Action".');
end
iln = iln+1;
end
end
function Data = dsGetRegExp1TokenString(~, tline, Expr, Action)
d = regexp(tline, Expr, 'tokens');
assert(~isempty(d), 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was not found.', Action);
assert(length(d)==1, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was matched several times.', Action);
assert(length(d{1})==1, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was matched several times.', Action);
Data = d{1}{1};
end
function Data = dsGetRegExp1TokenDouble(this, tline, Expr, Action)
str = this.dsGetRegExp1TokenString(tline, Expr, Action);
Data = str2double(str);
end
function [tok1, tok2] = dsGetRegExp2Tokens(~, tline, Expr, Action)
d = regexp(tline, Expr, 'tokens');
assert(~isempty(d), 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was not found.', Action);
assert(length(d)==1, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was matched several times.', Action);
assert(length(d{1})==2, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was matched not expected number of times.', Action);
tok1 = d{1}{1};
tok2 = d{1}{2};
end
function [Name, Value] = dsGetRegExpNameValue(this, tline, Expr, Action)
[Name, tok2] = this.dsGetRegExp2Tokens(tline, Expr, Action);
Value = str2double(tok2);
end
function ValidNames = dsValidateVariablesName(~, Names)
% replance non valid characters by "_"
ValidNames = regexprep(Names, '\W', '_');
% ensure that variables are not repeating after this correction
m=1;
while m<length(ValidNames)
n=m+1;
while n<length(ValidNames)
if strcmp(ValidNames{m}, ValidNames{n})
ValidNames{n} = [ValidNames{n} '_NameConflict'];
m=1; break % since we renamed one variable, start checking from begining
end
n=n+1;
end
m=m+1;
end
end
function [cVarNames, dVarValues, VarInds, iln] = dsGetAllIndependentVarsInBlockExceptPointVar(this, Lines, iln)
% reads data like:
% -------------------------------
% * Independent indexing: 0 0
% Ivalue 2: "s2" = 0.000254
% Ivalue 1: "s1" = 3.81e-005
% -------------------------------
cVarNames = {}; dVarValues = []; VarInds = [];
Action = 'Get independent indexing';
while iln<=length(Lines)
tline = Lines{iln};
if isempty(tline), iln = iln+1; continue; end
% if next vector set started, return
if tline(1)=='#', return; end
switch Action
case 'Get independent indexing'
if tline(1)=='*' && contains(tline,'Independent indexing:')
d = regexp(tline, '(\d)+', 'tokens');
NVars = length(d);
assert(NVars>0, 'ADSInterface:ReadDataset:UnexpectedNumOfTokens', 'Error in Action "%s": regexp pattern was not found.',Action);
VarInds = nan(NVars,1);
for n=1:NVars
VarInds(n) = str2double(d{n}{1})+1;
end
% next Action
cVarNames = cell(NVars,1);
dVarValues = nan(NVars,1);
iVar = 0;
Action = 'Get independent values';
end
case 'Get independent values'
if contains(tline,'Ivalue')
iVar = iVar+1;
[cVarNames{iVar}, dVarValues(iVar)] = this.dsGetRegExpNameValue(tline, '"(\w+)" = ((?:[-+]?\d*\.?\d+)(?:[eE]([-+]?\d+))?)', Action); %#ok<AGROW>
% next Action
if iVar==NVars
% we finished, return
return
end
end
otherwise, error('Bug: wrong "Action"');