<commit_before>/*
TODO:
- OpenWatcom
- Win16
- Test with Winelib, not just Cygwin.
- MinGW-w64 64-bit
- MSVC 64-bit
- Async system()
- Restrict countdown time edit control numerically.
*/

#include "resource.h"

#include "common.h"

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <windows.h>

#if defined __MINGW32__
#	define HAVE_WINDOWS_3 1
#	define OMIT_CRT 1
#endif

#ifdef _WIN32
#	include <tchar.h>
#else
#	define _tcschr strchr
#endif

#if HAVE_WINDOWS_3
DWORD _win_ver;
#endif

static const TCHAR _title[] = TEXT("Countdown Timer");

static void _stop_timer(HWND dlg, UINT_PTR *id)
{
	if(*id)
	{
		CheckDlgButton(dlg, IDC_START, BST_UNCHECKED);
		SetDlgItemText(dlg, IDC_START, TEXT("Start"));
		KillTimer(dlg, *id);
		*id = 0;
	}
}

static void _show_time(HWND dlg, LPTSTR time, DWORD seconds)
{
	if(seconds < 60)
		wsprintf(time, TEXT(":%.2d"), seconds);
	else if(seconds < 3600)
		wsprintf(time, TEXT("%d:%.2d"), seconds / 60, seconds % 60);
	else
		wsprintf(time, TEXT("%d:%.2d:%.2d"), seconds / 3600, (seconds / 60) % 60, seconds % 60);

	SetDlgItemText(dlg, IDC_TIME, time);
}

static BOOL _valid_time(LPTSTR time)
{
	DWORD dig = 0, field = 0;

	while(*time)
	{
		if(isdigit(*time))
		{
			if(field && dig >= 2)
				return FALSE;

			dig++;
		}
		else if(*time == ':')
		{
			if((!dig && field) || field == 2)
				return FALSE;

			dig = 0;
			field++;
		}
		else
		{
			return FALSE;
		}
		time++;
	}

	return TRUE;
}

