CnPack Forum


 
Subject: [分享]去掉行尾空格的脚本
ouyongke (I'm OuKe)
灌水科科长
Rank: 3Rank: 3


UID 12147
Digest Posts 1
Credits 102
Posts 38
点点分 102
Reading Access 10
Registered 2007-4-28
Location 湖南/北京
Status Offline
Post at 2008-5-2 14:40  Profile | Blog | P.M. 
[分享]去掉行尾空格的脚本

去掉行尾空格的脚本已经写好啦,先在这里发一下,代码没有做认真的处理。
原来PAS脚本是这样的,虽然还不是很看得懂,不过先拿他做个练习吧。

{*******************************************************}
{                                                       }
{       Pascal Script Source File                       }
{       Run by RemObjects Pascal Script in CnWizards    }
{                                                       }
{       Generated by CnPack IDE Wizards                 }
{                                                       }
{*******************************************************}
program TrimRightForFile;
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ToolsAPI, StdCtrls, ExtCtrls, Buttons, CnCommon, CnWizUtils, CnWizIdeUtils;
const
  SCnDefSourceMask = '.PAS;.DPR;CPP;.C;.HPP;.H;.CXX;.CC;.HXX;.HH;.ASM';

function TrimRight(const S: string): string;
var
  I: Integer;
begin
  if Trim(S) = '' then //不知道为什么,空串在下面的循环里报错
  begin
    Result := '';
    Exit;
  end;
  I := Length(S);
  while (I > 0) and (S[I] <= ' ') do
//    Dec(I);       //为什么不支持?
    I := I - 1;
  Result := Copy(S, 1, I)
end;

procedure ProcessFile(const FileName: string);
var
  Lines: TStringList;
  i: Integer;
  S: string;
begin
  Lines := TStringList.Create;
  try
    // todo: Process a file
    // IdeSetSourceByFileName(FileName, Lines, True);  // Write to file
    Lines.Text := IdeGetSourceByFileName(FileName);

    for i := 0 to Lines.Count - 1 do
      Lines[i] := TrimRight(Lines[i]);  //这里不支持,只好COPY一下改一改
    //写入文件
    IdeSetSourceByFileName(FileName, Lines, False);
  finally
    Lines.Free;
  end;
end;

procedure ProcessProject(Project: IOTAProject);
var
  i: Integer;
  FileName: string;
begin
  if Project = nil then Exit;
  ProcessFile(Project.GetFileName);        // 处理工程文件自身
//  ProcessFile(CnOtaGetFileNameOfCurrentModule(True));
  for i := 0 to Project.GetModuleCount - 1 do
  begin
    FileName := Project.GetModule(i).GetFileName;
    if IsSourceModule(FileName) then
      ProcessFile(FileName);
  end;
end;

procedure ProcessProjectGroup(ProjectGroup: IOTAProjectGroup);
var
  i: Integer;
begin
  if ProjectGroup = nil then Exit;
  for i := 0 to ProjectGroup.GetProjectCount - 1 do
  begin
    ProcessProject(ProjectGroup.GetProject(i));
  end;
end;

procedure ProcessOpenUnits;
var
  iModuleServices: IOTAModuleServices;
  i: Integer;
  FileName: string;
begin
  iModuleServices := IOTAModuleServices(BorlandIDEServices);
  for i := 0 to iModuleServices.GetModuleCount - 1 do
  begin
    FileName := CnOtaGetFileNameOfModule(iModuleServices.GetModule(i), True);
    ProcessFile(FileName);
  end;
end;

var
  FMasks: string;

procedure OnFindFile(const FileName: string;
  const Info: TSearchRec; var Abort: Boolean);
begin
  if FileMatchesExts(FileName, FMasks) then
  begin
    ProcessFile(FileName);
  end;
end;

procedure ProcessDir(const Dir, FileMask: string; IncludeSubDirs: Boolean);
begin
  if FileMask = '' then
    FMasks := SCnDefSourceMask
  else
    FMasks := FileMask;
  FindFile(Dir, '*.*', @OnFindFile, nil, IncludeSubDirs, False);
end;
var
  Form: TForm;
  rgKind: TRadioGroup;
  gbDir: TGroupBox;
  edtDir: TEdit;
  cbbMask: TComboBox;
  chkIncludeSubDirs: TCheckBox;
procedure btnSelectDirClick(Sender: TObject);
var
  NewDir: string;
begin
  NewDir := edtDir.Text;
  if GetDirectory('Select Directory', NewDir, True) then
    edtDir.Text := NewDir;
end;
begin
  Form := TForm.Create(nil);
  try
    with Form do
    begin
      ClientWidth := 400;
      ClientHeight := 262;
      Position := poScreenCenter;
      BorderStyle := bsDialog;
      Caption := 'TrimRight For File';
    end;
    rgKind := TRadioGroup.Create(Form);
    with rgKind do
    begin
      Parent := Form;
      SetBounds(8, 8, 385, 120);
      Caption := '&Kind';
      Items.Add('Current Unit');
      Items.Add('Opened Units');
      Items.Add('Project Units');
      Items.Add('Project Group Units');
      Items.Add('Files in Directory');
      ItemIndex := 0;
    end;
    gbDir := TGroupBox.Create(Form);
    with gbDir do
    begin
      Parent := Form;
      SetBounds(8, 132, 385, 89);
      Caption := '&Directory';
    end;
    with TLabel.Create(Form) do
    begin
      Parent := gbDir;
      SetBounds(8, 19, 70, 13);
      Caption := '&Directory:';
    end;
    with TLabel.Create(Form) do
    begin
      Parent := gbDir;
      SetBounds(8, 43, 70, 13);
      Caption := '&File Masks:';
    end;
    edtDir := TEdit.Create(Form);
    with edtDir do
    begin
      Parent := gbDir;
      SetBounds(88, 16, 257, 21);
    end;
    with TButton.Create(Form) do
    begin
      Parent := gbDir;
      SetBounds(352, 16, 21, 21);
      Caption := '...';
      OnClick := @btnSelectDirClick;
    end;
    cbbMask := TComboBox.Create(Form);
    with cbbMask do
    begin
      Parent := gbDir;
      SetBounds(88, 40, 257, 21);
      Items.Add('.pas;.dpr');
      Items.Add('.cpp;.c;.hpp;.h;.cxx;.cc;.hxx;.hh;.asm');
      Items.Add('.pas;.dpr;cpp;.c;.hpp;.h;.cxx;.cc;.hxx;.hh;.asm');
    end;
    chkIncludeSubDirs := TCheckBox.Create(Form);
    with chkIncludeSubDirs do
    begin
      Parent := gbDir;
      SetBounds(88, 64, 200, 17);
      Caption := 'Include &Sub Directories';
    end;
    with TBitBtn.Create(Form) do
    begin
      Parent := Form;
      SetBounds(220, 230, 80, 24);
      Kind := bkOk;
    end;
    with TBitBtn.Create(Form) do
    begin
      Parent := Form;
      SetBounds(308, 230, 80, 24);
      Kind := bkCancel;
    end;
    if Form.ShowModal = mrOk then
    begin
      case rgKind.ItemIndex of
        1:
          begin
            ProcessOpenUnits;
          end;
        2:
          begin
            ProcessProject(CnOtaGetCurrentProject);
          end;
        3:
          begin
            ProcessProjectGroup(CnOtaGetProjectGroup);
          end;
        4:
          begin
            ProcessDir(Trim(edtDir.Text), Trim(cbbMask.Text),
              chkIncludeSubDirs.Checked);
          end;
      else
        begin
          ProcessFile(CnOtaGetCurrentSourceFile);
        end;
      end;
    end;
  finally
    Form.Free;
  end;
end.






城市里,我像一颗尘埃,默默地存在.........
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6822
Posts 3584
点点分 6822
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2008-5-4 12:43  Profile | Blog | P.M. 
好东西。我们准备网站上开个脚本库,供用户交流下载脚本作品。
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6822
Posts 3584
点点分 6822
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2008-5-7 16:58  Profile | Blog | P.M. 
空串报错是因为Length为0,而S[0]在AnsiString中是不能直接访问的。
Top
kendling (小冬)
高级版主
Rank: 8Rank: 8
MyvNet


Medal No.1  
UID 703
Digest Posts 5
Credits 978
Posts 580
点点分 978
Reading Access 101
Registered 2005-2-18
Location 广东
Status Offline
Post at 2008-5-9 15:43  Profile | Site | Blog | P.M.  | QQ | Yahoo!
代码有点长。




小冬
http://MyvNet.com
Top
 




All times are GMT++8, the time now is 2024-10-31 08:53

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.006536 second(s), 7 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP