C标准库中的strtok

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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#ifdef WIN32
#define EU_STRTOK strtok_s
#define EU_STRDUP _strdup
#else
#define EU_STRTOK strtok_r
#define EU_STRDUP strdup
#endif
 
int main()
{
	char str[1000] = "Steven Jobs, Bruce Lee, Jack Chen, George W Bush";
	char* pNames[100];
	int idxNames = 0;
	char* pWords[100];
	int idxWords = 0;
	char* buf = str;
	char* outPtr = NULL;
	while (NULL != (buf = EU_STRTOK(NULL == outPtr? buf : NULL, ",", &outPtr)))
	{
		char* innerPtr = NULL;
		pNames[idxNames++] = EU_STRDUP(buf);
		while (NULL != (buf = EU_STRTOK(NULL == innerPtr? buf : NULL, " ", &innerPtr)))
		{
			pWords[idxWords++] = buf;
		}
	}
 
	return 0; // here, to watch pNames and pWords contents in debug mode.
}

strtok有一个缺陷,比如对于源串",hello,the,world!",分隔符",",分隔结果会少了空串""。

std版字符串token提取

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
#include <iostream>
#include <locale>
#include <sstream>
 
struct csv_whitespace : std::ctype<wchar_t>
{
	bool do_is(mask m, char_type c) const
	{
		if ((m & space) && c == L' ') {
			return false; // space will NOT be classified as whitespace
		}
		if ((m & space) && c == L',') {
			return true; // comma will be classified as whitespace
		}
		return ctype::do_is(m, c); // leave the rest to the parent class
	}
};
 
int main()
{
	std::wstring in = L"Column 1,  Column 2, Column 3\n123,456,789";
	std::wstring token;
 
	std::wcout << "default locale:\n";
	std::wistringstream s1(in);
	while (s1 >> token) {
		std::wcout << ">" << token << '\n';
	}
 
	std::wcout << "locale with modified ctype:\n";
	std::wistringstream s2(in);
	csv_whitespace* my_ws = new csv_whitespace;
	s2.imbue(std::locale(s2.getloc(), my_ws));
	while (s2 >> token) {
		std::wcout << ">" << token << '\n';
	}
	return 0;
}

std版宽字符转ASCII字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <locale>
 
int main()
{
	std::locale loc;
 
	const char narrow_phrase[] = "Seventy-seven foxes";
	wchar_t wide_phrase[sizeof(narrow_phrase)];
 
	std::wcout << L"The first wide character is: ";
	wchar_t wc = std::use_facet<std::ctype<wchar_t>>(loc).widen(*narrow_phrase);
	std::wcout << wc << std::endl;
 
	std::wcout << L"The wide-character phrase is: ";
	std::use_facet<std::ctype<wchar_t>>(loc).widen(narrow_phrase,
		narrow_phrase + sizeof(narrow_phrase),
		wide_phrase);
	std::wcout << wide_phrase << std::endl;
 
	return 0;
}

std版日期字符串解析

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
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
 
int main()
{
	std::tm t = {};
	std::istringstream ss("2011-Februar-18 23:12:34");
	//ss.imbue(std::locale("de_DE.utf-8"));
	try {
		ss.imbue(std::locale("de-DE"));
	}
	catch (std::exception e) {
		std::cout << e.what() << std::endl;
	}
	ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
	if (ss.fail()) {
		std::cout << "Parse failed\n";
	}
	else {
		std::cout << std::put_time(&t, "%c") << '\n';
	}
	return 0;
}

refer to:
https://en.cppreference.com/w/cpp/locale/locale/name
https://social.msdn.microsoft.com/Forums/en-US/0fb287af-bb58-4c60-a3da-14fe84c16948/stdlocaleglobalstdlocalequotzhcnquot-gets-quotbad-locale-namequot?forum=vcgeneral
https://docs.microsoft.com/en-us/cpp/c-runtime-library/locale-names-languages-and-country-region-strings?redirectedfrom=MSDN&view=msvc-160

