parse.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* parse.cpp - time parsing
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <sstream>
  18. #include <core/util/exceptions.h>
  19. #include <core/time/parse.h>
  20. namespace rainynite::core {
  21. TimePeriod parse_time_period(string const& s) {
  22. std::istringstream stream(s);
  23. return parse_time_period(stream);
  24. }
  25. TimePeriod parse_time_period(std::istream& stream) {
  26. if (stream.get() != '[')
  27. throw ParseError("Unsupported time period format: expected '['");
  28. auto a = parse_time(stream);
  29. if (stream.get() != ',')
  30. throw ParseError("Unsupported time period format: expected ','");
  31. auto b = parse_time(stream);
  32. if (stream.get() != ')')
  33. throw ParseError("Unsupported time period format: expected ')'");
  34. return TimePeriod(a, b);
  35. }
  36. Time parse_time(string const& s) {
  37. std::istringstream stream(s);
  38. return parse_time(stream);
  39. }
  40. Time parse_time(std::istream& stream) {
  41. Time result;
  42. double frames;
  43. if (stream >> frames) {
  44. if (stream.get() == 'f' && stream.get() == '@') {
  45. int fps;
  46. if (stream >> fps) {
  47. result.set_fps(fps);
  48. result.set_frames(frames);
  49. return result;
  50. }
  51. }
  52. } else {
  53. //
  54. }
  55. throw ParseError("Unsupported time format");
  56. }
  57. } // namespace rainynite::core