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.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they 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. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. */
  20. BEGIN_JUCE_NAMESPACE
  21. //==============================================================================
  22. void CommandLineTokenizer::parseCommandLine (const String& commandLine)
  23. {
  24. int section;
  25. String value, parse = commandLine.trim();
  26. while (parse.length() > 0)
  27. {
  28. section = parse.indexOf (0, " ");
  29. if (section == -1)
  30. section = parse.length();
  31. argv.add (parse.substring (0, section));
  32. if (section < parse.length() )
  33. parse = parse.substring (section+1,parse.length()).trim();
  34. else
  35. parse = String();
  36. }
  37. }
  38. //==============================================================================
  39. const String& CommandLineTokenizer::operator[] (const int index) const throw()
  40. {
  41. jassert (index >= 0 && index < argv.size());
  42. return argv[index];
  43. }
  44. const int CommandLineTokenizer::size()
  45. {
  46. return argv.size();
  47. }
  48. //==============================================================================
  49. int CommandLineTokenizer::searchToken (const String& tokenToSearch, const bool caseSensitive)
  50. {
  51. if (caseSensitive)
  52. {
  53. for (int i = 0; i < argv.size(); i++ )
  54. if (tokenToSearch.compare (argv[i]) == 0)
  55. return i;
  56. }
  57. else
  58. {
  59. for (int i = 0; i < argv.size(); i++ )
  60. if (tokenToSearch.compareIgnoreCase (argv[i]) == 0)
  61. return i;
  62. }
  63. return -1;
  64. }
  65. //==============================================================================
  66. String CommandLineTokenizer::getOptionString (const String& tokenToSearch,
  67. const String& defValue,
  68. const bool caseSensitive)
  69. {
  70. int i = searchToken (tokenToSearch, caseSensitive);
  71. if (i++ < 0)
  72. return defValue;
  73. if (i >= 0 && i < argv.size())
  74. return argv[i];
  75. else
  76. return defValue;
  77. }
  78. bool CommandLineTokenizer::getOptionBool (const String& tokenToSearch,
  79. const bool defValue,
  80. const bool caseSensitive)
  81. {
  82. int i = searchToken (tokenToSearch, caseSensitive);
  83. if (i++ < 0)
  84. return defValue;
  85. if (i >= 0 && i < argv.size())
  86. return argv[i].getIntValue() == 0 ? false : true;
  87. else
  88. return defValue;
  89. }
  90. int CommandLineTokenizer::getOptionInt (const String& tokenToSearch,
  91. const int defValue,
  92. const bool caseSensitive)
  93. {
  94. int i = searchToken (tokenToSearch, caseSensitive);
  95. if (i++ < 0)
  96. return defValue;
  97. if (i >= 0 && i < argv.size())
  98. return argv[i].getIntValue();
  99. else
  100. return defValue;
  101. }
  102. double CommandLineTokenizer::getOptionDouble (const String& tokenToSearch,
  103. const double defValue,
  104. const bool caseSensitive)
  105. {
  106. int i = searchToken (tokenToSearch, caseSensitive);
  107. if (i++ < 0)
  108. return defValue;
  109. if (i >= 0 && i < argv.size())
  110. return argv[i].getDoubleValue();
  111. else
  112. return defValue;
  113. }
  114. END_JUCE_NAMESPACE