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.

95 lines
2.9 KiB

  1. #include "src/utility/cstring.h"
  2. #include <cstring>
  3. #include "src/exceptions/WrongFormatException.h"
  4. #include "log4cplus/logger.h"
  5. #include "log4cplus/loggingmacros.h"
  6. extern log4cplus::Logger logger;
  7. namespace storm {
  8. namespace utility {
  9. namespace cstring {
  10. /*!
  11. * Calls strtol() internally and checks if the new pointer is different
  12. * from the original one, i.e. if str != *end. If they are the same, a
  13. * storm::exceptions::WrongFormatException will be thrown.
  14. * @param str String to parse
  15. * @param end New pointer will be written there
  16. * @return Result of strtol()
  17. */
  18. uint_fast64_t checked_strtol(const char* str, char** end) {
  19. uint_fast64_t res = strtol(str, end, 10);
  20. if (str == *end) {
  21. LOG4CPLUS_ERROR(logger, "Error while parsing integer. Next input token is not a number.");
  22. LOG4CPLUS_ERROR(logger, "\tUpcoming input is: \"" << std::string(str, 0, 16) << "\"");
  23. throw storm::exceptions::WrongFormatException("Error while parsing integer. Next input token is not a number.");
  24. }
  25. return res;
  26. }
  27. /*!
  28. * Calls strtod() internally and checks if the new pointer is different
  29. * from the original one, i.e. if str != *end. If they are the same, a
  30. * storm::exceptions::WrongFormatException will be thrown.
  31. * @param str String to parse
  32. * @param end New pointer will be written there
  33. * @return Result of strtod()
  34. */
  35. double checked_strtod(const char* str, char** end) {
  36. double res = strtod(str, end);
  37. if (str == *end) {
  38. LOG4CPLUS_ERROR(logger, "Error while parsing floating point. Next input token is not a number.");
  39. LOG4CPLUS_ERROR(logger, "\tUpcoming input is: \"" << std::string(str, 0, 16) << "\"");
  40. throw storm::exceptions::WrongFormatException("Error while parsing floating point. Next input token is not a number.");
  41. }
  42. return res;
  43. }
  44. /*!
  45. * Skips all numbers, letters and special characters.
  46. * Returns a pointer to the first char that is a whitespace.
  47. * @param buf The string buffer to operate on.
  48. * @return A pointer to the first whitespace character.
  49. */
  50. char* skipWord(char* buf){
  51. while(!isspace(*buf) && *buf != '\0') buf++;
  52. return buf;
  53. }
  54. /*!
  55. * Skips spaces, tabs, newlines and carriage returns. Returns a pointer
  56. * to first char that is not a whitespace.
  57. * @param buf The string buffer to operate on.
  58. * @return A pointer to the first non-whitespace character.
  59. */
  60. char* trimWhitespaces(char* buf) {
  61. while (isspace(*buf)) buf++;
  62. return buf;
  63. }
  64. /*!
  65. * @brief Encapsulates the usage of function @strcspn to forward to the end of the line (next char is the newline character).
  66. */
  67. char* forwardToLineEnd(char* buffer) {
  68. return buffer + strcspn(buffer, "\n\r\0");
  69. }
  70. /*!
  71. * @brief Encapsulates the usage of function @strchr to forward to the next line
  72. */
  73. char* forwardToNextLine(char* buffer) {
  74. char* lineEnd = forwardToLineEnd(buffer);
  75. while((*lineEnd == '\n') || (*lineEnd == '\r')) lineEnd++;
  76. return lineEnd;
  77. }
  78. } // namespace cstring
  79. } // namespace utility
  80. } // namespace storm