SourceInclude.c
// マルチバイト文字セット #include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #include "resource.h" // グローバル変数 static char g_acPath[_MAX_PATH] = ""; static char g_acDrive[_MAX_DRIVE]; static char g_acDir[_MAX_DIR]; static char g_acFname[_MAX_FNAME]; static char g_acExt[_MAX_EXT]; static char g_acLog[512]; // プロトタイプ宣言 INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); void OnBtnInclude(); int RenameFile(const char* pcTmp, const char* pcOrg); int SourceInclude(FILE* pfileDst, FILE* pfileSrc); int TimeStamp(FILE* pfileDst); int IncludeFile(const char* pcParam, FILE* pfileDst); const char* ParseParam(const char* pcParam, const char** ppcEntry, size_t* puiEntry, const char** ppcValue, size_t* puiValue); void OnDropFiles(HWND hWnd, WPARAM wParam); void OnGetMinMaxInfo(LPARAM lParam); void OnSize(HWND hDlg); void OnInitDialog(HWND hDlg); void CenterWindow(HWND hWnd); int StrMemCmp(const char* pcStr, const char* pcMem, size_t uiLen); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { if (1 < __argc) { strcpy_s(g_acPath, sizeof(g_acPath), __argv[1]); } DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, DlgProc); return 0; } INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { BOOL bRetVal = TRUE; switch (uMsg) { case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BTN_INCLUDE: OnBtnInclude(); break; } break; case WM_DROPFILES: OnDropFiles(hDlg, wParam); DragFinish((HDROP)wParam); break; case WM_GETMINMAXINFO: OnGetMinMaxInfo(lParam); break; case WM_SIZE: OnSize(hDlg); break; case WM_INITDIALOG: OnInitDialog(hDlg); bRetVal = TRUE; // TRUEならデフォルトフォーカスを使用 break; case WM_CLOSE: EndDialog(hDlg, IDOK); break; default: bRetVal = FALSE; } return bRetVal; } void OnBtnInclude() { FILE* pfileSrc; FILE* pfileDst; char acPathTmp[_MAX_PATH]; int iRet; g_acLog[0] = '\0'; if (g_acPath[0] == '\0') { return; } if (_splitpath_s(g_acPath, g_acDrive, _MAX_DRIVE, g_acDir, _MAX_DIR, g_acFname, _MAX_FNAME, g_acExt, _MAX_EXT)) { return; } if (memcmp(g_acExt, ".htm", 4)) { return; } if (_makepath_s(acPathTmp, _MAX_PATH, g_acDrive, g_acDir, g_acFname, ".tmp")) { return; } // if (fopen_s(&pfileSrc, g_acPath, "rt")) { return; } if (fopen_s(&pfileDst, acPathTmp, "wt")) { fclose(pfileSrc); return; } iRet = SourceInclude(pfileDst, pfileSrc); fclose(pfileDst); fclose(pfileSrc); if (iRet == 0) { RenameFile(acPathTmp, g_acPath); } } int RenameFile(const char* pcTmp, const char* pcOrg) { if (remove(pcOrg)) { return -1; } if (rename(pcTmp, pcOrg)) { return -1; } ShellExecute(NULL, "open", pcOrg, NULL, NULL, SW_NORMAL); return 0; } int SourceInclude(FILE* pfileDst, FILE* pfileSrc) { char acBuf[512]; char* pcCmd; size_t uiCmd; int iSkipFlag = 0; while (fgets(acBuf, sizeof(acBuf), pfileSrc)) { if (memcmp(acBuf, "<!--$", 5) == 0) { pcCmd = acBuf + 5; uiCmd = strcspn(pcCmd, " -\n"); if (StrMemCmp("end", pcCmd, uiCmd) == 0) { iSkipFlag = 0; } else if (StrMemCmp("include", pcCmd, uiCmd) == 0) { iSkipFlag = 1; fputs(acBuf, pfileDst); if (IncludeFile(pcCmd + 7, pfileDst)) { return -1; } } else if (StrMemCmp("timestamp", pcCmd, uiCmd) == 0) { iSkipFlag = 1; fputs(acBuf, pfileDst); TimeStamp(pfileDst); } } if (iSkipFlag == 0) { fputs(acBuf, pfileDst); } } return 0; } int TimeStamp(FILE* pfileDst) { struct tm stm; time_t lTime; char acBuf[256]; time(&lTime); localtime_s(&stm, &lTime); strftime(acBuf, sizeof(acBuf), "%#x 更新\n", &stm); fputs(acBuf, pfileDst); return 0; } int IncludeFile(const char* pcParam, FILE* pfileDst) { FILE* pfileSrc; char acPath[_MAX_PATH]; char acFile[256]; char* pcEntry; char* pcValue; size_t uiEntry; size_t uiValue; int iChar; while (1) { pcParam = ParseParam(pcParam, &pcEntry, &uiEntry, &pcValue, &uiValue); if (pcParam == NULL) { return -1; } if (StrMemCmp("-->", pcEntry, uiEntry) == 0) { break; } if (StrMemCmp("file", pcEntry, uiEntry) == 0) { if (uiValue == 0 || sizeof(acFile) <= uiValue) { return -1; } strncpy_s(acFile, sizeof(acFile), pcValue, uiValue); } } if (acFile[0] == '\0') { return -1; } // sprintf_s(acPath, sizeof(acPath), "%s%s%s", g_acDrive, g_acDir, acFile); if (fopen_s(&pfileSrc, acPath, "rt")) { return -1; } fprintf(pfileDst, "<p>\n<a href=\"%s\">%s</a>\n<p>\n", acFile, acFile); fprintf(pfileDst, "<textarea cols=\"80\" rows=\"25\">\n"); while ((iChar = fgetc(pfileSrc)) != EOF) { switch (iChar) { case '<': fputs("<", pfileDst); break; case '>': fputs(">", pfileDst); break; default: fputc(iChar, pfileDst); } } fprintf(pfileDst, "</textarea>\n"); fclose(pfileSrc); return 0; } const char* ParseParam(const char* pcParam, const char** ppcEntry, size_t* puiEntry, const char** ppcValue, size_t* puiValue) { const char* pcEntry; const char* pcValue; *ppcValue = NULL; *puiValue = 0; // Entry; pcEntry = pcParam; while (*pcEntry == ' ') { pcEntry++; } *puiEntry = strcspn(pcEntry, "= <\n"); *ppcEntry = pcEntry; // Value pcValue = pcEntry + *puiEntry; if (*pcValue != '=') { return pcValue; } pcValue++; if (*pcValue != '\"') { return NULL; } pcValue++; *puiValue = strcspn(pcValue, "\""); *ppcValue = pcValue; // Param pcParam = pcValue + *puiValue; if (*pcParam != '\"') { return NULL; } pcParam++; return pcParam; } void OnDropFiles(HWND hWnd, WPARAM wParam) { HDROP hDrop; hDrop = (HDROP)wParam; DragQueryFile(hDrop, 0, g_acPath, sizeof(g_acPath)); SetDlgItemText(hWnd, IDC_EDT_FILE, g_acPath); } void OnGetMinMaxInfo(LPARAM lParam) { MINMAXINFO *pmmi; pmmi = (MINMAXINFO*)lParam; pmmi->ptMinTrackSize.x = 360; pmmi->ptMinTrackSize.y = 240; } void OnSize(HWND hDlg) { RECT rc; POINT pt; HWND hWnd; UINT uiFlags = SWP_NOZORDER | SWP_NOMOVE; int iDluX; int iDluY; int iWidth; int iHeight; int iBtnWidth; int iBtnCleanX; // DLU rc.left = 4; rc.top = 8; MapDialogRect(hDlg, &rc); iDluX = rc.left; iDluY = rc.top; GetClientRect(hDlg, &rc); iWidth = rc.right - (8 * iDluX) / 4; iHeight = rc.bottom - (72 * iDluY) / 8; iBtnWidth = (rc.right - (12 * iDluX) / 4) / 2; iBtnCleanX = rc.right / 2 + (2 * iDluX) / 4; // File hWnd = GetDlgItem(hDlg, IDC_EDT_FILE); GetWindowRect(hWnd, &rc); SetWindowPos(hWnd, 0, 0, 0, iWidth, rc.bottom - rc.top, uiFlags); // Include hWnd = GetDlgItem(hDlg, IDC_BTN_INCLUDE); GetWindowRect(hWnd, &rc); SetWindowPos(hWnd, 0, 0, 0, iBtnWidth, rc.bottom - rc.top, uiFlags); // Clean hWnd = GetDlgItem(hDlg, IDC_BTN_CLEAN); GetWindowRect(hWnd, &rc); pt.x = rc.left; pt.y = rc.top; ScreenToClient(hDlg, &pt); SetWindowPos(hWnd, 0, iBtnCleanX, pt.y, iBtnWidth, rc.bottom - rc.top, SWP_NOZORDER); // Log hWnd = GetDlgItem(hDlg, IDC_EDT_LOG); SetWindowPos(hWnd, 0, 0, 0, iWidth, iHeight, uiFlags); } void OnInitDialog(HWND hDlg) { // 中央 CenterWindow(hDlg); SetDlgItemText(hDlg, IDC_EDT_FILE, g_acPath); } void CenterWindow(HWND hWnd) { RECT rcParent; RECT rcThis; int iWidth; int iHeight; GetWindowRect(GetDesktopWindow(), &rcParent); GetWindowRect(hWnd, &rcThis); iWidth = rcThis.right - rcThis.left; iHeight = rcThis.bottom - rcThis.top; MoveWindow(hWnd, (rcParent.right - iWidth) / 2, (rcParent.bottom - iHeight) / 2, iWidth, iHeight, FALSE); } int StrMemCmp(const char* pcStr, const char* pcMem, size_t uiLen) { int iRet; iRet = (int)(strlen(pcStr) - uiLen); if (iRet) { return iRet; } return strncmp(pcStr, pcMem, uiLen); }
resource.h
#define IDD_MAIN_DIALOG 100 #define IDC_EDT_FILE 1000 #define IDC_BTN_INCLUDE 1001 #define IDC_BTN_CLEAN 1002 #define IDC_EDT_LOG 1003
SourceInclude.rc
// リソーススクリプト #include <windows.h> #include "resource.h" IDD_MAIN_DIALOG DIALOGEX 0, 0, 240, 240 STYLE WS_POPUPWINDOW | WS_THICKFRAME | WS_MINIMIZEBOX EXSTYLE WS_EX_APPWINDOW | WS_EX_ACCEPTFILES CAPTION "SourceInclude" FONT 9, "MS Pゴシック" BEGIN EDITTEXT IDC_EDT_FILE,4,4,232,40,ES_MULTILINE PUSHBUTTON "Include",IDC_BTN_INCLUDE,4,48,114,16 PUSHBUTTON "Clean",IDC_BTN_CLEAN,122,48,114,16 EDITTEXT IDC_EDT_LOG,4,68,232,168,ES_MULTILINE | WS_DISABLED END