-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiscutil.cpp
More file actions
135 lines (103 loc) · 4.17 KB
/
Copy pathmiscutil.cpp
File metadata and controls
135 lines (103 loc) · 4.17 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
/*****************************************************************************
File name: miscutil.cpp
Project: FarVCS plugin
Purpose: General-purpose utilities
Compiler: MS Visual C++ 8.0
Authors: Michael Steinhause
Dependencies: STL, Win32
*****************************************************************************/
#include "miscutil.h"
using namespace std;
//==========================================================================>>
// Splits a string into a list of strings (like split in perl)
//==========================================================================>>
vector<string> SplitString( const string& s, char c )
{
vector<string> v;
// Check if the string is emtpy
if ( s.empty() )
return v;
vector<char> sz( s.length()+1 ); // Used instead of new to utilize auto-deletion
strcpy( &sz[0], s.c_str() );
char *pNext = &sz[0]; // The beginning of the next substring
for ( char *p = &sz[0]; *p != 0; ++p )
{
if ( *p == c ) {
*p = 0;
v.push_back( pNext );
if ( c == ' ' ) {
while ( *(++p) == c )
;
pNext = p;
}
pNext = p + 1;
}
}
if ( *pNext != 0 )
v.push_back( pNext );
return v;
}
//==========================================================================>>
// Replacement of the conventional Sleep, because the latter causes troubles
// with message processing -- see MSDN for details
//==========================================================================>>
void SafeSleep( DWORD dwMilliseconds )
{
static HANDLE hFakeEvent = ::CreateEvent( 0, true /*bManualReset*/, false /*bInitialState*/, 0 );
HANDLE ObjectsToWait[] = { hFakeEvent };
::WaitForMultipleObjects( 1, ObjectsToWait, false, dwMilliseconds );
}
//==========================================================================>>
// Thread class. Encapsulates thread starting and graceful stopping
//==========================================================================>>
HRESULT Thread::Start( void *Param )
{
if ( m_hThread ) // Already started
return E_FAIL;
m_Param = Param;
// Create the event to signal when we want the thread to terminate
m_hTerminateEvent = ::CreateEvent( 0, true /*bManualReset*/, false /*bInitialState*/, 0 );
if ( !m_hTerminateEvent )
return HRESULT_FROM_WIN32( ::GetLastError() );
// Spawn the thread
unsigned int dwDummyThreadID;
m_hThread = reinterpret_cast<HANDLE>( _beginthreadex( 0, 0, ThreadWrapper, this, 0, &dwDummyThreadID ) );
return m_hThread ? S_OK : E_FAIL;
}
void Thread::Stop( unsigned long dwMilliseconds )
{
// Stop the thread if it is running
if ( m_hThread )
if ( m_hTerminateEvent == 0 || !::SetEvent( m_hTerminateEvent ) || ::WaitForSingleObject( m_hThread, dwMilliseconds ) != WAIT_OBJECT_0 )
::TerminateThread( m_hThread, (DWORD)-1 );
// Close the temination event and the thread handle
if ( m_hTerminateEvent ) {
::CloseHandle( m_hTerminateEvent );
m_hTerminateEvent = 0;
}
if ( m_hThread ) {
::CloseHandle( m_hThread );
m_hThread = 0;
}
}
//==========================================================================>>
// GetTempPath/GetTempFileName wrappers
//==========================================================================>>
string GetTempPath()
{
char szTempPath[MAX_PATH];
DWORD dwLength = ::GetTempPath( array_size(szTempPath), szTempPath );
if ( dwLength < array_size(szTempPath) )
return szTempPath;
else if ( dwLength == 0 )
throw std::runtime_error( "::GetTempPath failed" );
else
throw std::runtime_error( "::GetTempPath returned a path that was too long" );
}
string GetTempFileName( const char *szPrefix )
{
char szTempFileName[MAX_PATH];
if ( ::GetTempFileName( GetTempPath().c_str(), szPrefix ? szPrefix : "", 0, szTempFileName ) == 0 )
throw std::runtime_error( "::GetTempFileName failed" );
return szTempFileName;
}