lametime.c

Go to the documentation of this file.
00001 /*
00002  *      Lame time routines source file
00003  *
00004  *      Copyright (c) 2000 Mark Taylor
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 /* $Id: lametime.c,v 1.18 2007/07/24 17:46:09 bouvigne Exp $ */
00023 
00024 /*
00025  * name:        GetCPUTime ( void )
00026  *
00027  * description: returns CPU time used by the process
00028  * input:       none
00029  * output:      time in seconds
00030  * known bugs:  may not work in SMP and RPC
00031  * conforming:  ANSI C
00032  *
00033  * There is some old difficult to read code at the end of this file.
00034  * Can someone integrate this into this function (if useful)?
00035  */
00036 
00037 #ifdef HAVE_CONFIG_H
00038 # include <config.h>
00039 #endif
00040 
00041 #include <assert.h>
00042 #include <stdio.h>
00043 #include <time.h>
00044 
00045 #ifdef WITH_DMALLOC
00046 #include <dmalloc.h>
00047 #endif
00048 
00049 #include "lametime.h"
00050 
00051 #if !defined(CLOCKS_PER_SEC)
00052 # warning Your system does not define CLOCKS_PER_SEC, guessing one...
00053 # define CLOCKS_PER_SEC 1000000
00054 #endif
00055 
00056 
00057 double
00058 GetCPUTime(void)
00059 {
00060     clock_t t;
00061 
00062 #if defined(_MSC_VER)  ||  defined(__BORLANDC__)
00063     t = clock();
00064 #else
00065     t = clock();
00066 #endif
00067     return t / (double) CLOCKS_PER_SEC;
00068 }
00069 
00070 
00071 /*
00072  * name:        GetRealTime ( void )
00073  *
00074  * description: returns real (human) time elapsed relative to a fixed time (mostly 1970-01-01 00:00:00)
00075  * input:       none
00076  * output:      time in seconds
00077  * known bugs:  bad precision with time()
00078  */
00079 
00080 #if defined(__unix__)  ||  defined(SVR4)  ||  defined(BSD)
00081 
00082 # include <sys/time.h>
00083 # include <unistd.h>
00084 
00085 double
00086 GetRealTime(void)
00087 {                       /* conforming:  SVr4, BSD 4.3 */
00088     struct timeval t;
00089 
00090     if (0 != gettimeofday(&t, NULL))
00091         assert(0);
00092     return t.tv_sec + 1.e-6 * t.tv_usec;
00093 }
00094 
00095 #elif defined(WIN16)  ||  defined(WIN32)
00096 
00097 # include <stdio.h>
00098 # include <sys/types.h>
00099 # include <sys/timeb.h>
00100 
00101 double
00102 GetRealTime(void)
00103 {                       /* conforming:  Win 95, Win NT */
00104     struct timeb t;
00105 
00106     ftime(&t);
00107     return t.time + 1.e-3 * t.millitm;
00108 }
00109 
00110 #else
00111 
00112 double
00113 GetRealTime(void)
00114 {                       /* conforming:  SVr4, SVID, POSIX, X/OPEN, BSD 4.3 *//* BUT NOT GUARANTEED BY ANSI */
00115     time_t  t;
00116 
00117     t = time(NULL);
00118     return (double) t;
00119 }
00120 
00121 #endif
00122 
00123 
00124 #if defined(_WIN32) || defined(__CYGWIN__)
00125 # include <io.h>
00126 # include <fcntl.h>
00127 #else
00128 # include <unistd.h>
00129 #endif
00130 
00131 int
00132 lame_set_stream_binary_mode(FILE * const fp)
00133 {
00134 #if   defined __EMX__
00135     _fsetmode(fp, "b");
00136 #elif defined __BORLANDC__
00137     setmode(_fileno(fp), O_BINARY);
00138 #elif defined __CYGWIN__
00139     setmode(fileno(fp), _O_BINARY);
00140 #elif defined _WIN32
00141     _setmode(_fileno(fp), _O_BINARY);
00142 #else
00143     (void) fp;          /* doing nothing here, silencing the compiler only. */
00144 #endif
00145     return 0;
00146 }
00147 
00148 
00149 #if defined(__riscos__)
00150 # include <kernel.h>
00151 # include <sys/swis.h>
00152 #elif defined(_WIN32)
00153 # include <sys/types.h>
00154 # include <sys/stat.h>
00155 #else
00156 # include <sys/stat.h>
00157 #endif
00158 
00159 off_t
00160 lame_get_file_size(const char *const filename)
00161 {
00162     struct stat sb;
00163 
00164     if (0 == stat(filename, &sb))
00165         return sb.st_size;
00166     return (off_t) - 1;
00167 }
00168 
00169 /* End of lametime.c */

Generated on Sun Dec 2 11:34:18 2007 for LAME by  doxygen 1.5.2