zorba.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. (*
  2. * Copyright 2006-2008 The FLWOR Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *)
  16. {$mode objfpc}{$H+}
  17. {$PACKRECORDS C}
  18. {$MACRO ON}
  19. interface
  20. uses
  21. Sysutils,ctypes, dynlibs, xqc;
  22. {$IFDEF UNIX}
  23. {$DEFINE extdecl:=cdecl}
  24. const
  25. zorbalib = 'libzorba_simplestore.'+sharedsuffix;
  26. zorbavlib = zorbalib+'.0.9.9';
  27. {$ENDIF}
  28. {$IFDEF WINDOWS}
  29. {$DEFINE extdecl:=stdcall}
  30. const
  31. zorbalib = 'zorba_simplestore.dll';
  32. zorbavlib = zorbalib;
  33. {$ENDIF}
  34. {$IFDEF LOAD_DYNAMICALLY}
  35. {$DEFINE D}
  36. {$ELSE}
  37. {$DEFINE S}
  38. {$ENDIF}
  39. {.$i zorba_config.inc}
  40. {$i zorba_options.inc}
  41. (**
  42. * The zorba_implementation function creates a new ::XQC_Implementation object.
  43. * Thereby, the Zorba processor is initialized.
  44. * The user is responsible for freeing the object by calling the free() function
  45. * of the XQC_Implementation struct.
  46. *
  47. * \param store A pointer to the store that is being used by the Zorba instance that is created
  48. * by this call.
  49. * \param[out] impl The newly created XQC_Implementation object.
  50. *
  51. * \retval ::XQC_NO_ERROR
  52. * \retval ::XQP0019_INTERNAL_ERROR
  53. *)
  54. {$IFDEF S}function{$ELSE}var{$ENDIF}zorba_implementation{$IFDEF D}: function{$ENDIF}(out impl: XQC_Implementation; store: Pointer): XQUERY_ERROR; extdecl;{$IFDEF S}external zorbalib;{$ENDIF}
  55. (* simplestorec.h *)
  56. {$IFDEF S}function{$ELSE}var{$ENDIF}create_simple_store{$IFDEF D}: function{$ENDIF}: Pointer; extdecl;{$IFDEF S}external zorbalib;{$ENDIF}
  57. {$IFDEF S}procedure{$ELSE}var{$ENDIF}shutdown_simple_store{$IFDEF D}: procedure{$ENDIF}(store: Pointer); extdecl;{$IFDEF S}external zorbalib;{$ENDIF}
  58. {$IFDEF LOAD_DYNAMICALLY}
  59. function InitializeZorba(const LibraryName: String = ''): Integer;
  60. function TryInitializeZorba(const LibraryName: string = ''): Integer;
  61. function ReleaseZorba: Integer;
  62. var
  63. ZorbaLibraryHandle: TLibHandle;
  64. {$ENDIF LOAD_DYNAMICALLY}
  65. implementation
  66. {$IFDEF LOAD_DYNAMICALLY}
  67. ResourceString
  68. SErrDefaultsFailed = 'Can not load default Zorba clients ("%s" or "%s"). Check your installation.';
  69. SErrLoadFailed = 'Can not load Zorba client library "%s". Check your installation.';
  70. SErrAlreadyLoaded = 'Zorba interface already initialized from library %s.';
  71. var
  72. RefCount : integer;
  73. LoadedLibrary : String;
  74. Function TryInitializeZorba(Const LibraryName : String) : integer;
  75. begin
  76. Result := 0;
  77. if (RefCount=0) then
  78. begin
  79. ZorbaLibraryHandle:=LoadLibrary(LibraryName);
  80. if (ZorbaLibraryHandle=nilhandle) then
  81. Exit;
  82. inc(RefCount);
  83. LoadedLibrary:=LibraryName;
  84. pointer(create_simple_store) :=GetProcedureAddress(ZorbaLibraryHandle,'create_simple_store');
  85. pointer(shutdown_simple_store) :=GetProcedureAddress(ZorbaLibraryHandle,'shutdown_simple_store');
  86. pointer(zorba_implementation) :=GetProcedureAddress(ZorbaLibraryHandle,'zorba_implementation');
  87. pointer(Zorba_CompilerHints_default) :=GetProcedureAddress(ZorbaLibraryHandle,'Zorba_CompilerHints_default');
  88. pointer(Zorba_SerializerOptions_default):=GetProcedureAddress(ZorbaLibraryHandle,'Zorba_SerializerOptions_default');
  89. pointer(Zorba_SerializerOptions_free) :=GetProcedureAddress(ZorbaLibraryHandle,'Zorba_SerializerOptions_free');
  90. pointer(Zorba_SerializerOptions_set) :=GetProcedureAddress(ZorbaLibraryHandle,'Zorba_SerializerOptions_set');
  91. end
  92. else
  93. inc(RefCount);
  94. Result := RefCount;
  95. end;
  96. function InitializeZorba(const LibraryName: String): Integer;
  97. begin
  98. Result := TryInitializeZorba( LibraryName);
  99. If Result = 0 then
  100. Raise EInOutError.CreateFmt(SErrLoadFailed,[LibraryName])
  101. else If (LibraryName<>LoadedLibrary) then
  102. begin
  103. Dec(RefCount);
  104. Result := RefCount;
  105. Raise EInOUtError.CreateFmt(SErrAlreadyLoaded,[LoadedLibrary]);
  106. end;
  107. end;
  108. function ReleaseZorba: Integer;
  109. begin
  110. if RefCount>1 then
  111. Dec(RefCount)
  112. else if UnloadLibrary(ZorbaLibraryHandle) then
  113. begin
  114. Dec(RefCount);
  115. ZorbaLibraryHandle := NilHandle;
  116. LoadedLibrary:='';
  117. end;
  118. end;
  119. {$ENDIF}
  120. end.