Audio plugin host https://kx.studio/carla
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.

common.hpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Cross-platform C++ library for Carla, based on Juce v4
  3. * Copyright (C) 2015 ROLI Ltd.
  4. * Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. */
  18. #include "maths/MathsFunctions.h"
  19. #include "misc/Result.h"
  20. #ifdef CARLA_OS_MAC
  21. # include "text/String.h"
  22. # import <Foundation/NSString.h>
  23. #endif
  24. #include <cerrno>
  25. //==============================================================================
  26. namespace water
  27. {
  28. #ifdef CARLA_OS_WIN
  29. static inline
  30. Result getResultForLastError()
  31. {
  32. TCHAR messageBuffer [256] = { 0 };
  33. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  34. nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  35. messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
  36. return Result::fail (String (messageBuffer));
  37. }
  38. static inline
  39. int64 water_fileSetPosition (void* handle, int64 pos)
  40. {
  41. LARGE_INTEGER li;
  42. li.QuadPart = pos;
  43. li.LowPart = SetFilePointer ((HANDLE) handle, (LONG) li.LowPart, &li.HighPart, FILE_BEGIN); // (returns -1 if it fails)
  44. return li.QuadPart;
  45. }
  46. #else
  47. static inline
  48. Result getResultForErrno()
  49. {
  50. #ifdef CARLA_PROPER_CPP11_SUPPORT
  51. return Result::fail (String (std::strerror (errno)));
  52. #else
  53. return Result::fail (String (strerror (errno)));
  54. #endif
  55. }
  56. static inline
  57. int getFD (void* handle) noexcept { return (int) (pointer_sized_int) handle; }
  58. static inline
  59. void* fdToVoidPointer (int fd) noexcept { return (void*) (pointer_sized_int) fd; }
  60. static inline
  61. int64 water_fileSetPosition (void* handle, int64 pos)
  62. {
  63. if (handle != nullptr && lseek (getFD (handle), pos, SEEK_SET) == pos)
  64. return pos;
  65. return -1;
  66. }
  67. #endif
  68. #ifdef CARLA_OS_MAC
  69. static inline
  70. String nsStringToWater (NSString* s)
  71. {
  72. return CharPointer_UTF8 ([s UTF8String]);
  73. }
  74. static inline
  75. NSString* waterStringToNS (const String& s)
  76. {
  77. return [NSString stringWithUTF8String: s.toUTF8()];
  78. }
  79. #endif
  80. }