Convert Legacy Places.fh_lua

--[[
@Title:			Convert Legacy Places
@Type:				Standard
@Author:			Mike Tate
@Contributors:	Calico Pie
@Version:			1.0
@LastUpdated:	13 Apr 2021
@Licence:			This plugin is copyright (c) 2021 Mike Tate & contributors and is licensed under the MIT License which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description:	Loads a GEDCOM file from Legacy and converts the Place data to FH format.
]]

local strTitle = "Convert Legacy Places 1.0"

require "iuplua"

if fhGetAppVersion() > 5 then								-- Cater for Unicode UTF-8 from FH Version 6 onwards
	fhSetStringEncoding("UTF-8")
	iup.SetGlobal("UTF8MODE","YES")
	iup.SetGlobal("UTF8MODE_FILE","YES")
	iup.SetGlobal("CUSTOMQUITMESSAGE","YES")				-- Needed for IUP 3.28
end

function main()
	local sFileName,strStatus = iup.GetFile("*.ged")
	if strStatus < 0 then return end
	local lines,err = fileToArray(fhConvertUTF8toANSI(sFileName))
	print(lines[2])
	if lines[2] ~= "1 SOUR Legacy" then
		fhMessageBox("The selected File does not come directly from Legacy")
		return
	end
	local sPlace = ""
	local iPlace = 0
	local bPlace = false
	if lines then
		print (#lines)  -- Prints number of lines in the file
		for k,v in ipairs(lines) do  -- to process contents
			if v:starts("0 _PLAC_DEFN") then
				print(v)
				bPlace = true
				lines[k] = " "
			else
				if v:starts("0") then
					bPlace = false
				end
				if bPlace then
					-- Working on Place data
					if v:starts "1 PLAC" then
						iPlace = iPlace + 1
						sPlace = v:sub(8)
						lines[k] = "0 @P"..iPlace.."@ _PLAC "..sPlace
					end
					if v:starts "2 ABBR" then
						local sAbbr = v:sub(8)
						if sAbbr == sPlace then
							sAbbr = ""
						end
						lines[k] = "1 STAN "..sAbbr
					end
					if v:starts "2 MAP" then
						lines[k] = "1 MAP "
					end
					if v:starts "3 LATI" then
						lines[k] = "2 LATI "..v:sub(8)
					end
					if v:starts "3 LONG" then
						lines[k] = "2 LONG "..v:sub(8)
					end	
					print(v, lines[k])
				end
			end
		end
	else
		print(err)
	end
	for k,v in ipairs(lines) do
		if v == " " then
			table.remove(lines,k)
		end
	end
	local path,file,extension = SplitFilename(sFileName)
	local sNewFile = path.."new_"..file
	SaveStringToFile(table.concat(lines,"\n").."\n",fhConvertUTF8toANSI(sNewFile))
	fhMessageBox("Changed file saved as: "..sNewFile.."\n "..iPlace.." places converted")
end

function SplitFilename(strFilename)
	-- Returns the Path, Filename, and Extension as 3 values
	return string.match(strFilename, "(.-)([^\\]-([^\\%.]+))$")
end

function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

function string.ends(String,End)
   return End=="" or string.sub(String,-string.len(End))==End
end

function fileToArray(filename)
	local file = io.open(filename)
	local tbllines = {}
	if file then
		for line in file:lines() do
			table.insert(tbllines,line)
		end
		file:close()
		return tbllines
	else
		return false,"File Failed To Open"
	end
end

-- Open File and return Handle --
function OpenFile(strFileName,strMode)
	local fileHandle, strError = io.open(strFileName,strMode)
	if not fileHandle then
		error("\n Unable to open file in \""..strMode.."\" mode. \n "..strFileName.." \n "..tostring(strError).." \n")
	end
	return fileHandle
end -- function OpenFile
 
-- Save string to file --
function SaveStringToFile(strString,strFileName)
	local fileHandle = OpenFile(strFileName,"w")
	fileHandle:write(strString)
	assert(fileHandle:close())
end -- function SaveStringToFile

function CheckVersionInStore(strPlugin,strVersion)							-- Check if later Version available in Plugin Store

	require "luacom"

	local function httpRequest(strRequest)										-- Luacom http request protected by pcall() below
		local http = luacom.CreateObject("winhttp.winhttprequest.5.1")
		http:Open("GET",strRequest,false)
		http:Send()
		return http.Responsebody
	end -- local function httpRequest

	local function intVersion(strVersion)										-- Convert version string to comparable integer
		local intVersion = 0
		local arrNumbers = {}
		strVersion:gsub("(%d+)", function(strDigits) table.insert(arrNumbers,strDigits) end)
		for i = 1, 4 do
			intVersion = intVersion * 1000 + tonumber(arrNumbers[i] or 0)
		end
		return intVersion
	end -- local function intVersion

	local strLatest = "0"
	if strPlugin then
		local strRequest = "http://www.family-historian.co.uk/lnk/checkpluginversion.php?name="..tostring(strPlugin)
		local isOK, strReturn = pcall(httpRequest,strRequest)
		if isOK and strReturn then
			strLatest = strReturn:match("([%d%.]*),%d*")						-- Version digits & dots then comma and Id digits 
		end
	end
	if intVersion(strLatest) > intVersion(strVersion or "0") then
		fhMessageBox("Later Version "..strLatest.." of this Plugin is available from the Plugin Store.")
	end
end -- function CheckVersionInStore

-------------------------------------- Run

	fhInitialise(5,0,0)

	CheckVersionInStore(strTitle:match("^(.+) +([%d%.]+)$"))				-- Notify if later Version

	main()

Source:Convert-Legacy-Places.fh_lua