Convert country names to datasection and map variables

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Convert country names to datasection and map variables

Post by Fangbeast »

Don't know if this is useful or not but I am always including country flags into all my programs these days and I needed something to automatically grab all the country names from a data section and convert them into both map data and datasection references to the images in a local directory. Saves heaps of typing.

Creates two .PBI files that you can include in your programs.

Thanks again to George Bisonte for showing me how to use maps efficiently

Code: Select all

; Format the passed heading to the correct field length for printing/copying to clipboard etc

Procedure.s FormatHeading(InString.s, FieldLen.i)
  InStringLen.i = Len(InString.s)
  SpaceLen.i    = FieldLen.i - InStringLen.i
  If SpaceLen.i > 1
    OutString.s = InString.s + Space(SpaceLen.i)
  Else
    OutString.s = InString.s
  EndIf
  ProcedureReturn OutString.s
EndProcedure

; 

FlagConstantsFile.i = CreateFile(#PB_Any, GetCurrentDirectory() + "Flags_Constants.pbi")

DataSectionFile.i   = CreateFile(#PB_Any, GetCurrentDirectory() + "Flags_Datasection.pbi")

If FlagConstantsFile.i  And DataSectionFile.i
  
  WriteStringN(FlagConstantsFile.i, #Empty$)
  
  ; Write the data structure for the flags to the constants file
  
  WriteStringN(FlagConstantsFile.i, ";--------------------------------------------------------------------------------------------------")
  WriteStringN(FlagConstantsFile.i, "; Flags of the world")
  WriteStringN(FlagConstantsFile.i, ";--------------------------------------------------------------------------------------------------")
  
  WriteStringN(FlagConstantsFile.i, #Empty$)
  
  WriteStringN(FlagConstantsFile.i, "Structure FlagData")
  WriteStringN(FlagConstantsFile.i, "  Image.i")
  WriteStringN(FlagConstantsFile.i, "  Name.s")
  WriteStringN(FlagConstantsFile.i, "  Address.i")
  WriteStringN(FlagConstantsFile.i, "EndStructure")
  
  WriteStringN(FlagConstantsFile.i, #Empty$)
  
  WriteStringN(FlagConstantsFile.i, "Global NewMap Flag.FlagData()")
  
  ; Write these only to flag map data file
  
  WriteStringN(FlagConstantsFile.i, #Empty$)
  
  WriteStringN(FlagConstantsFile.i, ";--------------------------------------------------------------------------------------------------")
  WriteStringN(FlagConstantsFile.i, "; Country flag map and keys")
  WriteStringN(FlagConstantsFile.i, ";--------------------------------------------------------------------------------------------------")

  WriteStringN(FlagConstantsFile.i, #Empty$)
  
  ;----------------------------------------------
  ; Write these only to the data section
  ;----------------------------------------------
  
  WriteStringN(DataSectionFile.i, "DataSection")
  
  WriteStringN(DataSectionFile.i, #Empty$)
  
  WriteStringN(DataSectionFile.i, "  ;----------------------------------------------------------------------------------------------------")
  WriteStringN(DataSectionFile.i, "  ; Country flag images")
  WriteStringN(DataSectionFile.i, "  ;----------------------------------------------------------------------------------------------------")

  WriteStringN(DataSectionFile.i, #Empty$)
  
  ;----------------------------------------------
  ; Write the map variables and the data section at the same time
  ;----------------------------------------------
  
  Restore Mapstart
  
  While TempString.s <> "EndMapstart"
    Read.s TempString.s
    If TempString.s <> "EndMapstart"
      TempStringLength.i = Len(TempString.s)
      WriteStringN(FlagConstantsFile.i, FormatHeading("Flag(" + #DQUOTE$  + TempString.s + #DQUOTE$  + ")\Address", 62) + "= ?_flags_" + TempString.s)
      WriteStringN(FlagConstantsFile.i, FormatHeading("Flag(" + #DQUOTE$  + TempString.s + #DQUOTE$  + ")\Image",   62) + "= CatchImage(#PB_Any, Flag(" + #DQUOTE$  + TempString.s + #DQUOTE$  + ")\Address, ?_flags_" + TempString.s + ")")
      WriteStringN(FlagConstantsFile.i, FormatHeading("Flag(" + #DQUOTE$  + TempString.s + #DQUOTE$  + ")\Name",    62) + "= " + #DQUOTE$  + ReplaceString(TempString.s, "_", " ", #PB_String_NoCase, 1) + #DQUOTE$)
      WriteStringN(FlagConstantsFile.i, #Empty$)
      
      WriteStringN(DataSectionFile.i,   Space(2)  + "_" + TempString.s + ":"  + Space(60 - Len(TempString.s)) +  ":"  + " IncludeBinary " + #DQUOTE$  + "Images\_16x16\_Flags\" + TempString.s  + ".ico"  + #DQUOTE$)
    EndIf
  Wend
  
  ; Write an end marker to the flag map key data
  
  WriteStringN(FlagConstantsFile.i, #Empty$)
  WriteStringN(FlagConstantsFile.i, ";--------------------------------------------------------------------------------------------------")
  WriteStringN(FlagConstantsFile.i, "; End of Country flag map and keys")
  WriteStringN(FlagConstantsFile.i, ";--------------------------------------------------------------------------------------------------")
  WriteStringN(FlagConstantsFile.i, #Empty$)
  
  ; Write an end marker to the flag images in case you have other constants to add manually
  
  WriteStringN(DataSectionFile.i, #Empty$)
  WriteStringN(DataSectionFile.i, "  ;----------------------------------------------------------------------------------------------------")
  WriteStringN(DataSectionFile.i, "  ; End of Country flag images")
  WriteStringN(DataSectionFile.i, "  ;----------------------------------------------------------------------------------------------------")
  WriteStringN(DataSectionFile.i, #Empty$)
  
  ; Write the end of data section keyword
  
  WriteStringN(DataSectionFile.i,   "EndDataSection")
  
  ; Close all opened files
  
  CloseFile(FlagConstantsFile.i)
  
  CloseFile(DataSectionFile.i)
  
Else
  
  Debug "Data section and flak map data created"
  
EndIf

End

; 

DataSection
  Mapstart:
  Data.s "Afghanistan"
  Data.s "Aland_Islands"
  Data.s "Albania"
  Data.s "Algeria"
  Data.s "American_Samoa"
  Data.s "Andorra"
  Data.s "Angola"
  Data.s "Anguilla"
  Data.s "Antarctica"
  Data.s "Antigua_and_Barbuda"
  Data.s "Argentina"
  Data.s "Armenia"
  Data.s "Aruba"
  Data.s "Australia"
  Data.s "Austria"
  Data.s "Azerbaijan"
  Data.s "Bahamas"
  Data.s "Bahrain"
  Data.s "Bangladesh"
  Data.s "Barbados"
  Data.s "Belarus"
  Data.s "Belgium"
  Data.s "Belize"
  Data.s "Benin"
  Data.s "Bermuda"
  Data.s "Bhutan"
  Data.s "Bolivia"
  Data.s "Bosnia_and_Herzegovina"
  Data.s "Botswana"
  Data.s "Bouvet_Island"
  Data.s "Brazil"
  Data.s "British_Indian_Ocean_Territory"
  Data.s "Brunei_Darussalam"
  Data.s "Bulgaria"
  Data.s "Burkina_Faso"
  Data.s "Burundi"
  Data.s "Cambodia"
  Data.s "Cameroon"
  Data.s "Canada"
  Data.s "Cape_Verde"
  Data.s "Cayman_Islands"
  Data.s "Central_African_Republic"
  Data.s "Chad"
  Data.s "Chile"
  Data.s "China"
  Data.s "Christmas_Island"
  Data.s "Cocos"
  Data.s "Colombia"
  Data.s "Comoros"
  Data.s "Congo"
  Data.s "Cook_Islands"
  Data.s "Costa_Rica"
  Data.s "Cote_dIvoire"
  Data.s "Croatia"
  Data.s "Cuba"
  Data.s "Cyprus"
  Data.s "Czech_Republic"
  Data.s "Denmark"
  Data.s "Djibouti"
  Data.s "Dominica"
  Data.s "Dominican_Republic"
  Data.s "Ecuador"
  Data.s "Egypt"
  Data.s "El_Salvador"
  Data.s "Equatorial_Guinea"
  Data.s "Eritrea"
  Data.s "Estonia"
  Data.s "Ethiopia"
  Data.s "Falkland_Islands_Malvinas"
  Data.s "Faroe_Islands"
  Data.s "Fiji"
  Data.s "Finland"
  Data.s "France"
  Data.s "French_Guiana"
  Data.s "French_Polynesia"
  Data.s "French_Southern_Territories"
  Data.s "Gabon"
  Data.s "Gambia"
  Data.s "Georgia"
  Data.s "Germany"
  Data.s "Ghana"
  Data.s "Gibraltar"
  Data.s "Greece"
  Data.s "Greenland"
  Data.s "Grenada"
  Data.s "Guadeloupe"
  Data.s "Guam"
  Data.s "Guatemala"
  Data.s "Guernsey"
  Data.s "Guinea"
  Data.s "Guinea_Bissau"
  Data.s "Guyana"
  Data.s "Haiti"
  Data.s "Heard_Island_and_McDonald_Islands"
  Data.s "Holy_See_Vatican_City_State"
  Data.s "Honduras"
  Data.s "Hong_Kong"
  Data.s "Hungary"
  Data.s "Iceland"
  Data.s "India"
  Data.s "Indonesia"
  Data.s "Iran"
  Data.s "Iraq"
  Data.s "Ireland"
  Data.s "Isle_of_Man"
  Data.s "Israel"
  Data.s "Italy"
  Data.s "Jamaica"
  Data.s "Japan"
  Data.s "Jersey"
  Data.s "Jordan"
  Data.s "Kazakhstan"
  Data.s "Kenya"
  Data.s "Kiribati"
  Data.s "Korea"
  Data.s "Kuwait"
  Data.s "Kyrgyzstan"
  Data.s "Lao_Peoples_Democratic_Republic"
  Data.s "Latvia"
  Data.s "Lebanon"
  Data.s "Lesotho"
  Data.s "Liberia"
  Data.s "Libyan_Arab_Jamahiriya"
  Data.s "Liechtenstein"
  Data.s "Lithuania"
  Data.s "Luxembourg"
  Data.s "Macao"
  Data.s "Macedonia"
  Data.s "Madagascar"
  Data.s "Malawi"
  Data.s "Malaysia"
  Data.s "Maldives"
  Data.s "Mali"
  Data.s "Malta"
  Data.s "Marshall_Islands"
  Data.s "Martinique"
  Data.s "Mauritania"
  Data.s "Mauritius"
  Data.s "Mayotte"
  Data.s "Mexico"
  Data.s "Micronesia"
  Data.s "Moldova"
  Data.s "Monaco"
  Data.s "Mongolia"
  Data.s "Montenegro"
  Data.s "Montserrat"
  Data.s "Morocco"
  Data.s "Mozambique"
  Data.s "Myanmar"
  Data.s "Namibia"
  Data.s "Nauru"
  Data.s "Nepal"
  Data.s "Netherlands"
  Data.s "Netherlands_Antilles"
  Data.s "New_Caledonia"
  Data.s "New_Zealand"
  Data.s "Nicaragua"
  Data.s "Niger"
  Data.s "Nigeria"
  Data.s "Niue"
  Data.s "Norfolk_Island"
  Data.s "Northern_Mariana_Islands"
  Data.s "Norway"
  Data.s "Oman"
  Data.s "Pakistan"
  Data.s "Palau"
  Data.s "Palestinian_Territory_Occupied"
  Data.s "Panama"
  Data.s "Papua_New_Guinea"
  Data.s "Paraguay"
  Data.s "Peru"
  Data.s "Philippines"
  Data.s "Pitcairn"
  Data.s "Poland"
  Data.s "Portugal"
  Data.s "Puerto_Rico"
  Data.s "Qatar"
  Data.s "Reunion"
  Data.s "Romania"
  Data.s "Russian_Federation"
  Data.s "Rwanda"
  Data.s "Saint_Barthelemy"
  Data.s "Saint_Helena_Ascension_and_Tristan_da_Cunha"
  Data.s "Saint_Kitts_and_Nevis"
  Data.s "Saint_Lucia"
  Data.s "Saint_Martin"
  Data.s "Saint_Pierre_and_Miquelon"
  Data.s "Saint_Vincent_and_the_Grenadines"
  Data.s "Samoa"
  Data.s "San_Marino"
  Data.s "Sao_Tome_and_Principe"
  Data.s "Saudi_Arabia"
  Data.s "Senegal"
  Data.s "Serbia"
  Data.s "Seychelles"
  Data.s "Sierra_Leone"
  Data.s "Singapore"
  Data.s "Slovakia"
  Data.s "Slovenia"
  Data.s "Solomon_Islands"
  Data.s "Somalia"
  Data.s "South_Africa"
  Data.s "South_Georgia_and_the_South_Sandwich_Islands"
  Data.s "Spain"
  Data.s "Sri_Lanka"
  Data.s "Sudan"
  Data.s "Suriname"
  Data.s "Svalbard_and_Jan_Mayen"
  Data.s "Swaziland"
  Data.s "Sweden"
  Data.s "Switzerland"
  Data.s "Syrian_Arab_Republic"
  Data.s "Taiwan"
  Data.s "Tajikistan"
  Data.s "Tanzania"
  Data.s "Thailand"
  Data.s "Timor_Leste"
  Data.s "Togo"
  Data.s "Tokelau"
  Data.s "Tonga"
  Data.s "Trinidad_and_Tobago"
  Data.s "Tunisia"
  Data.s "Turkey"
  Data.s "Turkmenistan"
  Data.s "Turks_and_Caicos_Islands"
  Data.s "Tuvalu"
  Data.s "Uganda"
  Data.s "Ukraine"
  Data.s "United_Arab_Emirates"
  Data.s "United_Kingdom"
  Data.s "United_States"
  Data.s "United_States_Minor_Outlying_Islands"
  Data.s "Uruguay"
  Data.s "Uzbekistan"
  Data.s "Vanuatu"
  Data.s "Venezuela"
  Data.s "Viet_Nam"
  Data.s "Virgin_Islands_British"
  Data.s "Virgin_Islands_US"
  Data.s "Wallis_and_Futuna"
  Data.s "Western_Sahara"
  Data.s "Yemen"
  Data.s "Zambia"
  Data.s "EndMapstart"
EndDataSection
Amateur Radio, D-STAR/VK3HAF
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Convert country names to datasection and map variables

Post by Denis »

Hi Fangbeast,

i certainly will use your code, at least a part of it.
Thanks.


(stage whisper :
In 1982, I was in the French army in Germany in the city Villingen-Schwenningen (1 year).
Due to the fact that I was in the 'radio transmissions' section, I did amateur radio (not in phony but in morse) from germany, our ID was DA2VR if I well remember.
That was a long time ago now...)
A+
Denis
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Convert country names to datasection and map variables

Post by davido »

@Fangbeast,
Nice code.
Thank you for sharing. :D
DE AA EB
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: Convert country names to datasection and map variables

Post by Thorsten1867 »

Why don't you use a resource file?
With qAES-Packer you can easily create the file and it will also create an include for your program.

qAES-Packer & ResourcesModule.pbi
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert country names to datasection and map variables

Post by Fangbeast »

Hi Fangbeast,

i certainly will use your code, at least a part of it.
Haha, thanks Denis. George just got through showing me an even better way of doing things blast it. Now I am in for weeks (maybe months!!) to try that new way of doing it. Oh, my aching brain. Still, I wish I had know how to do what I did here years ago.
Due to the fact that I was in the 'radio transmissions' section, I did amateur radio (not in phony but in morse) from germany, our ID was DA2VR if I well remember.
Well, I am not doing Amateur radio lately. I always hear the same, whiny, crabby old blokes who complain about everything and forget to use their full callsigns. And some really old ones who act like radio trolls. (Sigh).

Sold my HF gear years ago and only have UHF/VHM (DSTAR and analogue) now. Maybe I will be less bored if I finish this code:):)
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert country names to datasection and map variables

Post by Fangbeast »

davido wrote:@Fangbeast,
Nice code.
Thank you for sharing. :D
You can blame George Bisonte for teaching me new things. That's how my old brain came up with this code:):)
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert country names to datasection and map variables

Post by Fangbeast »

Thorsten1867 wrote:Why don't you use a resource file?
With qAES-Packer you can easily create the file and it will also create an include for your program.

qAES-Packer & ResourcesModule.pbi
When I do my own code, I learn better than using someone else's solution. I try as hard as I can to avoid using "plug-in" solutions because at my age, it's easy to forget and get lazy. But I will have a look and try to understand yours if possible.
Amateur Radio, D-STAR/VK3HAF
Post Reply