-
Notifications
You must be signed in to change notification settings - Fork 115
/
ADM_misc.cpp
335 lines (289 loc) · 7.32 KB
/
ADM_misc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/***************************************************************************
toolkit.cpp - description
-------------------
begin : Fri Dec 14 2001
copyright : (C) 2001 by mean
email : fixounet@free.fr
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include "ADM_default.h"
#include "ADM_assert.h"
#include "ADM_win32.h"
#include "ADM_vidMisc.h"
#undef fopen
#undef fclose
// Get tick (in ms)
// Call with a 0 to initialize
// Call with a 1 to read
//_____________________
uint32_t getTime(int called)
{
static struct timeval timev_s;
static struct timeval timev;
static TIMZ timez;
int32_t tt;
if (!called)
{
called = 1;
gettimeofday(&timev_s, &timez);
return 0;
}
gettimeofday(&timev, &timez);
tt = timev.tv_usec - timev_s.tv_usec;
tt /= 1000;
tt += 1000 * (timev.tv_sec - timev_s.tv_sec);
return (tt);
}
/**
\fn ADM_getSecondsSinceEpoch
\brief returns the number of seconds since epoch
*/
uint64_t ADM_getSecondsSinceEpoch(void)
{
struct timeval timev;
TIMZ timez;
uint64_t tt;
gettimeofday(&timev, &timez);
tt = (uint64_t)(timev.tv_sec);
return tt;
}
/**
\fn ADM_epochToString
\brief convert number of second to string
*/
const char *ADM_epochToString(uint64_t epoch)
{
time_t clock=(time_t)epoch;
return ctime(&clock);
}
uint32_t getTimeOfTheDay(void)
{
struct timeval timev;
TIMZ timez;
int32_t tt;
gettimeofday(&timev, &timez);
tt = timev.tv_usec;
tt /= 1000;
tt += 1000 * (timev.tv_sec);
return (tt&0xffffff);
}
#if 0
/// convert frame number and fps to hour/mn/sec/ms
void frame2time( uint32_t frame, uint32_t fps, uint32_t * hh, uint32_t * mm,
uint32_t * ss, uint32_t * ms)
{
UNUSED_ARG(fps);
double d;
uint32_t len2;
d=frame;
d=d/fps;
d*=1000000.;
len2 = (uint32_t)(d); //video_body->getTime(frame);
ms2time(len2,hh,mm,ss,ms);
}
void time2frame(uint32_t *frame, uint32_t fps, uint32_t hh, uint32_t mm,
uint32_t ss, uint32_t ms)
{
// convert everything to ms : uint32_t = 1000 hours, should be plenty enough
uint32_t count=0;
count+=ms;
count+=ss*1000;
count+=mm*60*1000;
count+=hh*3600*1000;
double d;
d=count;
// ms
d=d*fps;
d/=1000;
d/=1000;
*frame= (uint32_t)(floor(d+0.5));
}
#endif
uint64_t ADM_swap64(uint64_t in)
{
uint32_t low,high;
uint64_t out;
high=in>>32;
low=in&0xffffffff;
high=ADM_swap32(high);
low=ADM_swap32(low);
out=low;
out=(out<<32)+high;
return out;
}
// swap BE/LE : Ugly
uint32_t ADM_swap32( uint32_t in)
{
uint8_t r[4],u;
memcpy(&r[0],&in,4);
u=r[0];
r[0]=r[3];
r[3]=u;
u=r[1];
r[1]=r[2];
r[2]=u;
memcpy(&in,&r[0],4);
return in;
}
// swap BE/LE : Ugly
uint16_t ADM_swap16( uint16_t in)
{
return ( (in>>8) & 0xff) + ( (in&0xff)<<8);
}
#if 0
uint8_t identMovieType(uint32_t fps1000)
{
#define INRANGE(value,type) \
{\
if((fps1000 > value-300) &&( fps1000 < value+300))\
{\
r=type;\
printf("Looks like "#type" \n");\
}\
}
uint8_t r=0;
INRANGE(25000,FRAME_PAL);
INRANGE(23976,FRAME_FILM);
INRANGE(29970,FRAME_NTSC);
return r;
}
#endif
uint8_t ms2time(uint32_t ms, uint32_t *h,uint32_t *m, uint32_t *s,uint32_t *mms)
{
uint32_t sectogo;
int mm,ss,hh;
// d is in ms, divide by 1000 to get seconds
sectogo = (uint32_t) floor(ms / 1000.);
hh=sectogo/3600;
sectogo=sectogo-hh*3600;
mm=sectogo/60;
ss=sectogo%60;
*h=hh;
*m=mm;
*s=ss;
*mms=ms-1000*(ms/1000);
return 1;
}
/**
\fn The Office_6x08_HDTV.2HD.en
\brief convert a time in us to a string in 0:0:0'0 format
*/
const char *ADM_us2plain(uint64_t ams)
{
static char buffer[256];
uint32_t ms=(uint32_t)(ams/1000);
uint32_t hh,mm,ss,mms;
if(ams==ADM_NO_PTS)
sprintf(buffer,"xx:xx:xx,xxx");
else
{
ms2time(ms,&hh,&mm,&ss,&mms);
sprintf(buffer,"%02" PRIu32":%02" PRIu32":%02" PRIu32",%03" PRIu32,hh,mm,ss,mms);
}
return buffer;
}
/**
\fn ADM_LowerCase
\brief change to lower case in place the string
*/
void ADM_lowerCase(std::string &st)
{
std::transform(st.begin(), st.end(), st.begin(), ::tolower);
}
/**
*
* @param name
* @return
*/
uint8_t ADM_fileExist(const char *name)
{
FILE *file;
file=ADM_fopen(name,"rb");
if(!file) return 0;
fclose(file);
return 1;
}
/*
In some case (e.g. javascript), the reader expects unixish path
c:/foo/bar/c.c
and the "natural" path is c:\foo\bar
This function convert the later to the former
*/
extern char *ADM_slashToBackSlash(const char *in)
{
char *out,*cout;
int n;
n=(int)strlen(in);
cout=out=(char *)ADM_alloc(n+1);
for(int i=0;i<n+1;i++)
{
if( in[i]=='\\') out[i]='/';
else out[i]=in[i];
}
return cout;
}
/*
* \fn ADM_getCurrentDate
*/
const ADM_date ADM_getCurrentDate()
{
time_t timez;
tm *t;
time(&timez);
t=localtime(&timez);
ADM_date date;
if(t)
{
date.hours=t->tm_hour;
date.minutes=t->tm_min;
date.seconds=t->tm_sec;
}
return date;
}
/**
* \fn ADM_shutdown
* @return
*/
bool ADM_shutdown(bool suspend)
{
#ifdef _WIN32
return (shutdown_win32(suspend) == 0);
#else
UNUSED_ARG(suspend);
return (system("shutdown -P 0") == 0);
#endif
}
/**
*
* @return
*/
const std::string &ADM_getTimeDateAsString(const char *format)
{
static std::string text; // not thread safe...
char timestamp[32];
uint64_t ss=ADM_getSecondsSinceEpoch();
time_t tme=(time_t)ss;
struct tm *ct;
ct=localtime(&tme);
size_t l=strftime(timestamp,32,format? format : "%Y-%m-%d %H%M%S",ct);
if(l)
text=std::string(timestamp);
else
text=std::string("UNKNOWN");
return text;
}
//EOF