Solved

File question again

Posted on 2001-08-24
7
121 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
Comment Utility
Hi hulken,
send me your file via e-mail to: igor@ns.kz
I will do something possible.
-----
Igor
0
 

Author Comment

by:hulken
Comment Utility
Done!
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Hi hulken,

replace
var
  C:String to C: WideString;
0
 

Author Comment

by:hulken
Comment Utility
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
Comment Utility
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
Comment Utility
Thnks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now