Quinkcius
Сообщений: 1
Оценки: 0
Присоединился: 2010-08-04 10:40:19.243333
|
Здравствуйте, уважаемые специалисты Delphi ! Помогите, пожалуйста, разобраться с проблемой ! Я попытался открыть через Delphi dbf-файл очень старого формата технологией ADO. Я - совсем начинающий программист, поэтому досовскую кодировку настроить для нормального отображения в DBGrid у меня не получилось. То есть, рекомендации по настройкам драйверов в BDE Administrator, звучащие на форумах, я выполнил, но результата не достиг. В общем, написал перекодировщик. Он работает, но невероятно медленно ! За 12 часов работы он перекодировал только десятую часть 4,5-мегабайтного файла. Хотя претензий к качеству перекодировки у меня нет. Все известные мне методы оптимизации я уже использовал. Особого прироста скорости не обнаружилось. Хотелось бы узнать какие-нибудь ЭФФЕКТИВНЫЕ методы оптимизации ! Может, что-то и с алгоритмом не то. Короче, вопрос всего один: как добиться быстрой работы этого перекодировщика. Код получился довольно громоздким. Вот он: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls; type TForm1 = class(TForm) BitBtn1: TBitBtn; ProgressBar1: TProgressBar; procedure BitBtn1Click(Sender: TObject); procedure HexText; procedure TablesRecode(Flag: Boolean); procedure SearchChar(For_address: integer); procedure For_CodePage(Flag: Boolean); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Buffer: integer; Stream: TFileStream; For_address, For_StreamSize: integer; i: integer; flag: boolean; h:thandle; Stream_1: TFileStream; Buffer_1, index_1: integer; For_text: array[0..162] of integer; implementation {$R *.dfm} procedure TForm1.HexText; begin h:=CreateFile('c:\hexcode.txt',GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0); closehandle(h); Stream_1 := TFileStream.Create('c:\hexcode.txt', fmOpenReadWrite); for index_1 := 0 to 161 do begin Buffer_1 := 0; Buffer_1 := For_text[index_1]; Stream_1.Write(Buffer_1, 1); end; end; procedure TForm1.TablesRecode(Flag: Boolean); begin if flag = True then begin // Для перекодировки из WIN-1251 в CP866 For_text[0] := $A8; For_text[1] := $F0; For_text[2] := $C0; For_text[3] := $80; For_text[4] := $C1; For_text[5] := $81; For_text[6] := $C2; For_text[7] := $82; For_text[8] := $C3; For_text[9] := $83; For_text[10] := $C4; For_text[11] := $84; For_text[12] := $C5; For_text[13] := $85; For_text[14] := $C6; For_text[15] := $86; For_text[16] := $C7; For_text[17] := $87; For_text[18] := $C8; For_text[19] := $88; For_text[20] := $C9; For_text[21] := $89; For_text[22] := $CA; For_text[23] := $8A; For_text[24] := $CB; For_text[25] := $8B; For_text[26] := $CC; For_text[27] := $8C; For_text[28] := $CD; For_text[29] := $8D; For_text[30] := $CE; For_text[31] := $8E; For_text[32] := $CF; For_text[33] := $8F; For_text[34] := $D0; For_text[35] := $90; For_text[36] := $D1; For_text[37] := $91; For_text[38] := $D2; For_text[39] := $92; For_text[40] := $D3; For_text[41] := $93; For_text[42] := $D4; For_text[43] := $94; For_text[44] := $D5; For_text[45] := $95; For_text[46] := $D6; For_text[47] := $96; For_text[48] := $D7; For_text[49] := $97; For_text[50] := $D8; For_text[51] := $98; For_text[52] := $D9; For_text[53] := $99; For_text[54] := $DA; For_text[55] := $9A; For_text[56] := $DB; For_text[57] := $9B; For_text[58] := $DC; For_text[59] := $9C; For_text[60] := $DD; For_text[61] := $9D; For_text[62] := $DE; For_text[63] := $9E; For_text[64] := $DF; For_text[65] := $9F; For_text[66] := $E0; For_text[67] := $A0; For_text[68] := $E1; For_text[69] := $A1; For_text[70] := $E2; For_text[71] := $A2; For_text[72] := $E3; For_text[73] := $A3; For_text[74] := $E4; For_text[75] := $A4; For_text[76] := $E5; For_text[77] := $A5; For_text[78] := $E6; For_text[79] := $A6; For_text[80] := $E7; For_text[81] := $A7; For_text[82] := $E8; For_text[83] := $A8; For_text[84] := $E9; For_text[85] := $A9; For_text[86] := $EA; For_text[87] := $AA; For_text[88] := $EB; For_text[89] := $AB; For_text[90] := $EC; For_text[91] := $AC; For_text[92] := $ED; For_text[93] := $AD; For_text[94] := $EE; For_text[95] := $AE; For_text[96] := $EF; For_text[97] := $AF; For_text[98] := $F0; For_text[99] := $E0; For_text[100] := $F1; For_text[101] := $E1; For_text[102] := $F2; For_text[103] := $E2; For_text[104] := $F3; For_text[105] := $E3; For_text[106] := $F4; For_text[107] := $E4; For_text[108] := $F5; For_text[109] := $E5; For_text[110] := $F6; For_text[111] := $E6; For_text[112] := $F7; For_text[113] := $E7; For_text[114] := $F8; For_text[115] := $E8; For_text[116] := $F9; For_text[117] := $E9; For_text[118] := $FA; For_text[119] := $EA; For_text[120] := $FB; For_text[121] := $EB; For_text[122] := $FC; For_text[123] := $EC; For_text[124] := $FD; For_text[125] := $ED; For_text[126] := $FE; For_text[127] := $EE; For_text[128] := $FF; For_text[129] := $EF; For_text[130] := $B8; For_text[131] := $F1; For_text[132] := $20; For_text[133] := $A0; For_text[134] := $A7; For_text[135] := $FD; For_text[136] := $2D; For_text[137] := $AD; For_text[138] := $95; For_text[139] := $2A; For_text[140] := $96; For_text[141] := $2D; For_text[142] := $97; For_text[143] := $2D; For_text[144] := $91; For_text[145] := $22; For_text[146] := $92; For_text[147] := $22; For_text[148] := $82; For_text[149] := $22; For_text[150] := $93; For_text[151] := $22; For_text[152] := $94; For_text[153] := $22; For_text[154] := $84; For_text[155] := $22; For_text[156] := $8B; For_text[157] := $22; For_text[158] := $9B; For_text[159] := $22; For_text[160] := $B9; For_text[161] := $FC; HexText; end else begin // Для перекодировки из CP866 в WIN-1251 For_text[0] := $F0; For_text[1] := $A8; For_text[2] := $80; For_text[3] := $C0; For_text[4] := $81; For_text[5] := $C1; For_text[6] := $82; For_text[7] := $C2; For_text[8] := $83; For_text[9] := $C3; For_text[10] := $84; For_text[11] := $C4; For_text[12] := $85; For_text[13] := $C5; For_text[14] := $86; For_text[15] := $C6; For_text[16] := $87; For_text[17] := $C7; For_text[18] := $88; For_text[19] := $C8; For_text[20] := $89; For_text[21] := $C9; For_text[22] := $8A; For_text[23] := $CA; For_text[24] := $8B; For_text[25] := $CB; For_text[26] := $8C; For_text[27] := $CC; For_text[28] := $8D; For_text[29] := $CD; For_text[30] := $8E; For_text[31] := $CE; For_text[32] := $8F; For_text[33] := $CF; For_text[34] := $90; For_text[35] := $D0; For_text[36] := $91; For_text[37] := $D1; For_text[38] := $92; For_text[39] := $D2; For_text[40] := $93; For_text[41] := $D3; For_text[42] := $94; For_text[43] := $D4; For_text[44] := $95; For_text[45] := $D5; For_text[46] := $96; For_text[47] := $D6; For_text[48] := $97; For_text[49] := $D7; For_text[50] := $98; For_text[51] := $D8; For_text[52] := $99; For_text[53] := $D9; For_text[54] := $9A; For_text[55] := $DA; For_text[56] := $9B; For_text[57] := $DB; For_text[58] := $9C; For_text[59] := $DC; For_text[60] := $9D; For_text[61] := $DD; For_text[62] := $9E; For_text[63] := $DE; For_text[64] := $9F; For_text[65] := $DF; For_text[66] := $A0; For_text[67] := $E0; For_text[68] := $A1; For_text[69] := $E1; For_text[70] := $A2; For_text[71] := $E2; For_text[72] := $A3; For_text[73] := $E3; For_text[74] := $A4; For_text[75] := $E4; For_text[76] := $A5; For_text[77] := $E5; For_text[78] := $A6; For_text[79] := $E6; For_text[80] := $A7; For_text[81] := $E7; For_text[82] := $A8; For_text[83] := $E8; For_text[84] := $A9; For_text[85] := $E9; For_text[86] := $AA; For_text[87] := $EA; For_text[88] := $AB; For_text[89] := $EB; For_text[90] := $AC; For_text[91] := $EC; For_text[92] := $AD; For_text[93] := $ED; For_text[94] := $AE; For_text[95] := $EE; For_text[96] := $AF; For_text[97] := $EF; For_text[98] := $E0; For_text[99] := $F0; For_text[100] := $E1; For_text[101] := $F1; For_text[102] := $E2; For_text[103] := $F2; For_text[104] := $E3; For_text[105] := $F3; For_text[106] := $E4; For_text[107] := $F4; For_text[108] := $E5; For_text[109] := $F5; For_text[110] := $E6; For_text[111] := $F6; For_text[112] := $E7; For_text[113] := $F7; For_text[114] := $E8; For_text[115] := $F8; For_text[116] := $E9; For_text[117] := $F9; For_text[118] := $EA; For_text[119] := $FA; For_text[120] := $EB; For_text[121] := $FB; For_text[122] := $EC; For_text[123] := $FC; For_text[124] := $ED; For_text[125] := $FD; For_text[126] := $EE; For_text[127] := $FE; For_text[128] := $EF; For_text[129] := $FF; For_text[130] := $F1; For_text[131] := $B8; For_text[132] := $A0; For_text[133] := $20; For_text[134] := $FD; For_text[135] := $A7; For_text[136] := $AD; For_text[137] := $2D; For_text[138] := $2A; For_text[139] := $95; For_text[140] := $2D; For_text[141] := $96; For_text[142] := $2D; For_text[143] := $97; For_text[144] := $22; For_text[145] := $91; For_text[146] := $22; For_text[147] := $92; For_text[148] := $22; For_text[149] := $82; For_text[150] := $22; For_text[151] := $93; For_text[152] := $22; For_text[153] := $94; For_text[154] := $22; For_text[155] := $84; For_text[156] := $22; For_text[157] := $8B; For_text[158] := $22; For_text[159] := $9B; For_text[160] := $FC; For_text[161] := $B9; HexText; end; end; procedure TForm1.SearchChar(For_address: integer); begin i := 0; while i < 162 do begin Buffer_1 := 0; Stream_1.Seek(i, 0); Stream_1.Read(Buffer_1, 1); if Buffer = Buffer_1 then begin Stream_1.Seek(i+1, 0); Buffer := 0; Stream_1.Read(Buffer, 1); Stream.Seek(For_address, soFromBeginning); Stream.Write(Buffer, 1); break; end; i := i + 2; end; end; procedure TForm1.For_CodePage(Flag: Boolean); begin Buffer := 0; Stream.Seek(29, 0); Stream.Read(Buffer, 1); // Ставит 29-й байт dbf-файла в кодировку Win-1251 if (((Buffer = $0) OR (Buffer = $65)) AND (Flag = False)) then begin Buffer := $C9; Stream.Seek(29, soFromBeginning); Stream.Write(Buffer, 1); end; // Ставит 29-й байт dbf-файла в кодировку CP866 if (((Buffer = $0) OR (Buffer = $C9)) AND (Flag = True)) then begin Buffer := $65; Stream.Seek(29, soFromBeginning); Stream.Write(Buffer, 1); end; end; procedure TForm1.BitBtn1Click(Sender: TObject); begin TablesRecode(False); Stream := TFileStream.Create('D:\SPRUSE.DBF', fmOpenReadWrite); For_CodePage(False); For_address := 1869; For_StreamSize := Stream.Size; while For_address <= For_StreamSize do begin Buffer := 0; Stream.Seek(For_address, 0); Stream.Read(Buffer, 1); if ((Buffer <> $20) AND (Buffer <> $30) AND (Buffer <> $2E) AND (Buffer <> $2D)) then SearchChar(For_address); ProgressBar1.Position := round(For_address/Stream.Size*100); Sleep(100); For_address := For_address + 1; end; Stream.Free; Stream_1.Free; ShowMessage('Операция завершена'); end; end.
|