Windows驱动从设备名获取dos盘符路径

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
NTSTATUS querySymbolicLink(PUNICODE_STRING symbolicLinkName, PUNICODE_STRING linkTarget)
{
	OBJECT_ATTRIBUTES oa;
	NTSTATUS status;
	HANDLE handle;
 
	InitializeObjectAttributes(&oa, symbolicLinkName, OBJ_CASE_INSENSITIVE, 0, 0);
	status = ZwOpenSymbolicLinkObject(&handle, GENERIC_READ, &oa);
	if (!NT_SUCCESS(status))
		return status;
 
	linkTarget->MaximumLength = 200 * sizeof(WCHAR);
	linkTarget->Length = 0;
	linkTarget->Buffer = ExAllocatePool(PagedPool, linkTarget->MaximumLength);
	if (!linkTarget->Buffer)
	{
		ZwClose(handle);
		return STATUS_INSUFFICIENT_RESOURCES;
	}
	RtlZeroMemory(linkTarget->Buffer, linkTarget->MaximumLength);
 
	status = ZwQuerySymbolicLinkObject(handle, linkTarget, NULL);
	ZwClose(handle);
 
	if (!NT_SUCCESS(status))
	{
		ExFreePool(linkTarget->Buffer);
	}
	return status;
}
 
WCHAR *rtlVolumeDeviceToDosName(WCHAR *deviceName)
{
	NTSTATUS status;
	UNICODE_STRING driveLetterName;
	WCHAR driveLetterNameBuf[128];
	WCHAR c;
	WCHAR driLetter[3];
	UNICODE_STRING linkTarget;
 
	for (c = L'A'; c <= L'Z'; c++)
	{
		RtlInitEmptyUnicodeString(&driveLetterName, driveLetterNameBuf, sizeof(driveLetterNameBuf));
		RtlAppendUnicodeToString(&driveLetterName, L"\\??\\");
		driLetter[0] = c;
		driLetter[1] = L':';
		driLetter[2] = 0;
		RtlAppendUnicodeToString(&driveLetterName, driLetter);
 
		status = querySymbolicLink(&driveLetterName, &linkTarget);
		if (!NT_SUCCESS(status))
			continue;
 
		if (_wcsnicmp(linkTarget.Buffer, deviceName, linkTarget.Length) == 0)
		{
			deviceName += linkTarget.Length - 2;
			deviceName[0] = c;
			deviceName[1] = L":";
			ExFreePool(linkTarget.Buffer);
			break;
		}
		ExFreePool(linkTarget.Buffer);
	}
	return deviceName;
}
 
WCHAR *getDosPath(WCHAR *path)
{
	WCHAR device[] = L"\\Device\\";
 
	if (strIsStartWith(path, device))
	{
		path = rtlVolumeDeviceToDosName(path);
	}
 
	return path;
}

获取HANDLE对应的文件路径

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <Windows.h>
#include <psapi.h>
#include <wchar.h>
 
WCHAR *getFilePathByHandle(HANDLE hFile, WCHAR* filePath, int filePathLen)
{
	DWORD dwFileSizeHi = 0;
	DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
	HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSizeLo, NULL);
 
	filePath[0] = 0;
	if (NULL != hFileMap)
	{
		void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
		if (NULL != pMem)
		{
			if (0 == GetMappedFileNameW(GetCurrentProcess(), pMem, filePath, filePathLen))
			{
				// GetLastError
			}
			UnmapViewOfFile(pMem);
		}
		CloseHandle(hFileMap);
	}
	return filePath;
}
 
wchar_t toLower(wchar_t ch)
{
	if ('A' <= ch && ch <= 'Z')
		ch = ch - 'A' + 'a';
	return ch;
}
 
int isNoCaseBeginWith(wchar_t* l, wchar_t* r)
{
	if (*l == 0 && *r == 0)
		return 1;
	if (*l == 0 || *r == 0)
		return 0;
	for (; 0 != *l && 0 != *r; l++, r++)
	{
		if (toLower(*l) != toLower(*r))
			return 0;
	}
	return 1;
}
 
WCHAR* getDosFilePath(WCHAR *dosPath, int len, WCHAR* kernelPath)
{
	wchar_t path[3] = L"C:";
	wchar_t szBuf[MAX_PATH] = { 0 };
	for (wchar_t ch = L'A'; ch <= L'Z'; ch++)
	{
		path[0] = ch;
		QueryDosDevice(path, szBuf, MAX_PATH);
		if (isNoCaseBeginWith(kernelPath, szBuf))
		{
			swprintf_s(dosPath, len, L"%s%s", path, kernelPath + wcslen(szBuf));
			return dosPath;
		}
	}
	dosPath[0] = 0;
	return dosPath;
}
 
