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
###############################################################################
# Copyright (c) 2014-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform.  As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose.  Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
#   @script
#       This file txtFormat.tcl is an included file of createOverview.tcl.
#       It contains all procedures for printing content to a txt file.
#       Some references and comments relate to htmlFormat.tcl, which contains
#       all procedures for printing to a html file.
###############################################################################


#checker -scope global exclude warnRedefine


# =============================================================================
# PrintHeader - In htmlFormat.tcl, this proc printed html header components to
#               $out. $title appeared in the title line of the browser window,
#               In txtFormat.tcl it appears on the top of the page.
# =============================================================================
#
proc CreateOverview:PrintHeader {out title} {
    set toYear [clock format [clock seconds] -format %Y]
    set    copy ""
    append copy "Copy" "right (c) 2014-" $toYear " by Altair Engineering, Inc."
    CreateOverview:_printWrappedLine $out "Printout of ASCII text."
    CreateOverview:_printWrappedLine $out $copy
    CreateOverview:_printWrappedLine $out "TITLE: $title"
}


# =============================================================================
# PrintFooter - Prints footer data to the page:
#
#               - the creation date of the page to $out
#               - StarVision PRO Version
#               - format example:
#                 Wed Mar 26 2014 15:40:35 CET  StarVision PRO Version xx.xx.xx
#
#               The $lastAnchor parameter is a here unused remnant from
#               htmlFormat.tcl.
# =============================================================================
#
proc CreateOverview:PrintFooter {out lastAnchor} {
    set    line "\n"
    append line [CreateOverview:GetCreationDate]
    append line "   "
    append line "StarVision PRO Version: [zdb version TOOL]"
    CreateOverview:_printWrappedLine $out $line
}


# =============================================================================
# PrintPageHeadline - Prints $title as a page headline.
# =============================================================================
#
proc CreateOverview:PrintPageHeadline {out title} {
    CreateOverview:_printWrappedLine $out \n\n$title
}


# -----------------------------------------------------------------------------
# _printHeadline - Prints $title as headline.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_printHeadline {out title} {
    CreateOverview:_printWrappedLine $out \n$title
}


# =============================================================================
# PrintLine - Prints $line as a regular text line.
# =============================================================================
#
proc CreateOverview:PrintLine {out line} {
    CreateOverview:_printWrappedLine $out $line
}


# =============================================================================
# PrintSchematics - Prints the filenames of the schematic GIFs. The PNG files
#                   can be found in the output directory $outDir.
#                   (htmlFormat prints the preview schematic PNGs which have
#                   been exported from the GUI with links to the full view PNGs)
# =============================================================================
#
proc CreateOverview:PrintSchematics {out imageList} {
    CreateOverview:_printHeadline $out "Schematic images of this module:"

    ##
    # imageList contains quadruples of values:
    # fullviewImageBaseName previewImageBaseName previewWidth previewHeight
    #
    foreach {bn bnPre width height} $imageList {
        CreateOverview:_printWrappedLine $out "\timg/$bn"
    }
    CreateOverview:_printWrappedLine $out \n
}


# =============================================================================
# PrintPdfLink - Prints the name of the pdf file with the schematic which has
#                been exported from the GUI to $out.
# =============================================================================
#
proc CreateOverview:PrintPdfLink {out fName} {
    CreateOverview:_printWrappedLine \
        $out "PDF file of the schematics: \n\tpdf/$fName \n"
}


# =============================================================================
# PrintUnorderedList - Prints a nested unordered list in a specific format.
#
#                      Parameters:
#                      - listHeading is printed as a headline
#                      - chapterNr   is unused in txtFormat
#                      - listToPrint is a recursive list structure for printing
#                        an unordered list with unordered sublists under each
#                        list element:
#                        listItems subList ?listItems subList? ...
#                        {txt href|"" ?txt href|""? ...}   {listToPrint}
#
#                      The {txt href|""} pairs allow printing of words
#                      with/without href links.
#
#                      Example:
#                      3 planes deep list in _genModuleDetail (# Print all
#                      instances).
# =============================================================================
#
proc CreateOverview:PrintUnorderedList {out listHeading chNr listToPrint} {
    CreateOverview:_txtPrintUnorderedList $out $listHeading $listToPrint 1
}


