Solved

File question again

Posted on 2001-08-24
7
123 Views
Last Modified: 2010-04-06
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
Comment
Question by:hulken
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6422025
Hi hulken,
send me your file via e-mail to: igor@ns.kz
I will do something possible.
-----
Igor
0
 

Author Comment

by:hulken
ID: 6422042
Done!
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6422096
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 9

Expert Comment

by:ITugay
ID: 6422101
Hi hulken,

replace
var
  C:String to C: WideString;
0
 

Author Comment

by:hulken
ID: 6427942
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
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 6427977
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
 

Author Comment

by:hulken
ID: 6449867
Thnks!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question