int main()
{
	HANDLE hFile = CreateFileW(L"d:\\test.txt",
		GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL,
		OPEN_EXISTING,
		0x00,
		NULL);
 
	if (INVALID_HANDLE_VALUE == hFile)
	{
		return 0;
	}
 
	WCHAR path[1024];
	getFilePathByHandle(hFile, path, 1024);
 
	WCHAR dosPath[1024];
	getDosFilePath(dosPath, 1024, path);
 
	CloseHandle(hFile);
 
	return 0;
}

南北朝年表

五胡十六国

1
2
3
4
5
6
7
8
9
10
11
后赵/328-352/
	石勒/明帝/高鼻深目/诛杀司马氏五十八位王爷
	石虎/武帝/
冉魏/350-352/
	冉闵/平帝/
前秦/351-394/
	苻坚/宣昭帝/
后秦/384-417/
	姚苌/昭武帝/330-393
后燕/384-407/
	慕容垂/武帝/

北朝

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
北魏/386-534/
	拓跋珪/道武帝/371-409/立太子杀生母
	拓跋嗣/明元帝/392-423/
	拓跋焘/太武帝/408-452/灭佛,崔浩(381-450)
	元宏/孝文帝/467-499/冯太后/494年迁都/兰陵公主
	元诩/孝明帝/510-528
	胡充华/元恪之妃/毒死儿子元诩
	尔朱荣/高鼻深目/河阴之变
	元子攸
	尔朱兆/杀皇帝/喜欢高欢
	元修/独孤信
东魏/534-550/
西魏/535-556/
北齐/550-577/
	高欢/神武帝/496-547/大小尔朱皇后
	高澄/文襄帝/521-549
	高洋/文宣帝/529-559
	高殷/闵悼王/545-561
	高演/孝昭帝/535-561
	高湛/武成帝/537-568
	高纬/556-577/兰陵王
	高延宗/
	高恒/570-577
	高绍义
北周/557-581/

南朝

1
2
3
4
5
6
7
8
9
10
11
刘宋/420-479/
	刘裕/武帝/
	刘义隆/文帝/
萧齐/479-502/
	萧道成/高帝/
	萧宝卷/东昏侯/483-501/Masochism
萧梁/502-557/
	萧衍/武帝/
南陈/557-589/
	陈霸先/武帝/
	陈叔宝/后主/

refer to:
http://www.360doc.com/content/18/1206/17/38656042_799787806.shtml
https://baike.baidu.com/item/%E5%8C%97%E9%AD%8F/914008?fr=aladdin
https://baike.baidu.com/item/%E5%8C%97%E9%BD%90/913858?fr=aladdin
老蔡通史

自定义printf输出格式

glibc版,代码可参考

1
2
3
//libstrongswan/utils/printf_hook/printf_hook_glibc.c
register_printf_specifier
register_printf_function

要注意的是,由于gcc在编译时不认识自定义格式,默认会提示warning。
禁止此提示的方法是在gcc命令参数中加入

1
-Wno-format

vstr from http://www.and.org/vstr/
代码可参考

1
2
//libstrongswan/utils/printf_hook/printf_hook_vstr.c
vstr_fmt_add

在win32下,libstrongswan自已写了一个printf,代码在位置在

1
2
//libstrongswan/utils/printf_hook/printf_hook_builtin.c
builtin_vsnprintf

设置线程中止时的回调函数

pthread版

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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
 
#define WhThreadHandle pthread_t
#define WH_THREAD_DEF(_proc, _arg) void *_proc(void *_arg)
#define whThreadCreate(_handle, _loop, _param) pthread_create(&_handle, NULL, _loop, _param)
 
void cleanup(void *p)
{
	printf("thread killed.\n");
}
 
WH_THREAD_DEF(func1, arg)
{
	int oldState;
	pthread_cleanup_push(cleanup, NULL);
 
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldState);
	while (1)
		sleep(1); // here must call sleep, or else cleanup will not be called.
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
 
	pthread_cleanup_pop(0); // here if want cleanup called, set param to non-zero.
	return NULL;
}
 
WhThreadHandle threadHandle;
 
void onSignal(int sign)
{
    switch (sign)
    {
    case SIGINT:
	pthread_cancel(threadHandle);
        printf("process ctrl+c pressed.\n");
        break;
    }
}
 
int main()
{
	signal(SIGINT, onSignal);
 
	whThreadCreate(threadHandle, func1, 0);
 
	while (1)
		;
	return 1;
}