-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
769 lines (692 loc) · 16 KB
/
Copy pathutils.h
File metadata and controls
769 lines (692 loc) · 16 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#pragma once
#include <atlfile.h>
#include <atlsync.h>
#define NUM_CHANNELS 2
#define SAMPLE_SIZE 16
#define SAMPLE_FACTOR (NUM_CHANNELS * (SAMPLE_SIZE >> 3))
#define SAMPLE_FREQ (44100.0)
#define BYTES_PER_SEC (SAMPLE_FREQ * SAMPLE_FACTOR)
// aligns to the nearest sample (previous or next)
#define ALIGN_TO_SAMPLE(__ToAlign) { auto __mod = __ToAlign % SAMPLE_FACTOR; __ToAlign += (__mod > SAMPLE_FACTOR / 2) ? (SAMPLE_FACTOR - __mod) : (__mod * (-1)); }
// aligns to the previous sample
#define ALIGN_DOWN_TO_SAMPLE(__ToAlign) { __ToAlign -= (__ToAlign % SAMPLE_FACTOR); }
namespace local_utils
{
////////////////////////////////////////////////////////////////////
// global functions
HANDLE DuplicateHandle(HANDLE h);
ULONGLONG Shovel(HANDLE hFrom, HANDLE hTo, ULONGLONG nBytesToShovel, bool bInputHandleIsPipe = false);
HANDLE Exec(LPCWSTR strCmd, WORD wShowWindow = SW_SHOWNORMAL, PHANDLE phStdOut = NULL, PHANDLE phStdIn = NULL) noexcept;
std::wstring GetModulePath(HMODULE hModule = NULL, PCWSTR strModuleName = NULL);
std::wstring LocateModulePath(PCWSTR strModule, PCWSTR strDefaultExtension = L".exe");
bool WritePPMData(HANDLE hTo, const uint8_t* pFrameBuffer, int nWidth, int nHeight);
void InitBitmapInfo(__out_bcount(cbInfo) BITMAPINFO *pbmi, ULONG cbInfo, LONG cx, LONG cy, WORD bpp, bool bFlip = true);
HRESULT CreateDibmap(HDC hdc, const SIZE *psize, int nBits, __deref_opt_out void **ppvBits, __out HBITMAP* phBmp, bool bFlip = true);
///////////////////////////////////////////////////////////////////////
// CDummyLock
class CDummyLock
{
public:
// Acquire the critical section
_Acquires_lock_(*this)
void Enter()
{
}
// Release the critical section
_Releases_lock_(*this)
void Leave()
{
}
// Attempt to acquire the critical section
_When_(return != 0, _Acquires_lock_(*this))
BOOL TryEnter()
{
return TRUE;
}
};
///////////////////////////////////////////////////////////////////////
// CAutoSync
template<class T>
class CAutoSyncT
{
public:
CAutoSyncT(T* pObjectToLock)
{
m_pObjectToLock = NULL;
if (pObjectToLock)
Lock(pObjectToLock);
}
CAutoSyncT(T& ObjectToLock)
{
m_pObjectToLock = NULL;
Lock(&ObjectToLock);
}
~CAutoSyncT()
{
Unlock();
}
public:
inline void Lock(T* pObjectToLock)
{
ATLASSERT(!m_pObjectToLock);
m_pObjectToLock = pObjectToLock;
ATLASSERT(m_pObjectToLock);
m_pObjectToLock->Enter();
}
inline void Lock(T& ObjectToLock)
{
Lock(&ObjectToLock)
}
inline void Unlock()
{
if (m_pObjectToLock)
{
m_pObjectToLock->Leave();
m_pObjectToLock = NULL;
}
}
public:
T* m_pObjectToLock;
};
typedef CAutoSyncT<CCriticalSection> CAutoSync;
///////////////////////////////////////////////////////////////////////
// CMyQueue
template<class T, class __LockClass = CCriticalSection>
class CMyQueue : public std::list<T>
{
public:
CMyQueue()
{
m_bCanQueue = true;
ATLVERIFY(m_hEvent.Create(NULL, FALSE, FALSE, NULL));
}
public:
inline bool queue(T&& item)
{
CAutoSyncT<__LockClass> sync(m_mtx);
if (m_bCanQueue)
{
emplace_back(std::move(item));
sync.Unlock();
m_hEvent.Set();
return true;
}
return false;
}
inline bool unqueue(T& item)
{
CAutoSyncT<__LockClass> sync(m_mtx);
if (!__super::empty())
{
item = std::move(front());
pop_front();
return true;
}
return false;
}
inline bool peek(T& item) const
{
CAutoSyncT<__LockClass> sync(m_mtx);
if (!__super::empty())
{
item = front();
return true;
}
return false;
}
inline bool unqueue()
{
CAutoSyncT<__LockClass> sync(m_mtx);
if (!__super::empty())
{
T item = std::move(front());
pop_front();
sync.Unlock();
return true;
}
return false;
}
inline size_t size(void) const
{
CAutoSyncT<__LockClass> sync(m_mtx);
return __super::size();
}
inline size_t count(void) const
{
return size();
}
inline bool is_flushed() const
{
CAutoSyncT<__LockClass> sync(m_mtx);
return (!m_bCanQueue && _super::empty()) ? true : false;
}
inline void flush()
{
CAutoSyncT<__LockClass> sync(m_mtx);
m_bCanQueue = false;
sync.Unlock();
m_hEvent.Set();
}
inline bool empty() const
{
CAutoSyncT<__LockClass> sync(m_mtx);
return __super::empty() ? true : false;
}
inline void clear()
{
CAutoSyncT<__LockClass> sync(m_mtx);
m_hEvent.Reset();
__super::clear();
m_bCanQueue = true;
}
inline void Lock(bool bExcusive = true)
{
UNREFERENCED_PARAMETER(bExcusive);
m_mtx.Enter();
}
inline void Unlock()
{
m_mtx.Leave();
}
public:
CEvent m_hEvent;
protected:
mutable __LockClass m_mtx;
bool m_bCanQueue;
};
/////////////////////////////////////////////////////////////////////////
// CMyThread
class CMyThread
{
public:
CMyThread()
{
m_dwTimeout = INFINITE;
ATLVERIFY(m_hStopEvent.Create(NULL, TRUE, FALSE, NULL));
}
virtual ~CMyThread()
{
if (IsRunning())
Stop();
}
public:
bool Start(HANDLE hEvent)
{
if (hEvent)
{
Stop();
m_hEvent.Attach(DuplicateHandle(hEvent));
if (m_hEvent)
{
m_hThread.Attach((HANDLE)_beginthreadex(NULL, 0, _Run, this, CREATE_SUSPENDED, NULL));
if (m_hThread)
{
::ResumeThread(m_hThread);
return true;
}
}
Stop();
}
return false;
}
void Stop()
{
if (m_hThread)
{
m_hStopEvent.Set();
::WaitForSingleObject(m_hThread, INFINITE);
m_hThread.Close();
}
m_hEvent.Close();
m_hStopEvent.Reset();
}
inline bool IsRunning() const
{
return m_hThread && ::WaitForSingleObject(m_hThread, 0) != WAIT_OBJECT_0;
}
inline bool IsStopping() const
{
return ::WaitForSingleObject(m_hStopEvent, 0) == WAIT_OBJECT_0;
}
protected:
virtual bool OnStart()
{
return true;
}
virtual void OnStop()
{
}
virtual void OnTimeout()
{
}
virtual void OnEvent()
{
}
private:
static unsigned int __stdcall _Run(LPVOID pParam)
{
CMyThread* pThis = (CMyThread*)pParam;
return pThis->Run();
}
unsigned int Run()
{
if (OnStart())
{
HANDLE h[2];
h[0] = m_hStopEvent;
h[1] = m_hEvent;
bool bRun = true;
do
{
switch (::WaitForMultipleObjects(m_hEvent != NULL ? 2 : 1, h, FALSE, m_dwTimeout))
{
case WAIT_OBJECT_0:
{
OnStop();
bRun = false;
}
break;
case WAIT_TIMEOUT:
{
OnTimeout();
}
break;
default:
{
OnEvent();
}
break;
}
}
while (bRun);
}
_endthreadex(0);
return 0;
}
public:
CHandle m_hThread;
CEvent m_hStopEvent;
CHandle m_hEvent;
DWORD m_dwTimeout;
};
/////////////////////////////////////////////////////////////////////////////
// CFilePicker
class CFilePicker
{
public:
CFilePicker(DWORD dwFlags = MAXDWORD)
{
m_dwFlags = dwFlags;
}
/* "Alle Dateien", "*.*", .... */
BOOL Load(LPCTSTR Title, int AnzFilter, ...);
BOOL Save(LPCTSTR Title, int AnzFilter, ...);
LPCTSTR GetPathName()
{
return m_strPathName;
}
void SetPathName(LPCTSTR lpszPathName)
{
if (lpszPathName != NULL)
m_strPathName = lpszPathName;
}
void SetInitialDir(LPCTSTR);
void SetDefExt(LPCWSTR lpstrExt);
void SetDefExt(LPCSTR lpstrExt);
long GetSelectedType();
protected:
ATL::CString m_strPathName;
ATL::CString m_strInitialDir;
ATL::CString m_strDefExt;
long m_nSelectedType;
private:
TCHAR m_Buf[MAX_PATH];
DWORD m_dwFlags;
};
/////////////////////////////////////////////////////////
// CTemporaryFile
class CTemporaryFile
{
public:
CTemporaryFile(PCWSTR strExt = NULL, bool bDeleteOnClose = true, bool bOverlappedIO = false)
{
if (strExt != NULL)
m_strExt = strExt;
m_bDeleteOnClose = bDeleteOnClose;
memset((void*)&m_Operlapped, 0, sizeof(OVERLAPPED));
if (bOverlappedIO)
{
m_Operlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
ATLASSERT(m_Operlapped.hEvent);
}
}
~CTemporaryFile()
{
Close();
if (m_Operlapped.hEvent)
{
CloseHandle(m_Operlapped.hEvent);
memset((void*)&m_Operlapped, 0, sizeof(OVERLAPPED));
}
}
void Close()
{
if (m_file.m_h && m_file.m_h != INVALID_HANDLE_VALUE)
{
m_file.Close();
}
}
bool IsOpen() const
{
if (m_file.m_h && m_file.m_h != INVALID_HANDLE_VALUE)
return true;
return false;
}
HRESULT Create(LPCTSTR pszDir = NULL, DWORD dwDesiredAccess = GENERIC_READ|GENERIC_WRITE, DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
{
if (m_strTempFileName.empty())
{
TCHAR szPath[_MAX_PATH];
TCHAR tmpFileName[_MAX_PATH];
ATLASSERT(m_file.m_h == NULL);
if (pszDir == NULL)
{
DWORD dwRet = GetTempPath(_MAX_DIR, szPath);
if (dwRet == 0)
{
return AtlHresultFromLastError();
}
else if (dwRet > _MAX_DIR)
{
return DISP_E_BUFFERTOOSMALL;
}
}
else
{
if (Checked::tcsncpy_s(szPath, _countof(szPath), pszDir, _TRUNCATE) == STRUNCATE)
{
return DISP_E_BUFFERTOOSMALL;
}
}
if (!GetTempFileName(szPath, _T("UTL"), 0, tmpFileName))
{
return AtlHresultFromLastError();
}
tmpFileName[_countof(tmpFileName) - 1] = '\0';
m_strTempFileName = tmpFileName;
if (!m_strExt.empty())
{
::DeleteFile(m_strTempFileName.c_str());
m_strTempFileName += m_strExt;
}
}
return Open(false, dwDesiredAccess, dwShareMode);
}
inline HRESULT Open(bool bOpenExisting = true, DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE, DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
{
SECURITY_ATTRIBUTES secatt;
secatt.nLength = sizeof(secatt);
secatt.lpSecurityDescriptor = NULL;
secatt.bInheritHandle = TRUE;
return m_file.Create(m_strTempFileName.c_str(), dwDesiredAccess, dwShareMode, bOpenExisting ? OPEN_EXISTING : CREATE_ALWAYS, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_TEMPORARY | (m_bDeleteOnClose ? FILE_FLAG_DELETE_ON_CLOSE : 0), &secatt);
}
operator HANDLE()
{
return m_file;
}
inline HANDLE Detach()
{
return m_file.Detach();
}
inline bool SeekToBegin()
{
return SUCCEEDED(m_file.Seek(0, FILE_BEGIN)) ? true : false;
}
inline bool Delete()
{
if (IsOpen())
Close();
::DeleteFile(m_strTempFileName.c_str());
return !Exists();
}
inline bool Exists()
{
return PathFileExists(m_strTempFileName.c_str()) ? true : false;
}
inline void Clear()
{
if (IsOpen())
Close();
m_strTempFileName.clear();
}
public:
CAtlFile m_file;
std::wstring m_strTempFileName;
std::wstring m_strExt;
bool m_bDeleteOnClose;
OVERLAPPED m_Operlapped;
};
////////////////////////////////////////////////////////////////////////////
// CWaveHeader
#include "pshpack1.h"
class CWaveHeader
{
public:
struct
{
uint8_t chunkID[4];
uint32_t chunkSize;
uint8_t format[4];
uint8_t subchunk1ID[4];
uint32_t subchunk1Size;
uint16_t audioFormat;
uint16_t numChannels;
uint32_t sampleRate;
uint32_t byteRate;
uint16_t blockAlign;
uint16_t bitsPerSample;
uint8_t subchunk2ID[4];
uint32_t subchunk2Size;
} myData;
public:
CWaveHeader()
{
init();
}
void init(uint32_t sampleRate = (uint32_t)SAMPLE_FREQ, uint8_t bitDepth = SAMPLE_SIZE, uint8_t numChans = NUM_CHANNELS)
{
// set all the ASCII literals
memcpy(myData.chunkID , "RIFF", 4);
memcpy(myData.format , "WAVE", 4);
memcpy(myData.subchunk1ID , "fmt ", 4);
memcpy(myData.subchunk2ID , "data", 4);
// set all the known numeric values
myData.audioFormat = 1;
myData.chunkSize = 36;
myData.subchunk1Size = 16;
myData.subchunk2Size = 0;
// set the passed-in numeric values
myData.sampleRate = sampleRate;
myData.bitsPerSample = bitDepth;
myData.numChannels = numChans;
// calculate the remaining dependent values
myData.blockAlign = myData.numChannels * myData.bitsPerSample / 8;
myData.byteRate = myData.sampleRate * myData.blockAlign;
}
inline bool isValid() const
{
return memcmp(myData.chunkID, "RIFF", 4) == 0 ? true : false;
}
inline void update(uint32_t numBytes)
{
myData.subchunk2Size = numBytes;
myData.chunkSize = myData.subchunk2Size + 36;
}
inline uint8_t mySize()
{
return sizeof(myData);
}
inline uint32_t getPCMBytes() const
{
return myData.subchunk2Size;
}
inline double getPCMSeconds() const
{
return ((double)myData.subchunk2Size) / ((double)(myData.sampleRate * (myData.numChannels * (myData.bitsPerSample >> 3))));
}
};
#include "poppack.h"
////////////////////////////////////////////////////////////////////////////
// CAVCoder
class CAVCoder
{
public:
CAVCoder(PCWSTR strCoderName = L"ffmpeg");
const std::wstring& GetCoderPath()
{
return c_strPathToCoder;
}
HANDLE StartCoder(PHANDLE phStdOut = NULL, PHANDLE phStdIn = NULL);
HANDLE DecodeAudioToPipe(std::wstring strAudioSourceFile, PHANDLE phPipe, bool bRawPCM = true);
HANDLE DecodeAudioToFile(std::wstring strAudioSourceFile, std::wstring strAudioDestFile, bool bRawPCM = false);
HANDLE EncodeSlideShow(std::wstring strMP4DestFile, PHANDLE phPipe, int nFramesPerSecond = 25, std::wstring strAudioSourceFile = std::wstring(), PCWSTR strOutputCodec = L"libx264", PCWSTR strInputCodec = L"ppm");
protected:
static std::wstring c_strPathToCoder;
std::wstring m_strParamaters;
};
////////////////////////////////////////////////////////////////////////////
// CTokenizer
template<class T>
class CTokenizerT
{
public:
typedef T value_type;
typedef typename value_type::value_type char_type;
typedef typename value_type::const_iterator const_iterator;
typedef enum
{
invalid,
token,
sep,
quoted
} token_type;
typedef struct unnamed
{
inline value_type ToString() const
{
ATLASSERT(m_tt != token_type::invalid);
return value_type(&(*m_iToken), m_nLen);
}
inline char_type ToChar() const
{
ATLASSERT(m_tt != token_type::invalid);
return *m_iToken;
}
const_iterator m_iToken;
size_t m_nLen;
token_type m_tt;
} CToken;
public:
CTokenizerT(const value_type& strToParse, const value_type& strSeps, char_type QuotationMark = '\"')
{
m_strToParse = strToParse;
m_strSeps = strSeps;
m_QuotationMark = m_QuotationMark;
m_pCurrent = m_strToParse.begin();
}
inline void Restart(const value_type& strToParse, const value_type& strSeps, char_type QuotationMark)
{
m_strToParse = strToParse;
m_strSeps = strSeps;
m_QuotationMark = m_QuotationMark;
m_pCurrent = m_strToParse.begin();
}
inline void Restart(const value_type& strToParse, const value_type& strSeps)
{
m_strToParse = strToParse;
m_strSeps = strSeps;
m_pCurrent = m_strToParse.begin();
}
inline void Restart(const value_type& strToParse)
{
m_strToParse = strToParse;
m_pCurrent = m_strToParse.begin();
}
inline bool GetNext(CToken& result)
{
result.m_tt = token_type::invalid;
bool bFinished = false;
while (!bFinished && m_pCurrent != m_strToParse.end())
{
switch (result.m_tt)
{
case token_type::invalid:
{
if (m_QuotationMark && *m_pCurrent == m_QuotationMark)
{
result.m_tt = token_type::quoted;
++m_pCurrent;
result.m_iToken = m_pCurrent;
}
else if (m_strSeps.find(*m_pCurrent) != value_type::npos)
{
result.m_tt = token_type::sep;
result.m_iToken = m_pCurrent++;
}
else
{
result.m_tt = token_type::token;
result.m_iToken = m_pCurrent++;
}
}
break;
case token_type::quoted:
{
if (*m_pCurrent == m_QuotationMark)
bFinished = true;
else
++m_pCurrent;
}
break;
case token_type::sep:
{
if (m_strSeps.find(*m_pCurrent) == value_type::npos || (m_QuotationMark && *m_pCurrent == m_QuotationMark))
bFinished = true;
else
++m_pCurrent;
}
break;
case token_type::token:
{
if (m_strSeps.find(*m_pCurrent) != value_type::npos || (m_QuotationMark && *m_pCurrent == m_QuotationMark))
bFinished = true;
else
++m_pCurrent;
}
break;
default:
{
ATLASSERT(FALSE);
}
break;
}
}
if (result.m_tt != token_type::invalid)
{
result.m_nLen = m_pCurrent - result.m_iToken;
if (result.m_tt == token_type::quoted)
--result.m_nLen;
return true;
}
return false;
}
public:
value_type m_strToParse;
value_type m_strSeps;
char_type m_QuotationMark;
protected:
const_iterator m_pCurrent;
};
typedef CTokenizerT<std::wstring> CTokenizer;
typedef CTokenizerT<std::string> CTokenizerA;
}