⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.pas

📁 一个可以把源代码以语法高亮的形式转换成HTML格式或RTF格式。
💻 PAS
📖 第 1 页 / 共 4 页
字号:
  LStream: TMemoryStream;
  Convstion: TSourceToConfigConv;
begin
  Exit;
  // ToDo: 取消
  LStream := TMemoryStream.Create;
  Convstion := TSourceToConfigConv.Create;
  try
    Convstion.ObjPropertyToXML(LStream, Self);
    LStream.SaveToFile(FFileName);
  finally
    LStream.Free;
    Convstion.Free;
  end;
end;

procedure TSourceToConfig.DelAcciItem(Index: Integer);
begin
  if (Index >= FAcciList.Count) or (Index < 0) then
    raise Exception.Create(pubGet('List_OutOfBounds'))
  else
    FAcciList.Delete(Index);
end;

function TSourceToConfig.AddAcciItem(Item: Pointer): Integer;
begin
  Result := FAcciList.Add(Item);
end;

function TSourceToConfig.GetAcciCount: Integer;
begin
  Result := FAcciList.Count;
end;

function TSourceToConfig.GetAcciItemRec(Index: Integer): PItemRec;
begin
  if (Index >= FAcciList.Count) or (Index < 0) then
    raise Exception.Create(pubGet('List_OutOfBounds'))
  else
    Result := PItemRec(FAcciList.Items[Index])
end;

function TSourceToConfig.GetItemByName(AName: string): PItemRec;
var
  i: Integer;
begin
  Result := nil;
  for i := 0 to AcciList.Count - 1 do
    if AcciItem[i]^.Name = AName then
    begin
      Result := AcciItem[i];
      Break;
    end;
end;

function TSourceToConfig.RemoveAcciItem(Item: Pointer): Integer;
begin
  Result := FAcciList.Remove(Item);
end;

procedure TSourceToConfig.SetAcciItemRec(Index: Integer;
  const Value: PItemRec);
begin
  if (Index >= FAcciList.Count) or (Index < 0) then
    raise Exception.Create(pubGet('List_OutOfBounds'))
  else
    FAcciList.Items[Index] := Value;
end;


function TSourceToConfig.GetCfgValue(const Section, Name: string;
  Default: string): string;
begin
  //
end;
 }

