cresstr.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. {
  2. $Id$
  3. Copyright (c) 1999 by the Free Pascal development team
  4. Handles resourcestrings
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cresstr;
  19. interface
  20. procedure insertresourcestrings;
  21. procedure registerresourcestring(p : pchar;len : longint);
  22. function calc_resstring_hashvalue(p : pchar;len : longint) : longint;
  23. implementation
  24. uses
  25. aasm,verbose,files;
  26. const
  27. { we can use a static constant because we compile a program only once }
  28. { per compiler call }
  29. resstrcount : longint = 0;
  30. { calcs the hash value for a give resourcestring, len is }
  31. { necessary because the resourcestring can contain #0 }
  32. function calc_resstring_hashvalue(p : pchar;len : longint) : longint;
  33. Var hash,g,I : longint;
  34. begin
  35. hash:=len;
  36. For I:=0 to Len-1 do
  37. begin
  38. hash:=hash shl 4;
  39. inc(Hash,Ord(p[i]));
  40. g:=hash and ($f shl 28);
  41. if g<>0 then
  42. begin
  43. hash:=hash xor (g shr 24);
  44. hash:=hash xor g;
  45. end;
  46. end;
  47. If Hash=0 then
  48. Calc_resstring_hashvalue:=Not(0)
  49. else
  50. calc_resstring_hashvalue:=Hash;
  51. end;
  52. procedure insertresourcestrings;
  53. begin
  54. if not(assigned(resourcestringlist)) then
  55. resourcestringlist:=new(paasmoutput,init);
  56. resourcestringlist^.insert(new(pai_const,init_32bit(resstrcount)));
  57. resourcestringlist^.insert(new(pai_symbol,initname_global('RESOURCESTRINGLIST')));
  58. end;
  59. procedure registerresourcestring(p : pchar;len : longint);
  60. var
  61. l1 : pasmlabel;
  62. s : pchar;
  63. begin
  64. { shall we generate a po file? }
  65. { !!!!!! not yet implemented }
  66. { we don't need to generate consts in units }
  67. if current_module^.is_unit then
  68. exit;
  69. if not(assigned(resourcestringlist)) then
  70. resourcestringlist:=new(paasmoutput,init);
  71. inc(resstrcount);
  72. { an empty ansi string is nil! }
  73. if (p=nil) or (len=0) then
  74. resourcestringlist^.concat(new(pai_const,init_32bit(0)))
  75. else
  76. begin
  77. getdatalabel(l1);
  78. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  79. { first write the maximum size }
  80. consts^.concat(new(pai_const,init_32bit(len)));
  81. { second write the real length }
  82. consts^.concat(new(pai_const,init_32bit(len)));
  83. { redondent with maxlength but who knows ... (PM) }
  84. { third write use count (set to -1 for safety ) }
  85. consts^.concat(new(pai_const,init_32bit(-1)));
  86. consts^.concat(new(pai_label,init(l1)));
  87. getmem(s,len+1);
  88. move(p^,s^,len);
  89. s[len]:=#0;
  90. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  91. consts^.concat(new(pai_const,init_8bit(0)));
  92. end;
  93. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  94. resourcestringlist^.concat(new(pai_const,init_32bit(
  95. calc_resstring_hashvalue(p,len))));
  96. end;
  97. end.
  98. {
  99. $Log$
  100. Revision 1.2 1999-07-22 20:04:58 michael
  101. + Added computehashvalue
  102. Revision 1.1 1999/07/22 09:34:04 florian
  103. + initial revision
  104. }