DISTRHO Plugin Framework
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.

139 lines
3.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #define TTL_GENERATOR_WINDOWS
  22. #else
  23. #include <dlfcn.h>
  24. #endif
  25. #ifndef nullptr
  26. #define nullptr (0)
  27. #endif
  28. typedef void (*TTL_Generator_Function)(const char* basename);
  29. static int isPathSeparator(char c);
  30. static char* makeNormalPath(const char* path);
  31. // TODO support Unicode paths on the Windows platform
  32. int main(int argc, char* argv[])
  33. {
  34. if (argc != 2)
  35. {
  36. printf("usage: %s /path/to/plugin-DLL\n", argv[0]);
  37. return 1;
  38. }
  39. const char* path = argv[1];
  40. #ifdef TTL_GENERATOR_WINDOWS
  41. const HMODULE handle = LoadLibraryA(path);
  42. #else
  43. void* const handle = dlopen(path, RTLD_LAZY);
  44. #endif
  45. if (! handle)
  46. {
  47. #ifdef TTL_GENERATOR_WINDOWS
  48. printf("Failed to open plugin DLL\n");
  49. #else
  50. printf("Failed to open plugin DLL, error was:\n%s\n", dlerror());
  51. #endif
  52. return 2;
  53. }
  54. #ifdef TTL_GENERATOR_WINDOWS
  55. const TTL_Generator_Function ttlFn = (TTL_Generator_Function)GetProcAddress(handle, "lv2_generate_ttl");
  56. #else
  57. const TTL_Generator_Function ttlFn = (TTL_Generator_Function)dlsym(handle, "lv2_generate_ttl");
  58. #endif
  59. if (ttlFn != NULL)
  60. {
  61. // convert the paths to a normalized form, such that path separators are
  62. // replaced with '/', and duplicate separators are removed
  63. char* normalPath = makeNormalPath(path);
  64. // get rid of any "./" prefixes
  65. path = normalPath;
  66. while (path[0] == '.' && path[1] == '/')
  67. path += 2;
  68. // extract the file name part
  69. char* basename = strrchr(path, '/');
  70. if (basename != NULL)
  71. basename += 1;
  72. else
  73. basename = (char*)path;
  74. // remove the file extension
  75. char* dotPos = strrchr(basename, '.');
  76. if (dotPos)
  77. *dotPos = '\0';
  78. printf("Generate ttl data for '%s', basename: '%s'\n", path, basename);
  79. ttlFn(basename);
  80. free(normalPath);
  81. }
  82. else
  83. printf("Failed to find 'lv2_generate_ttl' function\n");
  84. #ifdef TTL_GENERATOR_WINDOWS
  85. FreeLibrary(handle);
  86. #else
  87. dlclose(handle);
  88. #endif
  89. return 0;
  90. }
  91. static int isPathSeparator(char c)
  92. {
  93. #ifdef TTL_GENERATOR_WINDOWS
  94. return c == '/' || c == '\\';
  95. #else
  96. return c == '/';
  97. #endif
  98. }
  99. static char* makeNormalPath(const char* path)
  100. {
  101. size_t i, j;
  102. size_t len = strlen(path);
  103. char* result = (char*)malloc(len + 1);
  104. int isSep, wasSep = 0;
  105. for (i = 0, j = 0; i < len; ++i)
  106. {
  107. isSep = isPathSeparator(path[i]);
  108. if (!isSep)
  109. result[j++] = path[i];
  110. else if (!wasSep)
  111. result[j++] = '/';
  112. wasSep = isSep;
  113. }
  114. result[j] = '\0';
  115. return result;
  116. }