{ TSourceToConfigConv 程序配置文件转换 

procedure TSourceToConfigConv.ElementsToObjProperty(Obj: TObject);
var
  i: Integer;
  LEle, LCEle: IDOMElement;
  ItemRec: PItemRec;
  AShell: TShellRec;
begin
  // XML -> Obj
  inherited;
  with TSourceToConfig(Obj), FXMLDoc.DOMDocument do
  begin
    // 语言
    Lang := EleAttrToStrDef(GetSingleEleByTagName(documentElement, 'Lang'),  'Value', 'zh');
    // 取得Shell设置
    LEle := GetSingleEleByTagName(documentElement, 'Shell');
    AShell.AddPop := EleAttrToBoolDef(LEle, 'AddPop', False);
    AShell.RunST := EleAttrToBoolDef(LEle, 'RunST', False);
    AShell.RunAuto := EleAttrToBoolDef(LEle, 'RunAuto', False);
    AShell.AccidenceFile := EleAttrToStrDef(LEle, 'AccidenceFile', '');
    AShell.DestFileType := EleAttrToStrDef(LEle, 'DestFileType', '');
    Shell := AShell;
    // 取得词法文件列表
    LEle := GetSingleEleByTagName(documentElement, 'AccidenceFiles');
    if LEle <> nil then
      for i := 0 to LEle.childNodes.length - 1 do
      begin
        if LEle.childNodes[i].nodeType = ELEMENT_NODE then
        begin
          New(ItemRec);
          LCEle := IDOMElement(LEle.childNodes[i]);
          ItemRec^.Name := EleAttrToStrDef(LCEle, 'Name', '');
          ItemRec^.FilePath := EleAttrToStrDef(LCEle, 'FilePath', '');
          AddAcciItem(ItemRec);
        end;
      end;
  end;
end;

procedure TSourceToConfigConv.GetDefaultXMLStream(XMLStream: TStream);
var
  PNode, CNode: IDOMElement;
begin
  // defaultXML -> Obj
  with FXMLDoc do
  begin
    XML.Clear;
    Active := True;
    Encoding := 'UTF-8';
    PNode := DOMDocument.createElement('SourceToConfig');
    DOMDocument.documentElement := PNode;
    CNode := AppendChildElement('Lang', '', PNode);
    CNode.setAttribute('Value', 'zh');
    CNode := AppendChildElement('Shell', '', PNode);
    CNode.setAttribute('AddPop', '0');
    CNode.setAttribute('RunST', '0');
    CNode.setAttribute('RunAuto', '0');
    CNode.setAttribute('AccidenceFile', '');
    CNode.setAttribute('DestFileType', 'HTML');
    AppendChildElement('AccidenceFiles', '', PNode);
  end;
  FXMLDoc.SaveToStream(XMLStream);
end;

procedure TSourceToConfigConv.ObjPropertyToElements(Obj: TObject);
var
  PNode,CNode: IDOMElement;
  i: Integer;
begin
  // Obj -> XML
  with TSourceToConfig(Obj), FXMLDoc.DOMDocument do
  begin
    PNode := createElement('SourceToConfig');
    documentElement := PNode;
    // 语言
    CNode := AppendChildElement('Lang', '', PNode);
    CNode.setAttribute('Value', Lang);               // ToDo: 替换 ini !
    // 外壳
    CNode := AppendChildElement('Shell', '', PNode);
    CNode.setAttribute('AddPop', BoolToStr(Shell.AddPop));
    CNode.setAttribute('RunST', BoolToStr(Shell.RunST));
    CNode.setAttribute('RunAuto', BoolToStr(Shell.RunAuto));
    CNode.setAttribute('AccidenceFile', Shell.AccidenceFile);
    CNode.setAttribute('DestFileType', Shell.DestFileType);
    // 词法文件配置
    CNode := AppendChildElement('AccidenceFiles', '', PNode);
    PNode := CNode;
    for i := 0 to AcciCount - 1 do
    begin
      CNode := AppendChildElement('AccidenceFile', '', PNode);
      CNode.setAttribute('Name', PItemRec(AcciItem[i])^.Name);
      CNode.setAttribute('FilePath', PItemRec(AcciItem[i])^.FilePath);
    end;
  end;
end;
}
{ TAccidenceConfigConv 词法配置文件转换 }

procedure TAccidenceConfigConv.ElementsToObjProperty(Obj: TObject);
var
  LEle, LEle1: IDOMElement;
  i, j: Integer;
  KWConfig: TKeyWordConfig;
  SMConfig: TSymbolConfig;
  
  procedure SetFontConfig(AFontConfig: TFontConfig; AEle: IDOMElement);
  var
    LStr: string;
  begin
    with AFontConfig do
    begin
      FontName := EleAttrToStrDef(AEle, 'FontName', Def_FontName);
      FontColor := EleAttrToIntDef(AEle, 'FontColor', clBlack);
      FontSize := EleAttrToIntDef(AEle, 'FontSize', Def_FontSize);
      LStr := EleAttrToStrDef(AEle, 'FontStyle', '');
      if Pos('Bold', LStr) > 0 then
        Include(FontStyle, fsBold);
      if Pos('Italic', LStr) > 0 then
        Include(FontStyle, fsItalic);
      if Pos('Underline', LStr) > 0 then
        Include(FontStyle, fsUnderline);
      if Pos('StrikeOut', LStr) > 0 then
        Include(FontStyle, fsStrikeOut);
    end;
  end;  // Local

begin   // Main
  // XML -> Obj
  with FXMLDoc.DOMDocument, TAccidenceConfig(Obj) do
  begin
    // 常规
    LEle := GetSingleEleByTagName(documentElement, 'General');
    if LEle = nil then
      raise EXMLDocError.Create(pubGet('Err_AcciInvalide'));
    GeneralConfig.IgnoreCase := EleAttrToBoolDef(LEle,  'IgnoreCase', False);
    GeneralConfig.ShowLine := EleAttrToBoolDef(LEle,  'ShowLine', False);
    GeneralConfig.BGround := EleAttrToIntDef(LEle, 'BGColor', clWhite);
    SetFontConfig(GeneralConfig.FFontConfig, LEle);
    // 数字
    LEle := GetSingleEleByTagName(documentElement, 'Nubmer');
    if LEle = nil then
      raise EXMLDocError.Create(pubGet('Err_AcciInvalide'));
    SetFontConfig(NumberConfig.FFontConfig, LEle);
    // 关键字
    LEle := GetSingleEleByTagName(documentElement, 'KeyWords');
    if LEle = nil then
      raise EXMLDocError.Create(pubGet('Err_AcciInvalide'));
    for i := 0 to LEle.childNodes.length - 1 do
    begin
      LEle1 := IDOMElement(LEle.childNodes[i]);
      KWConfig := TKeyWordConfig.Create;
      KWConfig.Name := EleAttrToStrDef(LEle1, 'Name', '');
      SetFontConfig(KWConfig.FontConfig, LEle1);
      for j := 0 to LEle1.childNodes.length - 1 do
        KWConfig.Values.Add(EleAttrToStrDef(IDOMEleMent(LEle1.childNodes[j]), 'Value', ''));
      KeyWords.Add(KWConfig);
    end;
    // 符号
    LEle := GetSingleEleByTagName(documentElement, 'Symbols');
    if LEle = nil then
      raise EXMLDocError.Create(pubGet('Err_AcciInvalide'));
    for i := 0 to LEle.childNodes.length - 1 do
    begin
      LEle1 := IDOMElement(LEle.childNodes[i]);
      SMConfig := TSymbolConfig.Create;
      SMConfig.Name := EleAttrToStrDef(LEle1, 'Name', '');
      SMConfig.Range := EleAttrToStrDef(LEle1, 'Range', 'OneWord');
      SMConfig.DoubleSymbol := EleAttrToBoolDef(LEle1, 'DoubleSymbol', False);
      SMConfig.HightLight := EleAttrToStrDef(LEle1, 'HightLight', 'Both');
      SMConfig.BeginValue := EleAttrToStrDef(LEle1, 'BeginValue', '');
      SMConfig.EndValue := EleAttrToStrDef(LEle1, 'EndValue', '');
      SMConfig.ESC := EleAttrToStrDef(LEle1, 'ESC', '');
      SetFontConfig(SMConfig.FontConfig, LEle1);
      Symbols.Add(SMConfig);
    end;
  end;
end;

procedure TAccidenceConfigConv.ObjPropertyToElements(Obj: TObject);
var
  PEle, CEle, CEle1: IDOMElement;
  LStr: string;
  i, j: Integer;
  KWConfig: TKeyWordConfig;
  SymbolConfig: TSymbolConfig;

  Function GetFontStyleAttr(AFontStyle: TFontStyles): string;
  begin
    Result := '';
    if fsBold in AFontStyle then
      Result :=  ',Bold';
    if fsItalic in AFontStyle then
      Result := Result + ',Italic';
    if fsUnderline in AFontStyle then
      Result := Result + ',Underline';
    if fsStrikeOut in AFontStyle then
      Result := Result + ',StrikeOut';
    if Result<>'' then Delete(Result,1,1);
  end;  // Local

begin   // Main
  // Obj -> XML
  with FXMLDoc.DOMDocument, TAccidenceConfig(Obj) do
  begin
    PEle := createElement('Accidences');
    // 常规
    documentElement := PEle;
    CEle := AppendChildElement('General', '', PEle);
    CEle.setAttribute('IgnoreCase', BoolToStr(GeneralConfig.IgnoreCase));
    CEle.setAttribute('ShowLine', BoolToStr(GeneralConfig.ShowLine));
    CEle.setAttribute('BGColor', IntToStr(GeneralConfig.FBGround));
    CEle.setAttribute('FontName', GeneralConfig.FontConfig.FontName);
    CEle.setAttribute('FontColor', IntToStr(GeneralConfig.FontConfig.FontColor));
    CEle.setAttribute('FontSize', IntToStr(GeneralConfig.FontConfig.FontSize));
    LStr := GetFontStyleAttr(GeneralConfig.FontConfig.FontStyle);
    CEle.setAttribute('FontStyle', LStr);
    // 数字
    CEle := AppendChildElement('Nubmer', '', PEle);
    CEle.setAttribute('FontName', NumberConfig.FontConfig.FontName);
    CEle.setAttribute('FontColor', IntToStr(NumberConfig.FontConfig.FontColor));
    CEle.setAttribute('FontSize', IntToStr(NumberConfig.FontConfig.FontSize));
    LStr := GetFontStyleAttr(NumberConfig.FontConfig.FontStyle);
    CEle.setAttribute('FontStyle', LStr);
    // 关键字
    PEle := AppendChildElement('KeyWords', '', PEle);
    for i := 0 to FKeyWords.Count - 1 do
    begin
      KWConfig := TKeyWordConfig(FKeyWords.Items[i]);
      CEle := AppendChildElement('KeyWord', '', PEle);
      CEle.setAttribute('Name', KWConfig.Name);
      CEle.setAttribute('FontName', KWConfig.FontConfig.FontName);
      CEle.setAttribute('FontColor', IntToStr(KWConfig.FontConfig.FontColor));
      CEle.setAttribute('FontSize', IntToStr(KWConfig.FontConfig.FontSize));
      LStr := GetFontStyleAttr(KWConfig.FontConfig.FontStyle);
      CEle.setAttribute('FontStyle', LStr);
      for j := 0 to KWConfig.FValues.Count - 1 do
      begin
        CEle1 := AppendChildElement('Item', '', CEle);
        CEle1.setAttribute('Value', KWConfig.FValues.Strings[j]);
      end;
    end;
    // 符号
    PEle := AppendChildElement('Symbols', '', documentElement);
    for i := 0 to Symbols.Count - 1 do
    begin
      SymbolConfig := TSymbolConfig(Symbols.Items[i]);
      CEle := AppendChildElement('Symbol', '', PEle);
      CEle.setAttribute('Name', SymbolConfig.Name);
      CEle.setAttribute('Range', SymbolConfig.Range);
      CEle.setAttribute('DoubleSymbol', BoolToStr(SymbolConfig.DoubleSymbol));
      CEle.setAttribute('HightLight', SymbolConfig.HightLight);
      CEle.setAttribute('BeginValue', SymbolConfig.BeginValue);
      CEle.setAttribute('EndValue', SymbolConfig.EndValue);
      CEle.setAttribute('FontName', SymbolConfig.FontConfig.FontName);
      CEle.setAttribute('FontColor', IntToStr(SymbolConfig.FontConfig.FontColor));
      CEle.setAttribute('FontSize', IntToStr(SymbolConfig.FontConfig.FontSize));
      LStr := GetFontStyleAttr(SymbolConfig.FontConfig.FontStyle);
      CEle.setAttribute('FontStyle', LStr);
      CEle.setAttribute('ESC', SymbolConfig.ESC);
    end;
  end;
end;

{ TAccidenceConfig 词法配置文件}

constructor TAccidenceConfig.Create(AcciName :string);
begin
  FAccidenceName := AcciName;
  FGeneralConfig := TGneralConfig.Create;
  FNumberConfig := TNumberConfig.Create;
  FKeyWords := TObjectList.Create;
  FSymbols := TObjectList.Create;
end;

destructor TAccidenceConfig.Destroy;
begin
  FGeneralConfig.Free;
  FNumberConfig.Free;
  FKeyWords.Free;
  FSymbols.Free;
  inherited;
end;

procedure TAccidenceConfig.Clear;
begin
  FKeyWords.Clear;
  FSymbols.Clear;
end;

procedure TAccidenceConfig.LoadFromFile(FileName: string);
var
  LStream: TMemoryStream;
  Conversion: TAccidenceConfigConv;
begin
  // XML -> Obj
  Clear;
  LStream := TMemoryStream.Create;
  Conversion := TAccidenceConfigConv.Create;
  try
    LStream.LoadFromFile(FileName);
    Conversion.XMLToObjProperty(LStream, self);
  finally
    LStream.Free;
    Conversion.Free;
  end;
end;

procedure TAccidenceConfig.SaveToFile(FileName: string);
var
  LStream: TMemoryStream;
  Conversion: TAccidenceConfigConv;
begin
  // Obj -> XML
  LStream := TMemoryStream.Create;
  Conversion := TAccidenceConfigConv.Create;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -