Solved

HOSTS file parsing

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

18 Experts available now in Live!

Get 1:1 Help Now