Solved

HOSTS file parsing

Posted on 2011-09-12
3
295 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 500 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

624 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