Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

time.cpp

00001 /*****************************************************************************
00002  * time.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: time.cpp 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #include <stdio.h>  // snprintf
00026 
00027 #include "time.hpp"
00028 #include <vlc/input.h>
00029 
00030 
00031 void StreamTime::set( float percentage, bool updateVLC )
00032 {
00033     VarPercent::set( percentage );
00034 
00035     // Avoid looping forever...
00036     if( updateVLC && getIntf()->p_sys->p_input )
00037     {
00038         vlc_value_t pos;
00039         pos.f_float = percentage;
00040 
00041         var_Set( getIntf()->p_sys->p_input, "position", pos );
00042     }
00043 }
00044 
00045 
00046 const string StreamTime::getAsStringPercent() const
00047 {
00048     int value = (int)(100. * get());
00049     // 0 <= value <= 100, so we need 4 chars
00050     char *str = new char[4];
00051     snprintf( str, 4, "%d", value );
00052     string ret = str;
00053     delete[] str;
00054 
00055     return ret;
00056 }
00057 
00058 
00059 const string StreamTime::getAsStringCurrTime() const
00060 {
00061     if( getIntf()->p_sys->p_input == NULL )
00062     {
00063         return "-:--:--";
00064     }
00065 
00066     vlc_value_t pos;
00067     var_Get( getIntf()->p_sys->p_input, "position", &pos );
00068     if( pos.f_float == 0.0 )
00069     {
00070         return "-:--:--";
00071     }
00072 
00073     vlc_value_t time;
00074     var_Get( getIntf()->p_sys->p_input, "time", &time );
00075 
00076     return formatTime( time.i_time / 1000000 );
00077 }
00078 
00079 
00080 const string StreamTime::getAsStringTimeLeft() const
00081 {
00082     if( getIntf()->p_sys->p_input == NULL )
00083     {
00084         return "-:--:--";
00085     }
00086 
00087     vlc_value_t pos;
00088     var_Get( getIntf()->p_sys->p_input, "position", &pos );
00089     if( pos.f_float == 0.0 )
00090     {
00091         return "-:--:--";
00092     }
00093 
00094     vlc_value_t time, duration;
00095     var_Get( getIntf()->p_sys->p_input, "time", &time );
00096     var_Get( getIntf()->p_sys->p_input, "length", &duration );
00097 
00098     return formatTime( (duration.i_time - time.i_time) / 1000000 );
00099 }
00100 
00101 
00102 const string StreamTime::getAsStringDuration() const
00103 {
00104     if( getIntf()->p_sys->p_input == NULL )
00105     {
00106         return "-:--:--";
00107     }
00108 
00109     vlc_value_t pos;
00110     var_Get( getIntf()->p_sys->p_input, "position", &pos );
00111     if( pos.f_float == 0.0 )
00112     {
00113         return "-:--:--";
00114     }
00115 
00116     vlc_value_t time;
00117     var_Get( getIntf()->p_sys->p_input, "length", &time );
00118 
00119     return formatTime( time.i_time / 1000000 );
00120 }
00121 
00122 
00123 const string StreamTime::formatTime( int seconds ) const
00124 {
00125     char *psz_time = new char[MSTRTIME_MAX_SIZE];
00126     snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
00127               (int) (seconds / (60 * 60)),
00128               (int) (seconds / 60 % 60),
00129               (int) (seconds % 60) );
00130 
00131     string ret = psz_time;
00132     delete[] psz_time;
00133 
00134     return ret;
00135 }

Generated on Tue Dec 20 10:14:43 2005 for vlc-0.8.4a by  doxygen 1.4.2