tools/installer/InstallerUtil.cpp
author Scott Ehlert <ds@alliedmods.net>
Tue May 13 08:22:49 2008 +0000 (2008-05-13)
changeset 2051 568678a2e847
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Trunk build fixes
Fixed up IGameHelpers.h such that it no longer requires HL2SDK headers if a function doesn't need them
ds@1952
     1
#include "InstallerUtil.h"
ds@1952
     2
#include "InstallerMain.h"
ds@1952
     3
#include <stdio.h>
ds@1952
     4
#include <windows.h>
ds@1952
     5
ds@1952
     6
int tstrcasecmp(const TCHAR *str1, const TCHAR *str2)
ds@1952
     7
{
ds@1952
     8
#if defined _UNICODE
ds@1952
     9
	return _wcsicmp(str1, str2);
ds@1952
    10
#else
ds@1952
    11
	return _stricmp(str1, str2);
ds@1952
    12
#endif
ds@1952
    13
}
ds@1952
    14
ds@1952
    15
size_t AnsiToUnicode(const char *str, wchar_t *buffer, size_t maxchars)
ds@1952
    16
{
ds@1952
    17
	if (maxchars < 1)
ds@1952
    18
	{
ds@1952
    19
		return 0;
ds@1952
    20
	}
ds@1952
    21
ds@1952
    22
	size_t total = 
ds@1952
    23
		(size_t)MultiByteToWideChar(CP_UTF8,
ds@1952
    24
		0,
ds@1952
    25
		str,
ds@1952
    26
		-1,
ds@1952
    27
		buffer,
ds@1952
    28
		(int)maxchars);
ds@1952
    29
ds@1952
    30
	return total;
ds@1952
    31
}
ds@1952
    32
ds@1952
    33
bool IsWhiteSpaceA(const char *stream)
ds@1952
    34
{
ds@1952
    35
	char c = *stream;
ds@1952
    36
	if (c & (1<<7))
ds@1952
    37
	{
ds@1952
    38
		return false;
ds@1952
    39
	}
ds@1952
    40
	else
ds@1952
    41
	{
ds@1952
    42
		return isspace(c) != 0;
ds@1952
    43
	}
ds@1952
    44
}
ds@1952
    45
ds@1952
    46
int BreakStringA(const char *str, char *out, size_t maxchars)
ds@1952
    47
{
ds@1952
    48
	const char *inptr = str;
ds@1952
    49
	while (*inptr != '\0' && IsWhiteSpaceA(inptr))
ds@1952
    50
	{
ds@1952
    51
		inptr++;
ds@1952
    52
	}
ds@1952
    53
ds@1952
    54
	if (*inptr == '\0')
ds@1952
    55
	{
ds@1952
    56
		if (maxchars)
ds@1952
    57
		{
ds@1952
    58
			*out = '\0';
ds@1952
    59
		}
ds@1952
    60
		return -1;
ds@1952
    61
	}
ds@1952
    62
ds@1952
    63
	const char *start, *end = NULL;
ds@1952
    64
ds@1952
    65
	bool quoted = (*inptr == '"');
ds@1952
    66
	if (quoted)
ds@1952
    67
	{
ds@1952
    68
		inptr++;
ds@1952
    69
		start = inptr;
ds@1952
    70
		/* Read input until we reach a quote. */
ds@1952
    71
		while (*inptr != '\0' && *inptr != '"')
ds@1952
    72
		{
ds@1952
    73
			/* Update the end point, increment the stream. */
ds@1952
    74
			end = inptr++;
ds@1952
    75
		}
ds@1952
    76
		/* Read one more token if we reached an end quote */
ds@1952
    77
		if (*inptr == '"')
ds@1952
    78
		{
ds@1952
    79
			inptr++;
ds@1952
    80
		}
ds@1952
    81
	}
ds@1952
    82
	else
ds@1952
    83
	{
ds@1952
    84
		start = inptr;
ds@1952
    85
		/* Read input until we reach a space */
ds@1952
    86
		while (*inptr != '\0' && !IsWhiteSpaceA(inptr))
ds@1952
    87
		{
ds@1952
    88
			/* Update the end point, increment the stream. */
ds@1952
    89
			end = inptr++;
ds@1952
    90
		}
ds@1952
    91
	}
ds@1952
    92
ds@1952
    93
	/* Copy the string we found, if necessary */
ds@1952
    94
	if (end == NULL)
ds@1952
    95
	{
ds@1952
    96
		if (maxchars)
ds@1952
    97
		{
ds@1952
    98
			*out = '\0';
ds@1952
    99
		}
ds@1952
   100
	}
ds@1952
   101
	else if (maxchars)
ds@1952
   102
	{
ds@1952
   103
		char *outptr = out;
ds@1952
   104
		maxchars--;
ds@1952
   105
		for (const char *ptr=start;
ds@1952
   106
			(ptr <= end) && ((unsigned)(outptr - out) < (maxchars));
ds@1952
   107
			ptr++, outptr++)
ds@1952
   108
			{
ds@1952
   109
			*outptr = *ptr;
ds@1952
   110
		}
ds@1952
   111
		*outptr = '\0';
ds@1952
   112
	}
ds@1952
   113
ds@1952
   114
	/* Consume more of the string until we reach non-whitespace */
ds@1952
   115
	while (*inptr != '\0' && IsWhiteSpaceA(inptr))
ds@1952
   116
	{
ds@1952
   117
		inptr++;
ds@1952
   118
	}
ds@1952
   119
ds@1952
   120
	return (int)(inptr - str);
ds@1952
   121
}
ds@1952
   122
