1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
34 #include <ETL/stringf>
47 extern "C" { int _isnan(double x); }
52 // For some reason isnan() isn't working on macosx any more.
53 // This is a quick fix.
54 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
58 inline bool isnan(double x) { return x != x; }
59 inline bool isnan(float x) { return x != x; }
60 #define SYNFIG_ISNAN_FIX 1
66 /* === U S I N G =========================================================== */
70 using namespace synfig;
72 #define tolower ::tolower
74 /* === M A C R O S ========================================================= */
76 /* === G L O B A L S ======================================================= */
78 /* === M E T H O D S ======================================================= */
80 Time::Time(const String &str_, float fps):
84 std::transform(str.begin(),str.end(),str.begin(),&tolower);
86 // Start/Begin Of Time
87 if(str=="sot" || str=="bot")
104 // Now try to read it in the letter-abbreviated format
105 while(pos<str.size() && sscanf(String(str,pos).c_str(),"%f%n",&amount,&read))
108 if(pos>=str.size() || read==0)
110 // Throw up a warning if there are no units
111 // and the amount isn't zero. There is no need
112 // to warn about units if the value is zero
113 // it is the only case where units are irrelevant.
115 synfig::warning("Time(): No unit provided in time code, assuming SECONDS (\"%s\")",str.c_str());
138 synfig::warning("Time(): Individual frames referenced, but frame rate is unknown");
141 // try to read it in as a traditional time format
143 int hour,minute,second;
145 if(fps && sscanf(str.c_str(),"%d:%d:%d.%f",&hour,&minute,&second,&frame)==4)
147 value_=frame/fps+(hour*3600+minute*60+second);
151 if(sscanf(str.c_str(),"%d:%d:%d",&hour,&minute,&second)==3)
153 value_=hour*3600+minute*60+second;
157 synfig::warning("Time(): Bad time format");
162 synfig::warning("Time(): Unexpected character '%c' when parsing time string \"%s\"",str[pos],str.c_str());
171 Time::get_string(float fps, Time::Format format)const
176 return "SOT"; // Start Of Time
178 return "EOT"; // End Of Time
182 if(ceil(time.value_)-time.value_<epsilon_())
183 time.value_=ceil(time.value_);
186 if(!(format<=FORMAT_FRAMES))
188 hour=time/3600;time-=hour*3600;
189 minute=time/60;time-=minute*60;
191 // <= is redefined, so this means "is the FORMAT_VIDEO bit set in the format?"
192 if(format<=FORMAT_VIDEO)
195 second=time;time-=second;
200 frame=round_to_int(time*fps);
202 return strprintf("%02d:%02d:%02d.%02d",hour,minute,second,frame);
205 return strprintf("%02d:%02d:%02d",hour,minute,second);
208 if (format <= FORMAT_FRAMES)
211 return strprintf("%df", round_to_int(time * fps));
213 return strprintf("%ds", round_to_int(time * 1));
217 bool started = false;
219 if(format<=FORMAT_FULL || hour)
221 ret+=strprintf("%dh",hour);
225 if(format<=FORMAT_FULL || minute)
227 if (!(format<=FORMAT_NOSPACES) && started)
230 ret += strprintf("%dm", minute);
238 second=time;time-=second;
241 if(format<=FORMAT_FULL || second)
243 if (!(format<=FORMAT_NOSPACES) && started)
246 ret += strprintf("%ds", (int)second);
250 if(format<=FORMAT_FULL || abs(frame) > epsilon_() || !started)
252 if (!(format<=FORMAT_NOSPACES) && started)
255 if(abs(frame-floor(frame) >= epsilon_()))
256 ret += strprintf("%0.3ff", frame);
258 ret += strprintf("%0.0ff", frame);
265 if(format<=FORMAT_FULL || second || !started)
267 if (!(format<=FORMAT_NOSPACES) && started)
270 if(abs(second-floor(second))>=epsilon_())
272 String seconds(strprintf("%0.8f",second));
274 // skip trailing zeros
276 String::reverse_iterator i = seconds.rbegin();
277 for ( ; (*i) == '0'; i++)
280 // if we removed too many, go back one place, leaving one zero
281 if (*i < '0' || *i > '9') count--;
283 ret += seconds.substr(0, seconds.size()-count) + "s";
286 ret+=strprintf("%0.0fs",second);
294 Time::round(float fps)const
298 value_type time(*this);
302 if(abs(time-floor(time))<0.5)
303 return floor(time)/fps;
305 return ceil(time)/fps;
312 return get_string().c_str();
318 Time::is_valid()const
320 return !isnan(value_);