static void _run_prog(HWND dlg)
{
	if(IsDlgButtonChecked(dlg, IDC_BEEP) != BST_CHECKED)
	{
		TCHAR cmd_line[1024];
		LPTSTR param, file = cmd_line;

		GetDlgItemText(dlg, IDC_COMMAND, cmd_line, arraysize(cmd_line));

#ifndef _WIN32
		if(system(cmd_line) == -1)
			MessageBox(dlg, "Error issuing a command.", _title, MB_ICONERROR | MB_OK);
#else
		while(isspace(*file))
			file++;

		if(*file == '"')
		{
			file++;
			/* GCC translates __builtin_strchr to simply strchr. */
			param = _tcschr(file, '"');

			if(param)
			{
				*param = 0;
				param++;
			}
		}
		else
		{
			param = file;

			while(*param && !isspace(*param))
				param++;

			if(*param)
			{
				*param = 0;
				param++;
			}
		}

		ShellExecute(NULL, NULL, file, param, NULL, SW_SHOWNORMAL);
#endif
	}
	else
	{
		MessageBox(dlg, TEXT("Time elapsed."), _title, MB_OK | MB_ICONINFORMATION);<commit_msg>Added error handling for the "Run Now" command.<commit_after>/*
TODO:
- OpenWatcom
- Win16
- HXDOS
- Test with Winelib, not just Cygwin.
- MinGW-w64 64-bit
- MSVC 64-bit
- Async system()
- Restrict countdown time edit control numerically.
- About button with copyright.
- ShellExecuteEx
*/

#include "resource.h"

#include "common.h"

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <windows.h>

#if defined __MINGW32__
#	define HAVE_WINDOWS_3 1
#	define OMIT_CRT 1
#endif

#ifdef _WIN32
#	include <tchar.h>
#else
#	define _tcschr strchr
#endif

#if HAVE_WINDOWS_3
DWORD _win_ver;
#endif

static const TCHAR _title[] = TEXT("Countdown Timer");

static void _stop_timer(HWND dlg, UINT_PTR *id)
{
	if(*id)
	{
		CheckDlgButton(dlg, IDC_START, BST_UNCHECKED);
		SetDlgItemText(dlg, IDC_START, TEXT("Start"));
		KillTimer(dlg, *id);
		*id = 0;
	}
}

static void _show_time(HWND dlg, LPTSTR time, DWORD seconds)
{
	if(seconds < 60)
		wsprintf(time, TEXT(":%.2d"), seconds);
	else if(seconds < 3600)
		wsprintf(time, TEXT("%d:%.2d"), seconds / 60, seconds % 60);
	else
		wsprintf(time, TEXT("%d:%.2d:%.2d"), seconds / 3600, (seconds / 60) % 60, seconds % 60);

	SetDlgItemText(dlg, IDC_TIME, time);
}

static BOOL _valid_time(LPTSTR time)
{
	DWORD dig = 0, field = 0;

	while(*time)
	{
		if(isdigit(*time))
		{
			if(field && dig >= 2)
				return FALSE;

			dig++;
		}
		else if(*time == ':')
		{
			if((!dig && field) || field == 2)
				return FALSE;

			dig = 0;
			field++;
		}
		else
		{
			return FALSE;
		}
		time++;
	}

	return TRUE;
}

static void _just_beep(HWND dlg)
{
	MessageBox(dlg, TEXT("Time elapsed."), _title, MB_OK | MB_ICONINFORMATION);
}

static void _run_prog(HWND dlg)
{
	if(IsDlgButtonChecked(dlg, IDC_BEEP) != BST_CHECKED)
	{
		TCHAR cmd_line[1024];
		LPTSTR param, file = cmd_line;
		UINT_PTR result;

		GetDlgItemText(dlg, IDC_COMMAND, cmd_line, arraysize(cmd_line));

#ifndef _WIN32
		if(system(cmd_line) == -1)
			MessageBox(dlg, "Error issuing a command.", _title, MB_ICONERROR | MB_OK);
#else
		while(isspace(*file))
			file++;

		if(*file == '"')
		{
			file++;
			/* GCC translates __builtin_strchr to simply strchr. */
			param = _tcschr(file, '"');

			if(param)
			{
				*param = 0;
				param++;
			}
		}
		else
		{
			param = file;

			while(*param && !isspace(*param))
				param++;

			if(*param)
			{
				*param = 0;
				param++;
			}
		}

		if(!*file)
		{
			_just_beep(dlg);
			return;
		}

		result = (UINT_PTR)ShellExecute(dlg, NULL, file, param, NULL, SW_SHOWNORMAL);
		if(result <= 32)
		{
			static const DWORD errors26[] =
			{
				ERROR_SHARING_VIOLATION,
				ERROR_NO_ASSOCIATION,
				ERROR_DDE_FAIL,
				ERROR_DDE_FAIL,
				ERROR_DDE_FAIL,
				ERROR_NO_ASSOCIATION,
				ERROR_MOD_NOT_FOUND
			};

			TCHAR *message;

			if(result >= 26)
				result = errors26[result - 26];

			if(FormatMessage(
				FORMAT_MESSAGE_ALLOCATE_BUFFER |
					FORMAT_MESSAGE_IGNORE_INSERTS |
					FORMAT_MESSAGE_FROM_SYSTEM |
					FORMAT_MESSAGE_MAX_WIDTH_MASK,
				NULL,
				result,
				0,
				(LPTSTR)&message,
				0,
				NULL))
			{
				MessageBox(dlg, message, _title, MB_ICONERROR | MB_OK);
				LocalFree(message);
			}
			else
			{
				TCHAR text[12];
				wsprintfA(text, "%d", result);
				MessageBox(dlg, text, _title, MB_ICONERROR | MB_OK);
			}
		}
#endif
	}
	else
	{
		_just_beep(dlg);
{'fraction_non_alphanumeric': 0.15134099616858238, 'fraction_numerical': 0.02159526297457332, 'mean_word_length': 3.427910562837317, 'pattern_counts': {'":': 2, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5332304', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2341, 'n_words': 519}