Commit f71869a4 authored by Fabrice Bellard's avatar Fabrice Bellard

simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c

Originally committed as revision 2228 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 742d87d6
...@@ -8,7 +8,7 @@ VPATH=$(SRC_PATH)/libavformat ...@@ -8,7 +8,7 @@ VPATH=$(SRC_PATH)/libavformat
CFLAGS= $(OPTFLAGS) -Wall -g -I.. -I$(SRC_PATH) -I$(SRC_PATH)/libavcodec -DHAVE_AV_CONFIG_H -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE CFLAGS= $(OPTFLAGS) -Wall -g -I.. -I$(SRC_PATH) -I$(SRC_PATH)/libavcodec -DHAVE_AV_CONFIG_H -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE
OBJS= utils.o cutils.o allformats.o OBJS= utils.o cutils.o os_support.o allformats.o
PPOBJS= PPOBJS=
# mux and demuxes # mux and demuxes
...@@ -31,14 +31,6 @@ OBJS+= pnm.o yuv.o png.o jpeg.o gifdec.o ...@@ -31,14 +31,6 @@ OBJS+= pnm.o yuv.o png.o jpeg.o gifdec.o
OBJS+= avio.o aviobuf.o file.o OBJS+= avio.o aviobuf.o file.o
OBJS+= framehook.o OBJS+= framehook.o
ifeq ($(BUILD_STRPTIME),yes)
OBJS+= strptime.o
endif
ifeq ($(BUILD_LOCALTIME_R),yes)
OBJS+= localtime_r.o
endif
ifeq ($(CONFIG_VIDEO4LINUX),yes) ifeq ($(CONFIG_VIDEO4LINUX),yes)
OBJS+= grab.o OBJS+= grab.o
endif endif
......
...@@ -12,6 +12,8 @@ extern "C" { ...@@ -12,6 +12,8 @@ extern "C" {
#define LIBAVFORMAT_IDENT "FFmpeg" LIBAVFORMAT_VERSION "b" LIBAVFORMAT_BUILD_STR #define LIBAVFORMAT_IDENT "FFmpeg" LIBAVFORMAT_VERSION "b" LIBAVFORMAT_BUILD_STR
#include <time.h>
#include "avcodec.h" #include "avcodec.h"
#include "avio.h" #include "avio.h"
...@@ -487,6 +489,9 @@ int audio_init(void); ...@@ -487,6 +489,9 @@ int audio_init(void);
int dv1394_init(void); int dv1394_init(void);
#ifdef HAVE_AV_CONFIG_H #ifdef HAVE_AV_CONFIG_H
#include "os_support.h"
int strstart(const char *str, const char *val, const char **ptr); int strstart(const char *str, const char *val, const char **ptr);
int stristart(const char *str, const char *val, const char **ptr); int stristart(const char *str, const char *val, const char **ptr);
void pstrcpy(char *buf, int buf_size, const char *str); void pstrcpy(char *buf, int buf_size, const char *str);
...@@ -502,6 +507,10 @@ do {\ ...@@ -502,6 +507,10 @@ do {\
__dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\ __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
} while(0) } while(0)
time_t mktimegm(struct tm *tm);
const char *small_strptime(const char *p, const char *fmt,
struct tm *dt);
struct in_addr; struct in_addr;
int resolve_host(struct in_addr *sin_addr, const char *hostname); int resolve_host(struct in_addr *sin_addr, const char *hostname);
......
...@@ -128,3 +128,112 @@ void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem) ...@@ -128,3 +128,112 @@ void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
*nb_ptr = nb; *nb_ptr = nb;
} }
time_t mktimegm(struct tm *tm)
{
time_t t;
int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
if (m < 3) {
m += 12;
y--;
}
t = 86400 *
(d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
return t;
}
/* get a positive number between n_min and n_max, for a maximum length
of len_max. Return -1 if error. */
static int date_get_num(const char **pp,
int n_min, int n_max, int len_max)
{
int i, val, c;
const char *p;
p = *pp;
val = 0;
for(i = 0; i < len_max; i++) {
c = *p;
if (!isdigit(c))
break;
val = (val * 10) + c - '0';
p++;
}
/* no number read ? */
if (p == *pp)
return -1;
if (val < n_min || val > n_max)
return -1;
*pp = p;
return val;
}
/* small strptime for ffmpeg */
const char *small_strptime(const char *p, const char *fmt,
struct tm *dt)
{
int c, val;
for(;;) {
c = *fmt++;
if (c == '\0') {
return p;
} else if (c == '%') {
c = *fmt++;
switch(c) {
case 'H':
val = date_get_num(&p, 0, 23, 2);
if (val == -1)
return NULL;
dt->tm_hour = val;
break;
case 'M':
val = date_get_num(&p, 0, 59, 2);
if (val == -1)
return NULL;
dt->tm_min = val;
break;
case 'S':
val = date_get_num(&p, 0, 59, 2);
if (val == -1)
return NULL;
dt->tm_sec = val;
break;
case 'Y':
val = date_get_num(&p, 0, 9999, 4);
if (val == -1)
return NULL;
dt->tm_year = val - 1900;
break;
case 'm':
val = date_get_num(&p, 1, 12, 2);
if (val == -1)
return NULL;
dt->tm_mon = val - 1;
break;
case 'd':
val = date_get_num(&p, 1, 31, 2);
if (val == -1)
return NULL;
dt->tm_mday = val;
break;
case '%':
goto match;
default:
return NULL;
}
} else {
match:
if (c != *p)
return NULL;
p++;
}
}
return p;
}
/* Convert a string representation of time to a time value.
Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "localtime_r.h"
#if !defined(HAVE_LOCALTIME_R)
/* Approximate localtime_r as best we can in its absence. */
#include <time.h>
struct tm *
localtime_r (t, tp)
const time_t *t;
struct tm *tp;
{
struct tm *l = localtime (t);
if (! l)
return 0;
*tp = *l;
return tp;
}
#endif /* !defined(HAVE_LOCALTIME_R) */
/* Convert a string representation of time to a time value.
Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifndef __LOCALTIME_R_H__
#define __LOCALTIME_R_H__
/*
* Version of "localtime_r()", for the benefit of OSes that don't have it.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if !defined(HAVE_LOCALTIME_R)
#include <time.h>
/* Approximate localtime_r as best we can in its absence. */
# define localtime_r my_localtime_r
extern struct tm *localtime_r(const time_t *, struct tm *);
#endif
#endif
/* /*
* Copyright (C) 2003 the ffmpeg project * Various utilities for ffmpeg system
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -14,12 +15,43 @@ ...@@ -14,12 +15,43 @@
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/ */
#include "avformat.h"
#ifdef CONFIG_WIN32
#include <sys/types.h>
#include <sys/timeb.h>
#elif defined(CONFIG_OS2)
#include <string.h>
#include <sys/time.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#endif
#include <time.h>
#ifndef STRPTIME_H int64_t av_gettime(void)
#define STRPTIME_H {
#ifdef CONFIG_WIN32
extern char *strptime(const char *, const char *, struct tm *); struct _timeb tb;
_ftime(&tb);
return ((int64_t)tb.time * int64_t_C(1000) + (int64_t)tb.millitm) * int64_t_C(1000);
#else
struct timeval tv;
gettimeofday(&tv,NULL);
return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
#endif
}
#endif // STRPTIME_H #if !defined(HAVE_LOCALTIME_R)
struct tm *localtime_r(const time_t *t, struct tm *tp)
{
struct tm *l;
l = localtime(t);
if (!l)
return 0;
*tp = *l;
return tp;
}
#endif /* !defined(HAVE_LOCALTIME_R) */
#ifndef _OS_SUPPORT_H
#define _OS_SUPPORT_H
/**
* @file os_support.h
* miscellaneous OS support macros and functions.
*
* - usleep() (Win32, BeOS, OS/2)
* - floatf() (OS/2)
* - strcasecmp() (OS/2)
*/
#ifdef __MINGW32__
# undef DATADIR /* clashes with /usr/include/w32api/objidl.h */
__declspec(dllimport) void __stdcall Sleep(unsigned long dwMilliseconds);
// # include <windows.h>
# define usleep(t) Sleep((t) / 1000)
#endif
#ifdef __BEOS__
# ifndef usleep
# include <OS.h>
# define usleep(t) snooze((bigtime_t)(t))
# endif
#endif
#if defined(CONFIG_OS2)
#include <stdlib.h>
static inline int usleep(unsigned int t) { return _sleep2(t / 1000); }
static inline int strcasecmp(const char* s1, const char* s2) { return stricmp(s1,s2); }
#endif
#endif /* _OS_SUPPORT_H */
This diff is collapsed.
...@@ -17,24 +17,6 @@ ...@@ -17,24 +17,6 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include "avformat.h" #include "avformat.h"
#ifdef CONFIG_WIN32
#define strcasecmp _stricmp
#include <sys/types.h>
#include <sys/timeb.h>
#elif defined(CONFIG_OS2)
#include <string.h>
#define strcasecmp stricmp
#include <sys/time.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#endif
#include <time.h>
#ifndef HAVE_STRPTIME
#include "strptime.h"
#endif
AVInputFormat *first_iformat; AVInputFormat *first_iformat;
AVOutputFormat *first_oformat; AVOutputFormat *first_oformat;
...@@ -1300,38 +1282,6 @@ int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) ...@@ -1300,38 +1282,6 @@ int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg)
return 0; return 0;
} }
int64_t av_gettime(void)
{
#ifdef CONFIG_WIN32
struct _timeb tb;
_ftime(&tb);
return ((int64_t)tb.time * int64_t_C(1000) + (int64_t)tb.millitm) * int64_t_C(1000);
#else
struct timeval tv;
gettimeofday(&tv,NULL);
return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
#endif
}
static time_t mktimegm(struct tm *tm)
{
time_t t;
int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
if (m < 3) {
m += 12;
y--;
}
t = 86400 *
(d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
return t;
}
/* Syntax: /* Syntax:
* - If not a duration: * - If not a duration:
* [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
...@@ -1373,7 +1323,7 @@ int64_t parse_date(const char *datestr, int duration) ...@@ -1373,7 +1323,7 @@ int64_t parse_date(const char *datestr, int duration)
q = NULL; q = NULL;
if (!duration) { if (!duration) {
for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
q = strptime(p, date_fmt[i], &dt); q = small_strptime(p, date_fmt[i], &dt);
if (q) { if (q) {
break; break;
} }
...@@ -1394,13 +1344,13 @@ int64_t parse_date(const char *datestr, int duration) ...@@ -1394,13 +1344,13 @@ int64_t parse_date(const char *datestr, int duration)
p++; p++;
for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
q = strptime(p, time_fmt[i], &dt); q = small_strptime(p, time_fmt[i], &dt);
if (q) { if (q) {
break; break;
} }
} }
} else { } else {
q = strptime(p, time_fmt[0], &dt); q = small_strptime(p, time_fmt[0], &dt);
if (!q) { if (!q) {
dt.tm_sec = strtol(p, (char **)&q, 10); dt.tm_sec = strtol(p, (char **)&q, 10);
dt.tm_min = 0; dt.tm_min = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment