Collection of tools useful for audio production
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.

84 lines
2.1KB

  1. /*
  2. * Carla common library code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.com>
  4. *
  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. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #ifndef CARLA_LIB_INCLUDES_H
  18. #define CARLA_LIB_INCLUDES_H
  19. #include "carla_includes.h"
  20. #ifdef Q_OS_WIN
  21. # include <cstdio>
  22. #endif
  23. static inline
  24. void* lib_open(const char* const filename)
  25. {
  26. Q_ASSERT(filename);
  27. #ifdef Q_OS_WIN
  28. return LoadLibraryA(filename);
  29. #else
  30. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  31. #endif
  32. }
  33. static inline
  34. bool lib_close(void* const lib)
  35. {
  36. Q_ASSERT(lib);
  37. #ifdef Q_OS_WIN
  38. return FreeLibrary((HMODULE)lib);
  39. #else
  40. return (dlclose(lib) == 0);
  41. #endif
  42. }
  43. static inline
  44. void* lib_symbol(void* const lib, const char* const symbol)
  45. {
  46. Q_ASSERT(lib);
  47. Q_ASSERT(symbol);
  48. #ifdef Q_OS_WIN
  49. return (void*)GetProcAddress((HMODULE)lib, symbol);
  50. #else
  51. return dlsym(lib, symbol);
  52. #endif
  53. }
  54. static inline
  55. const char* lib_error(const char* const filename)
  56. {
  57. Q_ASSERT(filename);
  58. #ifdef Q_OS_WIN
  59. static char libError[2048];
  60. memset(libError, 0, sizeof(char)*2048);
  61. LPVOID winErrorString;
  62. DWORD winErrorCode = GetLastError();
  63. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  64. snprintf(libError, 2048, "%s: error code %i: %s", filename, winErrorCode, (const char*)winErrorString);
  65. LocalFree(winErrorString);
  66. return libError;
  67. #else
  68. return dlerror();
  69. Q_UNUSED(filename);
  70. #endif
  71. }
  72. #endif // CARLA_LIB_INCLUDES_H