blob: 59ac34489387165b4ee1cce0c5ed3ba71f5d9485 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001#!/usr/bin/perl
2#
3# Copyright Statement:
4# --------------------
5# This software is protected by Copyright and the information contained
6# herein is confidential. The software may not be copied and the information
7# contained herein may not be used or disclosed except with the written
8# permission of MediaTek Inc. (C) 2005
9#
10# BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
11# THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
12# RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
13# AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
14# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
15# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
16# NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
17# SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
18# SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
19# THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
20# NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
21# SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
22#
23# BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
24# LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
25# AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
26# OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
27# MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
28#
29# THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
30# WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
31# LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
32# RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
33# THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
34#
35#
36#*****************************************************************************
37#
38# Filename:
39# ---------
40# hExpand.pl
41#
42# Description:
43# ------------
44# this script is used to expand files included by #include.
45#
46# Auther:
47# -------
48# Shinn Lin
49#
50# Note:
51# -----
52#
53# Log:
54# -----
55# 2008/06/30 Create.
56# 2010/01/29 Correct header exclusion (all filename to lower case)
57#
58
59#BEGIN { push @INC, "pcore/" , 'U:\\00MyPerlLib'} # add additional library path
60#package XXX;
61use strict;
62
63#******************************************************************************
64# Global Data
65#******************************************************************************
66
67my $lineRedirect = 1; # add '#line' in expanded file to redirect warning/error msg
68my $g_removeTempFile = 1;
69
70# these two only work when $lineRedirect = 0
71my $removeCommentOrNot = 1; # for correct #line redirect, should disable this
72my $removeConsecutiveEmpty = 0; # no much size difference
73
74my $g_nodeThreshold = 40;
75
76my @targetFileList = ();
77my $g_prjName = '';
78my $g_mcuPath = ".";
79my $g_outputPath = ".";
80
81my $logFilename = "hExpand.log";
82my $logPath = ".";
83my $logFileHandle;
84
85my $valSuccess = 0;
86my $valAlreadyIncluded = 1;
87my $valFileNotFound = 2;
88my $valRecursiveInclude = 3;
89my $valOthers = 4;
90
91my $idxPath = 1;
92my $idxAlwaysInc = 2;
93my $idxIncluded = 3;
94my %g_incInfo = (); # { filename => [1] path
95 # [2] always include or not
96 # [3] included or not}
97
98my @g_incPath = ();
99
100my %g_targetFileHash = ();
101my @nonExpandList = ("ps_trace.h");
102my %g_nonExpandList = ();
103
104my @incDeeps = ();
105my %incTreeHash = ();
106my $g_totalIncCount = 0;
107my $g_forceNotExpand = 0; # if set to 1, force NOT to expand #include
108
109my $foundFileList = {}; # found list
110my $notFoundFileList = {}; # not found list
111
112
113#******************************************************************************
114# Export Function
115#******************************************************************************
116
117#******************************************************************************
118# Internal Data
119#******************************************************************************
120
121#******************************************************************************
122# Program Start
123#******************************************************************************
124
125if (@ARGV)
126{
127 ($g_mcuPath, $g_outputPath, $g_prjName, @targetFileList) = @ARGV;
128}
129else
130{
131 die "\nUsage: 'hExpand.pl <mcu path> <output path> <prj name> <target filename>'\n".
132 " e.g. hExpand.pl \\server\\w08.20\\mcu \\server\\w08.20\\mcu\\expandOutput superman29v31_demo mmi_include.h\n";
133}
134
135open($logFileHandle, ">$logPath\\$logFilename") or die "can't open $logPath\\$logFilename!\n";
136
137printBoth("\n[start hExpand]\n\n");
138
139&timeCheck();
140
141# prepare non-expand list
142foreach my $file (@nonExpandList)
143{
144 $g_nonExpandList{lc $file} = 1;
145}
146
147my @incMods = ("plutommi", "kal");
148&readCompileInfo(\@incMods, \@g_incPath, 0);
149
150$g_targetFileHash{$_} = 1 foreach (@targetFileList);
151
152foreach my $targetFile (@targetFileList)
153{
154 printLog("[$targetFile]\n");
155
156 my @resultData = ();
157 @incDeeps = ();
158 %incTreeHash = ();
159 $g_totalIncCount = 0;
160 %g_incInfo = ();
161
162 my $fileHandle;
163 my $outputFile = "$g_outputPath\\$targetFile";
164 open($fileHandle, ">$outputFile");
165
166 # copy original header file to output folder
167 copyOriginalHeader($targetFile);
168
169 # add compile option for MoDIS
170 print $fileHandle "#ifdef WIN32\n";
171 print $fileHandle "#include \"_".$targetFile."\"\n";
172 print $fileHandle "#else\n";
173
174 my $nodeCount = 0;
175 &expandInc($fileHandle, $targetFile, 0, \$nodeCount);
176
177 #&findMismatchIf(\@resultData);
178
179 # end of compile option for MoDIS
180 print $fileHandle "#endif /* WIN32 */\n\n";
181
182 close($fileHandle);
183 printBoth("\n[hExpand.pl] $outputFile generated!\n\n");
184
185 &timeCheck();
186}
187
188
189close($logFileHandle); # log file
190
191exit 0;
192
193#******************************************************************************
194# Internal Function
195#******************************************************************************
196
197#******************************************************************************
198# FUNCTION
199# copyOriginalHeader
200# DESCRIPTION
201# xxx
202# PARAMETERS
203# xxx
204# RETURNS
205# xxx
206#******************************************************************************
207sub copyOriginalHeader()
208{
209 my ($incFile) = @_;
210
211 my $incPath = &searchIncPath($incFile, \@g_incPath);
212
213 open(hFile, "<$incPath\\$incFile") or die "can't open $incPath\\$incFile\n";
214 my @fileData = <hFile>;
215 close(hFile);
216
217 open(hFile, ">$g_outputPath\\_".$incFile) or die "can't open $g_outputPath\\_".$incFile."\n";
218
219 # add redirect info
220 my $incPrintPathName = "$incPath\\$incFile";
221 $incPrintPathName =~ s/\\/\\\\/g;
222 print hFile "#line 0 \"$incPrintPathName\"\n" if ($lineRedirect);
223 print hFile join('', @fileData);
224 close(hFile);
225}
226
227
228#******************************************************************************
229# FUNCTION
230# expandInc
231# DESCRIPTION
232# xxx
233# PARAMETERS
234# xxx
235# RETURNS
236# 0: success, 1: already included, 2: file not found, 3: recursive include
237#******************************************************************************
238sub expandInc()
239{
240 my ($fileHandle, $incFile, $alwaysExpand, $nodeCount_ref) = @_;
241 $incFile = lc $incFile;
242
243 my $incPath = "";
244 my @fileData = ();
245 my $printStr;
246
247 printLog("[$incFile] check if include required\n");
248
249 if (!(defined $g_incInfo{$incFile}))
250 {
251 # get path for every file's 1st entry
252
253 $incPath = &searchIncPath($incFile, \@g_incPath);
254 if ($incPath eq "")
255 {
256 printLog("[Warning] can't find path for $incFile\n");
257 return $valFileNotFound;
258 }
259 $g_incInfo{$incFile}[$idxPath] = $incPath;
260
261 # check for include condition
262 open(hFile, "<$incPath\\$incFile") or die "can't open $incPath\\$incFile\n";
263 @fileData = <hFile>;
264 close(hFile);
265 $g_incInfo{$incFile}[$idxIncluded] = 0;
266 if (&getIncWrapper(\@fileData) eq "")
267 {
268 $g_incInfo{$incFile}[$idxAlwaysInc] = 1;
269 }
270 else
271 {
272 $g_incInfo{$incFile}[$idxAlwaysInc] = 0;
273 }
274 printLog("[$incFile] ifAlwaysInc = ".$g_incInfo{$incFile}[$idxAlwaysInc]."\n");
275 }
276 else
277 {
278 # just return if this inc is already included
279 return $valAlreadyIncluded if ($g_incInfo{$incFile}[$idxIncluded]);
280 $incPath = $g_incInfo{$incFile}[$idxPath];
281 }
282
283 if (defined $incTreeHash{$incFile})
284 {
285 printLog("[Warning] recursive include!\n");
286 return $valRecursiveInclude;
287 }
288
289 $printStr = "//[hExpand.pl][$incFile] expanded (".join('->', @incDeeps)."->)\n";
290 printLog($printStr);
291 #print $fileHandle $printStr;
292
293 $g_totalIncCount++;
294 printBoth("[$g_totalIncCount][$incFile] included\n");
295
296 # mark as included if appropriate
297 $g_incInfo{$incFile}[$idxIncluded] = 1 if (!$alwaysExpand);
298 printLog("//[hExpand.pl][$incFile][alwaysExpand] = $alwaysExpand, [included] = ".$g_incInfo{$incFile}[$idxIncluded]."\n");
299
300 if (scalar(@fileData) == 0)
301 {
302 open(hFile, "<$incPath\\$incFile") or die "can't open $incPath\\$incFile\n";
303 @fileData = <hFile>;
304 close(hFile);
305 }
306
307 my @incOrderList = ();
308 if (!$lineRedirect)
309 {
310 @fileData = &removeComment(\@fileData) if ($removeCommentOrNot);
311 @fileData = &removeConsecutiveEmpty(\@fileData) if ($removeConsecutiveEmpty);
312 }
313 else
314 {
315 # find inc order first
316 my @tmpData = &removeComment(\@fileData);
317 foreach my $line (@tmpData)
318 {
319 if ($line =~ /^[\s]*\#include[\s]+([<\"])[\s]*([\w\.]+)[\s]*\1/)
320 {
321 push @incOrderList, lc $2;
322 }
323 }
324 }
325
326 # add line redirect
327 my $incPrintPathName = "$incPath\\$incFile";
328 $incPrintPathName =~ s/\\/\\\\/g;
329 print $fileHandle "#line 1 \"$incPrintPathName\"\n" if ($lineRedirect);
330
331 my $iMax = $#fileData;
332 my $conditionalInclude = 0;
333 my $ifDeeps = 0;
334 my $nodeMax = 0; # max node counts
335 for (my $i=0; $i<=$iMax; $i++)
336 {
337 my $line = $fileData[$i];
338 if ($line =~ /^[\s]*\#include[\s]+([<\"])[\s]*([\w\.]+)[\s]*\1/)
339 {
340 my $inc = lc $2;
341
342 # make sure we get correct #include (not in comments)
343 if ($lineRedirect)
344 {
345 if ($inc ne $incOrderList[0])
346 {
347 print $fileHandle $line;
348 next;
349 }
350 else
351 {
352 shift @incOrderList; # remove from list
353 }
354 }
355
356 # bypass all target files, since we'll expand them one by one
357 if ((defined $g_targetFileHash{$inc}) || $g_forceNotExpand || (defined $g_nonExpandList{$inc}))
358 {
359 printLog("[$inc] not to include");
360 print $fileHandle $line;
361 next;
362 }
363
364 printLog("[$inc] found\n");
365
366 push @incDeeps, $incFile;
367 $incTreeHash{$incFile} = 1;
368
369 my $nodeCount = 0;
370 my $res = expandInc($fileHandle, $inc, ($alwaysExpand || $conditionalInclude), \$nodeCount);
371 my $tmpLine = "";
372
373 #if ($conditionalInclude && !$alwaysExpand)
374 {
375 # get tree node counts
376 printLog("//[hExpand.pl][$inc]\t[nodes = $nodeCount]\t[".join('->', @incDeeps)."]\n");
377 }
378
379 pop @incDeeps;
380 delete $incTreeHash{$incFile};
381
382 if ($res == $valSuccess)
383 {
384 # do nothing
385 $nodeMax = $nodeCount if ($nodeCount > $nodeMax);
386 ($$nodeCount_ref) += 1 + $nodeCount; # calculate tree-node count
387 $tmpLine = "#line ".($i+1)." \"$incPrintPathName\"\n" if ($lineRedirect);
388 }
389 elsif ($res == $valAlreadyIncluded)
390 {
391 $printStr = "//[hExpand.pl][$inc] already included (".join('->', @incDeeps).")\n";
392 #print $fileHandle $printStr;
393 printLog($printStr);
394 $tmpLine = "//$line" if ($lineRedirect);
395 }
396 elsif ($res == $valFileNotFound)
397 {
398 $printStr = "//[hExpand.pl][$inc] file not found\n";
399 #print $fileHandle $printStr;
400 printLog($printStr);
401 $tmpLine = $line; # file not found, keep #include "xxx"
402 }
403 elsif ($res == $valRecursiveInclude)
404 {
405 $printStr = "//[hExpand.pl][$inc] recursive include (".join('->', @incDeeps).")\n";
406 #print $fileHandle $printStr;
407 printLog($printStr);
408 $tmpLine = "//$line" if ($lineRedirect);
409 }
410 print $fileHandle $tmpLine;
411 }
412 else
413 {
414 if ($line =~ /^[\s]*\#ifndef[\s]+([\w]+)/)
415 {
416 $ifDeeps++;
417 my $wrapper = $1;
418 if ((defined $fileData[$i+1]) && ($fileData[$i+1] =~ /^[\s]*\#define[\s]+$wrapper[^\w]+/) &&
419 ($wrapper =~ /_h[_]*$/i))
420 {
421 printLog("//[hExpand.pl][wrapper] $wrapper\n");
422 }
423 else
424 {
425 $conditionalInclude = $ifDeeps if ($conditionalInclude == 0);
426 }
427 }
428 elsif ($line =~ /^[\s]*\#if/)
429 {
430 $ifDeeps++;
431 $conditionalInclude = $ifDeeps if ($conditionalInclude == 0);
432 }
433 elsif ($line =~ /^[\s]*\#endif/)
434 {
435 if ($conditionalInclude == $ifDeeps)
436 {
437 $conditionalInclude = 0;
438 }
439 $ifDeeps--;
440 }
441 elsif ($line =~ /<hExpand[\s]+noExpand[\s]*>/i)
442 {
443 $g_forceNotExpand = 1;
444 }
445 elsif ($line =~ /<\/hExpand>/i)
446 {
447 $g_forceNotExpand = 0;
448 }
449
450 print $fileHandle $line;
451 }
452 }
453
454 if (($$nodeCount_ref - $nodeMax) >= $g_nodeThreshold)
455 {
456 printLog("//[hExpand.pl][$incFile][diff lt node threshold]\t[$$nodeCount_ref][$nodeMax][".($$nodeCount_ref - $nodeMax)."]\n");
457 }
458
459 print $fileHandle "\n";
460 return $valSuccess;
461}
462
463
464#******************************************************************************
465# FUNCTION
466# getIncWrapper
467# DESCRIPTION
468# xxx
469# PARAMETERS
470# xxx
471# RETURNS
472# xxx
473#******************************************************************************
474sub getIncWrapper()
475{
476 my ($fileData_aref) = @_;
477
478 my @tmpData = &removeComment($fileData_aref);
479
480 # remove beginning '#include'
481 my $iMax = $#tmpData;
482 for (my $i=0; $i<=$iMax; $i++)
483 {
484 my $line = $tmpData[$i];
485 if ($line =~ /^[\s]*\#include/)
486 {
487 }
488 elsif ($line =~ /^[\s]*$/)
489 {
490 # skip empty lines
491 }
492 elsif ($line =~ /^[\s]*\#if/)
493 {
494 @tmpData = @tmpData[$i..$iMax];
495 last;
496 }
497 }
498
499 my $str = join('', @tmpData);
500 if ($str =~ /^[\s]*\#ifndef[\s]+([\w]+).*?\#endif[\s]*$/s)
501 {
502 # print "[$1] inc wrapper\n";
503 return $1;
504 }
505 return "";
506}
507
508
509#******************************************************************************
510# FUNCTION
511# removeComment
512# DESCRIPTION
513# xxx
514# PARAMETERS
515# xxx
516# RETURNS
517# xxx
518#******************************************************************************
519sub removeComment()
520{
521 my ($fileData_aref) = @_;
522
523 my @outData = ();
524
525 # remove '//'
526 foreach my $line (@{$fileData_aref})
527 {
528 push @outData, $line if ($line !~ /^[\s]*\/\//);
529 }
530
531 my $tmpStr = join('', @outData);
532
533 $tmpStr =~ s/(?<!\\)\/\*.*?\*\///sg;
534 @outData = split("\n", $tmpStr);
535 @outData = map {$_."\n"} @outData;
536 return @outData;
537}
538
539
540#******************************************************************************
541# FUNCTION
542# removeConsecutiveEmpty
543# DESCRIPTION
544# xxx
545# PARAMETERS
546# xxx
547# RETURNS
548# xxx
549#******************************************************************************
550sub removeConsecutiveEmpty()
551{
552 my ($fileData_aref) = @_;
553
554 my $tmpStr = join('', @{$fileData_aref});
555 $tmpStr =~ s/\n{3,}/\n\n/sg;
556 my @outData = split("\n", $tmpStr);
557 @outData = map {$_."\n"} @outData;
558 return @outData;
559}
560
561
562#******************************************************************************
563# FUNCTION
564# searchIncPath
565# DESCRIPTION
566# xxx
567# PARAMETERS
568# xxx
569# RETURNS
570# xxx
571#******************************************************************************
572sub searchIncPath()
573{
574 my ($incFile, $incPath_aref) = @_;
575
576 if (defined $notFoundFileList->{$incFile})
577 {
578 return "";
579 }
580 my $incPath = $foundFileList->{$incFile};
581 if (defined $incPath)
582 {
583 return $incPath if (-e "$incPath\\$incFile")
584 }
585
586 foreach my $incPath (@{$incPath_aref})
587 {
588 my $filepath = "$incPath\\$incFile";
589 if (-e $filepath)
590 {
591 $foundFileList->{$incFile} = $incPath;
592 return $incPath;
593 }
594 }
595
596 $notFoundFileList->{$incFile} = 1;
597 return "";
598}
599
600
601
602#******************************************************************************
603# FUNCTION
604# readCompileInfo
605# DESCRIPTION
606# xxx
607# PARAMETERS
608# xxx
609# RETURNS
610# xxx
611#******************************************************************************
612sub readCompileInfo()
613{
614 my ($incMod_aref, $incPath_aref, $def_aref) = @_;
615
616 if ($g_prjName eq "")
617 {
618 push @{$incPath_aref}, "."; # no project specified, just put current dir to inc path
619 print "[Warning] no compile info!\n";
620 return;
621 }
622
623 my $infoFilename = "$g_mcuPath\\build\\$g_prjName\\log\\info.log";
624
625 my %defHash = ();
626 my %incHash = ();
627
628 my $inSection;
629 if ($incPath_aref || $def_aref)
630 {
631 # read common options and common include path from info.log
632 open(hFile, "<$infoFilename") or die "can't open $infoFilename";
633 my @fileData = <hFile>;
634 close(hFile);
635
636 foreach my $line (@fileData)
637 {
638 if ($line =~ /\[ COMMON OPTION \]/)
639 {
640 $inSection = 1;
641 next;
642 }
643 elsif ($line =~ /\[ COMMON INCLUDE PATH \]/)
644 {
645 $inSection = 2;
646 next;
647 }
648
649 if ($line =~ /(^[^\[][^\s]*)/)
650 {
651 if ($inSection == 1)
652 {
653 #print "$1\n";
654 if ($def_aref && !(defined $defHash{$1}))
655 {
656 push @{$def_aref}, $1;
657 $defHash{$1} = 1;
658 }
659 }
660 elsif ($inSection == 2)
661 {
662 my $incPath = "$1";
663
664 if ($incPath !~ /:/)
665 {
666 $incPath = "$g_mcuPath\\$incPath";
667 }
668 #print "$incPath\n";
669
670 if ($incPath_aref && !(defined $incHash{$incPath}))
671 {
672 push @{$incPath_aref}, $incPath;
673 $incHash{$incPath} = 1;
674 }
675 }
676 }
677 else
678 {
679 $inSection = 0;
680 }
681 }
682 }
683
684 # read inc from *.inc
685 if ($incPath_aref)
686 {
687 foreach my $myMod (@{$incMod_aref})
688 {
689 my @incFiles = ();
690 if (&getFileList("$g_mcuPath\\make\\$myMod\\*.inc", 1, \@incFiles) > 0)
691 {
692 foreach my $incFilename (@incFiles)
693 {
694 open(hFile, "<$incFilename") or die "can't open $incFilename";
695 my @fileData = <hFile>;
696 close(hFile);
697
698 foreach my $line (@fileData)
699 {
700 if ($line =~ /(^[^\s]+)([\s]*$)/)
701 {
702 my $incPath = "$g_mcuPath\\$1";
703 #$incPaths = "$incPaths\n-I$g_mcuPath\\$1";
704 if (!(defined $incHash{$incPath}))
705 {
706 push @{$incPath_aref}, $incPath;
707 $incHash{$incPath} = 1;
708 }
709 }
710 }
711 }
712 }
713 else
714 {
715 print "[Warning] can't find *.inc for $myMod\n";
716 }
717 }
718 }
719
720 # read macro from *.def
721 if ($def_aref)
722 {
723 foreach my $myMod (@{$incMod_aref})
724 {
725 my @defFiles = ();
726 if (&getFileList("$g_mcuPath\\make\\$myMod\\*.def", 1, \@defFiles) > 0)
727 {
728 foreach my $defFilename (@defFiles)
729 {
730 open(hFile, "<$defFilename") or die "can't open $defFilename";
731 my @fileData = <hFile>;
732 close(hFile);
733
734 foreach my $line (@fileData)
735 {
736 if ($line =~ /(^[^\s]+)([\s]*$)/)
737 {
738 #$commonOptions = "$commonOptions\n-D$1";
739 if (!(defined $defHash{$1}))
740 {
741 push @{$def_aref}, $1;
742 $defHash{$1} = 1;
743 }
744 }
745 }
746 }
747 }
748 else
749 {
750 print "[Warning] can't find *.def for $myMod\n";
751 }
752 }
753 }
754}
755
756
757#******************************************************************************
758# FUNCTION
759# getFileList
760# DESCRIPTION
761# get file/path list for given search string
762# PARAMETERS
763# xxx
764# RETURNS
765# xxx
766#******************************************************************************
767sub getFileList()
768{
769 my $rootDir;
770 my $incSubDir; # include sub dir (1) or not (0)
771 my $fileList_ref;
772
773 ($rootDir, $incSubDir, $fileList_ref) = @_;
774
775 my @fileData = ();
776
777 # get file list
778 my $tmpFilename = "~tmpFile.lst";
779
780 my $dirStr = "$rootDir /b";
781 $dirStr .= " /s" if ($incSubDir);
782
783 system("dir ".$dirStr." > $tmpFilename");
784 open(hFile, "<$tmpFilename") or die "[ERROR] can't open $tmpFilename\n";
785 @fileData = <hFile>;
786 close(hFile);
787 system("del $tmpFilename") if ($g_removeTempFile);
788
789 foreach my $line (@fileData)
790 {
791 my $name = $line;
792 push @{$fileList_ref}, $name if ($name !~ /^[\s]*$/);
793 #print "$name";
794 }
795 return scalar(@{$fileList_ref});
796}
797
798
799#******************************************************************************
800# FUNCTION
801# timeCheck
802# DESCRIPTION
803# print current time (in sec.) and time-difference to previous check if exists
804# PARAMETERS
805# none
806# RETURNS
807# current time and time difference if exists (both in sec.)
808#******************************************************************************
809my @timePrev = ();
810sub timeCheck()
811{
812 my @timeCurr = localtime(time);
813 printBoth("[Time: ".&timeInSec(\@timeCurr)." sec.");
814 if (@timePrev > 0) # previous-time exists
815 {
816 printBoth("(Diff = ".&timeDiffInSec(\@timeCurr, \@timePrev).")");
817 }
818 printBoth("]\n");
819 @timePrev = @timeCurr;
820}
821
822
823#******************************************************************************
824# FUNCTION
825# timeDiffInSec
826# DESCRIPTION
827# get time difference in sec.
828# PARAMETERS
829# para 1 - reference to time1 in localtime array format
830# para 2 - reference to time2 in localtime array format
831# RETURNS
832# time difference in sec.
833#******************************************************************************
834sub timeDiffInSec()
835{
836 my $time1;
837 my $time2;
838 my $timeDiff;
839
840 ($time1, $time2) = @_;
841
842 $timeDiff = timeInSec($time1) - timeInSec($time2);
843 if ($timeDiff < 0)
844 {
845 return (-$timeDiff);
846 }
847 return $timeDiff;
848}
849
850
851#******************************************************************************
852# FUNCTION
853# timeInSec
854# DESCRIPTION
855# time in sec.
856# PARAMETERS
857# para - time in localtime array format
858# RETURNS
859# time in sec.
860#******************************************************************************
861sub timeInSec()
862{
863 my @daysInMon = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
864 my $sec = $_[0][0];
865 my $min = $_[0][1];
866 my $hour = $_[0][2];
867 my $day = $_[0][3];
868 my $mon = $_[0][4]+1;
869 my $year = $_[0][5];
870 my $timeInSec;
871
872 if (isLeapYear($year+1900))
873 {
874 $daysInMon[1] = 29;
875 $timeInSec = (((($year*366+$mon*$daysInMon[$mon-1]+$day)*24 + $hour)*60 + $min)*60 + $sec);
876 }
877 else
878 {
879 $timeInSec = (((($year*365+$mon*$daysInMon[$mon-1]+$day)*24 + $hour)*60 + $min)*60 + $sec);
880 }
881 return $timeInSec;
882}
883
884
885#******************************************************************************
886# FUNCTION
887# isLeapYear
888# DESCRIPTION
889# is input year leap-year
890# PARAMETERS
891# year
892# RETURNS
893# leap-year or not
894#******************************************************************************
895sub isLeapYear()
896{
897 my $year = $_[0];
898
899 if (($year%4 == 0) && ($year%100 != 0))
900 {
901 return 1;
902 }
903 return 0;
904}
905
906
907sub findMismatchIf()
908{
909 my ($data_aref) = @_;
910
911 my $nestNum = 0;
912 my %nestHash = ();
913
914 printLog("\n[findMismatchIf]\n");
915
916 foreach my $line (@{$data_aref})
917 {
918 if ($line =~ /^[\s]*\#if/)
919 {
920 $nestNum++;
921 $nestHash{$nestNum} = $line;
922 #printLog("[$nestNum][+] $line");
923 printLog("$line");
924 }
925 elsif ($line =~ /^[\s]*\#endif/)
926 {
927 #printLog("[$nestNum][-] $nestHash{$nestNum}");
928 delete $nestHash{$nestNum};
929 $nestNum--;
930 }
931 }
932 my @nestIdx = sort keys(%nestHash);
933
934 foreach my $idx (@nestIdx)
935 {
936 printLog("[$idx] $nestHash{$idx}\n");
937 }
938}
939
940sub printBoth()
941{
942 my ($str) = @_;
943 print $str;
944 print $logFileHandle $str;
945}
946
947sub printLog()
948{
949 my ($str) = @_;
950 print $logFileHandle $str;
951}
952
953
954sub trim()
955{
956 my ($str) = @_;
957 $str =~ s/(^\s*)|(\s*$)//g;
958 return $str;
959}