# -----------------------------------------------------------------------------
# _txtPrintUnorderedList - This is the actual procedure that executes the
#                          function PrintUnorderedList, enhanced by the variable
#                          indent, necessary for the txt output indent level.
#                          In html format the indent level is handled
#                          automatically by the nested unordered lists in the
#                          browser.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_txtPrintUnorderedList {
    out listHeading listToPrint indent
} {
    if {[llength $listToPrint] == 0} {
        return
    }

    set indentUnit 4
    set indentChar " "

    ##
    # Calculate tab width and bullet characters depending on indent level
    #
    set tab [string repeat $indentChar [expr {$indent * $indentUnit}]]
    switch -- $indent {
        1 {set bullet "*) "}
        2 {set bullet "-) "}
        3 {set bullet "o) "}
        4 {set bullet ">) "}
        default {set bullet "+) "}
    }

    ##
    # Print listHeading if present
    #
    if {[string length $listHeading] > 0} {
        CreateOverview:_printHeadline $out $listHeading
    }

    ##
    # Print the unordered list.
    # When called recursively, it is called with an empty listHeading and
    # the incremented indent level for the next sublist plane
    #
    foreach {listItem subList} $listToPrint {
        set line ""
        if {$listItem != {}} {
            append line ${tab}$bullet
        }
        foreach {txt href} $listItem {
            append line $txt

            ##
            # Instead of a hyperlink, print 'for details see':
            #
            if {$href != ""} {
                append line "   for details see \[$href\]"
            }
        }
        CreateOverview:_printWrappedLine $out $line
        if {$subList != {}} {
            CreateOverview:_txtPrintUnorderedList \
                $out "" $subList [expr {$indent + 1}]
        }
    }
}


# =============================================================================
# PrintSourceFile - Prints the source file $fname as a txt file, each line with
#                   a maximum lineWidth of CreateOverview(charsPerLine)
#                   Output file: $CreateOverview(odir)/$targetfname
# =============================================================================
#
proc CreateOverview:PrintSourceFile {fname targetfname} {
    global CreateOverview

    set db   $CreateOverview(db)
    set odir $CreateOverview(odir)

    ##
    # Open the source file for reading
    #
    set readV [open $fname "r"]

    ##
    # Open the txt file (that will hold the txt formatted code) for writing
    #
    set out [open [file join $odir $targetfname] "w"]
    CreateOverview:PrintHeader $out [file tail $fname]
    CreateOverview:PrintPageHeadline $out "Program code of file $fname:"

    ##
    # Find the total number of lines
    # Calculate space for the largest linenumber
    #
    set lastLine [$db spos lineno -no $fname end]
    set digitSpace [expr {int((log10($lastLine)) + 1)}]

    ##
    # Print the total number of lines
    #
    CreateOverview:_printWrappedLine \
        $out "This file contains $lastLine lines of code."

    ##
    # Loop through all the lines
    #
    set lineNr 0
    while {![eof $readV]} {
        incr lineNr
        set line [gets $readV]   ;#Read one line

        ##
        # Write one line, beginning with a linenumber " nn " with leading
        # spaces if linenumber < (10 ^ $digitSpace)
        #
        set wrLine [format "%$digitSpace\d %s" $lineNr $line]
        CreateOverview:_printTxtLine $out $wrLine 0 [expr {$digitSpace + 1}]
    }

    ##
    # All lines have been processed; finish the page with footer data.
    # Close the open files.
    #
    CreateOverview:PrintFooter $out ""
    close $readV
    close $out
}


# -----------------------------------------------------------------------------
# _printTxtLine - The $line to be printed can be long - this proc prints it
#                 row by row by wrapping it to a max width of $maxChars.
#                 The 1st row is indented $indent1 characters, all following
#                 rows are indented $indentFF characters.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_printTxtLine {out line indent1 indentFF} {
    ##
    # TCL principle: all column and position numbering begins with 0.
    # Two indices:   $pos for the character position within $line
    #                $col for the character column within one printed row
    #                of up to $maxChars characters
    #
    set maxChars   [gui plugin getConfigValue CreateOverview lineSize]
    set indentChar " "

    ##
    # Throw away all trailing spaces+tabs to avoid unnecessary empty rows
    # Begin with the indent characters for row 1, add the line to be printed
    #
    set line [string trimright $line]
    set line [string repeat $indentChar $indent1]$line

    ##
    # Initialize variables
    #
    set lineLen [string length $line]
    set col $indent1    ;#points to the next column where a char is copied to
    set lastWhiteCol 0  ;# column with the rightmost space/tab
    set lastWhitePos 0  ;# position in $line with the rightmost space/tab
    set fromPos 0       ;# One printed row lasts from $fromPos ..
    set toPos 0         ;#                                      .. to $toPos
    set space4space 0   ;# =1 if a space/tab can be printed to col $maxChars

    ##
    # Loop through all characters of $line
    #
    for {set pos  0} {$pos < $lineLen} {incr pos} {
        set curChar [string index $line $pos]

        ##
        # Usually curChar is not printed to col=$maxChars.
        # Make an exception: if a space or tab could be printed there, do it
        #
        if {($col == $maxChars) && [string is space $curChar]} {
            set space4space 1
        } else {
            set space4space 0
        }

        ##
        # Check if a line wrap is necessary
        #
        if {$col < ($maxChars + $space4space)} {
            ##
            # No line-wrap: $curChar can be printed into the  present row
            #
            # Check if curChar is whiteSpace; if so, memorize it for a later
            # line wrap at this position.
            # Expand tab characters to a tab width of 8.
            #
            if {[string is space $curChar]} {
                set lastWhiteCol $col
                set lastWhitePos $pos
                if {$curChar == "\t"} {
                    ##
                    # tab expansion with modulo(8) function:
                    #
                    set tabExpanded [expr {8 - ($col % 8)}]
                    set col [expr {($col + $tabExpanded) -1}]
                }
            }
            incr col    ;# point to the next column to be written
            incr toPos  ;# update toPos pointer
        } else {
            ##
            # The current column exceeds maxChar --> line wrap necessary.
            # Wrap it at the rightmost column of a whitespace char, if
            # available. Do not wrap the row at very early columns <6
            #
            if {$lastWhiteCol > 6} {
                ##
                # Print the processed chars ($fromPos to $lastWhitePos incl.)
                #
                puts $out "[string range $line $fromPos $lastWhitePos]"

                ##
                # Set new fromPos and col accordingly.
                # Put the indentCharsFF in front of the new row.
                # Chars right of lastWhiteCol must follow.
                #
                set fromPos [expr {$lastWhitePos + 1}]
                puts -nonewline $out "[string repeat $indentChar $indentFF]"
                set col [expr {($indentFF + $pos) - ($fromPos + 1)}]
            } else {
                ##
                # There was no $lastWhiteCol in this row:
                # Print the line, wrapped after maxChar chars.
                #
                puts $out "[string range $line $fromPos $pos-1]"

                ##
                # Set new fromPos and col accordingly.
                # Put the indentCharsFF in front of the new row.
                #
                set fromPos $pos
                puts -nonewline $out "[string repeat $indentChar $indentFF]"
                set col [expr {$indentFF + 1}]
            }

            ##
            # We have finished one row - prepare for the next one.
            #
            set lastWhiteCol 0
            set lastWhitePos 0
        }
    }

    ##
    # We have finished the complete line.
    # Print the last row.
    #
    puts $out "[string range $line $fromPos end]"
}


# -----------------------------------------------------------------------------
# _printWrappedLine - Prints out one $line, wrapped after $maxChars characters,
#                     with a fixed indent level of 0 spaces before row 1 and a
#                     indent level of 8 spaces before all following rows.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_printWrappedLine {out line} {
    CreateOverview:_printTxtLine $out $line 0 8
}


# =============================================================================
# PrintCssFile - No CSS file is needed for txtFormat.tcl
# =============================================================================
#
proc CreateOverview:PrintCssFile {fname} {
}