0
0
mirror of https://codeberg.org/crimeflare/cloudflare-tor synced 2024-11-05 00:42:45 +00:00
This commit is contained in:
u 2020-11-11 03:32:05 +01:00
commit 359887b8af
23 changed files with 2784 additions and 675 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
### PGListUtil
This tool is useful to convert `CIDR list` to `Range format`, or remove unnecessary lines (such as duplicated ranges) automatically.
The software was released in the [year 2010](https://live28.5ch.net/test/read.cgi/download/1268903774/10).
Original author said: `You are free to modify or redistribute the source code`

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,10 @@
### Original files
- [PGListUtil.exe](https://codeberg.org/crimeflare/cloudflare-tor/raw/branch/master/tool/PGListUtil/releases/PGListUtil.exe)
- [readme.txt](readme.txt)
### English
- [PGListUtil_en.exe](https://codeberg.org/crimeflare/cloudflare-tor/raw/branch/master/tool/PGListUtil/releases/PGListUtil_en.exe)

View File

@ -0,0 +1,138 @@
PeerGuardian向けに作られたテキスト形式のリストファイルを
条件に合わせて新しいファイルに出力することができます
複数のリストファイルをひとつにまとめて出力することもできます
===================================================================================
コマンドライン引数にファイルパスを指定して起動すると、自動的に入力ファイルパスに追加できます
複数のパス指定に対応しています
コマンドに関しては「送る(SendTo)」にショートカットを作成して使用することを想定しています
(お手数ですが手動で作成してください)
リストファイルで1行のサイズが1024バイトを超えると正確な構文解析ができません
設定はどこにも保存しないのでアンインストールする場合はそのまま削除で問題ありません
ソースコードの改変や転載等は自由に行ってくれても構いません
当ソフトの使用はあくまで自己責任でお願いします
動作環境は(確認していませんが)Windows 2000以降のWindows NT系OS
動作確認はWindodws XP SP3でのみ行っています
===================================================================================
主な機能
・キャプションフィルター
大文字と小文字は区別しません
除外にチェックを入れると、一致したものを除外して出力するようになります
言い方を変えれば一致しないものを出力するようになります
何も入力しなければこの機能は無効となります
・IPの重複を解消
デフォルトで開始IPと終了IPが一致するものを出力しません
test1:1.2.3.4-5.6.7.8
test2:1.2.3.4-5.6.7.8 → test1とtest2のどちらかは出力しません
間に含まれるものは重複の対象外として普通に出力します
test1:1.2.3.4-5.6.7.100
test2:1.2.3.4-5.6.7.10 → 事実上重複ですがtest2は出力されます
・ソート機能
キャプションとIPアドレスでリストをソートすることができます
この二つのソートは併用するとキャプション優勢のソートになります
仕様上、重複IPを解消する場合はIPアドレスでソートを有効にしたほうが使用メモリを少なく抑えられます
・保存モード
「上書き」ファイルが存在する場合、以前のファイルの内容は破棄されます
「追記」 ファイルが存在する場合、以前のファイルの終端に追記されます
いずれもファイルが存在しない場合は新しいファイルが作成されます
・チェック機能
以下のようなありがちなミスをチェックします
構文エラー
キャプションの最後に : が無い
IPアドレスに4桁以上の数字
. と , の間違い
IPアドレス間の - が無い
マスク表記されたIP(例:0.0.0.0/24)
行末の文字
不正なIPアドレス
IPアドレスに256以上の数字
開始IPが終了IPより大きい
・自動修復
チェック時に「構文エラー (自動修復可)」と出たものは、ファイル出力時に自動的に修復、展開されます
:抜け、.と,の間違い、マスク表記されたIPなど
これらに当てはまっても数値が不正であればエラーになります
他は概ねウィンドウに書かれている通りです
===================================================================================
更新履歴
ver0.7.0
自動修復機能を追加
実行メニューに「チェック後、ファイル出力」を追加 (エラーが見つかったらファイル出力を行わない)
ファイル出力時のIPアドレスのチェックを省略 (問答無用で出力するため「チェック後、ファイル出力」を推奨)
その他細かい調整
ver0.6.1
視覚スタイルを適用
入力ファイル追加ダイアログで、ドライブ直下より深いディレクトリでファイルを複数選択した場合、リストに正しく追加できなかった不具合を修正
ver0.6.0
GUIの見直し
リソースエディタをResEditに変更 (コンパイラはWindows SDK 6以降で変更なし)
(0.5.0に含まれるresファイルはVC2002で組んだものをWindows SDK 6でコンパイルしたもの)
DLL化していた機能とチェック機能をUtilに統合し、CUI版チェッカーの配布を停止
重複IP解消の可否を設定できるようにした
入力ファイルリストの選択アイテムをDeleteキーで削除できるようにした
その他細かい調整
ver0.5.0
名前をPG2ListUtilからPGListUtilに変更
Windowsネイティブに移行 (.NET Frameworkは不要になりました)
機能の一部をDLLに移行
保存モード「保存しない」を削除
構文エラーを吐かないようにした (チェックはCUI版のチェッカーを使用してください)
キャプションフィルターをANDとORに分けた (今後同時に使用できるようにする可能性もあります)
キャプションフィルターの正規表現を削除 (今後実装する可能性もあります)
AND条件の仕様を変更 (文字列が重なる部分も一致するようにした)
ファイルのドラッグ&ドロップを入力ファイルと出力ファイルに分けた

View File

@ -0,0 +1,9 @@
#include <windows.h>
#include "WindowMain.h"
#include "resource.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
WindowMain::Init(hInstance);
DialogBox(hInstance, (LPTSTR)IDD_MAIN, NULL, (DLGPROC)(WindowMain::DlgProcMain));
return 0;
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="*.*.PGListUtil"
type="win32"
/>
<description>Description</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>

View File

@ -0,0 +1,6 @@
#ifndef PGLISTUTIL_H
#define PGLISTUTIL_H
extern HINSTANCE hInst;
#endif // PGLISTUTIL_H

View File

@ -0,0 +1,191 @@
#include "WindowMain.h"
#include "resource.h"
#include "../common/CErrorList.h"
#include "../common/CIpList.h"
namespace ThreadExecute {
namespace {
TCHAR g_tsTemp[STRLEN_TEMP];
TCHAR g_tsPath[STRLEN_PATH];
TCHAR g_tsFailedFileRead[STRLEN_RESULT];
TCHAR g_tsFailedFileWrite[STRLEN_RESULT];
TCHAR g_tsOutputComplete[STRLEN_RESULT];
TCHAR g_tsCheckComplete[STRLEN_RESULT];
TCHAR g_tsFoundError[STRLEN_RESULT];
TCHAR g_tsNotFoundError[STRLEN_RESULT];
TCHAR g_tsErrorSyntax[STRLEN_RESULT];
TCHAR g_tsErrorIp[STRLEN_RESULT];
TCHAR g_tsErrorSyntaxRestorable[STRLEN_RESULT];
CHAR *GetMBSfromWS(WCHAR *ws) {
int i = WideCharToMultiByte(CP_THREAD_ACP, 0, ws, -1, NULL, 0, NULL, NULL) + 1;
if(0 < i) {
CHAR *mbs = new CHAR[i];
if(!WideCharToMultiByte(CP_THREAD_ACP, 0, ws, -1, mbs, i, NULL, NULL)) {
delete[] mbs;
mbs = NULL;
}
return mbs;
} else {
return NULL;
}
}
char *ReleaseMBS(char *mbs) {
if(mbs) {
delete[] mbs;
mbs = NULL;
}
return mbs;
}
bool ExecCheckInfile(HWND hDlg) {
char *mbsPath;
int incomp = 0;
pglu::error::CErrorList errlist;
WindowMain::Edit_Result_SetText(hDlg, NULL);
int iInfileCount = WindowMain::ListView_Infile_GetItemCount(hDlg);
for(int iCountInfile = 0; iCountInfile < iInfileCount; ++iCountInfile) {
WindowMain::ListView_Infile_GetItemText(hDlg, iCountInfile, g_tsPath);
WindowMain::Edit_Result_AppendText(hDlg, g_tsPath);
mbsPath = GetMBSfromWS(g_tsPath);
if(mbsPath && errlist.LoadListFile(mbsPath)) {
int iErrCount = errlist.Count();
if(0 < iErrCount) {
WindowMain::Edit_Result_AppendText(hDlg, g_tsFoundError);
lstrcpy(g_tsTemp, TEXT("\r\n"));
for(pglu::error::CError * err = errlist.GetNext(); err; err = errlist.GetNext()) {
switch(err->kind) {
case pglu::error::SYNTAX:
wsprintf(g_tsTemp + 2, g_tsErrorSyntax, err->line);
++incomp;
break;
case pglu::error::IP:
wsprintf(g_tsTemp + 2, g_tsErrorIp, err->line);
++incomp;
break;
case pglu::error::SYNTAX_RESTORABLE:
wsprintf(g_tsTemp + 2, g_tsErrorSyntaxRestorable, err->line);
break;
}
WindowMain::Edit_Result_AppendText(hDlg, g_tsTemp);
}
} else {
WindowMain::Edit_Result_AppendText(hDlg, g_tsNotFoundError);
}
mbsPath = ReleaseMBS(mbsPath);
} else {
WindowMain::Edit_Result_AppendText(hDlg, g_tsFailedFileRead);
++incomp;
}
WindowMain::Edit_Result_AppendText(hDlg, TEXT("\r\n\r\n"));
errlist.Clear();
}
return (incomp == 0);
}
bool ExecOutput(HWND hDlg) {
bool succ = false;
char *mbsPath;
WindowMain::COption option;
pglu::ip::CIpList iplist;
// 設定取得
WindowMain::GetOption(hDlg, &option);
// フィルタ設定
WindowMain::Edit_Filter_GetText(hDlg, g_tsTemp);
if(0 < lstrlen(g_tsTemp)) {
char *mbsFilter = GetMBSfromWS(g_tsTemp);
if(mbsFilter) {
pglu::filter::EFilterMode filtMode = (option.filtAnd ? pglu::filter::AND : pglu::filter::OR);
iplist.SetFilter(mbsFilter, filtMode, option.filtDel);
mbsFilter = ReleaseMBS(mbsFilter);
}
}
// 読み込み
int iInfileCount = WindowMain::ListView_Infile_GetItemCount(hDlg);
for(int iCountInfile = 0; iCountInfile < iInfileCount; ++iCountInfile) {
WindowMain::ListView_Infile_GetItemText(hDlg, iCountInfile, g_tsPath);
mbsPath = GetMBSfromWS(g_tsPath);
if(mbsPath) {
succ = iplist.LoadListFile(mbsPath);
mbsPath = ReleaseMBS(mbsPath);
if(!succ) {
// 読み込み失敗
WindowMain::Edit_Result_SetText(hDlg, g_tsPath);
WindowMain::Edit_Result_SetText(hDlg, g_tsFailedFileRead);
return false;
}
}
}
// チェック ソート
iplist.CheckAndSort(option.sortCap, option.sortIp, option.delDupIp);
// 書き出し
WindowMain::Edit_Outfile_GetText(hDlg, g_tsPath);
mbsPath = GetMBSfromWS(g_tsPath);
if(mbsPath) {
succ = iplist.SaveListFile(mbsPath, option.saveAppend);
mbsPath = ReleaseMBS(mbsPath);
if(!succ) {
// 書き込み失敗
WindowMain::Edit_Result_SetText(hDlg, g_tsPath);
WindowMain::Edit_Result_SetText(hDlg, g_tsFailedFileWrite);
return false;
}
}
// 実行結果
int iIpCount = iplist.Count();
int iIpCountDisabled = iplist.CountDisabled();
wsprintf(g_tsTemp, g_tsOutputComplete, iIpCount, iIpCountDisabled, iIpCount - iIpCountDisabled);
WindowMain::Edit_Result_SetText(hDlg, g_tsTemp);
return true;
}
} // namespace
BOOL Init(HINSTANCE hInstance) {
LoadString(hInstance, IDS_FAILED_FILE_READ, g_tsFailedFileRead, STRLEN_RESULT);
LoadString(hInstance, IDS_FAILED_FILE_WRITE, g_tsFailedFileWrite, STRLEN_RESULT);
LoadString(hInstance, IDS_OUTPUT_COMPLETE, g_tsOutputComplete, STRLEN_RESULT);
LoadString(hInstance, IDS_CHECK_COMPLETE, g_tsCheckComplete, STRLEN_RESULT);
LoadString(hInstance, IDS_CHECK_FOUND_ERROR, g_tsFoundError, STRLEN_RESULT);
LoadString(hInstance, IDS_CHECK_NOTFOUND_ERROR, g_tsNotFoundError, STRLEN_RESULT);
LoadString(hInstance, IDS_CHECK_ERROR_SYNTAX, g_tsErrorSyntax, STRLEN_RESULT);
LoadString(hInstance, IDS_CHECK_ERROR_IP, g_tsErrorIp, STRLEN_RESULT);
LoadString(hInstance, IDS_CHECK_ERROR_SYNTAX_RESTORABLE, g_tsErrorSyntaxRestorable, STRLEN_RESULT);
return TRUE;
}
DWORD WINAPI ThreadExecCheckInfile(LPVOID lpParam) {
HWND hDlg = (HWND)lpParam;
ExecCheckInfile(hDlg);
WindowMain::Dlg_Executing(hDlg, FALSE);
ExitThread(TRUE);
}
DWORD WINAPI ThreadExecOutput(LPVOID lpParam) {
HWND hDlg = (HWND)lpParam;
ExecOutput(hDlg);
WindowMain::Dlg_Executing(hDlg, FALSE);
ExitThread(TRUE);
}
DWORD WINAPI ThreadExecCheckAndOutput(LPVOID lpParam) {
HWND hDlg = (HWND)lpParam;
if(ExecCheckInfile(hDlg))
ExecOutput(hDlg);
WindowMain::Dlg_Executing(hDlg, FALSE);
ExitThread(TRUE);
}
} // namespace ThreadExecute

View File

@ -0,0 +1,16 @@
#ifndef THREADEXECUTE_H
#define THREADEXECUTE_H
#include <windows.h>
namespace ThreadExecute {
BOOL Init(HINSTANCE hInstance);
DWORD WINAPI ThreadExecCheckInfile(LPVOID);
DWORD WINAPI ThreadExecOutput(LPVOID);
DWORD WINAPI ThreadExecCheckAndOutput(LPVOID);
}
#endif // THREADEXECUTE_H

View File

@ -0,0 +1,440 @@
#include "stdafx.h"
#include "WindowMain.h"
#include "resource.h"
#include "ThreadExecute.h"
#define WM_USER_EXECUTING WM_USER
namespace WindowMain {
namespace {
TCHAR g_tsTempA[STRLEN_TEMP];
TCHAR g_tsTempB[STRLEN_TEMP];
HINSTANCE g_hInst;
OPENFILENAME g_ofn = { 0 };
TCHAR g_tsExpFilt[STRLEN_EXPFILT];
TCHAR g_tsPath[STRLEN_PATH];
MENUITEMINFO g_mii = { 0 };
TCHAR g_tsSetInfile[STRLEN_RESULT];
TCHAR g_tsSetOutfile[STRLEN_RESULT];
TCHAR g_tsExecuting[STRLEN_RESULT];
WNDPROC WndProcListInfileDef;
WNDPROC WndProcEditOutfileDef;
//--------------------------------------
// Function
//--------------------------------------
void Menu_SwapIfChecked(HMENU hMenuMain, int nItemSrc, int nItemCmp) {
GetMenuItemInfo(hMenuMain, nItemCmp, FALSE, &g_mii);
if(g_mii.fState & MFS_CHECKED) {
g_mii.fState = MFS_CHECKED;
SetMenuItemInfo(hMenuMain, nItemSrc, FALSE, &g_mii);
g_mii.fState = MFS_UNCHECKED;
SetMenuItemInfo(hMenuMain, nItemCmp, FALSE, &g_mii);
}
}
void ListView_Infile_AddItem(HWND hListInfile, LPTSTR tsPath, LPTSTR tsFile) {
LVITEM lvItem = { 0 };
lvItem.mask = LVIF_TEXT;
lvItem.iSubItem = 0;
lvItem.pszText = tsPath;
lvItem.iItem = ListView_InsertItem(hListInfile, &lvItem);
lvItem.iSubItem = 1;
lvItem.pszText = tsFile;
ListView_SetItem(hListInfile, &lvItem);
}
void ListView_Infile_AddItem(HWND hListInfile, LPTSTR tsPath) {
LPTSTR tsFile = tsPath;
for(LPTSTR tsP = tsFile; *tsP != TEXT('\0'); ++tsP)
if(*tsP == TEXT('\\'))
tsFile = tsP;
ListView_Infile_AddItem(hListInfile, tsPath, tsFile + 1);
}
void ListView_Infile_DelSelItem(HWND hListInfile) {
int i = -1;
int j = -1;
while((i = ListView_GetNextItem(hListInfile, i, LVNI_SELECTED)) != -1) {
j = i;
ListView_DeleteItem(hListInfile, i--);
}
if(j != -1) {
if(0 < (i = ListView_GetItemCount(hListInfile))) {
if(j < i) {
ListView_SetItemState(hListInfile, j, LVIS_SELECTED, LVIS_SELECTED);
} else {
ListView_SetItemState(hListInfile, i - 1, LVIS_SELECTED, LVIS_SELECTED);
}
}
}
}
void ListView_Infile_DelDup(HWND hListInfile) {
int i = ListView_GetItemCount(hListInfile);
if(1 < i) {
for(--i; 0 < i; --i) {
ListView_GetItemText(hListInfile, i, 0, g_tsTempA, STRLEN_TEMP);
ListView_GetItemText(hListInfile, i - 1, 0, g_tsTempB, STRLEN_TEMP);
if(lstrcmpi(g_tsTempA, g_tsTempB) == 0)
ListView_DeleteItem(hListInfile, i);
}
}
}
BOOL IsEmptyInfile(HWND hListInfile, HWND hEditResult) {
if(ListView_GetItemCount(hListInfile) < 1) {
SetWindowText(hEditResult, g_tsSetInfile);
return TRUE;
}
return FALSE;
}
BOOL IsEmptyOutfile(HWND hEditOutfile, HWND hEditResult) {
if(GetWindowTextLength(hEditOutfile) < 1) {
SetWindowText(hEditResult, g_tsSetOutfile);
return TRUE;
}
return FALSE;
}
//--------------------------------------
// Sub Window Procedure
//--------------------------------------
LRESULT WndProcListInfileSub(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
int i, j;
switch(msg) {
case WM_KEYDOWN: {
if(wp == VK_DELETE)
ListView_Infile_DelSelItem(hWnd);
} break;
case WM_DROPFILES: {
j = DragQueryFile((HDROP)wp, -1, NULL, 0);
for(i = 0; i < j; ++i) {
DragQueryFile((HDROP)wp, i, g_tsTempA, STRLEN_TEMP);
ListView_Infile_AddItem(hWnd, g_tsTempA);
}
ListView_Infile_DelDup(hWnd);
} break;
default:
return CallWindowProc(WndProcListInfileDef, hWnd, msg, wp, lp);
}
return 0;
}
LRESULT WndProcEditOutfileSub(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
switch(msg) {
case WM_DROPFILES: {
DragQueryFile((HDROP)wp, 0, g_tsTempA, STRLEN_TEMP);
SetWindowText(hWnd, g_tsTempA);
} break;
default:
return CallWindowProc(WndProcEditOutfileDef, hWnd, msg, wp, lp);
}
return 0;
}
} // namespace
//--------------------------------------
// WindowMain
//--------------------------------------
BOOL Init(HINSTANCE hInstance) {
InitCommonControls();
g_hInst = hInstance;
// 文字列読み込み
LoadString(hInstance, IDS_SET_INFILE, g_tsSetInfile, STRLEN_RESULT);
LoadString(hInstance, IDS_SET_OUTFILE, g_tsSetOutfile, STRLEN_RESULT);
LoadString(hInstance, IDS_EXECUTING, g_tsExecuting, STRLEN_RESULT);
LoadString(hInstance, IDS_EXPFILT_PGLIST, g_tsExpFilt, STRLEN_EXPFILT);
// g_tsExpFiltは@をnull文字に変換
for(TCHAR *p = g_tsExpFilt; *p != TEXT('\0'); ++p)
if(*p == TEXT('@'))
*p = TEXT('\0');
// MENUITEMINFO設定
g_mii.cbSize = sizeof(g_mii);
g_mii.fMask = MIIM_STATE;
// OPENFILENAME設定
g_ofn.lStructSize = sizeof(g_ofn);
g_ofn.lpstrFilter = g_tsExpFilt;
g_ofn.nFilterIndex = 0;
g_ofn.lpstrFile = g_tsPath;
g_ofn.nMaxFile = STRLEN_PATH;
// 実行スレッド初期化
ThreadExecute::Init(hInstance);
return TRUE;
}
void Dlg_Executing(HWND hDlg, BOOL bExec) {
SendMessage(hDlg, WM_USER_EXECUTING, (WPARAM)bExec, 0);
}
int ListView_Infile_GetItemCount(HWND hDlg) {
return ListView_GetItemCount(GetDlgItem(hDlg, IDC_LISTVIEW_INFILE));
}
BOOL ListView_Infile_GetItemText(HWND hDlg, int nItem, LPTSTR tsPath) {
ListView_GetItemText(GetDlgItem(hDlg, IDC_LISTVIEW_INFILE), nItem, 0, tsPath, STRLEN_PATH);
return TRUE;
}
BOOL Edit_Outfile_GetText(HWND hDlg, LPTSTR tsPath) {
return GetDlgItemText(hDlg, IDC_EDIT_OUTFILE, tsPath, STRLEN_PATH);
}
BOOL Edit_Filter_GetText(HWND hDlg, LPTSTR tsText) {
return GetDlgItemText(hDlg, IDC_EDIT_FILTER, tsText, STRLEN_PATH);
}
void Edit_Result_SetText(HWND hDlg, LPCTSTR tsText) {
SetDlgItemText(hDlg, IDC_EDIT_RESULT, tsText);
}
BOOL GetOption(HWND hDlg, COption *option) {
HMENU hMenuMain = GetMenu(hDlg);
GetMenuItemInfo(hMenuMain, IDM_SORT_CAPTION, FALSE, &g_mii);
option->sortCap = ((g_mii.fState & MFS_CHECKED) != 0);
GetMenuItemInfo(hMenuMain, IDM_SORT_IP, FALSE, &g_mii);
option->sortIp = ((g_mii.fState & MFS_CHECKED) != 0);
GetMenuItemInfo(hMenuMain, IDM_CHECK_DUP_IP, FALSE, &g_mii);
option->delDupIp = ((g_mii.fState & MFS_CHECKED) != 0);
GetMenuItemInfo(hMenuMain, IDM_SAVE_APPEND, FALSE, &g_mii);
option->saveAppend = ((g_mii.fState & MFS_CHECKED) != 0);
option->filtAnd = (SendDlgItemMessage(hDlg, IDC_RADIO_FILTER_AND, BM_GETCHECK, 0, 0) == BST_CHECKED);
option->filtDel = (SendDlgItemMessage(hDlg, IDC_CHECK_FILTER_DEL, BM_GETCHECK, 0, 0) == BST_CHECKED);
return TRUE;
}
void Edit_Result_AppendText(HWND hDlg, LPCTSTR tsText) {
HWND hEditResult = GetDlgItem(hDlg, IDC_EDIT_RESULT);
int len = GetWindowTextLength(hEditResult);
SendMessage(hEditResult, EM_SETSEL, (WPARAM)len, (LPARAM)len);
SendMessage(hEditResult, EM_REPLACESEL, 0, (LPARAM)tsText);
}
BOOL CALLBACK DlgProcMain(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
static HMENU hMenuMain;
static HWND hListInfile;
static HWND hEditOutfile;
static HWND hEditFilter;
static HWND hEditResult;
static DWORD dwThreadId;
int i, j;
switch(msg) {
case WM_CLOSE: {
EndDialog(hWnd, 0);
} break;
case WM_INITDIALOG: {
HDC hDC;
RECT rect;
LVCOLUMN lvCol;
// 各ハンドル格納
hMenuMain = GetMenu(hWnd);
hListInfile = GetDlgItem(hWnd, IDC_LISTVIEW_INFILE);
hEditOutfile = GetDlgItem(hWnd, IDC_EDIT_OUTFILE);
hEditFilter = GetDlgItem(hWnd, IDC_EDIT_FILTER);
hEditResult = GetDlgItem(hWnd, IDC_EDIT_RESULT);
// ウィンドウ位置設定
hDC = GetDC(hWnd);
GetWindowRect(hWnd, &rect);
SetWindowPos(hWnd, HWND_TOP,
(GetDeviceCaps(hDC, HORZRES) - (rect.right - rect.left)) / 2,
(GetDeviceCaps(hDC, VERTRES) - (rect.bottom - rect.top)) / 2,
0, 0, SWP_NOSIZE);
ReleaseDC(hWnd, hDC);
// リストビュー設定
ListView_SetExtendedListViewStyle(hListInfile,
LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_LABELTIP);//LVS_EX_INFOTIP);
int nWidthScroll = GetSystemMetrics(SM_CXVSCROLL);
int nWidth3DEdge = GetSystemMetrics(SM_CXEDGE);
GetWindowRect(hListInfile, &rect);
lvCol.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvCol.fmt = LVCFMT_LEFT;
lvCol.cx = 72;
lvCol.iSubItem = 0;
LoadString(g_hInst, IDS_FILE_PATH, g_tsTempA, STRLEN_TEMP);
lvCol.pszText = g_tsTempA;
ListView_InsertColumn(hListInfile, 0, &lvCol);
lvCol.cx = (rect.right - rect.left) - 72 - nWidthScroll - nWidth3DEdge * 2;
lvCol.iSubItem = 1;
LoadString(g_hInst, IDS_FILE_NAME, g_tsTempA, STRLEN_TEMP);
lvCol.pszText = g_tsTempA;
ListView_InsertColumn(hListInfile, 1, &lvCol);
// チェック状態
SendDlgItemMessage(hWnd, IDC_RADIO_FILTER_AND, BM_SETCHECK, BST_CHECKED, 0);
#ifdef UNICODE
// コマンドライン引数からリストへ追加
LPWSTR *wsCmds = CommandLineToArgvW(GetCommandLineW(), &j);
for(i = 1; i < j; ++i)
ListView_Infile_AddItem(hListInfile, wsCmds[i]);
LocalFree((HLOCAL)wsCmds);
ListView_Infile_DelDup(hListInfile);
#endif
// サブクラス
WndProcListInfileDef = (WNDPROC)GetWindowLong(hListInfile, GWL_WNDPROC);
SetWindowLong(hListInfile, GWL_WNDPROC, (LONG)WndProcListInfileSub);
WndProcEditOutfileDef = (WNDPROC)GetWindowLong(hEditOutfile, GWL_WNDPROC);
SetWindowLong(hEditOutfile, GWL_WNDPROC, (LONG)WndProcEditOutfileSub);
} break;
case WM_COMMAND: {
switch(LOWORD(wp)) {
case IDM_EXIT: {
EndDialog(hWnd, IDOK);
//PostMessage(hWnd, WM_CLOSE, 0, 0);
} break;
case IDM_SORT_CAPTION:
case IDM_SORT_IP:
case IDM_CHECK_DUP_IP: {
GetMenuItemInfo(hMenuMain, LOWORD(wp), FALSE, &g_mii);
g_mii.fState = ((g_mii.fState & MFS_CHECKED) ? MFS_UNCHECKED : MFS_CHECKED);
SetMenuItemInfo(hMenuMain, LOWORD(wp), FALSE, &g_mii);
} break;
case IDM_SAVE_REPLACE: {
Menu_SwapIfChecked(hMenuMain, IDM_SAVE_REPLACE, IDM_SAVE_APPEND);
} break;
case IDM_SAVE_APPEND: {
Menu_SwapIfChecked(hMenuMain, IDM_SAVE_APPEND, IDM_SAVE_REPLACE);
} break;
case IDM_CHECK_INFILE: {
if(IsEmptyInfile(hListInfile, hEditResult))
break;
SendMessage(hWnd, WM_USER_EXECUTING, (WPARAM)TRUE, 0);
CreateThread(NULL, 0, ThreadExecute::ThreadExecCheckInfile, (LPVOID)hWnd, 0, &dwThreadId);
} break;
case IDM_OUTPUT: {
if(IsEmptyInfile(hListInfile, hEditResult) || IsEmptyOutfile(hEditOutfile, hEditResult))
break;
SendMessage(hWnd, WM_USER_EXECUTING, (WPARAM)TRUE, 0);
CreateThread(NULL, 0, ThreadExecute::ThreadExecOutput, (LPVOID)hWnd, 0, &dwThreadId);
} break;
case IDM_CHECK_AND_OUTPUT: {
if(IsEmptyInfile(hListInfile, hEditResult) || IsEmptyOutfile(hEditOutfile, hEditResult))
break;
SendMessage(hWnd, WM_USER_EXECUTING, (WPARAM)TRUE, 0);
CreateThread(NULL, 0, ThreadExecute::ThreadExecCheckAndOutput, (LPVOID)hWnd, 0, &dwThreadId);
} break;
case IDC_BUTTON_INFILE_ADD: {
// C:\a.txt0
// C:\0a.txt0b.txt00
// C:\dir0a.txt0b.txt00
memset(g_tsPath, 0, STRLEN_PATH);
g_ofn.hwndOwner = hWnd;
g_ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY;
if(GetOpenFileName(&g_ofn)) {
if(g_tsPath[g_ofn.nFileOffset - 1] != TEXT('\0'))
ListView_Infile_AddItem(hListInfile, g_tsPath, g_tsPath + g_ofn.nFileOffset);
else {
lstrcpy(g_tsTempA, g_tsPath);
LPTSTR tsFile = g_tsTempA + g_ofn.nFileOffset - 1;
if(*(tsFile - 1) != TEXT('\\')) {
*tsFile = TEXT('\\');
++tsFile;
}
for(LPTSTR tsP = g_tsPath + g_ofn.nFileOffset; *tsP != TEXT('\0'); ++tsP) {
lstrcpy(tsFile, tsP);
ListView_Infile_AddItem(hListInfile, g_tsTempA, tsFile);
while(*tsP != TEXT('\0'))
++tsP;
}
}
ListView_Infile_DelDup(hListInfile);
}
} break;
case IDC_BUTTON_INFILE_DEL: {
ListView_Infile_DelSelItem(hListInfile);
} break;
case IDC_BUTTON_INFILE_CLEAR: {
ListView_DeleteAllItems(hListInfile);
} break;
case IDC_BUTTON_OUTFILE_REF: {
memset(g_tsPath, 0, STRLEN_PATH);
g_ofn.hwndOwner = hWnd;
g_ofn.Flags = OFN_EXPLORER;
if(GetSaveFileName(&g_ofn))
SetWindowText(hEditOutfile, g_tsPath);
} break;
case IDC_BUTTON_FILTER_CLEAR: {
SetWindowText(hEditFilter, NULL);
} break;
default:
return FALSE;
}
} break;
case WM_USER_EXECUTING: {
if((BOOL)wp) {
g_mii.fState = MFS_GRAYED;
SetWindowText(hEditResult, g_tsExecuting);
} else {
g_mii.fState = MFS_ENABLED;
}
SetMenuItemInfo(hMenuMain, 2, TRUE, &g_mii);
DrawMenuBar(hWnd);
} break;
default:
return FALSE;
}
return TRUE;
}
} // namespace WindowMain

View File

@ -0,0 +1,39 @@
#ifndef WINDOWMAIN_H
#define WINDOWMAIN_H
#include <windows.h>
#define STRLEN_TEMP 1024
#define STRLEN_PATH 1024
#define STRLEN_EXPFILT 128
#define STRLEN_RESULT 64
namespace WindowMain {
struct COption {
bool sortCap;
bool sortIp;
bool delDupIp;
bool saveAppend;
bool filtAnd;
bool filtDel;
};
BOOL Init(HINSTANCE hInstance);
BOOL CALLBACK DlgProcMain(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);
void Dlg_Executing(HWND hDlg, BOOL bExec);
int ListView_Infile_GetItemCount(HWND hDlg);
BOOL ListView_Infile_GetItemText(HWND hDlg, int nItem, LPTSTR tsPath);
BOOL Edit_Outfile_GetText(HWND hDlg, LPTSTR tsPath);
BOOL Edit_Filter_GetText(HWND hDlg, LPTSTR tsText);
BOOL GetOption(HWND hDlg, COption *option);
void Edit_Result_SetText(HWND hDlg, LPCTSTR tsText);
void Edit_Result_AppendText(HWND hDlg, LPCTSTR tsText);
}
#endif // WINDOWMAIN_H

View File

@ -0,0 +1,42 @@
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#define IDC_EDIT_FILTER 1001
#define IDC_LISTVIEW_INFILE 1002
#define IDC_BUTTON_INFILE_ADD 1004
#define IDD_MAIN 1041
#define IDR_MENU_MAIN 1041
#define IDM_EXIT 40000
#define IDS_SET_INFILE 40000
#define IDM_SORT_CAPTION 40001
#define IDS_SET_OUTFILE 40001
#define IDM_SORT_IP 40002
#define IDS_FAILED_FILE_READ 40002
#define IDM_CHECK_DUP_IP 40003
#define IDS_FAILED_FILE_WRITE 40003
#define IDM_SAVE_REPLACE 40004
#define IDS_FILE_PATH 40004
#define IDM_SAVE_APPEND 40005
#define IDS_FILE_NAME 40005
#define IDM_CHECK_INFILE 40006
#define IDS_EXPFILT_PGLIST 40006
#define IDM_OUTPUT 40007
#define IDS_OUTPUT_COMPLETE 40007
#define IDM_CHECK_AND_OUTPUT 40008
#define IDS_EXECUTING 40008
#define IDS_CHECK_FOUND_ERROR 40009
#define IDS_CHECK_NOTFOUND_ERROR 40010
#define IDS_CHECK_COMPLETE 40011
#define IDS_CHECK_ERROR_SYNTAX 40012
#define IDS_CHECK_ERROR_IP 40013
#define IDS_CHECK_ERROR_SYNTAX_RESTORABLE 40014
#define IDC_EDIT_OUTFILE 40022
#define IDC_EDIT_RESULT 40026
#define IDC_BUTTON_INFILE_DEL 40027
#define IDC_BUTTON_INFILE_CLEAR 40028
#define IDC_BUTTON_OUTFILE_REF 40029
#define IDC_BUTTON_FILTER_CLEAR 40030
#define IDC_RADIO_FILTER_AND 40031
#define IDC_RADIO_FILTER_OR 40032
#define IDC_CHECK_FILTER_DEL 40033

View File

@ -0,0 +1,100 @@
// Generated by ResEdit 1.4.4.18
// Copyright (C) 2006-2008
// http://www.resedit.net
#include "resource.h"
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
//
// Menu resources
//
IDR_MENU_MAIN MENU
{
POPUP "ファイル(&F)"
{
MENUITEM "終了(&X)", IDM_EXIT
}
POPUP "設定(&O)"
{
MENUITEM "キャプションでソート(&C)", IDM_SORT_CAPTION, CHECKED
MENUITEM "IPアドレスでソート(&I)", IDM_SORT_IP, CHECKED
MENUITEM SEPARATOR
MENUITEM "IPアドレスの重複を解消(&D)", IDM_CHECK_DUP_IP, CHECKED
MENUITEM SEPARATOR
MENUITEM "上書き保存(&R)", IDM_SAVE_REPLACE, CHECKED
MENUITEM "追記保存(&A)", IDM_SAVE_APPEND
}
POPUP "実行(&E)"
{
MENUITEM "入力ファイルをチェック(&C)", IDM_CHECK_INFILE
MENUITEM SEPARATOR
MENUITEM "ファイル出力(&O)", IDM_OUTPUT
MENUITEM SEPARATOR
MENUITEM "チェック後、ファイル出力(&S)", IDM_CHECK_AND_OUTPUT
}
}
//
// Dialog resources
//
IDD_MAIN DIALOGEX 0, 0, 334, 228
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_GROUP | WS_POPUP | WS_SYSMENU
EXSTYLE WS_EX_OVERLAPPEDWINDOW
CAPTION "PGListUtil ver0.7.0"
MENU IDR_MENU_MAIN
FONT 9, "MS UI Gothic", 400, 0, 128
{
CONTROL "", IDC_LISTVIEW_INFILE, WC_LISTVIEW, WS_TABSTOP | WS_BORDER | LVS_ALIGNLEFT | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | LVS_REPORT | LVS_SORTASCENDING, 6, 16, 158, 118, WS_EX_ACCEPTFILES
PUSHBUTTON "追加", IDC_BUTTON_INFILE_ADD, 62, 138, 30, 16, BS_PUSHBUTTON
PUSHBUTTON "削除", IDC_BUTTON_INFILE_DEL, 98, 138, 30, 16, BS_PUSHBUTTON
PUSHBUTTON "クリア", IDC_BUTTON_INFILE_CLEAR, 134, 138, 30, 16, BS_PUSHBUTTON
EDITTEXT IDC_EDIT_OUTFILE, 6, 168, 122, 14, ES_AUTOHSCROLL, WS_EX_ACCEPTFILES
PUSHBUTTON "参照", IDC_BUTTON_OUTFILE_REF, 134, 167, 30, 16, BS_PUSHBUTTON
EDITTEXT IDC_EDIT_FILTER, 6, 196, 122, 14, ES_AUTOHSCROLL
PUSHBUTTON "クリア", IDC_BUTTON_FILTER_CLEAR, 134, 195, 30, 16, BS_PUSHBUTTON
AUTORADIOBUTTON "AND", IDC_RADIO_FILTER_AND, 6, 212, 26, 10, WS_GROUP | WS_TABSTOP | BS_AUTORADIOBUTTON
AUTORADIOBUTTON "OR", IDC_RADIO_FILTER_OR, 34, 212, 26, 10, WS_TABSTOP | BS_AUTORADIOBUTTON
AUTOCHECKBOX "除外", IDC_CHECK_FILTER_DEL, 102, 212, 26, 10, BS_AUTOCHECKBOX
EDITTEXT IDC_EDIT_RESULT, 170, 16, 158, 206, WS_VSCROLL | ES_MULTILINE | ES_READONLY
LTEXT "入力ファイルリスト", IDC_STATIC, 6, 6, 48, 8, SS_LEFT
LTEXT "出力ファイル", IDC_STATIC, 6, 158, 34, 8, SS_LEFT
LTEXT "キャプションフィルター", IDC_STATIC, 6, 186, 55, 8, SS_LEFT
LTEXT "実行結果", IDC_STATIC, 170, 6, 28, 8, SS_LEFT
}
//
// String Table resources
//
STRINGTABLE
{
IDS_SET_INFILE "入力ファイルを設定してください"
IDS_SET_OUTFILE "出力ファイルを設定してください"
IDS_FAILED_FILE_READ " の読み込みに失敗しました"
IDS_FAILED_FILE_WRITE " の書き込みに失敗しました"
IDS_FILE_PATH "ファイルパス"
IDS_FILE_NAME "ファイル名"
IDS_EXPFILT_PGLIST "テキストファイル (*.txt)@*.txt@すべてのファイル (*.*)@*.*@@"
IDS_OUTPUT_COMPLETE "ファイル出力が完了しました\r\n\r\n入力数 = %d\r\n無効数 = %d\r\n出力数 = %d\r\n"
IDS_EXECUTING "実行中..."
IDS_CHECK_FOUND_ERROR " に以下のエラーが見つかりました"
IDS_CHECK_NOTFOUND_ERROR " に想定内のエラーは見つかりませんでした"
IDS_CHECK_COMPLETE "チェックが完了しました"
IDS_CHECK_ERROR_SYNTAX "[ %d 行目 ] 構文エラー"
IDS_CHECK_ERROR_IP "[ %d 行目 ] 不正なIPアドレス"
IDS_CHECK_ERROR_SYNTAX_RESTORABLE "[ %d 行目 ] 構文エラー (自動修復可)"
}
//
// Manifest resources
//
1 RT_MANIFEST ".\\PGListUtil.exe.manifest"

View File

@ -0,0 +1,12 @@
#ifndef PGLU_STDAFX_H
#define PGLU_STDAFX_H
#include <windows.h>
#ifdef __MINGW32__
#define _WIN32_IE 0x0600
#endif
#include <commctrl.h>
#endif // PGLU_STDAFX_H

View File

@ -0,0 +1,171 @@
#include <stdio.h>
#include <boost/xpressive/xpressive.hpp>
#include "CErrorList.h"
namespace pglu {
namespace error {
CErrorList::CErrorList() :
m_pool(sizeof(CError)),
m_errFoot(&m_errHead),
m_errNext(&m_errHead),
m_count(0)
{
m_errHead.line = 0;
m_errHead.kind = SYNTAX;
m_errHead.next = NULL;
}
CErrorList::~CErrorList() {
Clear();
}
void CErrorList::Clear() {
m_pool.purge_memory();
m_errHead.next = NULL;
m_errFoot = &m_errHead;
m_errNext = &m_errHead;
m_count = 0;
}
bool CErrorList::LoadListFile(const char *path) {
char buf[PGLU_LENGTH_FILELINE];
uint ip_begin1, ip_begin2, ip_begin3, ip_begin4;
uint ip_end1, ip_end2, ip_end3, ip_end4;
FILE *fp;
CError *err = m_errFoot;
using namespace boost::xpressive;
static cmatch match;
static mark_tag tagIp1(1), tagIp2(2), tagIp3(3), tagIp4(4), tagIp5(5), tagIp6(6), tagIp7(7), tagIp8(8);
static mark_tag tagSep(9), tagMask(10);
static cregex reSyntax =
as_xpr(':') >> *_s >>
(tagIp1 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp2 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp3 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp4 = repeat<1, 3>(_d)) >>
*_s >> as_xpr('-') >> *_s >>
(tagIp5 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp6 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp7 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp8 = repeat<1, 3>(_d)) >>
*_s >> (_ln | eos);
static cregex reSyntaxRestorable =
(tagIp1 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp2 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp3 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp4 = repeat<1, 3>(_d)) >> *_s >> (
(tagSep = +as_xpr('-')) >> *_s >>
(tagIp5 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp6 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp7 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp8 = repeat<1, 3>(_d))
|
(tagSep = +(set = '/', '\\')) >> *_s >>
(tagMask = repeat<1, 2>(_d))
) >> *_s >> (_ln | eos);
#define reIpError repeat<3>(+_d >> *_s >> (set = '.', ',') >> *_s) >> +_d
static cregex reSyntaxError =
reIpError >> *_s >> (
*as_xpr('-') >> *_s >> reIpError |
+(set = '/', '\\') >> *_s >> +_d
);
fp = fopen(path, "r");
if(fp == NULL)
return false;
for(int line = 1; fgets(buf, PGLU_LENGTH_FILELINE, fp); ++line) {
if(regex_search(buf, match, reSyntax)) {
if(!(
(ip_begin1 = ParseDigit3(match[1].first, match[1].second)) < 256 &&
(ip_begin2 = ParseDigit3(match[2].first, match[2].second)) < 256 &&
(ip_begin3 = ParseDigit3(match[3].first, match[3].second)) < 256 &&
(ip_begin4 = ParseDigit3(match[4].first, match[4].second)) < 256 &&
(ip_end1 = ParseDigit3(match[5].first, match[5].second)) < 256 &&
(ip_end2 = ParseDigit3(match[6].first, match[6].second)) < 256 &&
(ip_end3 = ParseDigit3(match[7].first, match[7].second)) < 256 &&
(ip_end4 = ParseDigit3(match[8].first, match[8].second)) < 256 &&
(uint)((ip_begin1 << 24) | (ip_begin2 << 16) | (ip_begin3 << 8) | ip_begin4) <=
(uint)((ip_end1 << 24) | (ip_end2 << 16) | (ip_end3 << 8) | ip_end4)
)) {
++m_count;
err->next = (CError*)m_pool.malloc();
err = err->next;
err->line = line;
err->kind = IP;
}
} else if(regex_search(buf, match, reSyntaxRestorable)) {
++m_count;
err->next = (CError*)m_pool.malloc();
err = err->next;
err->line = line;
if(*(match[9].first) == '-') {
if(
(ip_begin1 = ParseDigit3(match[1].first, match[1].second)) < 256 &&
(ip_begin2 = ParseDigit3(match[2].first, match[2].second)) < 256 &&
(ip_begin3 = ParseDigit3(match[3].first, match[3].second)) < 256 &&
(ip_begin4 = ParseDigit3(match[4].first, match[4].second)) < 256 &&
(ip_end1 = ParseDigit3(match[5].first, match[5].second)) < 256 &&
(ip_end2 = ParseDigit3(match[6].first, match[6].second)) < 256 &&
(ip_end3 = ParseDigit3(match[7].first, match[7].second)) < 256 &&
(ip_end4 = ParseDigit3(match[8].first, match[8].second)) < 256 &&
(uint)((ip_begin1 << 24) | (ip_begin2 << 16) | (ip_begin3 << 8) | ip_begin4) <=
(uint)((ip_end1 << 24) | (ip_end2 << 16) | (ip_end3 << 8) | ip_end4)
) {
err->kind = SYNTAX_RESTORABLE;
} else {
err->kind = SYNTAX;
}
} else {
uint mask = ParseDigit3(match[10].first, match[10].second);
if(
ParseDigit3(match[1].first, match[1].second) < 256 &&
ParseDigit3(match[2].first, match[2].second) < 256 &&
ParseDigit3(match[3].first, match[3].second) < 256 &&
ParseDigit3(match[4].first, match[4].second) < 256 &&
mask < 33
) {
err->kind = SYNTAX_RESTORABLE;
} else {
err->kind = SYNTAX;
}
}
} else if(regex_search(buf, reSyntaxError)) {
++m_count;
err->next = (CError*)m_pool.malloc();
err = err->next;
err->line = line;
err->kind = SYNTAX;
}
#ifdef __MINGW32__
ZeroString(buf);
#endif
}
fclose(fp);
err->next = NULL;
m_errFoot = err;
return true;
}
int CErrorList::Count() {
return m_count;
}
CError * CErrorList::GetNext() {
if(m_errNext)
m_errNext = m_errNext->next;
return m_errNext;
}
} // namespace error
} // namespace pglu

View File

@ -0,0 +1,43 @@
#ifndef CERRLIST_H
#define CERRLIST_H
#include <boost/pool/pool.hpp>
#include "common.h"
namespace pglu {
namespace error {
typedef enum _EErrKind {
SYNTAX,
IP,
SYNTAX_RESTORABLE
} EErrKind;
typedef struct _CError {
int line;
EErrKind kind;
_CError * next;
} CError;
class CErrorList {
private:
boost::pool<> m_pool;
CError m_errHead;
CError * m_errFoot;
CError * m_errNext;
int m_count;
public:
CErrorList();
~CErrorList();
void Clear();
bool LoadListFile(const char *path);
int Count();
CError * GetNext();
};
} // namespace error
} // namespace pglu
#endif // CERRLIST_H

View File

@ -0,0 +1,125 @@
#include <ctype.h>
#include <string.h>
#include "CFilter.h"
namespace pglu {
namespace filter {
namespace {
char *strichr(const char *str, int chr) {
const char *p = str;
chr = tolower((unsigned char)chr);
for(; tolower((unsigned char)(*p)) != chr; ++p)
if(*p == '\0')
return NULL;
return (char*)p;
}
char *stristr(const char *str, const char *pattern) {
if(*pattern == '\0')
return (char*)str;
const char *p = str;
size_t len = strlen(pattern);
for(; (p = strichr(p, pattern[0])) != NULL; ++p)
if(!strnicmp(p, pattern, len))
return (char*)p;
return NULL;
}
bool search_and(const char *str, const char *terms, const bool del) {
do {
if(!stristr(str, terms))
return del; // not found
while(*terms != '\0')
++terms;
++terms;
} while(*terms != '\0');
return !del; // found all
}
bool search_or(const char *str, const char *terms, const bool del) {
do {
if(stristr(str, terms))
return !del; // found
while(*terms != '\0')
++terms;
++terms;
} while(*terms != '\0');
return del; // not found
}
} // namespace
//--------------------------------------
// CFilter class
//--------------------------------------
CFilter::CFilter() :
m_terms(NULL)
{
}
CFilter::CFilter(const char *strFilter, const EFilterMode mode, const bool del) :
m_terms(NULL)
{
Assign(strFilter, mode, del);
}
CFilter::~CFilter() {
Clear();
}
void CFilter::Assign(const char *strFilter, const EFilterMode mode, const bool del) {
Clear();
if(mode == AND)
m_search = search_and;
else if(mode == OR)
m_search = search_or;
else
return;
m_mode = mode;
m_del = del;
m_terms = new char[strlen(strFilter) + 2];
while(*strFilter == ' ' || *strFilter == '\t')
++strFilter;
char *strTerms = m_terms;
while(*strFilter != '\0') {
*(strTerms++) = *(strFilter++);
if(*strFilter == ' ' || *strFilter == '\t') {
*(strTerms++) = '\0';
do {
++strFilter;
} while(*strFilter == ' ' || *strFilter == '\t');
}
}
*strTerms = '\0';
*(strTerms + 1) = '\0';
}
void CFilter::Clear() {
if(m_terms) {
delete[] m_terms;
m_terms = NULL;
}
}
bool CFilter::IsEmpty() {
if(m_terms)
return *m_terms == '\0';
else
return true;
}
bool CFilter::IsMatch(const char *str) {
return m_search(str, m_terms, m_del);
}
} // namespace filter
} // namespace pglu

View File

@ -0,0 +1,34 @@
#ifndef CFILTER_H
#define CFILTER_H
namespace pglu {
namespace filter {
typedef enum _EFilterMode {
AND,
OR
} EFilterMode;
class CFilter {
private:
char * m_terms;
EFilterMode m_mode;
bool m_del;
bool (* m_search)(const char *, const char *, const bool);
public:
CFilter();
CFilter(const char *strFilter, const EFilterMode mode, const bool del);
~CFilter();
void Assign(const char *strFilter, const EFilterMode mode, const bool del);
void Clear();
bool IsEmpty();
bool IsMatch(const char *str);
};
} // namespace filter
} // namespace pglu
#endif // CFILTER_H

View File

@ -0,0 +1,355 @@
#include <stdio.h>
#include <string.h>
#include "CIpList.h"
namespace pglu {
namespace ip {
namespace {
inline void Ip_Swap(CIp **ipA, CIp **ipB) {
CIp *ip = *ipA;
*ipA = *ipB;
*ipB = ip;
}
void Ip_SortByIpQuick(CIp **ipBegin, CIp **ipEnd) {
CIp **ipA;
CIp **ipB;
int nGap = ipEnd - ipBegin;
if(nGap < 64)
return;
ipA = ipBegin + ((int)(nGap / 2));
if((*ipBegin)->ip64 > (*ipA)->ip64)
Ip_Swap(ipBegin, ipA);
if((*ipBegin)->ip64 > (*ipEnd)->ip64)
Ip_Swap(ipBegin, ipEnd);
if((*ipA)->ip64 > (*ipEnd)->ip64)
Ip_Swap(ipA, ipEnd);
ulong ip64 = (*ipA)->ip64;
ipB = ipEnd - 1;
Ip_Swap(ipA, ipB);
ipA = ipBegin;
for(; ; ) {
while((*(++ipA))->ip64 < ip64);
while((*(--ipB))->ip64 > ip64);
if(ipA > ipB)
break;
Ip_Swap(ipA, ipB);
}
Ip_Swap(ipA, ipEnd - 1);
Ip_SortByIpQuick(ipBegin, ipB);
Ip_SortByIpQuick(ipA + 1, ipEnd);
}
void Ip_SortByIpInsert(CIp **ipBegin, CIp **ipEnd) {
CIp **ipA = ipBegin + 1;
CIp **ipB;
CIp **ipAEnd = ipEnd + 1;
CIp **ipBEnd = ipBegin - 1;
ulong ip64;
for(; ipA != ipAEnd; ++ipA) {
ip64 = (*ipA)->ip64;
for(ipB = ipA - 1; ipB != ipBEnd && (*ipB)->ip64 > ip64; --ipB)
Ip_Swap(ipB, ipB + 1);
}
}
void Ip_SortByIp(CIp **ipBegin, CIp **ipEnd) {
Ip_SortByIpQuick(ipBegin, ipEnd);
Ip_SortByIpInsert(ipBegin, ipEnd);
}
CIp * Ip_SortByCaption(CIp *ipHeadA) {
if(!ipHeadA || !(ipHeadA->next))
return ipHeadA;
// split ipBを2倍で進めることでipAを中間位置に持っていく
CIp *ipA = ipHeadA;
CIp *ipB = ipHeadA->next->next;
while(ipB) {
ipA = ipA->next;
ipB = ipB->next;
if(ipB)
ipB = ipB->next;
}
CIp *ipHeadB = ipA->next;
ipA->next = NULL;
ipHeadA = Ip_SortByCaption(ipHeadA);
ipHeadB = Ip_SortByCaption(ipHeadB);
// merge
CIp ipMerged;
ipA = &ipMerged;
while(ipHeadA || ipHeadB) {
if(((ipHeadA && ipHeadB) && stricmp(ipHeadA->caption, ipHeadB->caption) <= 0) || !ipHeadB) {
ipA->next = ipHeadA;
ipHeadA = ipHeadA->next;
} else {
ipA->next = ipHeadB;
ipHeadB = ipHeadB->next;
}
ipA = ipA->next;
}
ipA->next = NULL;
return ipMerged.next;
}
} // namespace
//--------------------------------------
// CIpList class
//--------------------------------------
CIpList::CIpList() :
m_poolIp(sizeof(CIp)),
m_ipFoot(&m_ipHead),
m_count(0),
m_countDisabled(0)
{
m_ipHead.caption = NULL;
m_ipHead.ip64 = 0L;
m_ipHead.next = NULL;
}
CIpList::~CIpList() {
Clear();
}
void CIpList::Clear() {
for(CIp *ip = m_ipHead.next; ip; ip = ip->next)
delete[] ip->caption;
m_poolIp.purge_memory();
m_ipHead.next = NULL;
m_ipFoot = &m_ipHead;
m_count = 0;
m_countDisabled = 0;
UnSetFilter();
}
void CIpList::SetFilter(const char *filter, const filter::EFilterMode mode, const bool del) {
m_filter.Assign(filter, mode, del);
}
void CIpList::UnSetFilter() {
m_filter.Clear();
}
CIp * CIpList::CreateIp(boost::xpressive::cmatch & match) {
CIp *ip = (CIp*)m_poolIp.malloc();
const char *capBegin = match.prefix().first;
const char *capEnd = match[9].first;
size_t lenCap = capEnd - capBegin;
char *chunk = new char[lenCap + 1];
memcpy(chunk, capBegin, lenCap);
*(chunk + lenCap) = '\0';
ip->caption = chunk;
uchar *ip8 = ip->ip8;
ip8[4] = ParseDigit3(match[4].first, match[4].second);
ip8[5] = ParseDigit3(match[3].first, match[3].second);
ip8[6] = ParseDigit3(match[2].first, match[2].second);
ip8[7] = ParseDigit3(match[1].first, match[1].second);
if(*(match[10].first) == '-') {
ip8[0] = ParseDigit3(match[8].first, match[8].second);
ip8[1] = ParseDigit3(match[7].first, match[7].second);
ip8[2] = ParseDigit3(match[6].first, match[6].second);
ip8[3] = ParseDigit3(match[5].first, match[5].second);
} else {
ip->ip32[0] = ip->ip32[1] | (0xFFFFFFFF >> ParseDigit3(match[11].first, match[11].second));
}
return ip;
}
bool CIpList::LoadListFile(const char *path) {
char buf[PGLU_LENGTH_FILELINE];
char colon;
char *colon_p;
using namespace boost::xpressive;
static cmatch match;
static mark_tag tagIp1(1), tagIp2(2), tagIp3(3), tagIp4(4), tagIp5(5), tagIp6(6), tagIp7(7), tagIp8(8);
static mark_tag tagColon(9), tagSep(10), tagMask(11);
static cregex reSyntax = // bos >> (tag = *_) >> // slower
(tagColon = as_xpr(':')) >> *_s >>
(tagIp1 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp2 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp3 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp4 = repeat<1, 3>(_d)) >>
*_s >> (tagSep = as_xpr('-')) >> *_s >>
(tagIp5 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp6 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp7 = repeat<1, 3>(_d)) >> as_xpr('.') >>
(tagIp8 = repeat<1, 3>(_d)) >>
*_s >> (_ln | eos);
static cregex reSyntaxRestorable =
(tagColon = !as_xpr(':')) >> *_s >>
(tagIp1 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp2 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp3 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp4 = repeat<1, 3>(_d)) >> *_s >> (
(tagSep = +as_xpr('-')) >> *_s >>
(tagIp5 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp6 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp7 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
(tagIp8 = repeat<1, 3>(_d))
|
(tagSep = +(set = '/', '\\')) >> *_s >>
(tagMask = repeat<1, 2>(_d))
) >> *_s >> (_ln | eos);
FILE *fp = fopen(path, "r");
if(fp == NULL)
return false;
CIp *ip = m_ipFoot;
if(m_filter.IsEmpty()) {
while(fgets(buf, PGLU_LENGTH_FILELINE, fp)) {
if(regex_search(buf, match, reSyntax) || regex_search(buf, match, reSyntaxRestorable)) {
++m_count;
ip->next = CreateIp(match);
ip = ip->next;
}
#ifdef __MINGW32__
ZeroString(buf);
#endif
}
} else {
while(fgets(buf, PGLU_LENGTH_FILELINE, fp)) {
if(regex_search(buf, match, reSyntax) || regex_search(buf, match, reSyntaxRestorable)) {
colon_p = (char*)(match[9].first);
colon = *colon_p;
*colon_p = '\0';
if(m_filter.IsMatch(buf)) {
++m_count;
ip->next = CreateIp(match);
ip = ip->next;
}
*colon_p = colon;
}
#ifdef __MINGW32__
ZeroString(buf);
#endif
}
}
fclose(fp);
ip->next = NULL;
m_ipFoot = ip;
return true;
}
bool CIpList::SaveListFile(const char *path, const bool append) {
uchar *ip8;
FILE *fp = fopen(path, (append ? "a" : "w"));
if(fp == NULL)
return false;
for(CIp *ip = m_ipHead.next; ip; ip = ip->next) {
// IPが0Lなら書き出さない
if(ip->ip64 != 0L) {
ip8 = ip->ip8;
fprintf(fp,
"%s:%u.%u.%u.%u-%u.%u.%u.%u\n",
ip->caption,
ip8[7], ip8[6], ip8[5], ip8[4],
ip8[3], ip8[2], ip8[1], ip8[0]
);
}
}
fclose(fp);
return true;
}
void CIpList::CheckAndSort(const bool sortCap, const bool sortIp, const bool delDupIp) {
CIp **ipBegin;
CIp **ipEnd;
CIp *ip;
if(m_count < 2)
return;
if(sortIp || delDupIp) {
// リストから配列を複製
CIp **ipSort = new CIp*[m_count];
ipBegin = ipSort;
for(ip = m_ipHead.next; ip; ip = ip->next)
*(ipBegin++) = ip;
// 配列をソート
Ip_SortByIp(ipSort, ipSort + m_count - 1);
if(delDupIp) {
// すでにIPが0Lなものを無効としてカウント
ipBegin = ipSort;
ipEnd = ipBegin + m_count;
for(; ipBegin != ipEnd && (*ipBegin)->ip64 == 0L; ++ipBegin)
++m_countDisabled;
if(ipBegin == ipEnd)
goto END_SORT; // 全てのIPが0L
// 重複したIPは0Lにして無効としてカウント
for(--ipEnd; ipBegin != ipEnd; ++ipBegin)
if((*ipBegin)->ip64 == (*(ipBegin + 1))->ip64) {
(*ipBegin)->ip64 = 0L;
++m_countDisabled;
}
}
if(sortIp) {
// 配列の中身を連結
ipBegin = ipSort;
ipEnd = ipSort + m_count;
ip = &m_ipHead;
while(ipBegin != ipEnd) {
ip->next = *(ipBegin++);
ip = ip->next;
}
ip->next = NULL;
}
END_SORT:
delete[] ipSort;
}
if(sortCap)
m_ipHead.next = Ip_SortByCaption(m_ipHead.next);
if(sortIp || sortCap) {
// m_ipFootを再設定
ip = m_ipHead.next;
if(ip) {
for(; ip->next; ip = ip->next);
m_ipFoot = ip;
} else {
m_ipFoot = &m_ipHead;
}
}
}
int CIpList::Count() {
return m_count;
}
int CIpList::CountDisabled() {
return m_countDisabled;
}
} // namespace ip
} // namespace pglu

View File

@ -0,0 +1,56 @@
#ifndef CIPLIST_H
#define CIPLIST_H
#include <boost/pool/pool.hpp>
#include <boost/xpressive/xpressive.hpp>
#include "common.h"
#include "CFilter.h"
namespace pglu {
namespace ip {
struct CIp {
char * caption;
union {
ulong ip64;
uint ip32[2];
uchar ip8[8];
};
CIp * next;
};
class CIpList {
private:
boost::pool<> m_poolIp;
CIp m_ipHead;
CIp * m_ipFoot;
filter::CFilter m_filter;
int m_count;
int m_countDisabled;
CIp * CreateIp(boost::xpressive::cmatch & match);
public:
CIpList();
~CIpList();
void Clear();
void SetFilter(const char *filter, const filter::EFilterMode mode, const bool del);
void UnSetFilter();
bool LoadListFile(const char *path);
bool SaveListFile(const char *path, const bool append);
void CheckAndSort(const bool sortCap, const bool sortIp, const bool delDupIp);
int Count();
int CountDisabled();
};
} // namespace ip
} // namespace pglu
#endif // CIPLIST_H

View File

@ -0,0 +1,48 @@
#ifndef COMMON_H
#define COMMON_H
#ifdef _MSC_VER
typedef unsigned __int8 uchar;
typedef unsigned __int32 uint;
typedef unsigned __int64 ulong;
# else
#include <boost/cstdint.hpp>
typedef uint8_t uchar;
typedef uint32_t uint;
typedef uint64_t ulong;
#endif
namespace pglu {
#define PGLU_LENGTH_FILELINE 1024
inline uint ParseDigit3(const char *begin, const char *end) {
switch(end - begin) {
case 3:
return ((*begin & 0xF) * 100) + ((*(begin + 1) & 0xF) * 10) + (*(begin + 2) & 0xF);
case 2:
return ((*begin & 0xF) * 10) + (*(begin + 1) & 0xF);
case 1:
return (*begin & 0xF);
default:
return 256;
}
}
#ifdef __MINGW32__
inline void ZeroString(char *str) {
while(*str != '\0')
*(str++) = '\0';
}
#endif
} // namespace pglu
#endif // COMMON_H