diff --git a/MainFrm.dfm b/MainFrm.dfm index 64e74658d53f7694a93c171729c0f2c6c5f2ebae..f2292b8c632bd0a26499a2b1a31760ddcc2e466a 100644 --- a/MainFrm.dfm +++ b/MainFrm.dfm @@ -17,262 +17,262 @@ object FrmMain: TFrmMain OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 16 - object Panel2: TPanel + object Splitter1: TSplitter Left = 0 - Top = 0 - Width = 209 - Height = 571 - Align = alLeft - BevelOuter = bvNone - TabOrder = 0 - object DriveComboBox1: TDriveComboBox - Left = 0 - Top = 0 - Width = 209 - Height = 22 - Align = alTop - DirList = DirectoryListBox1 - TabOrder = 0 - end - object DirectoryListBox1: TDirectoryListBox - Left = 0 - Top = 22 - Width = 209 - Height = 549 - Align = alClient - DirLabel = lblPath - TabOrder = 1 - end + Top = 333 + Width = 994 + Height = 3 + Cursor = crVSplit + Align = alTop + ExplicitLeft = 8 + ExplicitTop = 8 + ExplicitWidth = 986 end - object Panel3: TPanel - Left = 209 - Top = 0 - Width = 785 - Height = 571 - Align = alClient + object StatusBar1: TStatusBar + Left = 0 + Top = 544 + Width = 994 + Height = 27 + Panels = <> + ParentFont = True + SimplePanel = True + UseSystemFont = False + end + object memRule: TMemo + Left = 0 + Top = 121 + Width = 994 + Height = 212 + Align = alTop + BevelInner = bvNone BevelOuter = bvNone + Lines.Strings = ( + '# '#33539#20363#65306#23558'getId'#26367#25442#20026'getCode'#65292#27880#24847#65292#25152#26377#26597#25214#19982#26367#25442#22343#21306#20998#22823#23567#20889 + 'getId=>getCode' + 'getcode()=>getCode()') + ScrollBars = ssBoth TabOrder = 1 - object Panel1: TPanel - Left = 0 - Top = 0 - Width = 785 - Height = 89 - Align = alTop - BevelInner = bvRaised - BevelOuter = bvLowered - ParentColor = True - TabOrder = 0 - object Label1: TLabel - Left = 22 - Top = 48 - Width = 150 - Height = 16 - Caption = #25991#20214#31867#22411#65288#25193#23637#21517#65289#65306 - end - object Label2: TLabel - Left = 22 - Top = 19 - Width = 150 - Height = 16 - Caption = #28304#20195#30721#25991#20214#25152#22312#36335#24452#65306 - end - object lblPath: TLabel - Left = 188 - Top = 20 - Width = 129 - Height = 13 - Caption = 'C:\Windows\System32' - Font.Charset = DEFAULT_CHARSET - Font.Color = clRed - Font.Height = -11 - Font.Name = 'Tahoma' - Font.Style = [fsBold] - ParentFont = False - end - object edtFileType: TEdit - Left = 188 - Top = 47 - Width = 128 - Height = 24 + ExplicitLeft = 8 + ExplicitTop = 8 + ExplicitWidth = 986 + end + object PageControl1: TPageControl + Left = 0 + Top = 336 + Width = 994 + Height = 208 + ActivePage = TabSheet1 + Align = alClient + Style = tsFlatButtons + TabOrder = 2 + object TabSheet1: TTabSheet + Caption = #25991#20214#28165#21333 + object DBGrid1: TDBGrid + Left = 0 + Top = 0 + Width = 986 + Height = 174 + Align = alClient + DataSource = dsView + Options = [dgTitles, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgTitleClick, dgTitleHotTrack] + ReadOnly = True TabOrder = 0 - Text = '*.java' - end - object chkSubFolder: TCheckBox - Left = 336 - Top = 51 - Width = 217 - Height = 17 - Caption = #21253#25324#23376#30446#24405#19968#36215#25628#32034#26367#25442 - Checked = True - State = cbChecked - TabOrder = 1 - end - object btnSearch: TBitBtn - Left = 559 - Top = 27 - Width = 95 - Height = 30 - Caption = #25628#32034'(&S)' - TabOrder = 2 - OnClick = btnSearchClick - end - object btnReplace: TBitBtn - Left = 672 - Top = 27 - Width = 95 - Height = 30 - Caption = #26367#25442'(&R)' - TabOrder = 3 - OnClick = btnReplaceClick + TitleFont.Charset = DEFAULT_CHARSET + TitleFont.Color = clWindowText + TitleFont.Height = -13 + TitleFont.Name = 'Tahoma' + TitleFont.Style = [] + OnDblClick = DBGrid1DblClick + Columns = < + item + Expanded = False + FieldName = 'FileName' + Title.Alignment = taCenter + Width = 760 + Visible = True + end + item + Alignment = taCenter + Expanded = False + FieldName = 'Count' + Title.Alignment = taCenter + Width = 65 + Visible = True + end + item + Alignment = taCenter + Expanded = False + FieldName = 'Replace' + Title.Alignment = taCenter + Width = 95 + Visible = True + end> end end - object PageControl1: TPageControl - Left = 0 - Top = 89 - Width = 785 - Height = 482 - ActivePage = TabSheet1 - Align = alClient - Style = tsFlatButtons - TabOrder = 1 - object TabSheet1: TTabSheet - Caption = #25805#20316#21488 - object DBGrid1: TDBGrid - Left = 0 - Top = 0 - Width = 777 - Height = 448 - Align = alClient - DataSource = dsView - ReadOnly = True - TabOrder = 0 - TitleFont.Charset = DEFAULT_CHARSET - TitleFont.Color = clWindowText - TitleFont.Height = -13 - TitleFont.Name = 'Tahoma' - TitleFont.Style = [] - OnDblClick = DBGrid1DblClick - Columns = < - item - Expanded = False - FieldName = 'FileName' - Title.Alignment = taCenter - Width = 600 - Visible = True - end - item - Expanded = False - FieldName = 'Count' - Title.Alignment = taCenter - Width = 55 - Visible = True - end - item - Alignment = taCenter - Expanded = False - FieldName = 'Replace' - Title.Alignment = taCenter - Width = 75 - Visible = True - end> + object TabSheet2: TTabSheet + Caption = #26367#25442#27979#35797 + ImageIndex = 1 + ExplicitHeight = 295 + object Panel5: TPanel + Left = 0 + Top = 0 + Width = 986 + Height = 39 + Align = alTop + BevelOuter = bvLowered + ParentColor = True + TabOrder = 0 + object Label3: TLabel + Left = 14 + Top = 12 + Width = 75 + Height = 16 + Caption = #27979#35797#25991#20214#65306 end - end - object TabSheet2: TTabSheet - Caption = #26367#25442#37197#32622 - ImageIndex = 1 - object Splitter1: TSplitter - Left = 0 - Top = 212 - Width = 777 - Height = 3 - Cursor = crVSplit - Align = alTop - ExplicitWidth = 236 + object sbSelectFile: TSpeedButton + Left = 526 + Top = 7 + Width = 24 + Height = 24 + Caption = '...' + OnClick = sbSelectFileClick end - object memRule: TMemo - Left = 0 - Top = 0 - Width = 777 - Height = 212 - Align = alTop - BevelInner = bvNone - BevelOuter = bvNone - Lines.Strings = ( - '# '#33539#20363#65306#23558'getId'#26367#25442#20026'getCode'#65292#27880#24847#65292#25152#26377#26597#25214#19982#26367#25442#22343#21306#20998#22823#23567#20889 - 'getId=>getCode' - 'getcode()=>getCode()') - ScrollBars = ssBoth + object btnLoadFile: TBitBtn + Left = 584 + Top = 6 + Width = 75 + Height = 25 + Caption = #36733#20837'(&L)' TabOrder = 0 + OnClick = btnLoadFileClick end - object Panel4: TPanel - Left = 0 - Top = 215 - Width = 777 - Height = 233 - Align = alClient - BevelOuter = bvNone + object BitBtn2: TBitBtn + Left = 680 + Top = 6 + Width = 75 + Height = 25 + Caption = #27979#35797'(&T)' TabOrder = 1 - object Panel5: TPanel - Left = 0 - Top = 0 - Width = 777 - Height = 39 - Align = alTop - BevelOuter = bvNone - ParentColor = True - TabOrder = 0 - object Label3: TLabel - Left = 14 - Top = 12 - Width = 75 - Height = 16 - Caption = #27979#35797#25991#20214#65306 - end - object sbSelectFile: TSpeedButton - Left = 526 - Top = 7 - Width = 24 - Height = 24 - Caption = '...' - OnClick = sbSelectFileClick - end - object btnLoadFile: TBitBtn - Left = 584 - Top = 6 - Width = 75 - Height = 25 - Caption = #36733#20837'(&L)' - TabOrder = 0 - OnClick = btnLoadFileClick - end - object BitBtn2: TBitBtn - Left = 680 - Top = 6 - Width = 75 - Height = 25 - Caption = #27979#35797'(&T)' - TabOrder = 1 - OnClick = BitBtn2Click - end - object edtTestFile: TEdit - Left = 95 - Top = 7 - Width = 425 - Height = 24 - TabOrder = 2 - end - end - object Memo1: TMemo - Left = 0 - Top = 39 - Width = 777 - Height = 194 - Align = alClient - ScrollBars = ssBoth - TabOrder = 1 - end + OnClick = BitBtn2Click + end + object edtTestFile: TEdit + Left = 95 + Top = 7 + Width = 425 + Height = 24 + TabOrder = 2 end end + object Memo1: TMemo + Left = 0 + Top = 39 + Width = 986 + Height = 135 + Align = alClient + ScrollBars = ssBoth + TabOrder = 1 + ExplicitTop = 0 + end + end + end + object Panel1: TPanel + Left = 0 + Top = 0 + Width = 994 + Height = 121 + Align = alTop + BevelOuter = bvNone + ParentColor = True + TabOrder = 3 + ExplicitLeft = 8 + ExplicitTop = -84 + object Label1: TLabel + Left = 535 + Top = 19 + Width = 75 + Height = 16 + Caption = #25991#20214#31867#22411#65306 + end + object Label2: TLabel + Left = 22 + Top = 19 + Width = 120 + Height = 16 + Cursor = crHandPoint + Caption = #28304#20195#30721#25991#20214#36335#24452#65306 + Font.Charset = DEFAULT_CHARSET + Font.Color = clBlue + Font.Height = -13 + Font.Name = 'Tahoma' + Font.Style = [fsUnderline] + ParentFont = False + OnClick = Label2Click + end + object Label4: TLabel + Left = 22 + Top = 55 + Width = 105 + Height = 16 + Cursor = crHandPoint + Caption = #26367#25442#35268#21017#36873#25321#65306 + Font.Charset = DEFAULT_CHARSET + Font.Color = clBlue + Font.Height = -13 + Font.Name = 'Tahoma' + Font.Style = [fsUnderline] + ParentFont = False + OnClick = Label4Click + end + object edtFileType: TEdit + Left = 616 + Top = 16 + Width = 97 + Height = 24 + TabOrder = 0 + Text = '*.java' + end + object chkSubFolder: TCheckBox + Left = 160 + Top = 88 + Width = 553 + Height = 17 + Caption = #21253#25324#23376#30446#24405#19968#36215#25628#32034#26367#25442#12290 + Checked = True + State = cbChecked + TabOrder = 1 + end + object btnSearch: TBitBtn + Left = 751 + Top = 28 + Width = 95 + Height = 30 + Caption = #25628#32034'(&S)' + TabOrder = 2 + OnClick = btnSearchClick + end + object btnReplace: TBitBtn + Left = 872 + Top = 28 + Width = 95 + Height = 30 + Caption = #26367#25442'(&R)' + TabOrder = 3 + OnClick = btnReplaceClick + end + object edtCodeFolder: TEdit + Left = 160 + Top = 16 + Width = 359 + Height = 24 + TabOrder = 4 + end + object cboRuleFile: TComboBox + Left = 160 + Top = 49 + Width = 553 + Height = 24 + Style = csDropDownList + TabOrder = 5 end end object cdsView: TClientDataSet @@ -287,8 +287,8 @@ object FrmMain: TFrmMain IndexName = 'cdsViewIndex1' Params = <> StoreDefs = True - Left = 248 - Top = 152 + Left = 56 + Top = 192 object cdsViewFileName: TWideStringField FieldName = 'FileName' Size = 255 @@ -303,12 +303,22 @@ object FrmMain: TFrmMain end object dsView: TDataSource DataSet = cdsView - Left = 248 - Top = 216 + Left = 56 + Top = 256 end object OpenDialog1: TOpenDialog Filter = 'java file|*.java' - Left = 249 - Top = 280 + Left = 57 + Top = 320 + end + object OpenDialog2: TOpenDialog + Filter = 'search replace script|*.srs' + Left = 57 + Top = 384 + end + object OpenDialog3: TOpenDialog + Filter = 'any file|*.*' + Left = 57 + Top = 448 end end diff --git a/MainFrm.pas b/MainFrm.pas index 7f37939dc6ec44c2ca4c7da3367f34fd6fcc5520..fc6af5d8d5f7c7dd457e6348d266bac5c49ea8c6 100644 --- a/MainFrm.pas +++ b/MainFrm.pas @@ -20,34 +20,34 @@ type dsView: TDataSource; cdsViewFileName: TWideStringField; cdsViewCount: TIntegerField; - Panel2: TPanel; - DriveComboBox1: TDriveComboBox; - DirectoryListBox1: TDirectoryListBox; - Panel3: TPanel; - Panel1: TPanel; - Label1: TLabel; - Label2: TLabel; - lblPath: TLabel; - edtFileType: TEdit; - chkSubFolder: TCheckBox; - btnSearch: TBitBtn; - btnReplace: TBitBtn; + OpenDialog1: TOpenDialog; + cdsViewReplace: TBooleanField; + OpenDialog2: TOpenDialog; + OpenDialog3: TOpenDialog; + StatusBar1: TStatusBar; + memRule: TMemo; + Splitter1: TSplitter; PageControl1: TPageControl; TabSheet1: TTabSheet; DBGrid1: TDBGrid; TabSheet2: TTabSheet; - memRule: TMemo; - OpenDialog1: TOpenDialog; - Panel4: TPanel; Panel5: TPanel; Label3: TLabel; + sbSelectFile: TSpeedButton; btnLoadFile: TBitBtn; BitBtn2: TBitBtn; edtTestFile: TEdit; Memo1: TMemo; - Splitter1: TSplitter; - sbSelectFile: TSpeedButton; - cdsViewReplace: TBooleanField; + Panel1: TPanel; + Label1: TLabel; + Label2: TLabel; + Label4: TLabel; + edtFileType: TEdit; + chkSubFolder: TCheckBox; + btnSearch: TBitBtn; + btnReplace: TBitBtn; + edtCodeFolder: TEdit; + cboRuleFile: TComboBox; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure btnSearchClick(Sender: TObject); @@ -58,15 +58,19 @@ type procedure cdsViewReplaceGetText(Sender: TField; var Text: string; DisplayText: Boolean); procedure DBGrid1DblClick(Sender: TObject); + procedure Label4Click(Sender: TObject); + procedure Label2Click(Sender: TObject); private { Private declarations } rules: TList; replaceFlag: Boolean; + ruleFile: string; procedure searchDirectory(path: string); procedure appendFile(path: string; sr: TSearchRec); function getReplaceCount(fileName: string): Integer; function getReplaceText(const text: string): string; procedure resetRules; + procedure loadRuleFile(const fileName: string); public { Public declarations } end; @@ -111,19 +115,25 @@ var dataFile: string; begin self.PageControl1.ActivePageIndex := 0; + Application.Title := self.Caption; + StatusBar1.SimpleText := '提示:目前仅支持 UTF8 文件类型, 并请在替换前做好文件备份!'; ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini')); try + edtCodeFolder.Text := ini.ReadString('config', 'CodeFolder', ExtractFilePath(Application.ExeName)); self.chkSubFolder.Checked := ini.ReadBool('config', 'SubFolder', self.chkSubFolder.Checked); self.edtFileType.Text := ini.ReadString('config', 'FileType', self.edtFileType.Text); self.edtTestFile.Text := ini.ReadString('config', 'TestFile', self.edtTestFile.Text); + + dataFile := ChangeFileExt(Application.ExeName, '.srs'); + ruleFile := ini.ReadString('config', 'RuleFile', dataFile); + if FileExists(ruleFile) then + loadRuleFile(ruleFile) + else + ruleFile := dataFile; finally ini.Free; end; - dataFile := ChangeFileExt(Application.ExeName, '.srs'); - if FileExists(dataFile) then - memRule.Lines.LoadFromFile(dataFile); - cdsView.CreateDataSet; rules := TList.Create; end; @@ -131,20 +141,47 @@ end; procedure TFrmMain.FormDestroy(Sender: TObject); var ini: TIniFile; - dataFile: string; begin rules.Free; ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini')); try + ini.WriteString('config', 'CodeFolder', edtCodeFolder.Text); ini.WriteBool('config', 'SubFolder', self.chkSubFolder.Checked); ini.WriteString('config', 'FileType', self.edtFileType.Text); ini.WriteString('config', 'TestFile', self.edtTestFile.Text); + + memRule.Lines.SaveToFile(ruleFile); + ini.WriteString('config', 'RuleFile', ruleFile); finally ini.Free; end; +end; - dataFile := ChangeFileExt(Application.ExeName, '.srs'); - memRule.Lines.SaveToFile(dataFile); +procedure TFrmMain.loadRuleFile(const fileName: string); +var + sr: TSearchRec; + rulePath: string; +begin + if not FileExists(ruleFile) then + exit; + + ruleFile := fileName; + memRule.Lines.LoadFromFile(ruleFile); + + cboRuleFile.Items.Clear; + rulePath := ExtractFilePath(ruleFile); + if FindFirst(rulePath + '*.srs', faAnyFile, sr) = 0 then + begin + repeat + if FileExists(rulePath + sr.Name) then + begin + cboRuleFile.Items.Add(rulePath + sr.Name); + if ruleFile = (rulePath + sr.Name) then + cboRuleFile.ItemIndex := cboRuleFile.Items.Count - 1; + end; + until FindNext(sr) <> 0; + FindClose(sr); + end; end; procedure TFrmMain.BitBtn2Click(Sender: TObject); @@ -164,7 +201,7 @@ begin Memo1.Lines.Strings[i] := t2; end; end; - showMessage(Format('测试完成,共替换 %d 次', [count])); + StatusBar1.SimpleText := Format('测试完成,共替换 %d 次', [count]); end; procedure TFrmMain.btnLoadFileClick(Sender: TObject); @@ -176,21 +213,40 @@ begin exit; if not fileExists(fileName) then begin - showMessage('测试文件没有找到!'); + StatusBar1.SimpleText := '测试文件没有找到!'; exit; end; self.Memo1.Text := LoadUTF8File(fileName); end; procedure TFrmMain.btnSearchClick(Sender: TObject); +var + total: Integer; begin + if not System.SysUtils.DirectoryExists(edtCodeFolder.Text) then + begin + StatusBar1.SimpleText := Format('目录未找到:%s', [edtCodeFolder.Text]); + exit; + end; + try self.PageControl1.ActivePageIndex := 0; Screen.Cursor := crHourGlass; cdsView.EmptyDataSet; resetRules; - searchDirectory(self.lblPath.Caption); + if copy(edtCodeFolder.Text, length(edtCodeFolder.Text), 1) = '\' then + searchDirectory(copy(edtCodeFolder.Text, 1, length(edtCodeFolder.Text) - 1)) + else + searchDirectory(edtCodeFolder.Text); + + total := 0; cdsView.First; + while not cdsView.Eof do + begin + total := total + cdsView.FieldByName('Count').AsInteger; + cdsView.Next; + end; + StatusBar1.SimpleText := Format('共找到 %d 个文件,合计 %d 处替换点', [cdsView.RecordCount, total]); finally Screen.Cursor := crDefault; end; @@ -318,6 +374,24 @@ begin end; end; +procedure TFrmMain.Label2Click(Sender: TObject); +begin + if OpenDialog3.Execute then + begin + if fileExists(OpenDialog3.FileName) then + edtCodeFolder.Text := ExtractFilePath(OpenDialog3.FileName); + end; +end; + +procedure TFrmMain.Label4Click(Sender: TObject); +begin + if OpenDialog2.Execute then + begin + if fileExists(OpenDialog2.FileName) then + loadRuleFile(OpenDialog2.FileName); + end; +end; + procedure TFrmMain.cdsViewReplaceGetText(Sender: TField; var Text: string; DisplayText: Boolean); begin diff --git a/Win32/Debug/codereplace.exe b/Win32/Debug/codereplace.exe index 6be2e24446eddc13448bfad4f89a530f48e1abec..1ebf77b484cdc23cf5993b002d52a9843408cd72 100644 Binary files a/Win32/Debug/codereplace.exe and b/Win32/Debug/codereplace.exe differ