Debian Codegraph @

src/pts.cpp

raw
1 /* dvbcut
2 Copyright (c) 2005 Sven Over <[email protected]>
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 2 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, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* $Id$ */
20
21 #include "pts.h"
22
23 #include <cstdio>
24 #include <cstdlib>
25 #include <string>
26 #include <list>
27
28 // convert a pts (1/90000 sec) to a readable timestamp string
29 std::string ptsstring(pts_t pts)
30 {
31 char *str=0;
32 const char *minus="";
33 if (pts<0) {
34 minus="-";
35 pts*=-1;
36 }
37
38 if (asprintf(&str,"%s%02d:%02d:%02d.%03d/%02d",
39 minus,
40 int(pts/90000)/3600,
41 (int(pts/90000)/60)%60,
42 int(pts/90000)%60,
43 int(pts/90)%1000,
44 int(pts%90) )<0 || !str)
45 return std::string();
46
47 std::string s(str);
48 free(str);
49 return s;
50 }
51
52 // parse timestamp string (hh:mm:ss.frac/nn) and convert to pts (1/90000 of a second)
53 pts_t string2pts(std::string str)
54 {
55 int hour=0,min=0,sec=0,ms=0,sub=0,sign=1;
56 double dsec;
57 std::list<std::string> tokens;
58
59 size_t from=0, pos;
60 while((pos=str.find(':',from))!=std::string::npos) {
61 tokens.push_back(str.substr(from,pos-from));
62 from=pos+1;
63 }
64 tokens.push_back(str.substr(from));
65
66 if(!tokens.empty()) {
67 std::string t=tokens.back();
68 pos=t.find('/');
69 dsec=atof(t.substr(0,pos).c_str());
70 if(dsec<0) {
71 dsec*=-1;
72 sign=-1;
73 }
74 sec=int(dsec);
75 ms=int(1000*(dsec-sec)+0.5);
76
77 if(pos!=std::string::npos)
78 sub=atoi(t.substr(pos+1).c_str())%90;
79 tokens.pop_back();
80
81 if(!tokens.empty()) {
82 min=atoi(tokens.back().c_str());
83 if(min<0) {
84 min*=-1;
85 sign=-1;
86 }
87 tokens.pop_back();
88
89 if(!tokens.empty()) {
90 hour=atoi(tokens.back().c_str());
91 if(hour<0) {
92 hour*=-1;
93 sign=-1;
94 }
95 }
96 }
97 }
98
99 return sign*((((hour*60 + min)*60 + sec)*1000 + ms)*90 + sub);
100 }