home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / IP2Country2042661152007.psc / IP2Country / frmConverter.frm next >
Text File  |  2007-01-15  |  9KB  |  274 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  4. Begin VB.Form frmConverter 
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "IP List Converter"
  7.    ClientHeight    =   3615
  8.    ClientLeft      =   45
  9.    ClientTop       =   405
  10.    ClientWidth     =   5895
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3615
  15.    ScaleWidth      =   5895
  16.    StartUpPosition =   2  'CenterScreen
  17.    Begin MSComDlg.CommonDialog CommonDialog 
  18.       Left            =   0
  19.       Top             =   3000
  20.       _ExtentX        =   847
  21.       _ExtentY        =   847
  22.       _Version        =   393216
  23.    End
  24.    Begin VB.Frame Frame1 
  25.       Caption         =   "Convert"
  26.       Height          =   1935
  27.       Left            =   120
  28.       TabIndex        =   1
  29.       Top             =   1560
  30.       Width           =   5655
  31.       Begin VB.PictureBox Picture1 
  32.          BorderStyle     =   0  'None
  33.          Enabled         =   0   'False
  34.          Height          =   580
  35.          Left            =   120
  36.          ScaleHeight     =   585
  37.          ScaleWidth      =   5415
  38.          TabIndex        =   6
  39.          Top             =   720
  40.          Width           =   5415
  41.          Begin VB.TextBox txtCSV 
  42.             Height          =   285
  43.             Left            =   400
  44.             TabIndex        =   7
  45.             Top             =   100
  46.             Width           =   4575
  47.          End
  48.       End
  49.       Begin MSComctlLib.ProgressBar ProgressBar1 
  50.          Height          =   215
  51.          Left            =   120
  52.          TabIndex        =   4
  53.          Top             =   1620
  54.          Width           =   5415
  55.          _ExtentX        =   9551
  56.          _ExtentY        =   370
  57.          _Version        =   393216
  58.          Appearance      =   0
  59.       End
  60.       Begin VB.CommandButton cmdConvert 
  61.          Caption         =   "Load IP List"
  62.          Height          =   330
  63.          Left            =   2280
  64.          TabIndex        =   2
  65.          Top             =   300
  66.          Width           =   1095
  67.       End
  68.       Begin VB.Label lblUpdate 
  69.          Caption         =   "Waiting..."
  70.          BeginProperty Font 
  71.             Name            =   "MS Sans Serif"
  72.             Size            =   8.25
  73.             Charset         =   0
  74.             Weight          =   700
  75.             Underline       =   0   'False
  76.             Italic          =   0   'False
  77.             Strikethrough   =   0   'False
  78.          EndProperty
  79.          Height          =   255
  80.          Left            =   120
  81.          TabIndex        =   3
  82.          Top             =   1320
  83.          Width           =   5295
  84.       End
  85.    End
  86.    Begin VB.Label lblHyperlink 
  87.       Alignment       =   2  'Center
  88.       Caption         =   "Download Latest IP-to-Country Database"
  89.       Height          =   255
  90.       Left            =   120
  91.       MouseIcon       =   "frmConverter.frx":0000
  92.       TabIndex        =   5
  93.       Top             =   1060
  94.       Width           =   5655
  95.    End
  96.    Begin VB.Label Label1 
  97.       Caption         =   $"frmConverter.frx":030A
  98.       Height          =   615
  99.       Left            =   120
  100.       TabIndex        =   0
  101.       Top             =   120
  102.       Width           =   5655
  103.    End
  104. End
  105. Attribute VB_Name = "frmConverter"
  106. Attribute VB_GlobalNameSpace = False
  107. Attribute VB_Creatable = False
  108. Attribute VB_PredeclaredId = True
  109. Attribute VB_Exposed = False
  110. Option Explicit
  111.  
  112. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
  113.                         ByVal hwnd As Long, _
  114.                         ByVal lpOperation As String, _
  115.                         ByVal lpFile As String, _
  116.                         ByVal lpParameters As String, _
  117.                         ByVal lpDirectory As String, _
  118.                         ByVal nShowCmd As Long) _
  119.                         As Long
  120.  
  121. Private Sub Form_Load()
  122.     lblHyperlink.ForeColor = vbBlue
  123. End Sub
  124.  
  125. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  126.     lblHyperlink.FontUnderline = False
  127.     lblHyperlink.MousePointer = 0
  128. End Sub
  129.  
  130. Private Sub lblHyperlink_Click()
  131.     ShellExecute Me.hwnd, "open", "http://ip-to-country.webhosting.info/node/view/6", vbNullString, vbNullString, vbNormal
  132. End Sub
  133.  
  134. Private Sub lblHyperlink_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  135.     lblHyperlink.FontUnderline = True
  136.     lblHyperlink.MousePointer = 99
  137.     lblHyperlink.ToolTipText = "http://ip-to-country.webhosting.info/node/view/6"
  138. End Sub
  139.  
  140. Private Sub cmdConvert_Click()
  141.     
  142.   On Error GoTo ErrHandler
  143.     
  144.     With CommonDialog
  145.         .Flags = cdlOFNExplorer + cdlOFNHideReadOnly + cdlOFNFileMustExist
  146.         .Filter = "ip-to-country (*.csv)|*.csv"
  147.         .DialogTitle = "ip-to-country.csv"
  148.         .InitDir = App.Path
  149.         .CancelError = True
  150.         .ShowOpen
  151.         If .FileName <> "" Then
  152.           If Dir(App.Path & "\IPList.dat", vbNormal) <> "" Then
  153.               If MsgBox("IPList.dat already exists. Do you want to overwrite it?", vbQuestion + vbYesNo, "Confirmation") = vbYes Then
  154.                 txtCSV.Text = .FileName
  155.                 ConvertIPList (.FileName)
  156.               End If
  157.           Else
  158.             txtCSV.Text = .FileName
  159.             ConvertIPList (.FileName)
  160.           End If
  161.         End If
  162.     
  163.     End With
  164.  
  165. ErrHandler:
  166.   'Cancel was selected
  167. End Sub
  168.  
  169. Private Function ConvertIPList(sFileName As String)
  170.   Dim ff As Integer
  171.   Dim ln As String
  172.   Dim arr() As String
  173.   Dim sArray() As String
  174.   
  175.   On Error GoTo ErrHandler
  176.   
  177.   ff = FreeFile
  178.   ProgressBar1.Max = FileLen(sFileName) 'Use progbar based on bytes read - counting text file lines takes too long
  179.   lblUpdate.Caption = "Converting IP list..."
  180.   
  181.   ReDim sArray(0)
  182.   Open sFileName For Input As #ff
  183.     Do While Not EOF(ff)
  184.       Line Input #ff, ln
  185.       DoEvents
  186.       ProgressBar1.Value = ProgressBar1.Value + Len(ln) + 2 'Update progressbar based on bytes read
  187.       ln = Replace(ln, Chr(34), "") 'Remove quotation marks
  188.       arr() = Split(ln, ",") 'Split items on the comma
  189.       ln = arr(4) & ":" & arr(3) & ":" & IPConvert(arr(0)) & ":" & IPConvert(arr(1)) 'Put useful items back in different order
  190.       sArray(UBound(sArray)) = ln 'Add to a new array
  191.       ReDim Preserve sArray(UBound(sArray) + 1)
  192.     Loop
  193.   Close #ff
  194.   ProgressBar1.Value = 0
  195.   
  196.   
  197.   QuickSort sArray, LBound(sArray), UBound(sArray) 'Sort the array
  198.  
  199.  
  200.   'We could simply dump the entire array into a text file with: "Print #1, Join(sArray, vbCrLf)"
  201.   'but the ReDim code above adds an empty line and QuickSort puts it at the start of the array,
  202.   'so the updated IPList.dat file also starts with an empty line. The code below won't add that
  203.   'empty line, it just takes slightly longer to save the array to the file
  204.   Dim i As Long
  205.   ProgressBar1.Max = UBound(sArray)
  206.   lblUpdate.Caption = "Saving IP list..."
  207.   Open App.Path & "\IPList.dat" For Output As #1
  208.     Do While i <= UBound(sArray)
  209.       DoEvents
  210.       ProgressBar1.Value = i
  211.       If sArray(i) <> "" Then 'Make sure it's not an empty line...
  212.         Print #1, sArray(i) 'then write it to the IPList.dat file
  213.       End If
  214.       i = i + 1
  215.     Loop
  216.   Close #1
  217.   ProgressBar1.Value = 0
  218.   lblUpdate.Caption = "Waiting..."
  219.   txtCSV.Text = ""
  220.  
  221.   MsgBox "Conversion done!", vbInformation, "Done"
  222. Exit Function
  223.  
  224. ErrHandler:
  225.   ProgressBar1.Value = 0
  226.   lblUpdate.Caption = "Waiting..."
  227.   txtCSV.Text = ""
  228.   
  229.   If Err.Number = 9 Then
  230.     MsgBox "There was an error reading the IP list on line: " & UBound(sArray) + 1 & vbCrLf & "Make sure it is in the correct format." _
  231.     & vbCrLf & vbCrLf & """33996344""" & ", " & """33996351""" & ", " & """GB""" & ", " & """GBR""" & ", " & """UNITED KINGDOM""", vbExclamation, "Error"
  232.   Else
  233.     MsgBox "An error has occured!" & vbCrLf & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "Error: " & Err.Number
  234.   End If
  235.   
  236.   Err.Clear
  237. End Function
  238.  
  239. Private Sub QuickSort(c() As String, ByVal First As Long, ByVal Last As Long)
  240.   Dim Low As Long, High As Long
  241.   Dim MidValue As String
  242.     
  243.   Low = First
  244.   High = Last
  245.   MidValue = c((First + Last) \ 2)
  246.     
  247.   Do
  248.       While c(Low) < MidValue
  249.           Low = Low + 1
  250.       Wend
  251.         
  252.       While c(High) > MidValue
  253.           High = High - 1
  254.       Wend
  255.         
  256.       If Low <= High Then
  257.           Swap c(Low), c(High)
  258.           Low = Low + 1
  259.           High = High - 1
  260.       End If
  261.   Loop While Low <= High
  262.     
  263.   If First < High Then QuickSort c, First, High
  264.   If Low < Last Then QuickSort c, Low, Last
  265. End Sub
  266.  
  267. Private Sub Swap(ByRef a As String, ByRef b As String)
  268.   Dim T As String
  269.     
  270.   T = a
  271.   a = b
  272.   b = T
  273. End Sub
  274.