C++: AHK source code: potential A_ variables

Talk about things C/C++, some related to AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

C++: AHK source code: potential A_ variables

21 Aug 2018, 14:13

- Here are some potential AHK A_ variables written in C++.
- Once inserted into the source code, and compiled, they work just like any other built-in variables.
- I would welcome people to post better more efficient versions of the A_ variable code.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: AHK source code: potential A_ variables

21 Aug 2018, 14:13

Code: Select all

==================================================

for AHK v1: A_ variable names:

A_AhkDir
A_AhkName
A_AhkNameNoExt
A_ChrSize
A_Comma
A_DQ
A_LoopFileAttribValue
A_LoopFileNameNoExt
[LoopFileTimeUTC] A_LoopFileTimeAccessedUTC
[LoopFileTimeUTC] A_LoopFileTimeCreatedUTC
[LoopFileTimeUTC] A_LoopFileTimeModifiedUTC
A_Recent
A_ScriptNameNoExt
A_ScriptPID
A_WA

- note: perhaps A_AhkXXX and A_ScriptXXX should both refer to the path of the compiled exe, when applied to compiled scripts (for use with #Include)
- note: perhaps there should be an option for where A_AhkXXX points to when the script is compiled
- note: if 'global A_XXX' were allowed in scripts, then new variables could be used without problems in all past, present and future scripts
- note: perhaps A_ScriptPID could be retrieved once and stored in a permanent variable

- note: I since thought of a new idea for handling A_LoopFileTimeXXXUTC, all 6 variables could point to the same function, and if the number of characters is 24+, use UTC
22	A_LoopFileTimeModified
22	A_LoopFileTimeAccessed
21	A_LoopFileTimeCreated
25	A_LoopFileTimeModifiedUTC
25	A_LoopFileTimeAccessedUTC
24	A_LoopFileTimeCreatedUTC
- note: I since realised that GetCurrentProcessId can be used for A_ScriptPID

==================================================

[script.h]

BIV_DECL_R (BIV_AhkDir);
BIV_DECL_R (BIV_AhkName);
BIV_DECL_R (BIV_AhkNameNoExt);
BIV_DECL_R (BIV_ChrSize);
BIV_DECL_R (BIV_Comma);
BIV_DECL_R (BIV_DQ);
BIV_DECL_R (BIV_LoopFileAttribValue);
BIV_DECL_R (BIV_LoopFileNameNoExt);
BIV_DECL_R (BIV_LoopFileTimeUTC);
BIV_DECL_R (BIV_Recent);
BIV_DECL_R (BIV_ScriptNameNoExt);
BIV_DECL_R (BIV_ScriptPID);
BIV_DECL_R (BIV_WA);

==================================================

[script.cpp]

	A_(AhkDir),
	A_(AhkName),
	A_(AhkNameNoExt),
	A_(ChrSize),
	A_(Comma),
	A_(DQ),
	A_(LoopFileAttribValue),
	A_(LoopFileNameNoExt),
	A_x(LoopFileTimeAccessedUTC, BIV_LoopFileTimeUTC),
	A_x(LoopFileTimeCreatedUTC, BIV_LoopFileTimeUTC),
	A_x(LoopFileTimeModifiedUTC, BIV_LoopFileTimeUTC),
	A_(Recent),
	A_(ScriptNameNoExt),
	A_(ScriptPID),
	A_(WA),

==================================================

[script2.cpp]

VarSizeType BIV_AhkDir(LPTSTR aBuf, LPTSTR aVarName)
{
	LPTSTR naked_filename;
	TCHAR buf[MAX_PATH];
	VarSizeType length = (VarSizeType)GetModuleFileName(NULL, buf, MAX_PATH);
    if (naked_filename = _tcsrchr(buf, '\\'))
        *naked_filename = '\0';
	if (aBuf)
		_tcscpy(aBuf, buf); // v1.0.47: Must be done as a separate copy because passing a size of MAX_PATH for aBuf can crash when aBuf is actually smaller than that (even though it's large enough to hold the string). This is true for ReadRegString()'s API call and may be true for other API calls like this one.
	return length;
}

VarSizeType BIV_AhkName(LPTSTR aBuf, LPTSTR aVarName)
{
	LPTSTR naked_filename;
	TCHAR buf[MAX_PATH];
	VarSizeType length = (VarSizeType)GetModuleFileName(NULL, buf, MAX_PATH);
    if (naked_filename = _tcsrchr(buf, '\\'))
        ++naked_filename;
	if (aBuf)
		_tcscpy(aBuf, naked_filename); // v1.0.47: Must be done as a separate copy because passing a size of MAX_PATH for aBuf can crash when aBuf is actually smaller than that (even though it's large enough to hold the string). This is true for ReadRegString()'s API call and may be true for other API calls like this one.
	return length;
}

VarSizeType BIV_AhkNameNoExt(LPTSTR aBuf, LPTSTR aVarName)
{
	LPTSTR naked_filename;
	LPTSTR file_ext;
	TCHAR buf[MAX_PATH];
	VarSizeType length = (VarSizeType)GetModuleFileName(NULL, buf, MAX_PATH);
    if (naked_filename = _tcsrchr(buf, '\\'))
        ++naked_filename;
	if (file_ext = _tcschr(naked_filename, '.')) // v1.0.48.01: Disqualify periods found in the path instead of the filename; e.g. path.name\FileWithNoExtension.
		*file_ext = '\0';
	if (aBuf)
		_tcscpy(aBuf, naked_filename); // v1.0.47: Must be done as a separate copy because passing a size of MAX_PATH for aBuf can crash when aBuf is actually smaller than that (even though it's large enough to hold the string). This is true for ReadRegString()'s API call and may be true for other API calls like this one.
	return length;
}

VarSizeType BIV_ChrSize(LPTSTR aBuf, LPTSTR aVarName)
{
#ifdef UNICODE
	if (aBuf)
	{
		*aBuf++ = '2';
		*aBuf = '\0';
	}
	return 1;
#else
	if (aBuf)
	{
		*aBuf++ = '1';
		*aBuf = '\0';
	}
	return 1;
#endif
}

VarSizeType BIV_Comma(LPTSTR aBuf, LPTSTR aVarName)
{
	if (aBuf)
	{
		*aBuf++ = ',';
		*aBuf = '\0';
	}
	return 1;
}

VarSizeType BIV_DQ(LPTSTR aBuf, LPTSTR aVarName)
{
	if (aBuf)
	{
		*aBuf++ = '\"';
		*aBuf = '\0';
	}
	return 1;
}

VarSizeType BIV_LoopFileAttribValue(LPTSTR aBuf, LPTSTR aVarName)
{
	TCHAR buf[64];
	LPTSTR target_buf = aBuf ? aBuf : buf;
	*target_buf = '\0'; // Set default.
	if (g->mLoopFile)
		ITOA(g->mLoopFile->dwFileAttributes, target_buf);
	return (VarSizeType)_tcslen(target_buf);
}

VarSizeType BIV_LoopFileNameNoExt(LPTSTR aBuf, LPTSTR aVarName)
{
	LPTSTR naked_filename;
	LPTSTR file_ext;
	if (g->mLoopFile)
	{
		// The loop handler already prepended the script's directory in here for us:
		if (naked_filename = _tcsrchr(g->mLoopFile->cFileName, '\\'))
			++naked_filename;
		else // No backslash, so just make it the entire file name.
			naked_filename = g->mLoopFile->cFileName;
	}
	else
		naked_filename = _T("");
	if (file_ext = _tcschr(naked_filename, '.')) // v1.0.48.01: Disqualify periods found in the path instead of the filename; e.g. path.name\FileWithNoExtension.
		*file_ext = '\0';
	if (aBuf)
		_tcscpy(aBuf, naked_filename);
	return (VarSizeType)_tcslen(naked_filename);
}

VarSizeType BIV_LoopFileTimeUTC(LPTSTR aBuf, LPTSTR aVarName)
{
	TCHAR buf[64];
	LPTSTR target_buf = aBuf ? aBuf : buf;
	*target_buf = '\0'; // Set default.
	if (g->mLoopFile)
	{
		FILETIME ft;
		switch(ctoupper(aVarName[14])) // A_LoopFileTime[A]ccessed
		{
		case 'M': ft = g->mLoopFile->ftLastWriteTime; break;
		case 'C': ft = g->mLoopFile->ftCreationTime; break;
		default: ft = g->mLoopFile->ftLastAccessTime;
		}
		FileTimeToYYYYMMDD(target_buf, ft, false);
	}
	return (VarSizeType)_tcslen(target_buf);
}

VarSizeType BIV_Recent(LPTSTR aBuf, LPTSTR aVarName)
{
	TCHAR buf[MAX_PATH];
	if (SHGetFolderPath(NULL, CSIDL_RECENT, NULL, SHGFP_TYPE_CURRENT, buf) != S_OK)
		*buf = '\0';
	VarSizeType length = (VarSizeType)strip_trailing_backslash(buf);
	if (aBuf)
		_tcscpy(aBuf, buf); // v1.0.47: Must be done as a separate copy because passing a size of MAX_PATH for aBuf can crash when aBuf is actually smaller than that (even though it's large enough to hold the string).
	return length;
}

VarSizeType BIV_ScriptNameNoExt(LPTSTR aBuf, LPTSTR aVarName)
{
	if (aBuf)
	{
		_tcscpy(aBuf, g_script.mFileName);
        LPTSTR file_ext;
        if (file_ext = _tcschr(aBuf, '.')) // v1.0.48.01: Disqualify periods found in the path instead of the filename; e.g. path.name\FileWithNoExtension.
            *file_ext = '\0';
    }
    return (VarSizeType)_tcslen(g_script.mFileName);
}

VarSizeType BIV_ScriptPID(LPTSTR aBuf, LPTSTR aVarName)
{
    TCHAR buf[64];
    LPTSTR target_buf = aBuf ? aBuf : buf;
    *target_buf = '\0'; // Set default.
    if (aBuf && g_hWnd)
    {
        DWORD pid;
        GetWindowThreadProcessId(g_hWnd, &pid);
        ITOA(pid, target_buf);
        return (VarSizeType)_tcslen(target_buf);
    }
    return MAX_INTEGER_LENGTH;
}

VarSizeType BIV_WA(LPTSTR aBuf, LPTSTR aVarName)
{
#ifdef UNICODE
	if (aBuf)
	{
		*aBuf++ = 'W';
		*aBuf = '\0';
	}
	return 1;
#else
	if (aBuf)
	{
		*aBuf++ = 'A';
		*aBuf = '\0';
	}
	return 1;
#endif
}

==================================================
See also:
[A_CParen, A_OParen, A_SQ]
C++: AHK source code: demo A_ variables - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=75&t=54677
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 8 guests