-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
61 lines (42 loc) · 1.17 KB
/
util.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
#include "util.h"
void UTIL_MakeFile( const char *szFile, string strContents )
{
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "wb" ) ) == NULL )
{
printf( "warning - unable to open %s for writing\n", szFile );
return;
}
fwrite( (void *)strContents.c_str( ), sizeof( char ), strContents.size( ), pFile );
fclose( pFile );
}
string UTIL_StringToHash( const string &strString )
{
// convert a readable string hash to a 20 character hash
string strHash;
if( strString.size( ) != 40 && strString.size() != 41 )
return string( );
if( strString.size() == 40 )
for( unsigned long i = 0; i < strString.size( ); i += 2 )
{
char pBuf[2];
memset( pBuf, 0, sizeof( char ) * 2 );
pBuf[0] = strString[i];
pBuf[1] = strString[i + 1];
unsigned int c;
sscanf( pBuf, "%02x", &c );
strHash += c;
}
else if( strString.size() == 41)
for( unsigned long i = 1; i < strString.size( ); i += 2 )
{
char pBuf[2];
memset( pBuf, 0, sizeof( char ) * 2 );
pBuf[0] = strString[i];
pBuf[1] = strString[i + 1];
unsigned int c;
sscanf( pBuf, "%02x", &c );
strHash += c;
}
return strHash;
}