Solved

HOSTS file parsing

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using FastMM4... should I enable MMX option? 9 75
build html 5 client that connect to indy Tidtcpserver 3 354
Convert Jpg /PNG To GIF 5 120
find a node in VST 2 63
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

11 Experts available now in Live!

Get 1:1 Help Now