Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HOSTS file parsing

Posted on 2011-09-12
3
Medium Priority
?
314 Views
Last Modified: 2012-05-12
From file sample posted below I would like to get TStringList with items:
127.0.0.1 losalhost
127.0.0.1 sampleone.com
127.0.0.1 sampletwo.com
127.0.0.1 samplethree.com

So, a parsing routine needed that ignores all commented (#) lines and recognizes multiple hosts in one line.
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
127.0.0.1       localhost
127.0.0.1 sampleone.com sampletwo.com samplethree.com # comment

Open in new window

0
Comment
Question by:3axap
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 36521682
assuming you have the HOSTS file loaded in a Memo, here is how to put the hosts found, both in a listbox (= TstringList) as you asked, and in a TreeView
procedure TForm1.btnGetHostsClick(Sender: TObject);
var
 i,P,P2:integer;
 L:String;
 HostList:TStringList;
 Node:TTreeNode; // we will add found values in a treeview
begin
 HostList:=TStringList.Create;
 HostList.Delimiter:=' '; // delimiter will be a space
 for i := 0 to Memo.Lines.Count-1 do
  begin
   L:=Trim(Memo.Lines[i]);
   P:=Pos('#',L);
   if P>0 then L:=Trim(Copy(L,1,P-1));
   L:=StringReplace(L,#8,' ',[rfReplaceAll]); // Replace tabs
   P:=Length(L);
   Repeat
    L:=StringReplace(L,'  ',' ',[rfReplaceAll]); // Strip dup spaces
    P2:=Length(L);
    if P=P2 then break; // Exit when no replace occured
    P:=P2;
   Until False; // Infinite Loop
   HostList.DelimitedText:=L;
   if HostList.Count>1 then
    begin
     Node:=TreeView.Items.Add(nil,HostList[0]); // Add in a treeview
     for P := 1 to HostList.Count-1 do
      begin
       TreeView.Items.AddChild(Node,HostList[P]); // add child in the treeview
       ListBox.Items.Add(HostList[0]+' '+HostList[P]); // add in a listbox
      end;
    end;
  end;
 HostList.Free;
end;

Open in new window

0
 

Author Closing Comment

by:3axap
ID: 36521870
Thank you Pasquier.
One related question if I may. What is the best practice to open text file for reading, as in my original question?
Read it to TStringList to Memo or to TFileStream?
Thanks
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36522054
Memo.Lines.LoadFromFile( aFileName );

if you don't need to display in a memo, use a TStringList :
aStringList.LoadFromFile( aFileName );
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

705 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