/xbmc/cores/dvdplayer/DVDSubtitles/DVDSubtitleParserSubrip.cpp

http://github.com/xbmc/xbmc · C++ · 94 lines · 58 code · 15 blank · 21 comment · 11 complexity · 4fc1e95adbe6664d2b86b21d3fc530cd MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it 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. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "DVDSubtitleParserSubrip.h"
  21. #include "DVDCodecs/Overlay/DVDOverlayText.h"
  22. #include "DVDClock.h"
  23. #include "utils/StdString.h"
  24. #include "DVDSubtitleTagSami.h"
  25. using namespace std;
  26. CDVDSubtitleParserSubrip::CDVDSubtitleParserSubrip(CDVDSubtitleStream* pStream, const string& strFile)
  27. : CDVDSubtitleParserText(pStream, strFile)
  28. {
  29. }
  30. CDVDSubtitleParserSubrip::~CDVDSubtitleParserSubrip()
  31. {
  32. Dispose();
  33. }
  34. bool CDVDSubtitleParserSubrip::Open(CDVDStreamInfo &hints)
  35. {
  36. if (!CDVDSubtitleParserText::Open())
  37. return false;
  38. CDVDSubtitleTagSami TagConv;
  39. if (!TagConv.Init())
  40. return false;
  41. char line[1024];
  42. CStdString strLine;
  43. while (m_pStream->ReadLine(line, sizeof(line)))
  44. {
  45. strLine = line;
  46. strLine.Trim();
  47. if (strLine.length() > 0)
  48. {
  49. char sep;
  50. int hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2;
  51. int c = sscanf(strLine.c_str(), "%d%c%d%c%d%c%d --> %d%c%d%c%d%c%d\n",
  52. &hh1, &sep, &mm1, &sep, &ss1, &sep, &ms1,
  53. &hh2, &sep, &mm2, &sep, &ss2, &sep, &ms2);
  54. if (c == 1)
  55. {
  56. // numbering, skip it
  57. }
  58. else if (c == 14) // time info
  59. {
  60. CDVDOverlayText* pOverlay = new CDVDOverlayText();
  61. pOverlay->Acquire(); // increase ref count with one so that we can hold a handle to this overlay
  62. pOverlay->iPTSStartTime = ((double)(((hh1 * 60 + mm1) * 60) + ss1) * 1000 + ms1) * (DVD_TIME_BASE / 1000);
  63. pOverlay->iPTSStopTime = ((double)(((hh2 * 60 + mm2) * 60) + ss2) * 1000 + ms2) * (DVD_TIME_BASE / 1000);
  64. while (m_pStream->ReadLine(line, sizeof(line)))
  65. {
  66. strLine = line;
  67. strLine.Trim();
  68. // empty line, next subtitle is about to start
  69. if (strLine.length() <= 0) break;
  70. TagConv.ConvertLine(pOverlay, strLine.c_str(), strLine.length());
  71. }
  72. TagConv.CloseTag(pOverlay);
  73. m_collection.Add(pOverlay);
  74. }
  75. }
  76. }
  77. m_collection.Sort();
  78. return true;
  79. }