home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / zap.arj / Z4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-12  |  13.4 KB  |  434 lines

  1. /***********************************************************************
  2. * Zap                                                                  *
  3. * ===                                                                  *
  4. *                                                                      *
  5. * Windows 3 Text Editor                                                *
  6. *                                                                      *
  7. * Misc editting commands module                                        *
  8. *   BOOL FAR PASCAL GotoLineProc(HWND, unsigned, WORD, LONG);          *
  9. *   long GetCurrentLine(int);                                          *
  10. *   void GotoLine(int, long);                                          *
  11. *                                                                      *
  12. *   BOOL FAR PASCAL SearchProc(HWND, unsigned, WORD, LONG);            *
  13. *   BOOL FAR PASCAL ReplaceProc(HWND, unsigned, WORD, LONG);           *
  14. *   BOOL FAR PASCAL ConfirmProc(HWND, unsigned, WORD, LONG);           *
  15. *   void TranslateString(char *, char *);                              *
  16. *   long lstrstr(LPSTR, LPSTR, BOOL);                                  *
  17. *                                                                      *
  18. * This programme was developed using Microsoft C6.0 and the Microsoft  *
  19. * SDK, however any ANSI C could be used if suitable libraries and the  *
  20. * Windows header are available.                                        *
  21. ***********************************************************************/
  22.  
  23. #include <windows.h>
  24. #include <string.h>
  25. #include "zap.h"
  26. #include "zapdlg.h"
  27.  
  28.  
  29. /***********************************************************************
  30. * External variables                                                   *
  31. ***********************************************************************/
  32.  
  33. extern HANDLE hInst;
  34. extern HWND   hMainWnd;
  35.  
  36. extern EDIT_WND EditWnd[MAX_FILES];
  37. extern int NumFiles, CurrentFile;
  38.  
  39. extern FARPROC lpNewEditWndProc;
  40.  
  41. extern LPSTR TransBuf;
  42.  
  43. extern int  CharWd, CharDp;
  44.  
  45. extern char WorkStr[WORK_STR_LEN];
  46.  
  47.  
  48. /***********************************************************************
  49. * Global variables                                                     *
  50. ***********************************************************************/
  51.  
  52. char SearchStr[WORK_STR_LEN],  TransSearchStr[WORK_STR_LEN],
  53.      ReplaceStr[WORK_STR_LEN], TransReplaceStr[WORK_STR_LEN];
  54.  
  55. BOOL CaseSensitive = FALSE;
  56.  
  57.  
  58. /***********************************************************************
  59. * Routine to go to a line number                                       *
  60. ***********************************************************************/
  61.  
  62. BOOL FAR PASCAL GotoLineProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  63. { WORD line;
  64.   char linestr[16];
  65.  
  66.   switch (message)
  67.   { case WM_INITDIALOG:
  68.       SendDlgItemMessage(hDlg, IDD_GOTO_LINE, EM_LIMITTEXT, 15, 0L);
  69.       line = GetCurrentLine((int) lParam);
  70.       SetDlgItemInt(hDlg, IDD_GOTO_LINE, line, FALSE);
  71.  
  72.       return(TRUE);
  73.  
  74.     case WM_COMMAND:
  75.       switch (wParam)
  76.       { case IDCANCEL:
  77.           EndDialog(hDlg, 0);
  78.           return(TRUE);
  79.  
  80.         case IDOK:
  81.           GetDlgItemText(hDlg, IDD_GOTO_LINE, linestr, 16);
  82.           if (sscanf(linestr, "%u", &line) != 1)
  83.           { sprintf(WorkStr, "Bad line number \"%s\"", linestr);
  84.             Comment(WorkStr);
  85.             return(TRUE);
  86.           }
  87.  
  88.           EndDialog(hDlg, line);
  89.           return(TRUE);
  90.       }
  91.   }
  92.  
  93.   return(FALSE);
  94. }
  95.  
  96.  
  97. /***********************************************************************
  98. * Routine to get the current line in an edit control                   *
  99. ***********************************************************************/
  100.  
  101. long GetCurrentLine(int Id)
  102. { long iL;
  103.  
  104.   iL = CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_GETSEL, 0, 0L);
  105.   return(CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_LINEFROMCHAR, LOWORD(iL), 0L) + 1);
  106. }
  107.  
  108.  
  109. /***********************************************************************
  110. * Routine to go to a line in an edit control                           *
  111. ***********************************************************************/
  112.  
  113. void GotoLine(int Id, long Line)
  114. { WORD iW;
  115.  
  116.   if (Line > 0)
  117.   { iW = CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_LINEINDEX, (WORD) (Line - 1), 0L);
  118.     CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_SETSEL, 0, MAKELONG(iW, iW));
  119.   }
  120. }
  121.  
  122.  
  123. /***********************************************************************
  124. * Message handler for Search dialog box                                *
  125. ***********************************************************************/
  126.  
  127. BOOL FAR PASCAL SearchProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  128. { long curpos, newpos, endpos;
  129.  
  130.   switch (message)
  131.   {
  132.  
  133. /*** On initialisation fill the search field */
  134.  
  135.     case WM_INITDIALOG:
  136.       SetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr);
  137.       CheckDlgButton(hDlg, IDD_SEARCH_CASE, CaseSensitive);
  138.       return(TRUE);
  139.  
  140.     case WM_COMMAND:
  141.       switch (wParam)
  142.       {
  143.  
  144. /*** On CANCEL close the box and return to editting */
  145.  
  146.         case IDCANCEL:
  147.           EndDialog(hDlg, TRUE);
  148.           return(TRUE);
  149.  
  150. /*** On OK execute the search */
  151.  
  152.         case IDOK:
  153.           GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
  154.  
  155. /*** Get the search fields */
  156.  
  157.           GetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr, WORK_STR_LEN);
  158.           TranslateString(TransSearchStr, SearchStr);
  159.  
  160.           CaseSensitive = IsDlgButtonChecked(hDlg, IDD_SEARCH_CASE);
  161.  
  162.           if (SearchStr[0] == '\0') return(TRUE);
  163.  
  164. /*** Get current position in file and search */
  165.  
  166.           curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
  167.           curpos = HIWORD(curpos);
  168.           newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
  169.  
  170. /*** If found highlight text */
  171.  
  172.           if (newpos >= 0)
  173.           { newpos += curpos;
  174.             endpos = newpos + strlen(TransSearchStr);
  175.             CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
  176.           }
  177.  
  178. /*** If not display error */
  179.  
  180.           else
  181.           { sprintf(WorkStr, "\"%s\" not found", SearchStr);
  182.             MessageBox(hDlg, WorkStr, "Search", MB_OK);
  183.           }
  184.  
  185.           return(TRUE);
  186.       }
  187.   }
  188.  
  189.   return(FALSE);
  190. }
  191.  
  192.  
  193. /***********************************************************************
  194. * Message handler for Replace dialog box                               *
  195. ***********************************************************************/
  196.  
  197. BOOL FAR PASCAL ReplaceProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  198. { long curpos, newpos, endpos;
  199.   FARPROC lp_proc;
  200.  
  201.   switch (message)
  202.   {
  203.  
  204. /*** On initialisation fill fields with last search */
  205.  
  206.     case WM_INITDIALOG:
  207.       SetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr);
  208.       SetDlgItemText(hDlg, IDD_SEARCH_REPLACE, ReplaceStr);
  209.       CheckDlgButton(hDlg, IDD_SEARCH_CASE, CaseSensitive);
  210.       return(TRUE);
  211.  
  212.     case WM_COMMAND:
  213.       switch (wParam)
  214.       {
  215.  
  216. /*** On Cancel close the box and return to editting */
  217.  
  218.         case IDCANCEL:
  219.           EndDialog(hDlg, TRUE);
  220.           return(TRUE);
  221.  
  222. /*** On OK execute the replace */
  223.  
  224.         case IDOK:
  225.           GetDlgItemText(hDlg, IDD_SEARCH_SEARCH, SearchStr, WORK_STR_LEN);
  226.           TranslateString(TransSearchStr, SearchStr);
  227.           GetDlgItemText(hDlg, IDD_SEARCH_REPLACE, ReplaceStr, WORK_STR_LEN);
  228.           TranslateString(TransReplaceStr, ReplaceStr);
  229.  
  230.           CaseSensitive = IsDlgButtonChecked(hDlg, IDD_SEARCH_CASE);
  231.  
  232.           if (SearchStr[0] == '\0') return(TRUE);
  233.  
  234.           EndDialog(hDlg, TRUE);
  235.           lp_proc = MakeProcInstance(ConfirmProc, hInst);
  236.           DialogBox(hInst, "ConfirmBox", hMainWnd, lp_proc);
  237.           FreeProcInstance(lp_proc);
  238.  
  239.           return(TRUE);
  240.       }
  241.   }
  242.  
  243.   return(FALSE);
  244. }
  245.  
  246.  
  247. /***********************************************************************
  248. * Message handler for Confirm dialog box                               *
  249. ***********************************************************************/
  250.  
  251. BOOL FAR PASCAL ConfirmProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  252. { long curpos, newpos, endpos;
  253.  
  254.   switch (message)
  255.   {
  256.  
  257. /*** On initialisation show search expressions and do first search */
  258.  
  259.     case WM_INITDIALOG:
  260.       GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
  261.       curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
  262.       curpos = HIWORD(curpos);
  263.       newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
  264.  
  265.       if (newpos < 0)
  266.       { sprintf(WorkStr, "\"%s\" not found", SearchStr);
  267.         MessageBox(hDlg, WorkStr, "Replace", MB_OK);
  268.         EndDialog(hDlg, TRUE);
  269.         return(TRUE);
  270.       }
  271.  
  272.       else
  273.       { newpos += curpos;
  274.         endpos = newpos + strlen(TransSearchStr);
  275.         CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
  276.       }
  277.  
  278.       sprintf(WorkStr, "Replace \"%s\"", SearchStr);
  279.       SetDlgItemText(hDlg, IDD_CONFIRM_SEARCH, WorkStr);
  280.       sprintf(WorkStr, "By \"%s\"", ReplaceStr);
  281.       SetDlgItemText(hDlg, IDD_CONFIRM_REPLACE, WorkStr);
  282.       return(TRUE);
  283.  
  284.     case WM_COMMAND:
  285.       switch (wParam)
  286.       {
  287.  
  288. /*** On Quit close the box and return to editting */
  289.  
  290.         case IDD_CONFIRM_QUIT:
  291.           EndDialog(hDlg, TRUE);
  292.           return(TRUE);
  293.  
  294. /*** On Yes replace text before then go on to next search */
  295.  
  296.         case IDD_CONFIRM_YES:
  297.           CallWindowProc(CurWndProc, CurWnd, EM_REPLACESEL, 0, (long) TransReplaceStr);
  298.  
  299. /*** On No go straight on to next search */
  300.  
  301.         case IDD_CONFIRM_NO:
  302.           GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
  303.           curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
  304.           curpos = HIWORD(curpos);
  305.           newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
  306.  
  307.           if (newpos < 0)
  308.           { EndDialog(hDlg, TRUE);
  309.             return(TRUE);
  310.           }
  311.           else
  312.           { newpos += curpos;
  313.             endpos = newpos + strlen(TransSearchStr);
  314.             CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
  315.           }
  316.  
  317.           return(TRUE);
  318.  
  319. /*** On Global replace the lot */
  320.  
  321.         case IDD_CONFIRM_GLOBAL:
  322.           for (;;)
  323.           { CallWindowProc(CurWndProc, CurWnd, EM_REPLACESEL, 0, (long) TransReplaceStr);
  324.  
  325.             GetWindowText(CurWnd, TransBuf, TRANS_BUF_SIZE);
  326.             curpos = CallWindowProc(CurWndProc, CurWnd, EM_GETSEL, 0, 0);
  327.             curpos = HIWORD(curpos);
  328.             newpos = lstrstr(TransBuf + curpos, TransSearchStr, CaseSensitive);
  329.  
  330.             if (newpos < 0)
  331.             { break;
  332.             }
  333.             else
  334.             { newpos += curpos;
  335.               endpos = newpos + strlen(TransSearchStr);
  336.               CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(newpos, endpos));
  337.             }
  338.           }
  339.  
  340.           EndDialog(hDlg, TRUE);
  341.           return(TRUE);
  342.       }
  343.   }
  344.  
  345.   return(FALSE);
  346. }
  347.  
  348.  
  349. /***********************************************************************
  350. * Routine to translate flags in a string to characters                 *
  351. *                                                                      *
  352. * Translates:                                                          *
  353. *   \r to 0x0D                                                         *
  354. *   \n to 0x0A                                                         *
  355. *   \t to 0x09                                                         *
  356. ***********************************************************************/
  357.  
  358. void TranslateString(char *To, char *From)
  359. { int i, j;
  360.  
  361.   i = j = -1;
  362.  
  363.   do
  364.   { i++;
  365.     j++;
  366.  
  367.     if (From[i] == '\\')
  368.     { i++;
  369.  
  370.       switch (From[i])
  371.       { case 'R':
  372.         case 'r':
  373.           To[j] = '\r';
  374.           break;
  375.  
  376.         case 'N':
  377.         case 'n':
  378.           To[j] = '\n';
  379.           break;
  380.  
  381.         case 'T':
  382.         case 't':
  383.           To[j] = '\t';
  384.           break;
  385.  
  386.         default:
  387.           To[j] = From[--i];
  388.       }
  389.     }
  390.  
  391.     else
  392.     { To[j] = From[i];
  393.     }
  394.   } while (To[j] != '\0');
  395. }
  396.  
  397.  
  398. /***********************************************************************
  399. * Routine to find a substring within a string                          *
  400. *                                                                      *
  401. * Arguments:                                                           *
  402. *  MainStr - LPSTR, string to be searched                              *
  403. *  SubStr  - LPSTR, string to search for                               *
  404. *  Case    - BOOL,  specifies a case sensitive search                  *
  405. *                                                                      *
  406. * Returns:                                                             *
  407. *  If string found, the offset of the substring in the main string     *
  408. *  If not found,    -1                                                 *
  409. ***********************************************************************/
  410.  
  411. long lstrstr(LPSTR MainStr, LPSTR SubStr, BOOL Case)
  412. { long i, j, k;
  413.   char c, d;
  414.  
  415.   for (i = 0; MainStr[i] != '\0'; i++)
  416.   { for (j = i, k = 0; SubStr[k] != '\0'; j++, k++)
  417.     { c = MainStr[j];
  418.       d = SubStr[k];
  419.  
  420.       if (!Case)
  421.       { AnsiUpperBuff(&c, 1);
  422.         AnsiUpperBuff(&d, 1);
  423.       }
  424.  
  425.       if (c != d) break;
  426.     }
  427.  
  428.     if (SubStr[k] == '\0') return(i);
  429.   }
  430.  
  431.   return(-1);
  432. }
  433.  
  434.