• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

File question again

I asked a question about how to lowercase all
strings between [ ] in a textfile ( all except [PRIMARY]

and now I have this:



var
 S: String;
 C: String;
 I: Integer;
 J: Integer;
 L: TStrings;
begin

 L := TStringList.Create;
 L.LoadFromFile('D:\000.TXT');
 S := L.Text;

 I := 1;
 while (I <= Length(S)) do
 begin
   while (I <= Length(S)) and (S[I] <> '[') do inc(I);
   inc(I);
   J := I;
   while (I <= Length(S)) and (S[I] <> ']') do inc(I);
   C := Copy(S, J, I - J);
   if C <> 'PRIMARY' then
   begin
     C := LowerCase(C);
     Delete(S, J, I - J);
     Insert(C, S, J);
   end;
 end;

 L.Text := S;
 L.SaveToFile('D:\000.TXT');
 L.Free;

end;


One problem is that the textfile i like to convert seems to be unicoded because wordpad cant read it and notepad can.

is there a way to modify the code abowe to have it handle unicoded files too?

0
hulken
Asked:
hulken
  • 4
  • 3
1 Solution
 
ITugayCommented:
Hi hulken,
send me your file via e-mail to: igor@ns.kz
I will do something possible.
-----
Igor
0
 
hulkenAuthor Commented:
Done!
0
 
ITugayCommented:
Hi hulken,

here is modifyed version:

var
  C: String;
  I: Integer;
  J: Integer;
  S: WideString;
  F: TFileStream;
begin
  F := TFileStream.Create('D:\000.SQL', fmOpenRead);
  SetLength(S, F.Size div 2);
  F.Read(S[1], F.Size);
  F.Free;

  I := 2;
  while (I <= Length(S)) do
  begin
    while (I <= Length(S)) and (S[I] <> '[') do inc(I);
    inc(I);
    J := I;
    while (I <= Length(S)) and (S[I] <> ']') do inc(I);
    C := Copy(S, J, I - J);
    if C <> 'PRIMARY' then
    begin
      C := LowerCase(C);
      Delete(S, J, I - J);
      Insert(C, S, J);
    end;
  end;

  F := TFileStream.Create('D:\001.SQL', fmCreate);
  F.Write(S[1], Length(S) * 2);
  F.Free;

end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ITugayCommented:
Hi hulken,

replace
var
  C:String to C: WideString;
0
 
hulkenAuthor Commented:
Thnks this works fine.

I just like to ask you 2 more things.

1.

Could you try to explain abit about the code. Then I might learn something.


2.

It would be nice if I could read the text into a Memo so I have a preview of it.
I can double the point's if you fix it.  Or should I add it as a new question?

0
 
ITugayCommented:
Hi,

here is code with some explanation and it allow you to show modified text into memo.

------
Igor.

PS: >>Or should I add it as a new question?
no, it is not necessary :-)

var
 C: WideString;
 I: Integer;
 J: Integer;
 S: WideString;
 F: TFileStream;
begin
 // open file for reading
 F := TFileStream.Create('D:\000.SQL', fmOpenRead);

 // every character in stream is two bytes length,
 // set length of string according file size div 2
 SetLength(S, F.Size div 2);

 // read file data into string
 F.Read(S[1], F.Size);

 // close file (free filestream)
 F.Free;

 //------------------------------
 // show source string in memo
 //------------------------------
 Memo2.Lines.Text := S;

 // first character is unicode identifier,
 // begin from second one
 I := 2;

 // do while not end of string
 while (I <= Length(S)) do
 begin
   // scan for '['
   while (I <= Length(S)) and (S[I] <> '[') do inc(I);
   inc(I);  // next character
   J := I;  // store position just after '['

   // scan for ']'
   while (I <= Length(S)) and (S[I] <> ']') do inc(I);

   // extract string between '[' and ']'
   C := Copy(S, J, I - J);

   // skip 'PRIMARY'
   if C <> 'PRIMARY' then
   begin
     // lowercase extracted string
     C := LowerCase(C);

     // delete last instance between '[' and ']'
     Delete(S, J, I - J);

     // insert new one in lowercase
     Insert(C, S, J);
   end;
 end;

 // open file for writing
 F := TFileStream.Create('D:\001.SQL', fmCreate);

 // every character of widestring is two bytes length
 // write string to the stream
 F.Write(S[1], Length(S) * 2);

 // close file (free stream)
 F.Free;

 //------------------------------
 // show modified string in memo
 //------------------------------
 Memo1.Lines.Text := S;
end;
0
 
hulkenAuthor Commented:
Thnks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now