Utils.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include <vector>
  14. #include <string>
  15. #include "Exceptions.hpp"
  16. bool getEnclosedString(const std::string source, std::string &result, std::size_t startPos, char endDelim, bool throwIfError);
  17. std::vector<std::string> splitString(const std::string &input, const std::string &delim)
  18. {
  19. size_t start = 0, end = 0, delimLen = delim.length();
  20. std::vector<std::string> list;
  21. while (end != std::string::npos)
  22. {
  23. end = input.find(delim, start);
  24. std::string item = input.substr(start, (end == std::string::npos) ? std::string::npos : end - start);
  25. if (!item.empty())
  26. list.push_back(item);
  27. start = ((end > (std::string::npos - delimLen)) ? std::string::npos : end + delimLen);
  28. }
  29. return list;
  30. }
  31. bool extractEnclosedString(const std::string source, std::string &result, char startDelim, char endDelim, bool optional)
  32. {
  33. bool rc = false;
  34. std::size_t startPos = source.find_first_of(startDelim);
  35. if (startPos != std::string::npos)
  36. {
  37. rc = getEnclosedString(source, result, startPos, endDelim, true);
  38. }
  39. else if (!optional)
  40. {
  41. std::string msg = "Bad string parse, expected string enclosed in '" + std::string(1, startDelim) + std::string(1, endDelim) + "' at or around: " + source;
  42. throw(ParseException(msg));
  43. }
  44. else
  45. {
  46. result = source;
  47. }
  48. return rc;
  49. }
  50. // throwIfError allows the caller to ignore an error finding the enclosed string and simply return the input string as the result string.
  51. // If true, the caller expects there to be an ending delimiter and wants an exception since this is an error condition as determined by the caller.
  52. // The return value will always reflect whether the result string was enclosed in the delimiter or not.
  53. bool getEnclosedString(const std::string source, std::string &result, std::size_t startPos, char endDelim, bool throwIfError)
  54. {
  55. bool rc = false;
  56. std::size_t endPos = source.find_first_of(endDelim, startPos+1);
  57. if (endPos != std::string::npos)
  58. {
  59. result = source.substr(startPos+1, endPos-startPos-1);
  60. rc = true;
  61. }
  62. else if (throwIfError)
  63. {
  64. std::string delim(1, endDelim);
  65. std::string msg = "Bad string parse, expectd '" + delim + "' at or around: " + source;
  66. throw(ParseException(msg));
  67. }
  68. else
  69. {
  70. result = source;
  71. }
  72. return rc;
  73. }