Marshalling.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 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; inline;
  43. class function ToString(const S: MarshaledAString): string; static; inline;
  44. end;
  45. { TMarshaller* }
  46. {$IFDEF FPC}
  47. TMarshaller = record
  48. {$ELSE}
  49. TMarshallerHelper = record helper for TMarshaller
  50. {$ENDIF}
  51. public
  52. function ToCString(const S: string): MarshaledAString; inline;
  53. function ToCNullableString(const S: string): MarshaledAString; inline;
  54. end;
  55. implementation
  56. { TMarshal* }
  57. class function {$IFDEF FPC}TMarshal{$ELSE}TMarshalHelper{$ENDIF}.ToBytes(
  58. const S: MarshaledAString; L: NativeUInt): TBytes;
  59. begin
  60. if (not Assigned(S)) or (L = 0) then
  61. Exit(nil);
  62. SetLength(Result, L);
  63. System.Move(S^, Result[0], L);
  64. end;
  65. class function {$IFDEF FPC}TMarshal{$ELSE}TMarshalHelper{$ENDIF}.ToString(
  66. const S: MarshaledAString): string;
  67. begin
  68. if not Assigned(S) then
  69. Exit('');
  70. {$IFDEF FPC}
  71. SetString(Result, S, Length(S));
  72. SetCodePage(RawByteString(Result), CP_UTF8, False);
  73. {$ELSE}
  74. Result := TMarshal.ReadStringAsUtf8(TPtrWrapper.Create(S));
  75. {$ENDIF}
  76. end;
  77. { TMarshaller* }
  78. function {$IFDEF FPC}TMarshaller{$ELSE}TMarshallerHelper{$ENDIF}.ToCString(
  79. const S: string): MarshaledAString;
  80. begin
  81. Result :=
  82. {$IFDEF FPC}
  83. MarshaledAString(S)
  84. {$ELSE}
  85. AsAnsi(S, CP_UTF8).ToPointer
  86. {$ENDIF};
  87. end;
  88. function {$IFDEF FPC}TMarshaller{$ELSE}TMarshallerHelper{$ENDIF}.ToCNullableString(
  89. const S: string): MarshaledAString;
  90. begin
  91. if S = '' then
  92. Exit(nil);
  93. Result := ToCString(S);
  94. end;
  95. end.