ds@1952
   123
size_t UTIL_Format(TCHAR *buffer, size_t count, const TCHAR *fmt, ...)
ds@1952
   124
{
ds@1952
   125
	va_list ap;
ds@1952
   126
	size_t len;
ds@1952
   127
	
ds@1952
   128
	va_start(ap, fmt);
ds@1952
   129
	len = UTIL_FormatArgs(buffer, count, fmt, ap);
ds@1952
   130
	va_end(ap);
ds@1952
   131
ds@1952
   132
	if (len >= count)
ds@1952
   133
	{
ds@1952
   134
		len = count - 1;
ds@1952
   135
		buffer[len] = '\0';
ds@1952
   136
	}
ds@1952
   137
ds@1952
   138
	return len;
ds@1952
   139
}
ds@1952
   140
ds@1952
   141
size_t UTIL_FormatArgs(TCHAR *buffer, size_t count, const TCHAR *fmt, va_list ap)
ds@1952
   142
{
ds@1952
   143
	size_t len = _vsntprintf(buffer, count, fmt, ap);
ds@1952
   144
ds@1952
   145
	if (len >= count)
ds@1952
   146
	{
ds@1952
   147
		len = count - 1;
ds@1952
   148
		buffer[len] = '\0';
ds@1952
   149
	}
ds@1952
   150
ds@1952
   151
	return len;
ds@1952
   152
}
ds@1952
   153
ds@1952
   154
size_t UTIL_PathFormat(TCHAR *buffer, size_t count, const TCHAR *fmt, ...)
ds@1952
   155
{
ds@1952
   156
	va_list ap;
ds@1952
   157
	size_t len;
ds@1952
   158
ds@1952
   159
	va_start(ap, fmt);
ds@1952
   160
	len = UTIL_FormatArgs(buffer, count, fmt, ap);
ds@1952
   161
	va_end(ap);
ds@1952
   162
ds@1952
   163
	for (size_t i = 0; i < len; i++)
ds@1952
   164
	{
ds@1952
   165
		if (buffer[i] == '/')
ds@1952
   166
		{
ds@1952
   167
			buffer[i] = '\\';
ds@1952
   168
		}
ds@1952
   169
	}
ds@1952
   170
ds@1952
   171
	return len;
ds@1952
   172
}
ds@1952
   173
ds@1952
   174
const TCHAR *GetFileFromPath(const TCHAR *path)
ds@1952
   175
{
ds@1952
   176
	size_t len = _tcslen(path);
ds@1952
   177
ds@1952
   178
	for (size_t i = len - 1;
ds@1952
   179
		 i >= 0 && i < len;
ds@1952
   180
		 i--)
ds@1952
   181
	{
ds@1952
   182
		if (path[i] == '\\' || path[i] == '/')
ds@1952
   183
		{
ds@1952
   184
			return &path[i+1];
ds@1952
   185
		}
ds@1952
   186
	}
ds@1952
   187
ds@1952
   188
	return NULL;
ds@1952
   189
}
ds@1952
   190
ds@1952
   191
void GenerateErrorMessage(DWORD err, TCHAR *buffer, size_t maxchars)
ds@1952
   192
{
ds@1952
   193
	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
ds@1952
   194
		NULL,
ds@1952
   195
		err,
ds@1952
   196
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
ds@1952
   197
		buffer,
ds@1952
   198
		(DWORD)maxchars,
ds@1952
   199
		NULL) == 0)
ds@1952
   200
	{
ds@1952
   201
		UTIL_Format(buffer, maxchars, _T("Unknown error"));
ds@1952
   202
	}
ds@1952
   203
}
ds@1952
   204
ds@1952
   205
