Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Delphi программирование > Общие вопросы Delphi
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 19.01.2008, 21:06   #1
Antonio86
 
Регистрация: 18.01.2008
Сообщений: 5
Сообщение Поиск в файле необходимого значения

Доброго времени суток!
возникла следующая проблема: необходимо открыть текстовый файл и вытянуть фразу, которая стоит после введенного слова.
мое решение данной задачи выглядит так:
unit Unit1;

interface

uses
Windows, SysUtils, Classes, Controls, Graphics, Forms, ImgList, TypInfo, Variants;

type
TFWResources = class(TObject)
private
FNames: TStringList;
FValues: TStringList;
public
constructor Create;
destructor Destroy; override;
function Get(const StrName: string): string;
function FindRead(tt : string):string;
procedure Clear;
procedure LocalizeForm(Form: TForm);
end;

function FWResources: TFWResources;
procedure Ts_init(P: PChar; m: Integer);
function Ts_Search(Text, p: PChar; m: Integer; Start: Longint): Longint;

implementation


var
FResources: TFWResources = nil;
shift: array[0..255] of Byte;
Look_At: Integer;
constructor TFWResources.Create;
begin
inherited;
FNames := TStringList.Create;
FValues := TStringList.Create;
FNames.Sorted := True;
end;

destructor TFWResources.Destroy;
begin
FNames.Free;
FValues.Free;
inherited;
end;

procedure TFWResources.Clear;
begin
FNames.Clear;
FValues.Clear;
end;

procedure TFWResources.LocalizeForm(Form: TForm);
var
i: Integer;
CompList: TStrings;
s, CompName, PropName, PropValue: string;
c: TComponent;

begin
i := FNames.IndexOf(Form.ClassName);
if i = -1 then Exit;

CompList := TStringList.Create;
CompList.Text := FValues[Integer(FNames.Objects[i])];

for i := 0 to CompList.Count - 1 do
begin
s := CompList.Names[i];
if s = '' then continue;
if Pos('.', s) = 0 then
begin
SetStrProp(Form, GetPropInfo(PTypeInfo(Form.ClassInf o), s), CompList.Values[s]);
continue;
end;

CompName := Copy(s, 1, Pos('.', s) - 1);
PropName := Copy(s, Pos('.', s) + 1, 255);
s := CompList[i];
PropValue := Copy(s, Pos('=', s) + 1, 4096);

c := Form.FindComponent(CompName);
if c <> nil then
SetStrProp(c, GetPropInfo(PTypeInfo(c.ClassInfo), PropName), PropValue);
end;

CompList.Free;
end;

procedure Ts_init(P: PChar; m: Integer);
var
i: Integer;
begin
{1.} for i := 0 to 255 do shift[i] := m + 1;
{2.} for i := 0 to m - 1 do Shift[Ord(p[i])] := m - i;

Look_at := 0;

{3.} while (look_At < m - 1) do
begin
if (p[m - 1] = p[m - (look_at + 2)]) then Exit
else
Inc(Look_at, 1);
end;
end;

function Ts_Search(Text, p: PChar; m: Integer; Start: Longint): Longint;
var
I: Longint;
T: PChar;
begin
T := Text + Start;
Result := -1;
repeat
i := m - 1;

while (t[i] <> p[i]) do t := t + shift[Ord(t[m])];
i := i - 1;
if i < 0 then i := 0;

while (t[i] = p[i]) do
begin
if i = 0 then Result := t - Text;
i := i - 1;
end;
if Result = -1 then t := t + Look_AT + shift[Ord(t[m + look_at])];
until Result <> -1;
end;

function TFWResources.FindRead(tt : string):string;
var
TName, dlm: string;
L: Integer;
ListCom : TStringList;
L2, sp, a: Longint;
F: file;
Size, i: Integer;
Buffer: PChar;
begin
L := Length(TT);
ts_init(PChar(TT), L);
try
AssignFile(F, 'test.txt');
Reset(F, 1);
Size := FileSize(F);
GetMem(Buffer, Size + L + 1);
try
BlockRead(F, Buffer^, Size);
StrCat(Buffer, PChar(TT));
SP := 0;
A := 0;
while SP < Size do
begin
L2 := Ts_Search(Buffer, PChar(TT), L, SP);
SP := L2 + L;
Inc(a);
end;
ListCom := TStringList.Create;
ListCom.Text := Buffer;
for i := 0 to ListCom.Count do
begin
dlm := ListCom.NameValueSeparator;
TName := ListCom.Values[tt];
end;
//Buffer := '';
result := TName;
finally
FreeMem(Buffer);
//FreeMem(ListCom.GetText);
end;
finally
CloseFile(F);
end;
end;


function TFWResources.Get(const StrName: string): string;
var
i: Integer;
begin
i := FNames.IndexOf(StrName);
if i <> -1 then
Result := FValues[Integer(FNames.Objects[i])] else
Result := StrName;
end;

function FWResources: TFWResources;
begin
if FResources = nil then
FResources := TFWResources.Create;
Result := FResources;
end;


initialization

finalization
if FResources <> nil then
FResources.Free;
FResources := nil;

end.

при вызове вункции FindRead
mode := 'ErrStart_1';
ins := UUnit1.FindRead(mode);
MessageDlg(ins, mtInformation, [mbOk], 0);

в окне появляется правильное значение.

Теперь собственно проблема: при повторном поиске значения программа выдает ошибку, а необходимо осуществить поиск нескольких знчений...

Ваши каментарии.
зарание благодарен.
Antonio86 вне форума Ответить с цитированием
Старый 23.06.2008, 11:24   #2
Air
Участник клуба
 
Аватар для Air
 
Регистрация: 30.04.2007
Сообщений: 1,307
По умолчанию

destructor Destroy; вызывали?
Всё гениальное - просто!
Air вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Поиск в файле Zandrey Microsoft Office Excel 8 05.09.2008 12:23
Поиск числового значения в текстовом файле GazimagomedovM Помощь студентам 6 07.07.2008 19:22
поиск в файле Elm0 Паскаль, Turbo Pascal, PascalABC.NET 14 07.06.2008 22:41
Поиск значения Жираффа Microsoft Office Excel 8 03.03.2008 05:47
Поиск в файле asale Microsoft Office Excel 1 15.05.2007 23:33