Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.1 KiB

  1. package HashText;
  2. ##
  3. ## For store hash to a static program file, please use dump
  4. ##
  5. ##
  6. ## Function: Store the hash back to text
  7. ##
  8. ## Prototype ($htype, $filename, $h1, $h2, $h3, ...)
  9. ##
  10. ## Read_Text_Hash
  11. ##
  12. ## Function: Read Text Table and store the data to a hash
  13. ##
  14. ## Prototype: read_text_hash($htype, $filename, $h1, $h2, $h3)
  15. ## ===============================================================
  16. ## $htype => set 0: hash-hash, 1: array-hash
  17. ## $filename => the .txt file contained the table
  18. ## $parameter hash / array pointer (more than one, if have two table in a text file)
  19. sub Read_Text_Hash {
  20. my ($htype, $filename, @ph)=@_; ## get parameters ($type=0:hash-hash(%%), 1:array-hash(@%)
  21. my ($lptr, $hptr,@doc, %tmphash)=(0,0); ## defined internal variables
  22. local *MFH; ## defined my file handle
  23. local $_;
  24. @ph = map( { (ref $_)?$_:\%tmphash; } @ph ); ## defined skip hash to internal temp hash
  25. open(MFH, $filename) or die "Can not open the file $filename"; ## read the document
  26. @doc=<MFH>;
  27. close (MFH);
  28. ## Look for whole document and Assign the data to defined hash
  29. SHN1: while (($lptr<@doc) && ($hptr < @ph)) {
  30. my ($head, @kname, $tempkey)=(0); ## initial set head not found & clean kname array
  31. ## LookFor the hash key name
  32. SHN2: while ($head == 0) { ## look for the table head start line
  33. next SHN1 if($lptr >= @doc);
  34. ($doc[$lptr]=~/^[\;\#]?\s*[\w\\\[]+[\s\]\[]{3}\s*[\w\[\]]+/o)?$head=1:$lptr++;
  35. }
  36. while ($head == 1) {
  37. my $t_i=0;
  38. next SHN1 if($lptr >= @doc);
  39. $_=$doc[$lptr];
  40. if (/(\=\=+)|(\-\-+)/) { ## -------- split line
  41. $head=2, $lptr++;
  42. } elsif (/^[\;\#]?\s*$/) { ## skip the empty line
  43. $lptr++;
  44. } elsif ((/^[^\;\#]\s*[\w\\\[]+[\s\]\[]{3}\s*[\w\[\]]+/)&&(@kname > 0)) { ## defined head & is data line
  45. $head=2;
  46. } elsif (!/^[\;\#]?\s*[\w\\\[]+[\s\]\[]{3}\s*[\w\[\]]+/) { ## skip the comment line
  47. $lptr++;
  48. } else {
  49. s/^[\;\#]?\s+//; ## find the first non-blank & non-;
  50. @kname = map ({ ## grep the first line key name
  51. /\[?(\w+)\]?/;
  52. $tempkey=$1;
  53. $kname[$t_i]="" if (!defined $kname[$t_i]);
  54. sprintf("%s%s",$kname[$t_i++],$tempkey);}
  55. split(/\s+/, $_));
  56. $lptr++;
  57. }
  58. }
  59. while ($head == 2) {
  60. next SHN1 if ($lptr >= @doc);
  61. ($_=$doc[$lptr])=~s/^\s+//;
  62. if ((/^[\;\#]/)||(/^\s*$/)) {
  63. $lptr++;
  64. } elsif ((!/^\s*[^\;\#]+\s/)&&(!/\w+\s{3}\s*\w+/o)) { ## not the data line
  65. $head =0;
  66. } elsif ($htype eq 1) { ## store to an array-hash
  67. my @ta=split(/\s+/, $_);
  68. my %th=map({ $kname[$_] => $ta[$_];} 0..(($#kname)-1));
  69. $th{$kname[$#kname]}=join(" ", @ta[$#kname..$#ta]); ## put the remains to the last one
  70. push @{$ph[$hptr]}, \%th;
  71. $lptr++;
  72. } else { ## store to the hash-hash and
  73. /\s*(\S+)/;
  74. my $key=$1; ## the first element is a key
  75. my @ta=split(/\s+/, $_);
  76. map({ ($_ <= $#kname)?
  77. (${${$ph[$hptr]}{$key}}{$kname[$_]}=$ta[$_]):
  78. (${${$ph[$hptr]}{$key}}{$kname[$#kname]}.=" $ta[$_]");} 1..$#ta); ## put the remains to the last one
  79. $lptr++;
  80. }
  81. }
  82. $hptr++;
  83. }
  84. }
  85. 1;