Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HOSTS file parsing

Posted on 2011-09-12
3
Medium Priority
?
335 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
  • 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

927 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