INT_PTR AskToExit(HWND hWnd)
ds@1952
   206
{
ds@1952
   207
	TCHAR verify_exit[100];
ds@1952
   208
ds@1952
   209
	if (LoadString(g_hInstance,
ds@1952
   210
		IDS_VERIFY_EXIT,
ds@1952
   211
		verify_exit,
ds@1952
   212
		sizeof(verify_exit) / sizeof(TCHAR)
ds@1952
   213
		) == 0)
ds@1952
   214
	{
ds@1952
   215
		return (INT_PTR)FALSE;
ds@1952
   216
	}
ds@1952
   217
ds@1952
   218
	int val = MessageBox(
ds@1952
   219
		hWnd, 
ds@1952
   220
		_T("Are you sure you want to exit?"), 
ds@1952
   221
		_T("SourceMod Installer"), 
ds@1952
   222
		MB_YESNO|MB_ICONQUESTION);
ds@1952
   223
ds@1952
   224
	if (val == 0 || val == IDYES)
ds@1952
   225
	{
ds@1952
   226
		UpdateGlobalPosition(hWnd);
ds@1952
   227
		EndDialog(hWnd, NULL);
ds@1952
   228
		return (INT_PTR)TRUE;
ds@1952
   229
	}
ds@1952
   230
	
ds@1952
   231
	return (INT_PTR)FALSE;
ds@1952
   232
}
ds@1952
   233
ds@1952
   234
size_t UTIL_GetFileSize(const TCHAR *file_path)
ds@1952
   235
{
ds@1952
   236
	HANDLE hFile;
ds@1952
   237
ds@1952
   238
	if ((hFile = CreateFile(file_path, 
ds@1952
   239
		GENERIC_READ,
ds@1952
   240
		FILE_SHARE_READ,
ds@1952
   241
		NULL,
ds@1952
   242
		OPEN_EXISTING,
ds@1952
   243
		FILE_ATTRIBUTE_NORMAL,
ds@1952
   244
		NULL)) 
ds@1952
   245
		!= INVALID_HANDLE_VALUE)
ds@1952
   246
	{
ds@1952
   247
		LARGE_INTEGER size;
ds@1952
   248
ds@1952
   249
		if (GetFileSizeEx(hFile, &size))
ds@1952
   250
		{
ds@1952
   251
			CloseHandle(hFile);
ds@1952
   252
			return (size_t)size.QuadPart;
ds@1952
   253
		}
ds@1952
   254
ds@1952
   255
		CloseHandle(hFile);
ds@1952
   256
	}
ds@1952
   257
ds@1952
   258
	return 0;
ds@1952
   259
}
ds@1952
   260
ds@1952
   261
#if 0
ds@1952
   262
size_t UTIL_GetFolderSize(const TCHAR *basepath)
ds@1952
   263
{
ds@1952
   264
	HANDLE hFind;
ds@1952
   265
	WIN32_FIND_DATA fd;
ds@1952
   266
	TCHAR search_path[MAX_PATH];
ds@1952
   267
	size_t total = 0;
ds@1952
   268
ds@1952
   269
	UTIL_PathFormat(search_path,
ds@1952
   270
		sizeof(search_path) / sizeof(TCHAR),
ds@1952
   271
		_T("%s\\*.*"),
ds@1952
   272
		basepath);
ds@1952
   273
ds@1952
   274
	if ((hFind = FindFirstFile(search_path, &fd)) == INVALID_HANDLE_VALUE)
ds@1952
   275
	{
ds@1952
   276
		return 0;
ds@1952
   277
	}
ds@1952
   278
ds@1952
   279
	do
ds@1952
   280
	{
ds@1952
   281
		if (tstrcasecmp(fd.cFileName, _T(".")) == 0
ds@1952
   282
			|| tstrcasecmp(fd.cFileName, _T("..")) == 0)
ds@1952
   283
		{
ds@1952
   284
			continue;
ds@1952
   285
		}
ds@1952
   286
ds@1952
   287
		if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
ds@1952
   288
		{
ds@1952
   289
			UTIL_PathFormat(search_path,
ds@1952
   290
				sizeof(search_path) / sizeof(TCHAR),
ds@1952
   291
				_T("%s\\%s"),
ds@1952
   292
				basepath,
ds@1952
   293
				fd.cFileName);
ds@1952
   294
			total += UTIL_GetFolderSize(search_path);
ds@1952
   295
		}
ds@1952
   296
		else
ds@1952
   297
		{
ds@1952
   298
			UTIL_PathFormat(search_path,
ds@1952
   299
				sizeof(search_path) / sizeof(TCHAR),
ds@1952
   300
				_T("%s\\%s"),
ds@1952
   301
				basepath,
ds@1952
   302
				fd.cFileName);
ds@1952
   303
ds@1952
   304
			total += UTIL_GetFileSize(search_path);
ds@1952
   305
		}
ds@1952
   306
	} while (FindNextFile(hFind, &fd));
ds@1952
   307
ds@1952
   308
	FindClose(hFind);
ds@1952
   309
ds@1952
   310
	return total;
ds@1952
   311
}
ds@1952
   312
#endif