home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- * Zap *
- * === *
- * *
- * Windows 3 Text Editor *
- * *
- * Misc editting commands module *
- * BOOL FAR PASCAL GotoLineProc(HWND, unsigned, WORD, LONG); *
- * long GetCurrentLine(int); *
- * void GotoLine(int, long); *
- * *
- * BOOL FAR PASCAL SearchProc(HWND, unsigned, WORD, LONG); *
- * BOOL FAR PASCAL ReplaceProc(HWND, unsigned, WORD, LONG); *
- * BOOL FAR PASCAL ConfirmProc(HWND, unsigned, WORD, LONG); *
- * void TranslateString(char *, char *); *
- * long lstrstr(LPSTR, LPSTR, BOOL); *
- * *
- * This programme was developed using Microsoft C6.0 and the Microsoft *
- * SDK, however any ANSI C could be used if suitable libraries and the *
- * Windows header are available. *
- ***********************************************************************/
-
- #include <windows.h>
- #include <string.h>
- #include "zap.h"
- #include "zapdlg.h"
-
-
- /***********************************************************************
- * External variables *
- ***********************************************************************/
-
- extern HANDLE hInst;
- extern HWND hMainWnd;
-
- extern EDIT_WND EditWnd[MAX_FILES];
- extern int NumFiles, CurrentFile;
-
- extern FARPROC lpNewEditWndProc;
-
- extern LPSTR TransBuf;
-
- extern int CharWd, CharDp;
-
- extern char WorkStr[WORK_STR_LEN];
-
-
- /***********************************************************************
- * Global variables *
- ***********************************************************************/
-
- char SearchStr[WORK_STR_LEN], TransSearchStr[WORK_STR_LEN],
- ReplaceStr[WORK_STR_LEN], TransReplaceStr[WORK_STR_LEN];
-
- BOOL CaseSensitive = FALSE;
-
-
- /***********************************************************************
- * Routine to go to a line number *
- ***********************************************************************/
-
- BOOL FAR PASCAL GotoLineProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
- { WORD line;
- char linestr[16];
-
- switch (message)
- { case WM_INITDIALOG:
- SendDlgItemMessage(hDlg, IDD_GOTO_LINE, EM_LIMITTEXT, 15, 0L);
- line = GetCurrentLine((int) lParam);
- SetDlgItemInt(hDlg, IDD_GOTO_LINE, line, FALSE);
-
- return(TRUE);
-
- case WM_COMMAND:
- switch (wParam)
- { case IDCANCEL:
- EndDialog(hDlg, 0);
- return(TRUE);
-
- case IDOK:
- GetDlgItemText(hDlg, IDD_GOTO_LINE, linestr, 16);
- if (sscanf(linestr, "%u", &line) != 1)
- { sprintf(WorkStr, "Bad line number \"%s\"", linestr);
- Comment(WorkStr);
- return(TRUE);
- }
-
- EndDialog(hDlg, line);
- return(TRUE);
- }
- }
-
- return(FALSE);
- }
-
-
- /***********************************************************************
- * Routine to get the current line in an edit control *
- ***********************************************************************/
-
- long GetCurrentLine(int Id)
- { long iL;
-
- iL = CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_GETSEL, 0, 0L);
- return(CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_LINEFROMCHAR, LOWORD(iL), 0L) + 1);
- }
-
-
- /***********************************************************************
- * Routine to go to a line in an edit control *
- ***********************************************************************/
-
- void GotoLine(int Id, long Line)
- { WORD iW;
-
- if (Line > 0)
- { iW = CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_LINEINDEX, (WORD) (Line - 1), 0L);
- CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_SETSEL, 0, MAKELONG(iW, iW));
- }
- }
-
-
- /***********************************************************************
- * Message handler for Search dialog box *
- ***********************************************************************/
-
- BOOL FAR PASCAL SearchProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
- { long curpos, newpos, endpos;
-
- switch (message)
- {
-
- /*** On initialisation fill the search field */
-
- case WM_INITDIALOG:
- SetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr);
- CheckDlgButton(hDlg, IDD_SEARCH_CASE, CaseSensitive);
- return(TRUE);
-
- case WM_COMMAND:
- switch (wParam)
- {
-
- /*** On CANCEL close the box and return to editting */
-
- case IDCANCEL:
- EndDialog(hDlg, TRUE);
- return(TRUE);
-
- /*** On OK execute the search */
-
- case IDOK:
- GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
-
- /*** Get the search fields */
-
- GetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr, WORK_STR_LEN);
- TranslateString(TransSearchStr, SearchStr);
-
- CaseSensitive = IsDlgButtonChecked(hDlg, IDD_SEARCH_CASE);
-
- if (SearchStr[0] == '\0') return(TRUE);
-
- /*** Get current position in file and search */
-
- curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
- curpos = HIWORD(curpos);
- newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
-
- /*** If found highlight text */
-
- if (newpos >= 0)
- { newpos += curpos;
- endpos = newpos + strlen(TransSearchStr);
- CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
- }
-
- /*** If not display error */
-
- else
- { sprintf(WorkStr, "\"%s\" not found", SearchStr);
- MessageBox(hDlg, WorkStr, "Search", MB_OK);
- }
-
- return(TRUE);
- }
- }
-
- return(FALSE);
- }
-
-
- /***********************************************************************
- * Message handler for Replace dialog box *
- ***********************************************************************/
-
- BOOL FAR PASCAL ReplaceProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
- { long curpos, newpos, endpos;
- FARPROC lp_proc;
-
- switch (message)
- {
-
- /*** On initialisation fill fields with last search */
-
- case WM_INITDIALOG:
- SetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr);
- SetDlgItemText(hDlg, IDD_SEARCH_REPLACE, ReplaceStr);
- CheckDlgButton(hDlg, IDD_SEARCH_CASE, CaseSensitive);
- return(TRUE);
-
- case WM_COMMAND:
- switch (wParam)
- {
-
- /*** On Cancel close the box and return to editting */
-
- case IDCANCEL:
- EndDialog(hDlg, TRUE);
- return(TRUE);
-
- /*** On OK execute the replace */
-
- case IDOK:
- GetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr, WORK_STR_LEN);
- TranslateString(TransSearchStr, SearchStr);
- GetDlgItemText(hDlg, IDD_SEARCH_REPLACE, ReplaceStr, WORK_STR_LEN);
- TranslateString(TransReplaceStr, ReplaceStr);
-
- CaseSensitive = IsDlgButtonChecked(hDlg, IDD_SEARCH_CASE);
-
- if (SearchStr[0] == '\0') return(TRUE);
-
- EndDialog(hDlg, TRUE);
- lp_proc = MakeProcInstance(ConfirmProc, hInst);
- DialogBox(hInst, "ConfirmBox", hMainWnd, lp_proc);
- FreeProcInstance(lp_proc);
-
- return(TRUE);
- }
- }
-
- return(FALSE);
- }
-
-
- /***********************************************************************
- * Message handler for Confirm dialog box *
- ***********************************************************************/
-
- BOOL FAR PASCAL ConfirmProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
- { long curpos, newpos, endpos;
-
- switch (message)
- {
-
- /*** On initialisation show search expressions and do first search */
-
- case WM_INITDIALOG:
- GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
- curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
- curpos = HIWORD(curpos);
- newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
-
- if (newpos < 0)
- { sprintf(WorkStr, "\"%s\" not found", SearchStr);
- MessageBox(hDlg, WorkStr, "Replace", MB_OK);
- EndDialog(hDlg, TRUE);
- return(TRUE);
- }
-
- else
- { newpos += curpos;
- endpos = newpos + strlen(TransSearchStr);
- CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
- }
-
- sprintf(WorkStr, "Replace \"%s\"", SearchStr);
- SetDlgItemText(hDlg, IDD_CONFIRM_SEARCH, WorkStr);
- sprintf(WorkStr, "By \"%s\"", ReplaceStr);
- SetDlgItemText(hDlg, IDD_CONFIRM_REPLACE, WorkStr);
- return(TRUE);
-
- case WM_COMMAND:
- switch (wParam)
- {
-
- /*** On Quit close the box and return to editting */
-
- case IDD_CONFIRM_QUIT:
- EndDialog(hDlg, TRUE);
- return(TRUE);
-
- /*** On Yes replace text before then go on to next search */
-
- case IDD_CONFIRM_YES:
- CallWindowProc(CurWndProc, CurWnd, EM_REPLACESEL, 0, (long) TransReplaceStr);
-
- /*** On No go straight on to next search */
-
- case IDD_CONFIRM_NO:
- GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
- curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
- curpos = HIWORD(curpos);
- newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
-
- if (newpos < 0)
- { EndDialog(hDlg, TRUE);
- return(TRUE);
- }
- else
- { newpos += curpos;
- endpos = newpos + strlen(TransSearchStr);
- CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
- }
-
- return(TRUE);
-
- /*** On Global replace the lot */
-
- case IDD_CONFIRM_GLOBAL:
- for (;;)
- { CallWindowProc(CurWndProc, CurWnd, EM_REPLACESEL, 0, (long) TransReplaceStr);
-
- GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
- curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
- curpos = HIWORD(curpos);
- newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
-
- if (newpos < 0)
- { break;
- }
- else
- { newpos += curpos;
- endpos = newpos + strlen(TransSearchStr);
- CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
- }
- }
-
- EndDialog(hDlg, TRUE);
- return(TRUE);
- }
- }
-
- return(FALSE);
- }
-
-
- /***********************************************************************
- * Routine to translate flags in a string to characters *
- * *
- * Translates: *
- * \r to 0x0D *
- * \n to 0x0A *
- * \t to 0x09 *
- ***********************************************************************/
-
- void TranslateString(char *To, char *From)
- { int i, j;
-
- i = j = -1;
-
- do
- { i++;
- j++;
-
- if (From[i] == '\\')
- { i++;
-
- switch (From[i])
- { case 'R':
- case 'r':
- To[j] = '\r';
- break;
-
- case 'N':
- case 'n':
- To[j] = '\n';
- break;
-
- case 'T':
- case 't':
- To[j] = '\t';
- break;
-
- default:
- To[j] = From[--i];
- }
- }
-
- else
- { To[j] = From[i];
- }
- } while (To[j] != '\0');
- }
-
-
- /***********************************************************************
- * Routine to find a substring within a string *
- * *
- * Arguments: *
- * MainStr - LPSTR, string to be searched *
- * SubStr - LPSTR, string to search for *
- * Case - BOOL, specifies a case sensitive search *
- * *
- * Returns: *
- * If string found, the offset of the substring in the main string *
- * If not found, -1 *
- ***********************************************************************/
-
- long lstrstr(LPSTR MainStr, LPSTR SubStr, BOOL Case)
- { long i, j, k;
- char c, d;
-
- for (i = 0; MainStr[i] != '\0'; i++)
- { for (j = i, k = 0; SubStr[k] != '\0'; j++, k++)
- { c = MainStr[j];
- d = SubStr[k];
-
- if (!Case)
- { AnsiUpperBuff(&c, 1);
- AnsiUpperBuff(&d, 1);
- }
-
- if (c != d) break;
- }
-
- if (SubStr[k] == '\0') return(i);
- }
-
- return(-1);
- }
-
-