Advertisement
Guest User

SubRip file format sucks

a guest
Oct 18th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Icon 1.99 KB | None | 0 0
  1. global lineno, line, seqno
  2.  
  3. procedure nextline()
  4.     initial lineno := 0
  5.     return (lineno +:= 1 & line := trim(read()))
  6. end
  7.  
  8. procedure die(args[])
  9.     writes(&errout, &progname, ": ", lineno, ": ")
  10.     stop!args
  11. end
  12.  
  13. record time(h, m, s, ms)
  14.  
  15. procedure parsetime()
  16.     local h, m, s, ms
  17.     h := m := ms := 0
  18.     s := tab(many(&digits)) | fail
  19.     if =":" then {
  20.         m := s
  21.         s := tab(many(&digits)) | fail
  22.     }
  23.     if =":" then {
  24.         h := m
  25.         m := s
  26.         s := tab(many(&digits)) | fail
  27.     }
  28.     if tab(any('.,')) then
  29. {
  30.         ms := tab(many(&digits)) | fail
  31.         ms := left(ms, 3, "0")
  32.     }
  33.     (m < 60 & s < 60 & ms < 1000) | fail
  34.     return time(h, m, s, ms)
  35. end
  36.  
  37. procedure time2str(t)
  38.     return right(t.h, 2, "0") ||
  39.         ":" || right(t.m, 2, "0") ||
  40.         ":" || right(t.s, 2, "0") ||
  41.         "," || left(t.ms, 3, "0")
  42. end
  43.  
  44. procedure caption()
  45.     local t0, t1, op, i
  46.     initial seqno := 0
  47.     while line == "" do
  48.         nextline() | fail
  49.     if line ? (tab(many(&digits)) & pos(0)) then
  50.         nextline() | fail
  51.     seqno +:= 1
  52.     write(seqno)
  53.     line ? {
  54.         t0 := parsetime() |
  55.             die("invalid syntax: ", tab(0))
  56.         tab(many(' \t'))
  57.         op := tab(match("-->" | "for")) |
  58.             die("invalid syntax: ", tab(0))
  59.         tab(many(' \t'))
  60.         t1 := parsetime() |
  61.             die("invalid syntax: ", tab(0))
  62.         pos(0) | die("invalid syntax: ", tab(0))
  63.         if op == "for" then {
  64.             every i := 1 to 4 do t1[i] +:= t0[i]
  65.             t1.s +:= t1.ms / 1000
  66.             t1.ms %:= 1000
  67.             t1.m +:= t1.s / 60
  68.             t1.s %:= 60
  69.             t1.h +:= t1.m / 60
  70.             t1.m %:= 60
  71.         }
  72.         write(time2str(t0), " --> ", time2str(t1))
  73.     }
  74.     nextline() | die("missing caption")
  75.     until line == "" do {
  76.         write(line)
  77.         nextline() | break
  78.     }
  79.     write()
  80.     return
  81. end
  82.  
  83. procedure main()
  84.     nextline()
  85.     while caption()
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement