Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2009.12.06;
Скачать: CL | DM;

Вниз

Ошибка при обращении к StrList из своего объекта   Найти похожие ветки 

 
Алексей К.   (2008-04-24 09:22) [0]

Здравствуйте!
На днях написал модуль для работы с Inf-файлами и решил переписать него на KOL. Но тут трабла постоянно выскакивает access violation. вот привожу код переписанного модуля (некоторые ф-ии вырезаны из-за благого мата движка форума =)):


unit KOLInfFiles;

interface

uses KOL;

type

 PInfFile = ^TInfFile;

 TInfFile = object (TObj)
 public
   procedure LoadFromFile(const FileName: string);
   procedure Save;
   procedure SaveAs(const FileName: string);
   procedure ReadSections(Strings: PStrList);
// procedure ReadStrings(const Section: string; Strings: PStrList);
   procedure WriteString(const Section: string; const Ext: string);
   procedure WriteStrings(const Section: string; Strings: PStrList);
   procedure ReplaceString(const Section: string;
                           const Old: string; const New: string);
   procedure ReplaceStrings(const Section: string;
                            const Old: string; const New: PStrList);
   procedure DeleteString(const Section: string; const Target: string);
 protected
   FFileName: string;
   FFileBuffer: PStrList;
   procedure Init; virtual;
   destructor Destroy; virtual;
   function ParseComments(const S: string): string;
   function IsSection(const S: string): boolean;
   function GetSectionIndex(const Section: string): integer;
 //function RemoveStr(const Section: string; const Target: string): integer;
 end;

implementation

procedure TInfFile.Init;
begin
 FFileBuffer := NewStrList;
 FFileName := "";
end;

destructor TInfFile.Destroy;
begin
 FFileName := "";
 FFileBuffer.Free;
 inherited;
end;

function TInfFile.ParseComments(const S: string): string;
var
 i, len: integer;
 tmp: string;
begin
 len := length(S);
 i := 1;
 while (i < len+1) and (S[i] <> ";") do
 begin
   tmp := tmp + S[i];
   Inc(i);
 end;
 result := tmp;
end;

function TInfFile.IsSection(const S: string): boolean;
var
 tmp: string;
begin
 Result := false;
 if S <> "" then
 begin
   tmp := Trim(s);
   if (tmp[1] = "[") and (tmp[Length(tmp)] = "]") then
     Result := true;
 end;
end;

function TInfFile.GetSectionIndex(const Section: string): integer;
begin
 Result := FFileBuffer.IndexOf("["+Section+"]");
end;

procedure TInfFile.LoadFromFile(const FileName: string);
begin
 if FileExists(FileName) then
 begin
   FFileName := FileName;
   FFileBuffer.LoadFromFile(FFileName);
 end;
end;

procedure TInfFile.Save;
begin
 FFileBuffer.SaveToFile(FFileName);
end;

procedure TInfFile.SaveAs(const FileName: string);
begin
 FFileBuffer.SaveToFile(FFileName);
end;

procedure TInfFile.ReadSections(Strings: PStrList);
var
 i: integer;
 Section: string;
 BufCount: integer;
begin
 Strings.Clear;
 bufcount := FFileBuffer.Count;
 if BufCount > 0 then
 begin
   i := 0;
   while (i < BufCount-1) do
   begin
     Section := ParseComments(Trim(FFileBuffer.Items[i]));
     if Section <> "" then
     if IsSection(Section) then
     begin
       Delete(Section, 1, 1);
       Delete(Section, Length(Section), 1);
       Strings.Add(Section);
     end;
     Inc(i);
   end;
 end;
end;

procedure TInfFile.WriteString(const Section: string; const Ext: string);
var
 i: integer;
begin
 i := GetSectionIndex("["+Section+"]");
 if (i <> -1) then
 begin
   Inc(i);
   FFileBuffer.Insert(i, Ext);
 end else
 begin
   FFileBuffer.Add("");
   FFileBuffer.Add("["+Section+"]");
   FFileBuffer.Add(Ext);
 end;
end;

procedure TInfFile.WriteStrings(const Section: string; Strings: PStrList);
var
 i, j, count: integer;
begin
 i := GetSectionIndex("["+Section+"]");
 if (i <> -1) then
 begin
   Inc(i);
   count := Strings.Count;
   if count > 0 then
   for j := count-1 downto 0 do
     FFileBuffer.Insert(i, Strings.Items[j]);
 end else
 begin
   FFileBuffer.Add("");
   FFileBuffer.Add("["+Section+"]");
   FFileBuffer.AddStrings(Strings);
 end;
end;

{ !Изменено }
{ Если строка Old не будет найдена, New записана не будет! }
procedure TInfFile.ReplaceString(const Section: string; const Old: string; const New: string);
var
 i: integer;
 index: integer;
begin
 i := GetSectionIndex(Section);
 if (i <> -1) then
 begin
   index := RemoveStr(Section, Old);
   if (index <> -1) then
       FFileBuffer.Insert(index, New)
 end;
end;

procedure TInfFile.ReplaceStrings(const Section: string; const Old: string; const New: PStrList);
var
 index, count, j: integer;
begin
 index := RemoveStr(Section, Old);
 if (index <> -1) then
 begin
   count := New.Count;
   if count > 0 then
   for j := count-1 downto 0 do
   FFileBuffer.Insert(index, New.Items[j]);
 end;
end;

procedure TInfFile.DeleteString(const Section: string; const Target: string);
begin
 RemoveStr(Section, Target);
end;

end.


в программе объявляю (как и написано в книге):

var
Inf: PInfFile;
begin
...
New(inf); // тут вроде все нормально

а вот дальнейшая работа с объектом падает при любом обращении к FFileBuffer.

пара вопросов:
1) метод init нужно вызывать вручную? (если так, то я пробовал переместить его в public и обратиться к нему... - падает на  
FFileBuffer := NewStrList; =(
2) что я еще не правильно сделал, подправтьте пожалуйста, а то я толком не работал с KOL, но сейчас это действительно нужно ;)


 
Дмитрий К ©   (2008-04-24 09:56) [1]


> New(inf); // тут вроде все нормально

New(inf, Create);


 
Алексей К.   (2008-04-24 10:16) [2]

посиб!
просто когда я написал New(
Delphi мне автоматически выдал подсказку Var X: Pointer и я подумал, что более одного параметра New не принимает.. теперь будем знать ;)



Страницы: 1 вся ветка

Текущий архив: 2009.12.06;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.008 c
4-1224487927
worldmen
2008-10-20 11:32
2009.12.06
Вывести список компонент чужого окна.


15-1254414106
boriskb
2009-10-01 20:21
2009.12.06
И не сомневался даже... к сожалению


15-1255119652
Kolan
2009-10-10 00:20
2009.12.06
А кто-нибудь пробовал писать визуализаторы?


2-1255442299
Б
2009-10-13 17:58
2009.12.06
Чем писать лог?


11-1208498240
Дмитрий Пырин
2008-04-18 09:57
2009.12.06
Работа с потоками