Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: installer/src/installer-lib/process.cpp

Issue 4790070691233792: Case-insensitive string class (Closed)
Patch Set: Created March 30, 2014, 7:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <stdexcept> 1 #include <stdexcept>
2 #include <functional> 2 #include <functional>
3 #include <wctype.h> 3 #include <wctype.h>
4 // <thread> is C++11, but implemented in VS2012 4 // <thread> is C++11, but implemented in VS2012
5 #include <thread> 5 #include <thread>
6 6
7 #include "installer-lib.h" 7 #include "installer-lib.h"
8 #include "process.h" 8 #include "process.h"
9 9
10 //------------------------------------------------------- 10 //-------------------------------------------------------
11 // process_by_name_CI
12 //-------------------------------------------------------
13 process_by_name_CI::process_by_name_CI( const wchar_t * name )
14 : name( name ), length( wcslen( name ) )
15 {}
16
17 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process )
18 {
19 return 0 == wcsncmpi( process.szExeFile, name, length ) ;
20 }
21
22 //-------------------------------------------------------
23 // process_by_any_exe_name_CI
24 //-------------------------------------------------------
25 bool process_by_any_exe_name_CI::operator()( const PROCESSENTRY32W & process )
26 {
27 return names.find( process.szExeFile ) != names.end() ;
28 }
29
30 //-------------------------------------------------------
31 // wcscmpi
32 //-------------------------------------------------------
33 int wcscmpi( const wchar_t * s1, const wchar_t * s2 )
34 {
35 // Note: Equality of character sequences is case-insensitive in all predicates below.
36 // Loop invariant: s1[0..j) == s2[0..j)
37 const size_t LIMIT( 65535 ) ; // Runaway limit of 2^16 - 1 should be acceptabl y long.
38 for ( size_t j = 0 ; j < LIMIT ; ++j )
39 {
40 wchar_t c1 = towupper( *s1++ ) ;
41 wchar_t c2 = towupper( *s2++ ) ;
42 if ( c1 != c2 )
43 {
44 // Map to -1/+1 because c2 - c1 may not fit into an 'int'.
45 return ( c1 < c2 ) ? -1 : 1 ;
46 }
47 else
48 {
49 if ( c1 == L'\0' )
50 {
51 // Assert length( s1 ) == length( s2 ) == j
52 // Assert strings are equal at length < n
53 return 0 ;
54 }
55 }
56 }
57 // Assert j == LIMIT
58 // Assert s1[0..LIMIT) == s2[0..LIMIT)
59 // Both strings are longer than 64K, which violates the precondition
60 throw std::runtime_error( "String arguments too long for wcscmpi" ) ;
61 }
62
63 //-------------------------------------------------------
64 // wcsncmpi
65 //-------------------------------------------------------
66 int wcsncmpi( const wchar_t * s1, const wchar_t * s2, unsigned int n )
67 {
68 // Note: Equality of character sequences is case-insensitive in all predicates below.
69 // Loop invariant: s1[0..j) == s2[0..j)
70 for ( unsigned int j = 0 ; j < n ; ++j )
71 {
72 wchar_t c1 = towupper( *s1++ ) ;
73 wchar_t c2 = towupper( *s2++ ) ;
74 if ( c1 != c2 )
75 {
76 // Map to -1/+1 because c2 - c1 may not fit into an 'int'.
77 return ( c1 < c2 ) ? -1 : 1 ;
78 }
79 else
80 {
81 if ( c1 == L'\0' )
82 {
83 // Assert length( s1 ) == length( s2 ) == j
84 // Assert strings are equal at length < n
85 return 0 ;
86 }
87 }
88 }
89 // Assert j == n
90 // Assert s1[0..n) == s2[0..n)
91 // The semantics of n-compare ignore everything after the first 'n' characters .
92 return 0 ;
93 }
94
95 //-------------------------------------------------------
96 // creator_process 11 // creator_process
97 //------------------------------------------------------- 12 //-------------------------------------------------------
98 DWORD creator_process( HWND window ) 13 DWORD creator_process( HWND window )
99 { 14 {
100 DWORD pid ; 15 DWORD pid ;
101 DWORD r = GetWindowThreadProcessId( window, & pid ) ; 16 DWORD r = GetWindowThreadProcessId( window, & pid ) ;
102 if ( r == 0 ) 17 if ( r == 0 )
103 { 18 {
104 // Assert GetWindowThreadProcessId returned an error 19 // Assert GetWindowThreadProcessId returned an error
105 // If the window handle is invalid, we end up here. 20 // If the window handle is invalid, we end up here.
(...skipping 19 matching lines...) Expand all
125 PROCESSENTRY32W * Snapshot::next() 40 PROCESSENTRY32W * Snapshot::next()
126 { 41 {
127 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ; 42 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ;
128 } 43 }
129 44
130 void Snapshot::refresh() 45 void Snapshot::refresh()
131 { 46 {
132 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ; 47 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ;
133 } 48 }
134 49
135
136 //------------------------------------------------------- 50 //-------------------------------------------------------
137 // send_message, send_endsession_messages 51 // send_message, send_endsession_messages
138 //------------------------------------------------------- 52 //-------------------------------------------------------
139 /** 53 /**
140 * Default process exit wait time (per message) 5000 ms 54 * Default process exit wait time (per message) 5000 ms
141 * 55 *
142 * 5 seconds is time that the system will wait before it considers a process non -responsive. 56 * 5 seconds is time that the system will wait before it considers a process non -responsive.
143 */ 57 */
144 static const unsigned int timeout = 5000 ; // milliseconds 58 static const unsigned int timeout = 5000 ; // milliseconds
145 59
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 if ( ! is_running() ) 293 if ( ! is_running() )
380 { 294 {
381 return true ; 295 return true ;
382 } 296 }
383 } 297 }
384 // Assert is_running() 298 // Assert is_running()
385 } 299 }
386 // No control path leaves the for-loop. 300 // No control path leaves the for-loop.
387 } ; 301 } ;
388 302
OLDNEW

Powered by Google App Engine
This is Rietveld