The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

216 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. //==============================================================================
  19. int64 calculateStreamHashCode (InputStream& stream);
  20. int64 calculateFileHashCode (const File& file);
  21. bool areFilesIdentical (const File& file1, const File& file2);
  22. bool overwriteFileWithNewDataIfDifferent (const File& file, const char* data, int numBytes);
  23. bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData);
  24. bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData);
  25. bool containsAnyNonHiddenFiles (const File& folder);
  26. //==============================================================================
  27. // String::hashCode64 actually hit some dupes, so this is a more powerful version.
  28. const int64 hashCode64 (const String& s);
  29. const String randomHexString (Random& random, int numChars);
  30. const String hexString8Digits (int value);
  31. const String createAlphaNumericUID();
  32. const String createGUID (const String& seed); // Turns a seed into a windows GUID
  33. const String unixStylePath (const String& path);
  34. const String windowsStylePath (const String& path);
  35. bool shouldPathsBeRelative (String path1, String path2);
  36. //==============================================================================
  37. bool isJuceFolder (const File& folder);
  38. const File findParentJuceFolder (const File& file);
  39. const File findDefaultJuceFolder();
  40. //==============================================================================
  41. const String createIncludeStatement (const File& includeFile, const File& targetFile);
  42. const String makeHeaderGuardName (const File& file);
  43. const String replaceCEscapeChars (const String& s);
  44. const String makeValidCppIdentifier (String s,
  45. const bool capitalise,
  46. const bool removeColons,
  47. const bool allowTemplates);
  48. //==============================================================================
  49. const String boolToCode (const bool b);
  50. const String floatToCode (const float v);
  51. const String doubleToCode (const double v);
  52. const String colourToCode (const Colour& col);
  53. const String justificationToCode (const Justification& justification);
  54. const String castToFloat (const String& expression);
  55. //==============================================================================
  56. const String indentCode (const String& code, const int numSpaces);
  57. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex);
  58. //==============================================================================
  59. class FileModificationDetector
  60. {
  61. public:
  62. FileModificationDetector (const File& file_)
  63. : file (file_)
  64. {
  65. }
  66. const File& getFile() const { return file; }
  67. bool hasBeenModified() const
  68. {
  69. return fileModificationTime != file.getLastModificationTime()
  70. && (fileSize != file.getSize()
  71. || calculateFileHashCode (file) != fileHashCode);
  72. }
  73. void updateHash()
  74. {
  75. fileModificationTime = file.getLastModificationTime();
  76. fileSize = file.getSize();
  77. fileHashCode = calculateFileHashCode (file);
  78. }
  79. private:
  80. File file;
  81. Time fileModificationTime;
  82. int64 fileHashCode, fileSize;
  83. };
  84. //==============================================================================
  85. static const double tickSizes[] = { 1.0, 2.0, 5.0,
  86. 10.0, 20.0, 50.0,
  87. 100.0, 200.0, 500.0, 1000.0 };
  88. class TickIterator
  89. {
  90. public:
  91. TickIterator (const double startValue_, const double endValue_, const double valuePerPixel_,
  92. int minPixelsPerTick, int minWidthForLabels)
  93. : startValue (startValue_),
  94. endValue (endValue_),
  95. valuePerPixel (valuePerPixel_)
  96. {
  97. tickLevelIndex = findLevelIndexForValue (valuePerPixel * minPixelsPerTick);
  98. labelLevelIndex = findLevelIndexForValue (valuePerPixel * minWidthForLabels);
  99. tickPosition = pixelsToValue (-minWidthForLabels);
  100. tickPosition = snapValueDown (tickPosition, tickLevelIndex);
  101. }
  102. bool getNextTick (float& pixelX, float& tickLength, String& label)
  103. {
  104. const double tickUnits = tickSizes [tickLevelIndex];
  105. tickPosition += tickUnits;
  106. const int totalLevels = sizeof (tickSizes) / sizeof (*tickSizes);
  107. int highestIndex = tickLevelIndex;
  108. while (++highestIndex < totalLevels)
  109. {
  110. const double ticksAtThisLevel = tickPosition / tickSizes [highestIndex];
  111. if (fabs (ticksAtThisLevel - floor (ticksAtThisLevel + 0.5)) > 0.000001)
  112. break;
  113. }
  114. --highestIndex;
  115. if (highestIndex >= labelLevelIndex)
  116. label = getDescriptionOfValue (tickPosition, labelLevelIndex);
  117. else
  118. label = String::empty;
  119. tickLength = (highestIndex + 1 - tickLevelIndex) / (float) (totalLevels + 1 - tickLevelIndex);
  120. pixelX = valueToPixels (tickPosition);
  121. return tickPosition < endValue;
  122. }
  123. private:
  124. double tickPosition;
  125. int tickLevelIndex, labelLevelIndex;
  126. const double startValue, endValue, valuePerPixel;
  127. int findLevelIndexForValue (const double value) const
  128. {
  129. int i;
  130. for (i = 0; i < sizeof (tickSizes) / sizeof (*tickSizes); ++i)
  131. if (tickSizes [i] >= value)
  132. break;
  133. return i;
  134. }
  135. double pixelsToValue (int pixels) const
  136. {
  137. return startValue + pixels * valuePerPixel;
  138. }
  139. float valueToPixels (double value) const
  140. {
  141. return (float) ((value - startValue) / valuePerPixel);
  142. }
  143. static double snapValueToNearest (const double t, const int valueLevelIndex)
  144. {
  145. const double unitsPerInterval = tickSizes [valueLevelIndex];
  146. return unitsPerInterval * floor (t / unitsPerInterval + 0.5);
  147. }
  148. static double snapValueDown (const double t, const int valueLevelIndex)
  149. {
  150. const double unitsPerInterval = tickSizes [valueLevelIndex];
  151. return unitsPerInterval * floor (t / unitsPerInterval);
  152. }
  153. static inline int roundDoubleToInt (const double value)
  154. {
  155. union { int asInt[2]; double asDouble; } n;
  156. n.asDouble = value + 6755399441055744.0;
  157. #if TARGET_RT_BIG_ENDIAN
  158. return n.asInt [1];
  159. #else
  160. return n.asInt [0];
  161. #endif
  162. }
  163. static const String getDescriptionOfValue (const double value, const int valueLevelIndex)
  164. {
  165. return String (roundToInt (value));
  166. }
  167. };