2
0

Marshalling.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. { Useful types for marshalling arguments. }
  26. unit Marshalling;
  27. {$IFDEF FPC}
  28. {$MODE DELPHI}
  29. {$ENDIF}
  30. interface
  31. uses
  32. SysUtils;
  33. type
  34. { TMarshal* }
  35. {$IFDEF FPC}
  36. TMarshal = record
  37. {$ELSE}
  38. TMarshalHelper = class helper for TMarshal
  39. {$ENDIF}
  40. public
  41. class function ToBytes(const S: MarshaledAString;
  42. L: NativeUInt): TBytes; static; {$IFNDEF DEBUG}inline;{$ENDIF}
  43. class function ToString(const S: MarshaledAString): string; static;
  44. {$IFNDEF DEBUG}inline;{$ENDIF}
  45. end;
  46. { TMarshaller* }
  47. {$IFDEF FPC}
  48. TMarshaller = record
  49. {$ELSE}
  50. TMarshallerHelper = record helper for TMarshaller
  51. {$ENDIF}
  52. public
  53. function Length(const S: string): Integer; {$IFNDEF DEBUG}inline;{$ENDIF}
  54. function ToCString(const S: string): MarshaledAString;
  55. {$IFNDEF DEBUG}inline;{$ENDIF}
  56. function ToCNullableString(const S: string): MarshaledAString;
  57. {$IFNDEF DEBUG}inline;{$ENDIF}
  58. end;
  59. implementation
  60. { TMarshal* }
  61. class function {$IFDEF FPC}TMarshal{$ELSE}TMarshalHelper{$ENDIF}.ToBytes(
  62. const S: MarshaledAString; L: NativeUInt): TBytes;
  63. begin
  64. if (not Assigned(S)) or (L = 0) then
  65. Exit(nil);
  66. SetLength(Result, L);
  67. System.Move(S^, Result[0], L);
  68. end;
  69. class function {$IFDEF FPC}TMarshal{$ELSE}TMarshalHelper{$ENDIF}.ToString(
  70. const S: MarshaledAString): string;
  71. begin
  72. if not Assigned(S) then
  73. Exit('');
  74. {$IFDEF FPC}
  75. SetString(Result, S, Length(S));
  76. SetCodePage(RawByteString(Result), CP_UTF8, False);
  77. {$ELSE}
  78. Result := TMarshal.ReadStringAsUtf8(TPtrWrapper.Create(S));
  79. {$ENDIF}
  80. end;
  81. { TMarshaller* }
  82. function {$IFDEF FPC}TMarshaller{$ELSE}TMarshallerHelper{$ENDIF}.Length(
  83. const S: string): Integer;
  84. begin
  85. Result := {$IFDEF FPC}S.Length{$ELSE}System.Length(UTF8String(S)){$ENDIF};
  86. end;
  87. function {$IFDEF FPC}TMarshaller{$ELSE}TMarshallerHelper{$ENDIF}.ToCString(
  88. const S: string): MarshaledAString;
  89. begin
  90. Result :=
  91. {$IFDEF FPC}
  92. MarshaledAString(S)
  93. {$ELSE}
  94. AsAnsi(S, CP_UTF8).ToPointer
  95. {$ENDIF};
  96. end;
  97. function {$IFDEF FPC}TMarshaller{$ELSE}TMarshallerHelper{$ENDIF}.ToCNullableString(
  98. const S: string): MarshaledAString;
  99. begin
  100. if S = '' then
  101. Exit(nil);
  102. Result := ToCString(S);
  103. end;
  104. end.